Results 1 to 7 of 7
- 02-10-2011, 02:29 PM #1
Member
- Join Date
- Feb 2011
- Posts
- 4
- Rep Power
- 0
Event-Driven Painting With Data Model
Hey all.
Studying computer science at the moment, however still a pretty weak programmer, prefer the math sides of things tbh. Currently I'm doing a project that involves Java2D and the visualisation of a data model (a Binary Tree). Note: I'm not asking anyone to write up the code for me, I just want to know whether I'm thinking along the right lines when concerned with paintComponent() and repaint(), I can easily "put" graphical objects on the screen with certain attritbutes, however I can't seem to get them onto the screen when I, say, click a button? I'm thinking that I've got the logic of the paint() method mixed up. :confused:
So I have "Main" class. Thats the JFrame and all the buttons and that jazz. This includes inner classes for the ActionListener Buttons.
I have a "Canvas" class which extends JPanel so I can draw onto it with the paintComponent() method, the nodes of binary tree:Java Code:class InsertListener implements ActionListener{ public void actionPerformed(ActionEvent event){ try{ String value = inputBox.getText(); Integer newValue = Integer.parseInt(value); NumberList.add(newValue); System.out.println("The value " + newValue + " was added to the list"); System.out.println("The List Contains: " + NumberList.toString()); } catch(Exception e){} repaint(); } }
However, since, the nodes need to have their own attributes, such as "value" of node (as a string), I was considering having a "seperate" class for the properties of this node (drawing a rectangle, having three attributes such as string [value] and xOrigin, yOrigin [to help "paint" onto the canvas]).Java Code:public class DrawingCanvas extends JPanel{ private static int xOrigin = 0; private static int yOrigin = 0; public DrawingCanvas(){ this.setPreferredSize(new Dimension (600,550)); } public void paintComponent(){ //code to draw stuff in here; } }
So in effect, I shift the paintComponent() method to the "node" class rather than the canvas class? So every time, I click that insert button, a node object is created of type GraphicsNode and is painted onto the canvas?
Is this the right logic for painting in java or am I going about it the wrong way?
- 02-11-2011, 07:00 PM #2
Member
- Join Date
- Feb 2011
- Posts
- 4
- Rep Power
- 0
I got a reply to this thread, via email, but somehow it didn't get posted in here? Strange.
Thanks for your input.I'm new to Java as well, so perhaps someone else will give you a better answer, but if I understand you I think I can say that this is not the right logic for painting in Java. Instead, the idea is to put all your painting code in yourCanvas.paintComponent(). You maintain your state variables separately from your painting code, and when you want the screen to update you call yourCanvas.repaint().
So with this in mind, I'd have to creat a seperate method to draw the actual node itself, with parameter of the node + Graphics as the signature?
i.e:
public void drawComponent(Graphics2D g2d, Node n){...}
The paintComponent method would call this method INSIDE the method body it seems?
- 02-11-2011, 07:54 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
Nah, too complicated; I normally do it like this: I define an interface:
A Painter is supposed to paint itself, given a Graphics object. All a paintComponent method does is call the paint( ... ) method in a list of painters:Java Code:public interface Painter { public void paint(Graphics g); }
The above method is my implementation of a clas that extends a Swing JComponent. Other methods can add Painters to the painters list and they'll get painted whenever the paintComponent( ... ) method is called.Java Code:private List<Painter) painters= ...; public void paintComponent(Graphics g) { super.paintComponent(g); // paint the background for (Painter painter: painters) painter.paint(g); }
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 02-12-2011, 07:59 PM #4
Member
- Join Date
- Feb 2011
- Posts
- 4
- Rep Power
- 0
Ah thats a great idea Jos,
I might just do that. That way you can have various paint methods being called, specifying different parts of a "visualisation", withoug clogging up the paintComponent() method?
- 02-12-2011, 08:08 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
AFAICT it doesn't clog up anything, i.e. the paintComponent( ... ) method simply calls all the Painters that do the actual work. Those Painters are added, removed or whatever from that list by other objects. That first method keeps on doing what it has to do: call all those Painters.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 02-15-2011, 02:21 PM #6
Member
- Join Date
- Feb 2011
- Posts
- 4
- Rep Power
- 0
Thanks you guys, problem now solved! :)
- 02-15-2011, 03:58 PM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
accessing custom data model values - how?
By dave247 in forum New To JavaReplies: 11Last Post: 03-07-2010, 12:20 PM -
deleting the data in the column model
By bigj in forum New To JavaReplies: 0Last Post: 02-08-2010, 08:15 AM -
Dynamic data model in JComboBox
By agreed in forum AWT / SwingReplies: 5Last Post: 11-15-2009, 08:30 PM -
Filter table model by data in specific column
By ribbs2521 in forum New To JavaReplies: 9Last Post: 10-29-2009, 04:41 AM -
Data from a model class won't show up in the table
By ayampanggang in forum AWT / SwingReplies: 3Last Post: 11-27-2008, 08:20 PM


LinkBack URL
About LinkBacks


Bookmarks