I am starting java and for my first project I started to create an exclusive game.
I am creating a GUI in SWING using the Graphics method.
There were two problems to which I can not find the answer.
Firstly, it seems that I cannot set the background color for my JPanel, which I previously did in a similar way in another JPanel in the same project.
Secondly, I get NullPointerExceptionwhen I try to add an image. I managed to fix this error with try/catch, but it seems that the graphics will not draw. I used the same method of uploading and adding images to a previous JPanel and it worked.
I should mention that my JFrame currently contains 3 elements, each of which has separate classes and is added through BorderLayout ().
This is the code for the class creating the problems:
public class MonopolyBoard extends JPanel{
Image atlantic;
MonopolyBoard() {
this.setBorder(new EtchedBorder());
this.setBackground(new Color( (80), (180), (210) ));
try{
ImageIcon a = new ImageIcon(this.getClass().getResource("../Card/Atlantic Ave.jpg"));
atlantic = a.getImage();
}
catch(NullPointerException e){}
}
public void paint(Graphics g){
}
Graphics2D g2 = (Graphics2D) g;
g2.drawImage(atlantic, 100, 100, null);
g.drawImage(atlantic, 100, 100, this);
};
}
source
share