# need an Idea ...

Show 40 post(s) from this thread on one page
Page 1 of 3 123 Last
• 11-15-2009, 05:20 PM
Mekonom
need an Idea ...
Hi I have eliips and they are connected with lines ..3 elips 2 lines .. I have array lists for eliips and lines .and I can move eliips.but I want to move lines too when I move elipps. But problem is for example when I move ellips whic stands on corner ,it has connected two line. I try to move lines if ellips is on upped side of line then I change only X1 and Y1 coordinates to position of mouse. But when I sellect center ellips there is two line and how can choose that one line will change only X1 and Y1 and the other will change X2 and Y2 ... Any idea??
• 11-15-2009, 09:39 PM
zweibieren
Quote:

I have array lists for eliips and lines
Here's an idea: have another list of points.
The elipses and lines should not have their own points
but instead refer to the points in the list.
Then changing a point will change
the location of lines and ellipses that use that point.
• 11-15-2009, 10:11 PM
Mekonom
in this new list I must save coordinates of lines and ellips?
• 11-17-2009, 07:42 AM
zweibieren
for each point: store x and y
for each line: store the index in the above points of the two end points
for each ellipse: store the index in the above points of the corner points

an ellipse can be defined by corner points
or by the foci and the end points
or in several other ways

instead of storing an index, just store the Point object itself
(a Point object can be stored in more than one place)

Suppose a line is defined by Point objects a and b
Then store a and b as the endpoints of the line
and also store a and b in the array of points.
Code:

```void createLine(Point a, Point b) {     // make a new entry in the list of lines     lineList.add(new Line(a,b));     pointList.add(a);     pointList.add(b); }```
When a or b is changed and the image redrawn, the line will move.
• 11-17-2009, 10:52 PM
Mekonom

here Line(a,b) wil lbe my function or it is from library? because I use Line2d(x,y,x2,y2). it is with 4 value but your example with 2 ?. a and b is point and have x,y .I mus use like
Line2D(a.x,a.y,b.x,b.y) ; ? And I must do it for my ellipses too? create such function ?
• 11-18-2009, 12:47 AM
zweibieren
Yes, you will create objects to represent lines and ellipses.
You may even want your own Point object.
Defining objects just for your project is valuable,

Objects can be defined within outer objects. Static or not.
I googled "defining inner classes example" and got several useful examples.
In the following example I defined a class just to associate a tagname and its order value.
Code:

```        static class OrderData {                 String [COLOR="Green"]tagname[/COLOR]; double [COLOR="Green"]order[/COLOR];                 OrderData(String s, double d) { tagname = s; order = d; }         }         static OrderData [] odata = {                 new OrderData("Condition.Add", 11.0),                 new OrderData("Condition.Color", 12.0),                 new OrderData("Condition.Attr.Caption", 13.0),                 ...         };         public static void main(String args[]) {                 TagFiles tf = Tagger.getStandardTagFiles();                 try {                         Writer wr = new java.io.OutputStreamWriter(System.out);                         // replace selected tags with OrderTag tags                         for (OrderData od : odata) {                                 // replace the tag named od.tagname                                 // with an OrderTag having the sortOrder value order                                 Tag existingTag = tf.root.findQChild(od.tagname);                                 OrderTag newt = new OrderTag();                                 newt.name = existingTag.name;                                 newt.sortOrder = od.order;                                 existingTag.replace(newt);                                 newt.xmlBody(wr, "");                                 wr.flush();                         }                         Tagger.saveStandardTagFiles(tf);                                        wr.close();                 } catch (IOException ex) {                     System.err.println("oops " + ex);                 }         }```
Every Tag object has a sortOrder field;
but some Tags have pre-defined sortOreder values.
To preserve these values when reading/writing Tags,
those special tags are of sub-type Tag.OrderTag.
Here OrderTag is another little class.
I needed it to override the serialization methods.
It extends Tag.Fat, which is the class of all Tags
that have serialization data.

Code:

```        static class OrderTag extends Tag.Fat {                 // SUBCLASS SERIALIZATION                 @Override                 public String getSerializationParameters() {                         return "" + sortOrder;                 }                 @Override                 public void setSerializationParameters(String parms) {                         try {                                 sortOrder = Double.parseDouble(parms);                         } catch (NumberFormatException ex) {                                 Util.log.severe(                                                 "Illegal sortOrder value: " + parms);                         }                 }         }```
OrderData and OrderTag are both static.
They are independent of any outer Tag value.
OrderTag is itself a Tag (specifically a Tag.Fat),
but no Tag needs to be provided when creating an OrderTag.
• 11-18-2009, 01:14 AM
Mekonom
for each point: store x and y
for each line: store the index in the above points of the two end points
for each ellipse: store the index in the above points of the corner points

here I tryed to store x and y for poınt.But when I wrote

void createPoint(int x ,int y){
// here I dont decide how can i get mouse event here to take coordinates? Like Point p = e.getSource();
}
• 11-18-2009, 02:48 AM
zweibieren
MyPoint p = new MyPoint(e.getX(), e.getY());
• 11-18-2009, 02:18 PM
Mekonom
Here you used MyPoint..is it class? do u prefer to create new class for poınts and use them as a object?.And can I use this e.getX() out of mouse listener? or it must be in mouse listener?
• 11-18-2009, 06:41 PM
zweibieren
And I quote:
Quote:

