5

I am a java beginner and for my first project I started building a Monopoly game.

I'm building the GUI in SWING using the Graphics method.

Two problems appeared which I can't find an answer to.

The first one is that it seems that I can't set the Background color to my JPanel which I had previously done the same way in another JPanel in the same project.

The second one is that I get a NullPointerException while trying to add an image.I managed to correct this error with a try/catch but it seems that the Graphics won't paint.Again I've used the same method to load and add images in a previous JPanel and it worked.

I should mention that my JFrame at the moment contains 3 elements everyone in there separate classes and are added via BorderLayout().

This is the code for the class that is creating 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);

    };
}
7
  • Don't use the try/catch there. Post the stacktrace. Jan 12, 2012 at 21:34
  • 4
    "Swing programs should override paintComponent() instead of overriding paint()."—Painting in AWT and Swing: The Paint Methods.
    – trashgod
    Jan 12, 2012 at 21:34
  • "this throws exception without try catch" - this is incorrect. If the block does throw an exception, it does so regardless of the try/catch. What your try/catch block does is hide that exception. Which, in turn, makes it hard to impossible to debug. You need to understand why that exception is thrown, not hide it under the rug.
    – Mat
    Jan 12, 2012 at 21:34
  • Consider starting with a simpler game.
    – trashgod
    Jan 12, 2012 at 21:35
  • ok then I have no clue why the exception is thrown I have previously aded Images in the JPanel via the constructer and this dident happen Jan 12, 2012 at 21:36

3 Answers 3

1

You won't know unless you print the stacktrace inside the catch block. If the constructor, new ImageIcon(), isn't throwing the exception and instead is returning a null object, the next line, a.getImage(), will definitely cause a NPE because you can't call a method on a 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 this

// 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();        

The line

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

Basically, you need to start by seeing what might cause the constructor if ImageIcon to return a null object. That will get you on the right track. It might be something due to a failed getResource() call. A simple way of finding out would be to separate the above line into it's parts and give them their own result variables. It's messy and ineficient but that's how troubleshooting goes sometimes.

// 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");
}

You get the picture

1
  • I've managed to solve the Image problem it seems I dident put the image in the right package now my only problem left is why isent the background being set? Jan 12, 2012 at 22:12
1

I am very confused with your code but I think the problem is that your panel is not drawing!, your paint method it should be

@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);
}
4
  • Do you use eclipse? If not you should, it should stop you from having as many problems :) have fun!
    – Sam Palmer
    Jan 12, 2012 at 22:06
  • I use Netbeans and it was my bad it dident draw the image wasent in the right package thats why I was geting the exception now the only problem I have is that the background of the JPanel isent setting as it should Jan 12, 2012 at 22:13
  • the only reason i can see is the answer I have posted, that in your paint method nothing is being called. The answer above should work
    – Sam Palmer
    Jan 12, 2012 at 22:15
  • Also a helpful tip you may need later on. If panels and so forth update their graphics, use invalidate() on the parent container. This will then redraw all the children by invoking the paintComponent method
    – Sam Palmer
    Jan 12, 2012 at 22:16
1

Setting the Background color does not have any effect because you override paint(). Paint is responsible for drawing the background.

You need to paint the background in your paint method:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.