1

I'm writing a simulation in Java in Netbeans, and the actual non-graphical coding is mostly done. However, I'd like to do a graphical implementation where I use icons to represent the variables changing in the simulation.

The simulation simulates trucks driving along roads, and I'd like an icon to represent every truck. The code shows each truck and each road as a separate object, each with their own attributes, but only a few of the attributes need to be modeled in the graphical implementation. For instance, the position of each truck is an attribute of the road, showing how far along the road the truck has traveled.

What is the easiest way to model this in a graphical interface? I assume I need to assign an icon to a graphical structure in Netbeans and then have it update itself according to the distance attribute of the road, but I have no idea how to approach this.

Any help will be appreciated.

Martin
  • 277
  • 2
  • 5
  • 17
  • Are you looking for any animation? – Paul Samsotha Dec 16 '13 at 12:45
  • And what exactly does this _"the position of each truck is an attribute of the road"_ mean? – Paul Samsotha Dec 16 '13 at 12:46
  • Well, animations would be nice, but if it will be too much extra coding, simply jumping from place to place would be fine – Martin Dec 16 '13 at 12:47
  • How much experience do you have with Swing? Do you know how to use the `Graphics` class? – Paul Samsotha Dec 16 '13 at 12:47
  • Each road is initialized as a custom object that I created. Distance that a vehicle has traveled along the road is a variable stored inside this object. – Martin Dec 16 '13 at 12:48
  • I've used Graphics in some rudimentary drawing before, but nothing too complicated. I'm game to learn, though – Martin Dec 16 '13 at 12:49
  • You know how they have the horse races at carnivals where players do something to make the their horse advance horizontally? Is that the kind of look you want? – Paul Samsotha Dec 16 '13 at 12:49
  • Basically, but I'd want the roads to not all be parallel, but sort of in a map of what the roads would actually be like. It's not too many, though, so the map won't be too complicated. I assume I'd have to plot this "map" via XY coordinates in a Swing window, but how do I initialize the trucks as icons and how do I update their positions along these roads? – Martin Dec 16 '13 at 12:51

1 Answers1

3

Using the Graphics class, you can draw the roads, the cars(using images), and animate with a Swing Timer.

To draw the cars you can use paint the images onto the screen

public class Map extends JPanel {
    BufferedImage car1;
    BufferedImage car2;
    BufferedImage car3;

    public Map(){
        try {
            car1 = ImageIO.read(getClass().getResource("somecarimage.png"));
            car3 = ImageIO.read(getClass().getResource("somecarimage.png"));
            car3 = ImageIO.read(getClass().getResource("somecarimage.png"));
        }
    }

    protected void paintComponent(Graphics g){
        super.paintComponent(g);

        // use the drawImage method
        g.drawImage(car1, xLocation, yLocation, height, width, this);
        g.drawImage(car2, xLocation, yLocation, height, width, this);
        g.drawImage(car2, xLocation, yLocation, height, width, this);
    }
}

As you can see, I drew three car onto the screen. You can use your class with the data as the xLocation and yLocation

If you want to animate the cars, you can use a Swing Timer

Timer timer = new Timer(100, new ActionListener(){   // causes an action every 100 millis
    public void actionPerformed(ActionEvent e){
        // change the xLocation and yLocation of each car
        car1.xLocation += 5;
        car1.yLocation += 5;
        car2.xLocation += 5;
        car2.yLocation += 5;
        car3.xLocation += 5;
        car3.yLocation += 5;

        repaint();
    }
});
timer.start();

You can have an if statement somewhere in the actionPerformed telling the timer when to stop.

Javadocs and tutorials

Timer javadoc | Timer tutorial | Graphics javadoc | Graphics tutorial

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • Thank you so much. I'll try this, but it looks like exactly what I had in mind. – Martin Dec 16 '13 at 13:02
  • Also, I noticed you haven't accepted any answers. For future reference, if an answer helps you with solving you problem, you should accept it. That way any future readers who come along your post know which answers is the most helpful. (It's the check mark below the vote arrows) – Paul Samsotha Dec 16 '13 at 13:24
  • Okay thanks. I'm rather new to StackOverflow, so every little bit helps. – Martin Dec 16 '13 at 13:35
  • 1
    @Martin: A related example is seen [here](http://stackoverflow.com/a/14887457/230513). – trashgod Dec 16 '13 at 14:22