Results 1 to 7 of 7
  1. #1
    ThatJavaGuy is offline Member
    Join Date
    Feb 2011
    Posts
    4
    Rep Power
    0

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

    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();
    				
    			}
    		}
    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:
    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;
    
    }
    
    
    }
    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]).

    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?

  2. #2
    ThatJavaGuy is offline Member
    Join Date
    Feb 2011
    Posts
    4
    Rep Power
    0

    Default

    I got a reply to this thread, via email, but somehow it didn't get posted in here? Strange.

    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().
    Thanks for your input.

    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?

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,525
    Blog Entries
    7
    Rep Power
    20

    Default

    Nah, too complicated; I normally do it like this: I define an interface:

    Java Code:
    public interface Painter {
       public void paint(Graphics g);
    }
    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:
    private List<Painter) painters= ...;
    public void paintComponent(Graphics g) {
       super.paintComponent(g); // paint the background
       for (Painter painter: painters)
          painter.paint(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.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    ThatJavaGuy is offline Member
    Join Date
    Feb 2011
    Posts
    4
    Rep Power
    0

    Default

    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?

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,525
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by ThatJavaGuy View Post
    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?
    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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    ThatJavaGuy is offline Member
    Join Date
    Feb 2011
    Posts
    4
    Rep Power
    0

    Default

    Thanks you guys, problem now solved! :)

  7. #7
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,525
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by ThatJavaGuy View Post
    Thanks you guys, problem now solved! :)
    Good to read; did you use my suggestion?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. accessing custom data model values - how?
    By dave247 in forum New To Java
    Replies: 11
    Last Post: 03-07-2010, 12:20 PM
  2. deleting the data in the column model
    By bigj in forum New To Java
    Replies: 0
    Last Post: 02-08-2010, 08:15 AM
  3. Dynamic data model in JComboBox
    By agreed in forum AWT / Swing
    Replies: 5
    Last Post: 11-15-2009, 08:30 PM
  4. Filter table model by data in specific column
    By ribbs2521 in forum New To Java
    Replies: 9
    Last Post: 10-29-2009, 04:41 AM
  5. Data from a model class won't show up in the table
    By ayampanggang in forum AWT / Swing
    Replies: 3
    Last Post: 11-27-2008, 08:20 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •