java 在Java中用图像填充矩形
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15247971/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Fill rectangle with image in Java
提问by Daniel Patilea
How can I fill the following rectangle using an image? Can anyone help me please?
如何使用图像填充以下矩形?有人可以帮我吗?
public void paintComponent(Graphics g) {
setOpaque(false);
//Paint a filled rectangle at user's chosen point.
if (point != null) {
g.drawRect(0, 0,
rectWidth - 1, rectHeight - 1);
g.setColor(Color.yellow);
g.fillRect(1, 1,
rectWidth - 2, rectHeight - 2);
}}
I tried this code but I couldn't find a way to make it work:
我试过这段代码,但找不到让它工作的方法:
File imageFile = new File("duck.jpg");
BufferedImage img;
Graphics2D graph = img.createGraphics();
graph.setColor(Color.BLACK);
graph.fill(new Rectangle(1, 2, rectWidth, rectHeight));
graph.dispose();
ImageIO.write(img, "jpg", new File("duck.jpg"));
采纳答案by Dariusz
You have to load an image into an Image object (like BufferedImage) and then call
您必须将图像加载到 Image 对象(如 BufferedImage)中,然后调用
graphics.drawImage()
graphics.drawImage()
on that image, giving the coordinates and other info.
在那个图像上,给出坐标和其他信息。
Look in the tutorialfor more info
查看教程以获取更多信息