Java Graphics Issues

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) )); //this code dosent work

        //this throws exception without try catch
        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; 
         //this code should draw the image but it dosent
          g2.drawImage(atlantic, 100, 100, null);
          g.drawImage(atlantic, 100, 100, this);

    };
}
+5
source share
3 answers

You will not recognize if you do not print stacktrace inside the catch block. If the constructor new ImageIcon()does not throw an exception and instead returns a null object, the next line a.getImage()will certainly call NPE, because you cannot call the method on the null object.

Instead of this

 //this throws exception without try catch         
 try
 {           
     ImageIcon a = new ImageIcon(this.getClass().getResource("../Card/AtlanticAve.jpg"));
     atlantic = a.getImage();        
 }        
 catch(NullPointerException e){}   

try it

// the next line may be wrapped incorrectly due to MarkDown
ImageIcon a = new ImageIcon(this.getClass().getResource("../Card/AtlanticAve.jpg"));
if (a == null)
{
    System.out.println("Can not find AtlanticAve.jpg");
    return;
}
     atlantic = a.getImage();        

Line

 // the next line may be wrapped incorrectly due to MarkDown
 ImageIcon a = new ImageIcon(this.getClass().getResource("../Card/AtlanticAve.jpg"));

, , , ImageIcon . . , getResource(). . , .

// using _var_ because I'm too lazy to look up the return types of the methods
var x1 = this.getClass().getResource("../Card/AtlanticAve.jpg");
if (x1 == null)
{
   System.out.println("Can't find my resource");
}

+1

, , , !,

@Override
public void paintComponent(Graphics g){
    Graphics2D g2 = (Graphics2D) g; 
    //this code should draw the image but it dosent
     g2.drawImage(atlantic, 100, 100, null);
     g.drawImage(atlantic, 100, 100, this);
}
+1

, paint(). Paint .

:

public void paint(Graphics g){
    // paint the background
    g.fill(); // not sure about the parameters
    // paint your image
    g.drawImage(...);
}
+1

All Articles