Solved

GDI

Posted on 2007-03-29
4
231 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 500 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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java class and jar 3 59
What browser will run Java? 7 171
Eclipse neon2 "Java build path" correctness 7 43
Selenium WebElement Login button findElement 9 14
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

734 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