我無法在我寫的paint方法上插入圖像。我想要在某些座標處將圖像與paint方法重疊。如何在JPanel(在JPanel)paint方法上添加圖像?
我的代碼:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Polygon;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class testguipaint {
public static void main(String[] args) {
testguipaint img = new testguipaint();
}
public testguipaint() {
JFrame frame = new JFrame();
frame.add(crafting, BorderLayout.CENTER);
frame.setSize(442, 284);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setVisible(true);
frame.setDefaultCloseOperation(3);
}
static JPanel crafting = new JPanel() {
public void paint(Graphics g) {
Color darkGrey = new Color(153, 153, 153);
g.setColor(darkGrey);
g.fillRect(0, 0, 436, 252);
Color lightGrey = new Color(198, 198, 198);
g.setColor(lightGrey);
g.fill3DRect(3, 3, 430, 246, true);
g.setColor(darkGrey);
g.fill3DRect(16, 16, 222, 222, true);
g.fill3DRect(320, 78, 100, 100, true);
g.fillRect(248, 121, 39, 12);
Polygon triangle = new Polygon();
triangle.addPoint(287, 103);
triangle.addPoint(287, 151);
triangle.addPoint(311, 127);
g.fillPolygon(triangle);
g.setColor(Color.white);
g.fill3DRect(88, 16, 3, 222, true);
g.fill3DRect(163, 16, 3, 222, true);
g.fill3DRect(16, 88, 222, 3, true);
g.fill3DRect(16, 163, 222, 3, true);
//BufferedImage image = new ImageIO.read(new File("/minecraft jpeg's/Products/Bread.png"));
//g.drawImage(image, 44, 191, null);
//44, 191
}
};
}
你是否意味着將圖像作爲背景,然後使用其他繪畫到JPanel? – mKorbel 2012-03-05 18:37:40
不,我需要將圖像放在上面看到的繪製方法(重疊)上。 – Lee 2012-03-05 18:40:27
好,如果你需要,但是這個圖像可以覆蓋使用的油漆 – mKorbel 2012-03-05 18:42:39