I am having trouble inserting an image on top of a paint method that I’ve written. I want to have images overlap the paint method at certain coordinates.
My code:
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
}
};
}
not an answer how to overlay or overlap
1)
testguipaintshould beTestGuiPaintmore about Java Naming Conventions here or here2) Swing GUI rellated code should be wrapped into
invokeLater(), more about in the Initial Threads3) for painting in Swing JComponents there is method
paintComponent()instead ofpaint()method, more about in the 2D Graphics