?
Solved

GDI

Posted on 2007-03-29
4
Medium Priority
?
245 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
ID: 18817328
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
ID: 18831258
>>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 2000 total points
ID: 18833822
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
ID: 18834602
Thank you *covey* :o)

Really appreciate your help ..
regards,
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Suggested Courses
Course of the Month13 days, 12 hours left to enroll

755 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