TechArena Community

TechArena Community (http://forums.techarena.in/)
-   Software Development (http://forums.techarena.in/software-development/)
-   -   Diagram editor in java (http://forums.techarena.in/software-development/1270659.htm)

SoftWore 13-11-2009 11:52 AM

Diagram editor in java
 
Hi
I have done some programs in java. Now I want to create a diagram editor. My idea is , there will be a rectangle and I will connect them with clicking each of them. I created a rectangle on the screen but I can not choose rectangles. It creates but not recognıze previous one or some one which I clicked with the mouse. Any idea to correct this problem or nay other way round to create such a project. any help will be appreciated. Thanks in advance.

Praetor 13-11-2009 12:01 PM

Re: Diagram editor in java
 
You said 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 the example there is a list of trees, each represented as an object. Each object has an associated hotsp i.e. a Shape (circle) at a particular coordinate location. One particular tree is the value of the global variable hottr. This tree is drawn in a different color.
Code:

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.hotsp);
                }
        }

Code:

public void mouseMoved(MouseEvent e) {
                TreeInfo oldHot = hottr;
                hottr = null;
                // if we are in range of one of 'trees', set hottr
                for (Tree t : trees) {
                        if (t.hotSpot.contains(1.0 * e.getX(), 1.0 * e.getY())) {
                                hottr = tree;
                                break;
                        }
                }
                if (oldHot != hottr) repaint();
        }


SoftWore 13-11-2009 12:04 PM

Re: Diagram editor in java
 
Thanks for your reply.
I tried to create object like creating array list and adding them, then usıng Poınt p = e.getpoint and compareing if(s.contains(p)). But the problem is getlocation always return 0.0 it is located at the top left of the 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 gave real coordinates and choosing work.But I want to create my own object and rectangle

Praetor 13-11-2009 12:07 PM

Re: Diagram editor in java
 
Hi
The best idea is to 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 rectangle was drawn in the window. Check your program for the same and if any problem then post by your query.


All times are GMT +5.5. The time now is 12:24 AM.