Results 1 to 20 of 44
Thread: need an Idea ...
- 11-15-2009, 05:20 PM #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??
- 11-15-2009, 09:39 PM #2
Senior Member
- Join Date
- Aug 2009
- Location
- Pittsburgh, PA
- Posts
- 285
- Rep Power
- 12
I have array lists for eliips and lines
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 #3
Member
- Join Date
- Nov 2009
- Posts
- 71
- Rep Power
- 0
in this new list I must save coordinates of lines and ellips?
- 11-17-2009, 07:42 AM #4
Senior Member
- Join Date
- Aug 2009
- Location
- Pittsburgh, PA
- Posts
- 285
- Rep Power
- 12
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 lineList.add(new Line(a,b)); pointList.add(a); pointList.add(b); }
- 11-17-2009, 10:52 PM #5
Member
- Join Date
- Nov 2009
- Posts
- 71
- Rep Power
- 0
lineList.add(new Line(a,b));
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 #6
Senior Member
- Join Date
- Aug 2009
- Location
- Pittsburgh, PA
- Posts
- 285
- Rep Power
- 12
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,
because you can add additional fields and methods as needed.
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.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); } }
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); } } }
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 #7
Member
- Join Date
- Nov 2009
- Posts
- 71
- Rep Power
- 0
I see...But I have questıon about your ıdea..you sad
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 #8
Senior Member
- Join Date
- Aug 2009
- Location
- Pittsburgh, PA
- Posts
- 285
- Rep Power
- 12
MyPoint p = new MyPoint(e.getX(), e.getY());
- 11-18-2009, 02:18 PM #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?
- 11-18-2009, 06:41 PM #10
Senior Member
- Join Date
- Aug 2009
- Location
- Pittsburgh, PA
- Posts
- 285
- Rep Power
- 12
And I quote:
I googled "defining inner classes example" and got several useful examples.
- 11-18-2009, 07:38 PM #11
Member
- Join Date
- Nov 2009
- Posts
- 71
- Rep Power
- 0
Thansk for helping :)))
- 11-19-2009, 12:44 AM #12
Senior Member
- Join Date
- Aug 2009
- Location
- Pittsburgh, PA
- Posts
- 285
- Rep Power
- 12
See also Learning Java especially the bit abot Objects and Classes.
- 11-19-2009, 05:24 PM #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) { 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 #14
Senior Member
- Join Date
- Aug 2009
- Location
- Pittsburgh, PA
- Posts
- 285
- Rep Power
- 12
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 #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:
- 11-19-2009, 07:34 PM #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(); }
- 11-19-2009, 09:50 PM #17
Senior Member
- Join Date
- Aug 2009
- Location
- Pittsburgh, PA
- Posts
- 285
- Rep Power
- 12
If the mouse is up, there has been a mouseReleased() event.
Take a look at what it does.
- 11-19-2009, 10:03 PM #18
Member
- Join Date
- Nov 2009
- Posts
- 71
- Rep Power
- 0
yes...I delete it and tryed againg but it is againg not working..
- 11-19-2009, 10:08 PM #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(); }
- 11-19-2009, 11:01 PM #20
Senior Member
- Join Date
- Aug 2009
- Location
- Pittsburgh, PA
- Posts
- 285
- Rep Power
- 12
One problem area is this line:
public boolean dragging, ChoosedBooth, firstClicked, secondClicked, lineSelected;
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.
Similar Threads
-
I need a Project Idea..
By vinoth in forum New To JavaReplies: 11Last Post: 11-08-2009, 02:06 AM -
I need a Project Idea..
By vinoth in forum Java SoftwareReplies: 5Last Post: 08-14-2009, 05:37 AM -
I have an idea !!
By HosHos in forum New To JavaReplies: 1Last Post: 08-12-2009, 07:38 AM -
Python SDK in IDEA 8
By detonator413 in forum IntelliJ IDEAReplies: 0Last Post: 01-26-2009, 09:45 PM -
i couid not get the idea:
By sivasayanth in forum New To JavaReplies: 2Last Post: 01-18-2008, 06:52 PM
Bookmarks