The Artima Developer Community
Sponsored Link

Java Answers Forum
Help with updating graphical objects in my GUI.

7 replies on 1 page. Most recent reply: Oct 7, 2005 12:53 AM by Heidi Kjaer

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 7 replies on 1 page
Heidi Kjaer

Posts: 4
Nickname: heidi83
Registered: Oct, 2005

Help with updating graphical objects in my GUI. Posted: Oct 5, 2005 3:21 AM
Reply to this message Reply
Advertisement
My program:
a GUI, when you push a button it draws 5 circles with random colors, size, and randomly placed inside the window.

My classes:
1) main-method
2) panel-class with the ActionListener inside.
3) Circle-class

When I execute the program it has drawn the 5 circles as I wanted, but how do I make it redraw the circle-objects everytime I push the button?

Here's the panel class:
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
 
public class MySplatPanel extends JPanel
{
   private MyCircle circle1, circle2, circle3, circle4, circle5;
   private JButton button1;
		
   //-----------------------------------------------------------------
   //  Constructor: Sets up GUI-components and creates five Circle objects.
   //-----------------------------------------------------------------
   public MySplatPanel()
   {
      /*GUI*/
      button1 = new JButton("Touch me!");
      button1.addActionListener (new ButtonListener());
		
      /*add graphic componenents to panel*/
      add (button1);
		
		
      /*Circles*/
      circle1 = new MyCircle (30);
      circle2 = new MyCircle (50);
      circle3 = new MyCircle (100);
      circle4 = new MyCircle (45);
      circle5 = new MyCircle (60);
 
      setPreferredSize (new Dimension(300, 200));
      setBackground (Color.black);
 
	}
 
   //-----------------------------------------------------------------
   //  Draws this panel by requesting that each circle draw itself.
   //-----------------------------------------------------------------
   public void paintComponent (Graphics page)
   {
      super.paintComponent(page);
 
      circle1.draw(page);
      circle2.draw(page);
      circle3.draw(page);
      circle4.draw(page);
      circle5.draw(page);
		
   }
	
	private class ButtonListener implements ActionListener
	{		
	    public void actionPerformed (ActionEvent event)
	    {
	    circle1 = new MyCircle (30);
      	    circle2 = new MyCircle (50);
      	    circle3 = new MyCircle (100);
      	    circle4 = new MyCircle (45);
     	    circle5 = new MyCircle (60);
	    }	
		
	   //------------------------------
	   //If I add the following it won't compile
	   //-------------------------------
		
	   /*public void paintComponent (Graphics page)
   	   {
    	   super.paintComponent(page);

    	   circle1.draw(page);
     	   circle2.draw(page);
      	   circle3.draw(page);
            circle4.draw(page);
            circle5.draw(page);
	   }*/
    }
}
 


Kondwani Mkandawire

Posts: 530
Nickname: spike
Registered: Aug, 2004

Re: Help with updating graphical objects in my GUI. Posted: Oct 5, 2005 4:54 AM
Reply to this message Reply
It will actually be doing that, just that you probably
don't have a clear button to verify this. If it quickly
paints over it, how will you know whether it actually
re-drew or not?

If you're not sure stick a print line in the actionPerformed
method of your ActionListener for the button. You will
find that it will be executed each time.

Heidi Kjaer

Posts: 4
Nickname: heidi83
Registered: Oct, 2005

Re: Help with updating graphical objects in my GUI. Posted: Oct 5, 2005 6:51 AM
Reply to this message Reply
Thank you for your reply, I'll see what I can do.

Amol Brid

Posts: 43
Nickname: amolbrid
Registered: Feb, 2004

Re: Help with updating graphical objects in my GUI. Posted: Oct 6, 2005 5:05 AM
Reply to this message Reply
You need to store the cricles,created when button is pressed, in List probably Vector/ArrayList. So that if you again press the button the new cricles are added to the list and older one are preserved. In your code the earlier cricle references are deleted when button is pressed. Code should be somewhat like this:
private Vector circles;
 
public MySplatPanel()
{
   //code
   circles = new Vector();
   circles.add(new MyCircle (30));
   circles.add(new MyCircle (50));
   circles.add(new MyCircle (100));
   circles.add(new MyCircle (45));
   circles.add(new MyCircle (60));
   //code
}
 
public void paintComponent (Graphics page)
{
   super.paintComponent(page);
   for(int n=0; n < circles.size(); n++)
   {
      ((MyCircle)circles.get(n)).draw(page);
   }		
}
 
public void actionPerformed (ActionEvent event)
{
   circles.add(new MyCircle (30));
   circles.add(new MyCircle (50));
   circles.add(new MyCircle (100));
   circles.add(new MyCircle (45));
   circles.add(new MyCircle (60));
}	



Regards,
Amol Brid.

Kondwani Mkandawire

Posts: 530
Nickname: spike
Registered: Aug, 2004

Re: Help with updating graphical objects in my GUI. Posted: Oct 6, 2005 5:17 AM
Reply to this message Reply
Actually that probably isn't it. I just resolved a
very similar problem, two minutes ago - I was trying
to get a Dialog to add JLabels and CheckBoxes at
runtime to a JPanel. If you screen through the
JPanel doc, you will notice that there is an
updateUI.

Just call it at the end of each paint operation
(you do extend JPanel so that should not be a
problem).

updateUI();

Kondwani Mkandawire

Posts: 530
Nickname: spike
Registered: Aug, 2004

Re: Help with updating graphical objects in my GUI. Posted: Oct 6, 2005 5:21 AM
Reply to this message Reply
Actually to be more specific, update after every
action is performed. i.e in you ActionListener
you should then have:
class SomeActionListener implements ActionListener{
    public void actionPerformed(ActionEvent evt){
        //   your operations...
        //   more of your operations....
        //   bored hence more operations
        updateUI();
    }
}

Heidi Kjaer

Posts: 4
Nickname: heidi83
Registered: Oct, 2005

Re: Help with updating graphical objects in my GUI. Posted: Oct 6, 2005 11:40 AM
Reply to this message Reply
Thanks a lot Kondwani, I'm sure this will help a lot, thanks! :)

Heidi Kjaer

Posts: 4
Nickname: heidi83
Registered: Oct, 2005

Re: Help with updating graphical objects in my GUI. Posted: Oct 7, 2005 12:53 AM
Reply to this message Reply
It worked adding the updateUI(); in the actionPerformed :)

Flat View: This topic has 7 replies on 1 page
Topic: Problem with Jackcess 1.0 Previous Topic   Next Topic Topic: An easy question,but I don't know

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use