I am trying to code a board game in Java. I have the board which is an image and the pawns which are shapes (for example a rectangle). I want the board to be the background in the JFrame and the pawns to be over the board. I have tried multiple times but either the board covers the shape or only the shape appears. I could join the shape with the image..and create a new image but this is not what i want. Any ideas?
public class Board extends JPanel {
@Override
public void paintComponent(Graphics g) {
try {
BufferedImage img = ImageIO.read(new File("C:\\Users\\i\\Desktop\\j.png"));
Graphics2D g2 = (Graphics2D) g;
super.paintComponent(g);
g.drawImage(img, 0, 0, null);
}catch (IOException ex) {
Logger.getLogger(Board.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
And my other class
public class GameGUI extends javax.swing.JFrame {
public GameGUI() {
Board b=new Board();
setContentPane(b);
setSize(600,600);
setTitle("gui");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
}
The class for the player
public class Player extends JPanel{
int xPos = 100, yPos = 100, vwidth = 100, vheight = 100;
Rectangle r = new Rectangle(xPos, yPos, vwidth, vheight);
public Player(){ .....}
@Override
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
super.paintComponent(g);
g.setColor(Color.black);
if (fill) {
g.fillRect((int) r.getX(), (int) r.getY(), (int) r.getWidth(), (int) r.getHeight());
}
g2.draw(r);
}
}