Okay then.
This is a pretty simple little collection of classes I made. At the end I posted a list of where exactly I get all the library classes I used. I believe everything should work, however I just typed this from scratch so it’s possible something doesn’t.
//A basic class that just creates the view and puts your drawing panel into it.
public class Main extends JFrame
{
//The area we will draw on - this is a custom class specified below.
private MyPanel myPanel;
public Main()
{
myPanel = new MyPanel();
//You can do this since Java 1.5. Otherwise you need to say getContentPane().add()
add(myPanel);
setSize(500,500);
setVisible(true);
}
public static void main(String[] args)
{
new Main();
}
}
//A class that represents the drawing context within your window. It should probably handle
//most game logic as well, if you want to keep things simple.
public class MyPanel extends JPanel implements ActionPerformed
{
private ArrayList<MyGamePiece> gamePieces;
private Timer timer;
public MyPanel()
{
//A theoretical list representing all your game pieces.
gamePieces = new ArrayList<MyGamePiece>();
//The timer will repaint your game periodically. Although this is not regarded as the best method to use for
//a game, it is simple to use and should suit your simple purposes. Also put all game logic for the main
//loop in this timer. Every time the timer goes off, the actionPerformed method will be called.
timer = new Timer(this,30);
}
//The method that is called every time your timer goes off (in this case, every 30 ms). You should put all
//periodic game logic in here, and also call repaint() to refresh your picture.
public void actionPerformed(ActionEvent e)
{
repaint();
}
//Whenever repaint is called, this method goes off. It will give you the graphics context of this Panel,
//and will also start the process of erasing old stuff from the screen. Make sure to call the super
//version of this method, or you won't erase all things and everything will look wrong.
public void paintComponent(Graphics g)
{
super.paintComponent(g);
//Loop through all your game pieces and tell them to draw.
for (int i = 0; i < gamePieces.size(); i++)
gamePieces.get(i).draw(g);
}
}
//A basic class representing some of the functionality of your game pieces.
public class MyGamePiece
{
//The location and scale of this game piece, just like you would use for your panels.
private int x, y, width, height;
//Your image representing this game piece. There are other ways to represent it as well, but this is the best and easiest.
//Just use a string path like with everything else and have ImageIO turn the path into a BufferedImage for you.
private BufferedImage image;
public MyGamePiece(int newX, int newY, int w, int h, String imageLoc)
{
x = newX;
y = newY;
width = w;
height = h;
try
{
image = ImageIO.read(new File(imageLoc));
}
catch (Exception e)
{
e.printStackTrace();
}
}
//The drawing method for this game piece, will draw at the proper location.
public void draw(Graphics g)
{
//The built-in function for drawing images using Java2D.
g.drawImage(image,x,y,width,height,null);
}
}
javax.swing.JFrame;
javax.swing.JPanel;
java.awt.event.ActionPerformed;
java.awt.event.ActionEvent;
java.awt.Graphics;
java.awt.image.BufferedImage;
javax.imageio.ImageIO;
javax.swing.Timer;
java.util.ArrayList;