Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (576)
games submitted by our members
Games in WIP (498)
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 1426 times)
0 Members and 1 Guest are viewing this topic.
Offline sw@T

Junior Newbie




Java games rock!


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

Hi,

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 http://java.sun.com/products/jfc/tsc/articles/painting/index.html

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?

Thanks.
Offline rdcarvallo

Senior Member


Projects: 5
Exp: 15 years


2D Java games forever!


« Reply #1 - Posted 2003-03-19 00:55:52 »

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

First Painter.java
1  
2  
3  
4  
5  
6  
7  
8  
9  
class Painter extends Canvas{
  Graph theGraph;
  public void setGraph(Graph g){
    theGraph = g;
  }
  public void paint(Graphics g){
    theGraph.drawIn(g);
  }
}


Second the Application.java
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
class Application implements Runnable{
  Graph gr;
  Painter painter;
  public Application(Painter p){
    painter = p;
    //your init code
   painter.setGraph(gr);
  }
  public void run(){
    //your algorithm code
   painter.repaint();
    //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);
    t.start();
  }
}


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).

 Rafael.
Offline Herkules

Senior Member




Friendly fire isn't friendly!


« Reply #2 - Posted 2003-03-19 07: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 http://www.cs.umd.edu/hcil/jazz/ 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 15: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 Model.java and a View.java 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.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (15 views)
2014-04-15 18:08:23

BurntPizza (13 views)
2014-04-15 03:46:01

UprightPath (27 views)
2014-04-14 17:39:50

UprightPath (12 views)
2014-04-14 17:35:47

Porlus (29 views)
2014-04-14 15:48:38

tom_mai78101 (51 views)
2014-04-10 04:04:31

BurntPizza (110 views)
2014-04-08 23:06:04

tom_mai78101 (211 views)
2014-04-05 13:34:39

trollwarrior1 (179 views)
2014-04-04 12:06:45

CJLetsGame (185 views)
2014-04-01 02:16:10
List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:05:20
java-gaming.org 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‑gaming.org
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!