6

I have a quick question about Java. I'm sorry if this question is really basic, but I'm a beginner Java programmer :D

I want to render a 2d image in a window, but I can't figure it out. I've looked at the graphics API here:

http://download.oracle.com/javase/1.4.2/docs/api/java/awt/Graphics.html

and the only method I could find that might work is drawImage(). It wasn't working for me, though, but maybe it has to do with the ImageObserver Observer parameter? I just put null for that following some tutorial I found somewhere, but I still get a compile error: Here is my paint method:

public void paint(Graphics g)
{   
    Image img1 = Toolkit.getDefaultToolkit().getImage("theImage.png");
    g.drawImage(img1, 100, 100, null);
} // public void paint(Graphics g)

and here are the methods that call it:

public static void main(String[] args)
{
    MyGame game = new MyGame();
    game.setVisible(true);
    game.play();      

} // public static void main(String[] args)



/** The play method is where the main game loop resides. 
 */
public void play()
{
    boolean playing = true;
    //Graphics g = new Graphics();
    while (playing)
    {
        paint();
    }

} //  public void play()

The thing is when I call paint in the while loop, I get this error: paints(java.awt.Graphics) in MyGame cannot be applied to ()

What does that mean? How can I fix it so I can successfully render a 2d image?

Thanks in advance :D

mre
  • 43,520
  • 33
  • 120
  • 170
Steve
  • 63
  • 1
  • 1
  • 4
  • 3
    Besides the other comments. Don't load images in either the `paint()` or `paintComponent()` methods! Load them at some other time (start-up is good) and store them as attributes of the class. `ImageIO` offers a blocking method for loading images as well. Easier than adding a `MediaTracker` to the image load after calling the `Toolkit` method.. ;) – Andrew Thompson Aug 05 '11 at 19:41

3 Answers3

8

Instead of paint(); use repaint();

corsiKa
  • 81,495
  • 25
  • 153
  • 204
  • THAT WORKED!!! omg thank you so much, I was working on this for at least 3 hrs last night and I could't get it to work, but now it does!! THANKS! – Steve Aug 05 '11 at 18:32
3

You should be overriding paintComponent(Graphics g). Also, as @TotalFrickinRockstarFromMars suggested, you should be invoking repaint().

Community
  • 1
  • 1
mre
  • 43,520
  • 33
  • 120
  • 170
  • When do paintComponent(Graphics g), it not longer works (but just using my code from above does with repaint())... – Steve Aug 05 '11 at 18:33
  • I agree that using `paintComponent(Graphics)` is a recommended practice. Although for very simple things (prototypes and throwaway things) I often am guilty of sneaking it in paint instead of paintComponent. :-X – corsiKa Aug 05 '11 at 18:33
  • @Steve, That's because a container doesn't have a `paintComponent` method. You should be overriding the `paintComponent` method of its content pane, or setting your own `JPanel` instance as the content pane, and then overriding its `paintComponent` method. – mre Aug 05 '11 at 18:41
1
  • You overrid the paintComponent(Graphics g)

class Game extends JComponent { // Your game class

Image img = null;

public Game() {
   img = getImage("/theImage.png");
}

private Image getImage(String imageUrl) {

try {

    return ImageIO.read(getClass().getResource(imageUrl));

} catch (IOException exp) {}

return null;

}

paintComponent(Graphics g) {

   g.drawImage(img, 100, 100, null);

}

}

Mohammed Aslam
  • 995
  • 9
  • 14