Updating Graphic object fillOval through mouseClick

Hill all

I have this Circle Object  with variables
(myColor, xValue, yValue, rValue)

 This Circle Object is have a class of it own with
a constructor:   public Circle(Color c, int x, int y, int r)

I have in  public void paint(Graphics g)    
   
 g.fillOval(myCircle.getX(),myCircle.getY(),  myCircle.getRadius(), myCircle.getRadius()); // myCircle is an Instance of Circle Object
     
Now the big Question is!!!, How can I dynamically change the position of myCircle and repaint it
When the mouse is click on the screen.        

Knowing that I have
public void mouseClicked (MouseEvent m)
with m.getX( ); and m.getX( );

A tutorial or Code will be helpfull
         
ZURINETAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
All you need to do is capture the mouse click in a MouserListener and call repaint after changing the instance variables of your Circle class. Override paintComponent, not paint if using a JPanel subclass
0
CEHJCommented:
The code there would be something like the following:
public void mouseClicked (MouseEvent m) {
	myCircle.setX(m.getX());
	myCircle.setY(m.getY());
	repaint();
}

Open in new window

0
ZURINETAuthor Commented:
Thanks CEHJ..

For reasons of specification, myCircle has no set method. :-(

What I decided to do is to create a new class that takes the instance of myCircle and Point p
see code below.


The question is How do I build or call an instance of myCircles() ?

using the class circle "public Circle(Color c, int x, int y, int r)"
g.fillOval(myCircle.getX(),myCircle.getY(),  myCircle.getRadius(), myCircle.getRadius()); // myCircle is an Instance of Circle Object

Hence "new myCircles()" will produce/create an instance of myCircles.

Hope this description is not complicated.

Thanks in Advance
Class BuildCircles 
{
 myCircles c;
 Point p;
}
 
 public  BuildCircles (myCircles myc, Point po)
{
 c = myc;
 p = po;
}
 
 
public void mousePressed (MouseEvent event)
 
//ArrayList<BuildCircles> arrayMyCircles
 
arrayMyCircles.add(new myCircles(new myCircles(), event.getPoint) ));
repaint();
....................bla bla bla

Open in new window

0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

CEHJCommented:
>>For reasons of specification, myCircle has no set method. :-(

That sounds odd. If the instance variables in that class are private then you'll have to use the constructor and create new instances of it every time - not efficient, but it'll  work
0
ZURINETAuthor Commented:
Some example code will be nice :-)
Just in a hurry
0
CEHJCommented:
Your own code is more or less OK, but i don't see why you need yet another class, so

>>ArrayList<BuildCircles> arrayMyCircles

should be

ArrayList<myCircle> arrayMyCircles

and declared as an instance variable, not in that method. The following will then do fine:

>>
arrayMyCircles.add(new myCircles(new myCircles(), event.getPoint) ));
repaint();
>>

btw, class names should begin with a capital letter

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CEHJCommented:
:-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.