Results 1 to 5 of 5
  1. #1
    vinaykumar is offline Member
    Join Date
    Oct 2013
    Posts
    4
    Rep Power
    0

    Default repaint() method

    Hey,


    Im using basic GUI to draw a binary tree in a panel. Using buttons. When you click on a button it creates a node and draws an oval on the screen. But when i try to create another node the repaint() method erases the previously drawn node>.. :/
    Im stuck. Please help.

    public class InputPanel extends JPanel implements ActionListener
    {
    JButton rt =new JButton("Root");
    JButton l=new JButton("Left");
    JButton r=new JButton("Right");
    JButton n=new JButton("Null");
    JTextField tf=new JTextField("Click on root to create the root",15);
    JPanel h=new JPanel();
    int x=0;
    public InputPanel()
    {
    setLayout(new FlowLayout());
    setSize(200,200);
    setBounds(0,0,400,700);
    setBackground(Color.red);
    tf.setEditable(false); // set to read-only
    tf.setBackground(Color.WHITE);
    add(rt);
    add(l);
    add(r);
    add(n);
    add(tf);
    add(h);


    rt.addActionListener(this);
    l.addActionListener(this);
    r.addActionListener(this);
    n.addActionListener(this);

    }
    @Override
    public void actionPerformed(ActionEvent e) {
    if (e.getSource() == rt)
    {
    if(isempty()==true){
    atree[i]++; i++; x++;
    tf.setText("Root Created");
    repaint();
    }

    }
    else if (e.getSource() == l)
    {
    if(i%2==0){
    atree[i]++; i++; x++;
    tf.setText("Left Node added");
    repaint();
    }


    }

    else if (e.getSource() == r)
    {
    if(i%2!=0){
    atree[i]++; i++;x++;
    tf.setText("Right Node added");
    repaint();
    }

    }
    else if (e.getSource() == n)
    {
    i++;x++;
    tf.setText("NULL Node added");

    }
    }
    protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    if(x==1)
    g.drawOval(200,50,40,40);
    //g.drawLine(205,85,180,150);
    if(x==2)
    g.drawOval(160,150,40,40);
    if(x==3)
    g.drawOval(240,150,40,40);
    //g.drawOval(120,250,40,40);g.drawOval(180,250,40,40 );g.drawOval(220,250,40,40);g.drawOval(280,250,40, 40);
    //g.drawLine(50,50,40,40);
    }

    }

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,762
    Rep Power
    5

    Default Re: repaint() method

    It clears the previous node because super.paintComponent(g) repaints the background. This is normal behavior.
    Normally, what one does is to continue to repaint the previous nodes again as new ones are created.

    Note that if the background was not repainted each time and you had an object you wanted to
    move, it would simply drag across the panel like a big paintbrush.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    vinaykumar is offline Member
    Join Date
    Oct 2013
    Posts
    4
    Rep Power
    0

    Default Re: repaint() method

    Thank you.

    I removed the the super.paintComponent(g) statement and my program worked just fine, but a few irregularities surfaced. Like some of the buttons and textfields were created two times instead of one and the panel wasn't properly created. So i guess to rectify this i need to store the co-ordinates of the previously created nodes and just repaint them. But im having a hard time figuring out how to do that too. :/

  4. #4
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,762
    Rep Power
    5

    Default Re: repaint() method

    Quote Originally Posted by vinaykumar View Post
    So i guess to rectify this i need to store the co-ordinates of the previously created nodes and just repaint them. But im having a hard time figuring out how to do that too.
    That's how it is normally done. You could just use a List implementation (perhaps ArrayList) and store instances of type Point. There are a variety of ways to do it. Usually, one does all of that outside of the paint routine. Then just add the new coordinates to the list and call repaint().

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    vinaykumar is offline Member
    Join Date
    Oct 2013
    Posts
    4
    Rep Power
    0

    Default Re: repaint() method

    Thanks a lot. I think i should be able to do it now. :)

Similar Threads

  1. repaint method called twice?
    By rajke88 in forum Java Applets
    Replies: 4
    Last Post: 02-09-2013, 04:58 PM
  2. Replies: 8
    Last Post: 11-18-2012, 06:02 PM
  3. Replies: 4
    Last Post: 01-16-2012, 04:55 PM
  4. Replies: 3
    Last Post: 02-04-2011, 06:30 AM
  5. repaint method
    By manojp476 in forum CLDC and MIDP
    Replies: 10
    Last Post: 08-12-2010, 03:12 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
  •