Drawing 2D graphics [closed]
I would like to make a simple game in Java that has already been designed. I just need a way to draw sprites, etc. It doesn't have to be anything complicated. What would be the first choice you'd recommend for this?
I would heavily suggest you go with a sprite system built on top of OpenGL, like Slick2D or libgdx. Java 2D graphics drawing is too slow to be used for sprite-based games without major headaches. I speak from bitter experience.
I recommend Java 2D.
extending a JPanel is a good start:
public class SpriteDrawer extends JPanel
{
public SpriteDrawer()
{
try
{
sprite = ImageIO.read(new File("..//images//sprite.PNG"));
}catch(Exception e){e.printStackTrace();}
frame = new JFrame("Sprite Drawer");
frame.add(this);
frame.setSize(400,400);
frame.setVisible(true);
}
public void paint(Graphics g)
{
Graphics2D g2 = (Graphics2D)g;
g2.drawImage(0,0,400,400);
}
private JFrame frame;
private Image sprite;
}
this is a good example of overriding the paint method in JPanel. I hope this is what you were looking for, if not let me know and i can help you out.
You might find the Slick2D framework useful - it's well designed for simple 2D games and includes tools for sound effects, input handling etc.
精彩评论