我会使用Canvas并重写paint(Graphics g)并使用它绘制各种元素。然后你可以用计时器或游戏循环调用repaint()。
public class MyCanvas extends Canvas
{
public void gameLoop()
{
//Don't do it this way, this is just a quick example.
//Instead look up better game loop options.
while (true)
{
repaint(); //automatically calls paintComponent
Thread.yield();
}
}
//Put all the stuff that gets drawn in here.
//@Override
public void paint(Graphics g)
{
super.paint(g);
for (int i = 0; i < chips.size(); i++)
{
chips.get(i).draw(g);
}
}
}
public class Chip
{
private int x;
private int y;
public void draw(Graphics g)
{
g.setColor(Color.BROWN);
g.fillRect(x, y, 50, 50);
//etc.
}
}
来源
2011-06-08 22:59:52
Eli
可能应该移动到gamedev.stackexchange。虽然有一个类似的问题可以提供答案:http://gamedev.stackexchange.com/questions/2911/2d-game-dev-and-java-other-possibilities-than-opengl – thedaian 2011-06-08 21:26:28