# Thread: need an Idea ...

1. Member
Join Date
Nov 2009
Posts
71
Rep Power
0

## 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??

2. Senior Member
Join Date
Aug 2009
Location
Pittsburgh, PA
Posts
284
Rep Power
4
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.

3. Member
Join Date
Nov 2009
Posts
71
Rep Power
0
in this new list I must save coordinates of lines and ellips?

4. Senior Member
Join Date
Aug 2009
Location
Pittsburgh, PA
Posts
284
Rep Power
4
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.
Java Code:
```void createLine(Point a, Point b) {
// make a new entry in the list of lines
}```
When a or b is changed and the image redrawn, the line will move.

5. Member
Join Date
Nov 2009
Posts
71
Rep Power
0

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 ?

6. Senior Member
Join Date
Aug 2009
Location
Pittsburgh, PA
Posts
284
Rep Power
4
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.
Java 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.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.

Java 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.

7. Member
Join Date
Nov 2009
Posts
71
Rep Power
0
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();
}

8. Senior Member
Join Date
Aug 2009
Location
Pittsburgh, PA
Posts
284
Rep Power
4
MyPoint p = new MyPoint(e.getX(), e.getY());

9. Member
Join Date
Nov 2009
Posts
71
Rep Power
0
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?

10. Senior Member
Join Date
Aug 2009
Location
Pittsburgh, PA
Posts
284
Rep Power
4
And I quote:
I googled "defining inner classes example" and got several useful examples.

11. Member
Join Date
Nov 2009
Posts
71
Rep Power
0
Thansk for helping :)))

12. Senior Member
Join Date
Aug 2009
Location
Pittsburgh, PA
Posts
284
Rep Power
4

13. Member
Join Date
Nov 2009
Posts
71
Rep Power
0
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?

Java 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) {

}

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;
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();
}
}```

14. Senior Member
Join Date
Aug 2009
Location
Pittsburgh, PA
Posts
284
Rep Power
4
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.

15. Member
Join Date
Nov 2009
Posts
71
Rep Power
0
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:

16. Member
Join Date
Nov 2009
Posts
71
Rep Power
0
I used like this
Java 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 :((

17. Senior Member
Join Date
Aug 2009
Location
Pittsburgh, PA
Posts
284
Rep Power
4
If the mouse is up, there has been a mouseReleased() event.
Take a look at what it does.

18. Member
Join Date
Nov 2009
Posts
71
Rep Power
0
yes...I delete it and tryed againg but it is againg not working..

19. Member
Join Date
Nov 2009
Posts
71
Rep Power
0
Also I am changing dragging = true in mouseMove but it is again false.
Java 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();
}```

20. Senior Member
Join Date
Aug 2009
Location
Pittsburgh, PA
Posts
284
Rep Power
4
One problem area is this line:
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.

Page 1 of 3 123 Last

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•