Page 1 of 3 123 LastLast
Results 1 to 20 of 44
  1. #1
    Mekonom is offline Member
    Join Date
    Nov 2009
    Posts
    71
    Rep Power
    0

    Default 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. #2
    zweibieren is offline Senior Member
    Join Date
    Aug 2009
    Location
    Pittsburgh, PA
    Posts
    284
    Rep Power
    6

    Default

    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. #3
    Mekonom is offline Member
    Join Date
    Nov 2009
    Posts
    71
    Rep Power
    0

    Default

    in this new list I must save coordinates of lines and ellips?

  4. #4
    zweibieren is offline Senior Member
    Join Date
    Aug 2009
    Location
    Pittsburgh, PA
    Posts
    284
    Rep Power
    6

    Default

    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);
    }
    When a or b is changed and the image redrawn, the line will move.

  5. #5
    Mekonom is offline Member
    Join Date
    Nov 2009
    Posts
    71
    Rep Power
    0

    Default

    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 ?

  6. #6
    zweibieren is offline Senior Member
    Join Date
    Aug 2009
    Location
    Pittsburgh, PA
    Posts
    284
    Rep Power
    6

    Default

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

    Default

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

  8. #8
    zweibieren is offline Senior Member
    Join Date
    Aug 2009
    Location
    Pittsburgh, PA
    Posts
    284
    Rep Power
    6

    Default

    MyPoint p = new MyPoint(e.getX(), e.getY());

  9. #9
    Mekonom is offline Member
    Join Date
    Nov 2009
    Posts
    71
    Rep Power
    0

    Default

    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. #10
    zweibieren is offline Senior Member
    Join Date
    Aug 2009
    Location
    Pittsburgh, PA
    Posts
    284
    Rep Power
    6

    Default

    And I quote:
    I googled "defining inner classes example" and got several useful examples.

  11. #11
    Mekonom is offline Member
    Join Date
    Nov 2009
    Posts
    71
    Rep Power
    0

    Default

    Thansk for helping :)))

  12. #12
    zweibieren is offline Senior Member
    Join Date
    Aug 2009
    Location
    Pittsburgh, PA
    Posts
    284
    Rep Power
    6

    Default

    See also Learning Java especially the bit abot Objects and Classes.

  13. #13
    Mekonom is offline Member
    Join Date
    Nov 2009
    Posts
    71
    Rep Power
    0

    Default

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

  14. #14
    zweibieren is offline Senior Member
    Join Date
    Aug 2009
    Location
    Pittsburgh, PA
    Posts
    284
    Rep Power
    6

    Default

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

    Default

    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. #16
    Mekonom is offline Member
    Join Date
    Nov 2009
    Posts
    71
    Rep Power
    0

    Default

    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. #17
    zweibieren is offline Senior Member
    Join Date
    Aug 2009
    Location
    Pittsburgh, PA
    Posts
    284
    Rep Power
    6

    Default

    If the mouse is up, there has been a mouseReleased() event.
    Take a look at what it does.

  18. #18
    Mekonom is offline Member
    Join Date
    Nov 2009
    Posts
    71
    Rep Power
    0

    Default

    yes...I delete it and tryed againg but it is againg not working..

  19. #19
    Mekonom is offline Member
    Join Date
    Nov 2009
    Posts
    71
    Rep Power
    0

    Default

    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. #20
    zweibieren is offline Senior Member
    Join Date
    Aug 2009
    Location
    Pittsburgh, PA
    Posts
    284
    Rep Power
    6

    Default

    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 LastLast

Similar Threads

  1. I need a Project Idea..
    By vinoth in forum New To Java
    Replies: 11
    Last Post: 11-08-2009, 01:06 AM
  2. I need a Project Idea..
    By vinoth in forum Java Software
    Replies: 5
    Last Post: 08-14-2009, 04:37 AM
  3. I have an idea !!
    By HosHos in forum New To Java
    Replies: 1
    Last Post: 08-12-2009, 06:38 AM
  4. Python SDK in IDEA 8
    By detonator413 in forum IntelliJ IDEA
    Replies: 0
    Last Post: 01-26-2009, 08:45 PM
  5. i couid not get the idea:
    By sivasayanth in forum New To Java
    Replies: 2
    Last Post: 01-18-2008, 05:52 PM

Posting Permissions

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