Hi !
Featured games (85)
games approved by the League of Dukes
Games in Showcase (623)
Games in Android Showcase (176)
games submitted by our members
Games in WIP (676)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Animated Graph Drawing using Java2D  (Read 2024 times)
0 Members and 1 Guest are viewing this topic.
Offline sw@T

Junior Newbie

Java games rock!

« Posted 2003-03-18 09:51:43 »


I'm not sure if this post should be in the newless cluebies section, but here goes.

I have read (and, I think, understood) the whole article on "Painting in AWT and Swing" at

I am writing an app that needs to draw a graph (a mathematical one - nodes, edges etc). The graph will be changing rapidly so the drawing needs to be animated. I plan on using Java2D for this.

I was just wondering if someone could post a basic class structure for doing this. i.e. Should I extend JPanel or Component? Should I use paintImmediately() or will repaint() suffice?

Also how should I thread the actual algorithm that manipulates the graph so that when the algorithm is running, the GUI can still be used. Should I put the algorithm in its own class implementing Runnable?

Any design patterns that you guys commonly use? e.g. Model, View, Controller?

Offline rdcarvallo

Senior Devvie

Projects: 5
Exp: 15 years

2D Java games forever!

« Reply #1 - Posted 2003-03-18 23:55:52 »

 Just my idea, in 3 clases Painter(JPanel, Canvas),  App(the algorithm, application) and Graph(the graph to be displayed).

class Painter extends Canvas{
  Graph theGraph;
  public void setGraph(Graph g){
    theGraph = g;
  public void paint(Graphics g){

Second the
class Application implements Runnable{
  Graph gr;
  Painter painter;
  public Application(Painter p){
    painter = p;
    //your init code
  public void run(){
    //your algorithm code
    //maybe a Thread.sleep()...
  public static void main(String[] args){
    Painter pa = new Painter();
    Application app = new Application(pa);
    //Put the Painter in a Frame and display it
    Thread t = new Thread(app);

For the Graph, it must have a method drawIn(Graphics g) where the graph draws the nodes and edges.

 You must finish the code to take out the flickering it will produce (no double buffer).

Offline Herkules

Senior Devvie

Friendly fire isn't friendly!

« Reply #2 - Posted 2003-03-19 06:54:15 »

There is an animated graph in the JDK samples.

Additionally, I'd like to guide your interest to the libs jazz and picollo that you might find helpful.

We created a graph lib in top of jazz easily.

HARDCODE    --     DRTS/FlyingGuns/JPilot/JXInput  --    skype me: joerg.plewe
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline sw@T

Junior Newbie

Java games rock!

« Reply #3 - Posted 2003-04-01 13:37:06 »

Thanks a lot for the replies.

Rafael, your code structure is quite interesting. I didn't end up implementing it exactly the way you described, but for my next project I think I might give your structure a try. My program actually turned out quite well. I basically Had a and a and a seperate class extending JPanel in which I overwrote the paintComponent(Graphics g) method. I will post the full structure up for criticism when I get the time (my uni lecturers are slave drivers at the moment).

Jazz also sounds very interesting.

Ahh, Java never ceases to amaze me with its incredible depth of quality packages and support for so many different APIs. Java kicks.
Pages: [1]
  ignore  |  Print  
You cannot reply to this message, because it is very, very old.

BurntPizza (9 views)
2015-10-07 02:11:23

KaiHH (12 views)
2015-10-06 20:22:20

KaiHH (11 views)
2015-10-06 19:41:59

BurntPizza (23 views)
2015-10-06 19:04:48

basil_ (44 views)
2015-09-30 17:04:40

shadowstryker (22 views)
2015-09-29 15:55:06

TheSpaceHedgehog (28 views)
2015-09-29 01:58:48

GamerC4 (54 views)
2015-09-24 21:10:38

GamerC4 (71 views)
2015-09-24 21:09:48

htuy (28 views)
2015-09-24 04:57:24
Math: Inequality properties
by Roquen
2015-10-01 13:30:46

Math: Inequality properties
by Roquen
2015-09-30 16:06:05

HotSpot Options
by Roquen
2015-08-29 11:33:11

Rendering resources
by Roquen
2015-08-17 12:42:29

Rendering resources
by Roquen
2015-08-17 09:36:56

Rendering resources
by Roquen
2015-08-13 07:40:51

Networking Resources
by Roquen
2015-08-13 07:40:43

List of Learning Resources
by gouessej
2015-07-09 11:29:36 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!