-
creating diagram editor
Hi all.. I want to create diagram editor ..there will be rectaangles and I will connect them with clikcing each of them . I cerated something but on scren I can not choose rectangles ..It creates but not recognıze previous one or some one whic ı clicked whit mouse..any idea to correct this? or other way to creat such project?
-
If your code just draws rectangles,
then there will indeed be no way to select one with the mouse.
Your code must keep track of what rectangle have been drawn
and then check this list when a mouse click arrives.
In one application, there is a list of trees, each represented as an object.
Each object has an associated hotSpot -- that is,
a Shape (circle) at a particular coordinate location.
One particular tree is the value of the global variable hotTree.
This tree is drawn in a different color.
Drawing the diagram and trees looks like this:
Code:
@Override
public void paint(Graphics g) {
g.drawImage(diagram, 0, 0, this);
// draw all the items on the Tree list
for (Tree t : trees) {
gOff.setColor((t==hotTree) ? hotColor : treeColor);
gOff.fill(t.hotSpot);
}
}
The mouse movement event finds out which tree, if any, is near the mouse.
That tree becomes the value of hotTree.
Code:
public void mouseMoved(MouseEvent e) {
TreeInfo oldHot = hotTree;
hotTree = null;
// if we are in range of one of 'trees', set hotTree
for (Tree t : trees) {
if (t.hotSpot.contains(1.0 * e.getX(), 1.0 * e.getY())) {
hotTree = tree;
break;
}
}
if (oldHot != hotTree) repaint();
}
-
Thans for reply but I try to reach object lıke creatıng array lıst and addıng them . then usıng Poınt p = e.getpoint and compareing if(s.contains(p)). but here problem is getlocation allways return 0.0 it isthe top left of window .when I am creating object it creates on screen but when I ask its location it says me that is on 0.0 point.But it works when I used Rectangle2d class from library .it gaves real corrdinates and choosing work.BUt I want to create my own object and rectangle :)
-
You can create your own objects with four rectangle coordinates.
Or you might create a subclass of Rectangle.
The previous post mentions "if(s.contains(p))",
but fails to say what s is.
It would have to be a Shape or rectangle
describing where the rectange was drawn in the window.
-
yes.. it is Shape s = new Shape().... but it allways 0.0 point .strange :)
-
The default location for a Shape is 0,0.
If the code draws it at x,y, then the Shape origin is still 0,0.
Only if the code sets the x,y value in the Shape will it have some other origin.
-
but ı am movıng ıt whıt mouse .. Whıt button creatıng lots of shape and movıng ıt wıth mouse. how orgın can be 0.0?
-
I tryed to set corrdinates after creating and moving it with mouse but it adds new value then removes and 0.0