• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 267
  • Last Modified:

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
         
0
ZURINET
Asked:
ZURINET
  • 5
  • 2
1 Solution
 
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
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
 
CEHJCommented:
:-)
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now