Search code examples
javaswinguser-interfaceimageicon

Java Game moving real time(AP CS Final)


I'm taking the AP computer science class at my school. They never taught us about GUIs because the AP test didn't require you to know how to make one.

For our final project we wanted to make something like tron (game where bikes move around an are creating walls of light behind them in an attempt to crash the other player). Before we continue i just want to make sure we are going in the right direction. Should we use ImageIcon for the players or maybe something else?

We still have a lot to learn, but i thought this would be a good thing to start with. The reason why i'm asking is because i'm not sure if we would be able to move them without opening another window every time we want to move something.


Solution

  • This is probably a matter of opinion, but personally, I would use BufferedImage's as the primary image container.

    The main reasons are:

    • They are easy to paint and easy to manipulate, you can actually draw onto them if you need to.
    • Loading a BufferedImage is done through ImageIO.read, it guarantees that the entire image is loaded before the read method returns and will throw an IOException if it can't read the image for some reason, which is better than ImageIcon which loads the image in a background thread and doesn't report errors if the image failed to load

    Movement or placement of the images would be done, typically, by painting them onto an output Graphics context. This is (typically) done by overriding the paintComponent method of something that extends JComponent and using the passed Graphics context and Graphics#drawImage

    Have a look at Performing Custom Painting, 2D Graphics and Reading/Loading an Image for more details