我无法在我编写的绘画方法上插入图像。我希望图像在某些坐标处与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
}
};
}
答案 0 :(得分:2)
只是一个建议,在paint组件中最后绘制图像。这将允许通过在其他对象之上绘制图像。另外,我知道你可能只是测试一些东西,但是你班级名字中每个单词的第一个字母应该大写。
答案 1 :(得分:2)
不是如何叠加或重叠的答案
1)testguipaint
应该TestGuiPaint
更多关于Java命名约定here或here
2)Swing GUI相关代码应该包含在invokeLater()
中,更多关于Initial Threads
3)在Swing JComponents中绘画有方法paintComponent()
而不是paint()
方法,更多关于2D Graphics
答案 2 :(得分:0)
此处仅为短代码压缩导入,仅实现MKorbel提出的2个建议(1和3),为了简洁而扩展JFrame,从main中删除了实例,这是从未使用过的。没有你要求的。
但是,每次面板需要重新绘制时,您肯定不希望从HDD中反复读取图像。所以我们创建一个包含属性的实例,即图像。
import java.awt.*;
import javax.swing.*;
import javax.swing.JPanel;
import java.awt.image.*;
import javax.imageio.*;
import java.io.*;
public class TestGuiPaint extends JFrame {
public static void main (String [] args) {
new TestGuiPaint ();
}
public TestGuiPaint () {
super ("TestGuiPaint");
add (new CraftingPanel (), BorderLayout.CENTER);
setSize (442, 284);
setLocationRelativeTo (null);
setResizable (false);
setVisible (true);
setDefaultCloseOperation (3);
}
class CraftingPanel extends JPanel {
BufferedImage image = null;
public CraftingPanel () {
try {
image = ImageIO.read (new File ("./maeander3.png"));
} catch (java.io.IOException ioe)
{
System.err.println (ioe.getMessage ());
}
}
public void paintComponent (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);
g.drawImage (image, 44, 191, null);
}
};
}
我希望你明白我为图片选择了不同的文件名。 :)