?
Solved

GDI

Posted on 2007-03-29
4
Medium Priority
?
238 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
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…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month9 days, 21 hours left to enroll

762 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