Solved

GDI

Posted on 2007-03-29
4
213 Views
Last Modified: 2010-03-31
Hi Experts ..

Assume that I draw a polygon in a panel (swing app), How can  I change the color of the polygon when it is clicked??  I have image/map in a panel, the user draw polygon or rectangle to surround an area in the map!! I want to allow  the user to be able to move this polygon or rectangle!! so when he clicks on it, its color changed showing that it is selected!! and he can move it!!
At the first stage I want to know how can I change its color??  
0
Comment
Question by:DrAske
  • 2
  • 2
4 Comments
 
LVL 3

Expert Comment

by:cavey_79
Comment Utility
This is a bit more difficult than it seems. First you would need some custom code to figure out which polygon was selected - that involves basically checking if the click point is very close to any of the polygon's edges.


Then, you would need to remember which polygon was clicked and inside the method you do the drawing, change the color before drawing and then change it back to black (or whatever you're using)

for (Polygon p : polygons){
if (p.isSelected()){
graphics.setColor(Color.RED);
}
//your usual polygon drawing stuff code here
if(p.isSelected()){
graphics.setColor(Color.BLACK);
}
}
0
 
LVL 9

Author Comment

by:DrAske
Comment Utility
>>that involves basically checking if the click point is very close to any of the polygon's edges.
I think this can be done, but what about circle?? how can I determine If I click inside it?? it doesn't have edges??
0
 
LVL 3

Accepted Solution

by:
cavey_79 earned 500 total points
Comment Utility
You will only want clicks on the edge of the circle, that means distance to center = circle radius.
for (Circle c: circles){
  if distanceTo(clickPoint, c.getCenter())==c.getRadius){
  c.setSelected(true);
}
}

You can replace this:
  if distanceTo(clickPoint, c.getCenter())==c.getRadius){
to
   if Math.abs(distanceTo(clickPoint, c.getCenter())-c.getRadius))<1{

Play with different values of the last number (1) to see which make sense.
0
 
LVL 9

Author Comment

by:DrAske
Comment Utility
Thank you *covey* :o)

Really appreciate your help ..
regards,
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Java virtual machine monitoring tools 3 39
Receive file in Servlet 1 36
powerN  challenge 3 46
count11 challenge 6 47
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now