I googled "defining inner classes example" and got several useful examples.
• 11-18-2009, 07:38 PM
Mekonom
Thansk for helping :)))
• 11-19-2009, 12:44 AM
zweibieren
• 11-19-2009, 05:24 PM
Mekonom
Hi again..I made also it working ..but now mouseDraged not working..ıt was working before but now every time if(dragging) returns false...I recognized that when i press mosu it runs it two times . may be is it problem?

Code:

```public class ShapeControl extends MouseInputAdapter {     MainPanel selectablePanel;     public boolean dragging, ChoosedBooth, firstClicked, secondClicked, lineSelected;     Point selectedPoint;     Line2D current_line;     List lineList;     List ellipsList;     List pointList;     Point mainPoint, a, b;     public ShapeControl(MainPanel mp) {         pointList = new ArrayList();         ellipsList = new ArrayList();         lineList = new ArrayList();         selectablePanel = mp;         dragging = false;         firstClicked = false;         secondClicked = false;     }     public void initLine(Point a, Point b) {         lineList.add(new Line2D.Double(a, b));     }     public void initCircle(Point c) {         int w = 50;         int h = 50;         ellipsList.add(new Ellipse2D.Double(c.x - 25, c.y - 25, w, h));     }     @Override     public void mouseClicked(MouseEvent e) {         Point p = e.getPoint();         if (selectablePanel.circleSelected) {             if (selectablePanel.clicked) {                 Point center = new Point(e.getX(), e.getY());                 mainPoint = center;                 pointList.add(center);                 initCircle(center);                 selectablePanel.clicked = false;             }         }         if (selectablePanel.drawlineSelected) {             for (int j = 0; j < ellipsList.size(); j++) {                 Ellipse2D.Double circle = (Ellipse2D.Double) ellipsList.get(j);                 if (e.getClickCount() == 2) {                     firstClicked = true;                     if (firstClicked) {                         Point point = (Point) pointList.get(j);                         if (circle.contains(p)) {                             a = point;                             firstClicked = false;                             secondClicked = true;                         }                     }                 } else if (secondClicked) {                     Point point = (Point) pointList.get(j);                     if (circle.contains(p)) {                         b = point;                         firstClicked = true;                         secondClicked = false;                         initLine(a, b);                     }                 }             }         }     }     public void mousePressed(MouseEvent e) {         Point p = e.getPoint();         for (int j = 0; j < pointList.size(); j++) {             Point point = (Point) pointList.get(j);             if (p.x <= point.x + 25 && p.x >= point.x - 25 && p.y <= point.y + 25 && p.y >= point.y - 25) {                 selectedPoint = point;                 dragging = true;                 break;             }         }     }     public void mouseReleased(MouseEvent e) {         dragging = false;     }     public void mouseDragged(MouseEvent e) {         Point p = e.getPoint();         if (dragging) { //HERE IT RETURNS ALWAYS  false ...             selectedPoint = p;         }         selectablePanel.repaint();     } }```
• 11-19-2009, 06:46 PM
zweibieren
It looks like the code expects to drag a point while no mouse button is down.
To do that, the code should override mouseMoved instead of mouseDragged.

The code also mixes pressed, released, and clicked.
I've never made that work. I usually override only pressed and released.
• 11-19-2009, 07:19 PM
Mekonom
it means I will create new functıon lıke mouseMove? I did not understand ıt was working with previous impemantation..I did not change any thing..Only added new list....strange..:confused:
• 11-19-2009, 07:34 PM
Mekonom
I used like this
Code:

```    @Override     public void mouseMoved(MouseEvent e) {       super.mouseMoved(e);           Point p = e.getPoint();         if (dragging) { //HERE IT RETURNS ALWAYS  false ...  System.out.print("MOVINGGGGGGGGGG");             selectedPoint = p;         }         selectablePanel.repaint();     }```
but ıt doesnt work ...dragging again zero :((
• 11-19-2009, 09:50 PM
zweibieren
If the mouse is up, there has been a mouseReleased() event.
Take a look at what it does.
• 11-19-2009, 10:03 PM
Mekonom
yes...I delete it and tryed againg but it is againg not working..
• 11-19-2009, 10:08 PM
Mekonom
Also I am changing dragging = true in mouseMove but it is again false.
Code:

```    @Override     public void mouseMoved(MouseEvent e) {       super.mouseMoved(e);             dragging=true;//HERE           Point p = e.getPoint();         if (dragging) { //HERE IT RETURNS ALWAYS  false ...  System.out.print("MOVINGGGGGGGGGG");             selectedPoint = p;         }         selectablePanel.repaint();     }```
• 11-19-2009, 11:01 PM
zweibieren
One problem area is this line:
Quote:

public boolean dragging, ChoosedBooth, firstClicked, secondClicked, lineSelected;
There are two many booleans and not enough comments.
Some of the booleans should probably be replaced with a state variable.
All of the booleans and states should be documented with invariants;
that is, statements as to what it means for that boolean or state to be set.

Example for the "dragging" variable (I don't know if this is correct):
When dragging is set, the mouse is down,
the mouse is in selectedPanel,
selectedPoint is set to a point that is being dragged,
and the cursor is within 25 pixels of the current value of the selected point.

Example of a state variable: replace firstClicked and secondClicked
with a varible that has three or four possible values depending
on how many clicks there have been.

After clarifying the variables, it is a "simple" (LOL) matter of checking the code
to be sure that the invariants are never violated.
That is, the invariant must be true on exit from any of the mouse event methods.
Show 40 post(s) from this thread on one page
Page 1 of 3 123 Last