I assume most of your drawings appears inside a JPanel, because that’s what my sample code is on.
//This class draws every element in the game
import java.awt.*;
import javax.swing.*;
public class View extends JPanel
{
private ImageIcon pic;
public View()
{
pic = new ImageIcon("images/guy.gif");
}
//Called by the model (the class controlling the entire game) every timestep
public void update()
{
repaint();
}
//By overriding this method, you can add in your own things to draw
public void paintComponent(Graphics g)
{
//Call the super method so that the screen is refreshed
super.paintComponent(g);
//Draw the image, but make sure it is loaded all the way first
if (pic.getImageLoadStatus() == MediaTracker.COMPLETE)
g.drawImage(pic.getImage(), xPos, yPos, width, height, pic.getImageObserver());
}
}
And that will work, as long as you have your own values for xPos, yPos, width, height. If you don’t understand something, let me know.
Basically, repaint() tells a JComponent to redraw everything to its new positions. This calls the screenUpdate (?) method (which you don’t need to worry about) to erase the screen, then calls paintComponent to draw the background and foreground, etc. If you override this method, you can add in things to draw.