Problem painting an image

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mrjohn
    New Member
    • May 2009
    • 31

    #1

    Problem painting an image

    Hey, I've got some code that is supposed to paint part of an image on a JPanel. Unfortunately, when I run it, I get a nullPointer Exception.

    [code="java"]public void paintMap()
    {
    Graphics g = this.getGraphic s(); //Create a new Graphics object.
    Graphics2D g2d = (Graphics2D) g; //Cast it to a Graphics2D object (Has more options)
    Image spaceImages = Toolkit.getDefa ultToolkit().ge tImage ( "stars.png" );
    System.out.prin tln(spaceImages .getWidth(null) ); //Returns -1. Is that a problem?
    g2d.drawImage(s paceImages, 0, 0, 50, 50, 0, 0, 50, 50, null);
    //more code[/code]

    The error occurs on the last line of code (Line 7), but I wonder if I'm not creating the image correctly, as when I try to get the width of the image it returns -1 (on line 6). Any thoughts?
  • jx2
    New Member
    • Feb 2007
    • 228

    #2
    Are you sure that the file is in a correct folder?
    you can try this:
    Code:
    BufferedImage img = ImageIO.read(new File("imagepath.jpg"));
    I know this one is working, and you'll get IOException if it cannot open a file

    regards jan

    Comment

    • mrjohn
      New Member
      • May 2009
      • 31

      #3
      Thanks. I think that Eclipse was unable to see it.

      Comment

      Working...