【发布时间】:2013-03-06 12:58:06
【问题描述】:
如何使用图像填充以下矩形?谁能帮帮我?
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);
}}
我尝试了这段代码,但找不到让它工作的方法:
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"));
【问题讨论】:
-
您创建变量
imageFile是为了什么?还有,你知道dispose是什么意思吗,你看过同名方法的评论吗? -
顺便问一下这条线上有
NullPointerException吗..Graphics2D graph = img.createGraphics();
标签: java