Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

GDI

Posted on 2007-03-29
4
Medium Priority
?
243 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
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:
Suggested Courses

636 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