views:

91

answers:

4

I haven't done any swing programming in a while, so I'm looking for some GUI examples that are at least close to what I'm trying to do.

The gui that I'll need to be representing is small nodes (let's say ants) travelling around collecting food from food piles (which just means small nodes travelling to bigger nodes). Once the node (ant) takes a piece of food, the pile shrinks a bit and the ant takes it back home (to ANOTHER circle).

This SOUNDS pretty trivial, but all of the boilerplate involved in setting up a java GUI just makes little logical sense to me, and the GUI is such a small piece of my project. Any examples that would be great for this style of project would be greatly appreciated.

Thanks!

+1  A: 

You could take a look at the code project associated with the College Board's Advanced Placement Computer Science exam, called GridWorld. As the name implies, it is a discrete, grid-based simulation that is fairly powerful and flexible, and might even serve as a foundation for your entire project. I believe it is open source and the GUI portion of it is already built and functional. Even if you cannot adapt it directly to your needs, it may still give you a lot of hints.

Also, the student manual provides a nice, readable overview of the code and how it works.

Tim Isganitis
A: 

Maybe check out the Clojure ant simulation?

Ken Liu
A: 

There are a lot of different methods you could use. The probably easiest/quickest (though not "cleanest" [actually dirty]) way to achieve what you want to do is probably by extending JFrame and adding a custom JPanel on which you draw. e.g. (Code not tested!):

public class AntGUI extends JFrame
{
    [...]
    private JPanel drawingSurface;

    public AntGUI()
    {
      drawingSurface = new JPanel();
      this.add(drawingSurface);
    }

    public void step(){//put logic here / delegate logic to lib}

    public void paintAnts()
    {
      Graphics2D g = drawingSurface.createGraphics() // or getGraphics, I can't recall exactly
      g.setColor(Color.white);
      g.fillRect(0,0,drawingSurface.getWidth(), drawingSurface.getHeight());
      for(Ant a : ants)
      {
        paintAnt(g, a);
      }
    }

    public void paintAnt(Graphics2D g, Ant a)
    {
      // paint Ant
    }

}

And then in your main app redraw every 42ms or so.

Tedil
A: 

I'd advise using a library that can set up Java2D hardware image acceleration and other environmental stuff for you if you want to consider Java2D drawing, such as GTGE. The source code is available if you want to see how they do stuff.

Java Specific Features

  • Double buffering with bufferstrategy or volatile image
  • OpenGL renderer via JOGL or LWJGL
  • Hardware-accelerated images
  • High-resolution timer, support for time drift calculation
  • Fully object oriented library, utilizing Java OOP to maximum
Chris Dennett