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

    Default

    i seperate them .and when debuging i saw that first dragging is false ,then in mousePressed it change it to true ,but after leaving mousePressed it changes again false.IN my code only in ShapeControl() function has dragging=false..it is main funtion.aftes finishing moussePressed it returns it? and changes to false?but for dragging first i must press and drag.. i think my implementation of dragging is on true position.but i tryed every thing and i cant do it..with same codeds it was working.I only changed here like you sad added new point list and when dragging i am changing thoose point.why not eork i dont understand.

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

    Default

    Hi again..with long thinking term I found my mistake :D I forget add mousemotionListener to my object .I had only Mouse Listener. :) now drag working but my new problem is my prewous problem ..When I creat 3 circle and draw line it draws .whem drag eliips ellips and line movin .but when I draw new line I can only move this last line and when I move ellips whic I created preveously It moves but without line. :( I think my current line work not good .It forgets previous ..and I dont know ıf on my ellips had 2or more line connected how it coud remember them and move together?
    Java Code:
    public class ShapeControl extends MouseInputAdapter {
    
        MainPanel selectablePanel;
        public boolean dragging, ChoosedBooth, firstClicked, secondClicked, lineSelected, flag;
        Point selectedPoint;
        Line2D current_line;
        Ellipse2D selectedEllips;
        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));
            pointList.add(a);
            pointList.add(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());
    
                    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);
                         
                           for (int i = 0; i < lineList.size(); i++) {
                                Line2D.Double line = (Line2D.Double) lineList.get(i);
                                current_line = line;    //Current LINE HERE
    
                            }
                        }
                    }
                }
    
            }
    
        }
    
        public void mousePressed(MouseEvent e) {
            Point p = e.getPoint();
    
            for (int j = 0; j < pointList.size(); j++) {
                Point point = (Point) pointList.get(j);
                Ellipse2D.Double circle = (Ellipse2D.Double) ellipsList.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;
                    selectedEllips = circle;
    
                    dragging = true;
                    System.out.print("FOund");
                    break;
    
                }
            }
        }
    
        public void mouseReleased(MouseEvent e) {
            dragging = false;
        }
    
        public void mouseDragged(MouseEvent e) {
    
            Point p = e.getPoint();
    
            if (dragging == true) { 
              
    
                selectedPoint.setLocation(p);
                selectedEllips.setFrame(p.x - 25, p.y - 25, 50, 50);
                current_line.setLine(a, b);
            }
    
            selectablePanel.repaint();
        }
    }

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

    Default

    Your code cannot become correct without improvements to the documentation.
    Especially for the line
    public boolean dragging, ChoosedBooth, firstClicked, secondClicked, lineSelected, flag;

    ----

    As to the problem with ellipses not moving as their points are moved:

    Let's look at mouseClicked():
    Java Code:
        public void mouseClicked(MouseEvent e) {
                    Point center = new Point(e.getX(), e.getY());
                    pointList.add(center);
                    initCircle(center);
                    selectablePanel.clicked = false;
    The code adds center to the list of points. Good.
    Presumably the x and y values in center are modified as the point is dragged. Also good.

    Now consider initCircle(). An ellipsis is stored in ellipsList.
    Java Code:
        public void initCircle(Point c) {
            int w = 50;
            int h = 50;
            ellipsList.add(new Ellipse2D.Double(c.x - 25, c.y - 25, w, h));
        }
    BUT, the value in the ellipsList is just an Ellipse2D object.
    It is initialized from center, but retains no knowledge of center.

    The ellipsList should be storing an application specific object, say MyEllipse,
    which stores the center object itself. When the paintComponent()
    method paints a MyEllipse object it should do a drawEllipse operation
    based on the then-current value in the center object.
    This value will have changed if the center object has had new values inserted by mouse drag.
    (The MyEllipse object may also have its own Ellipse2D object to use for checking if the mouse is near that ellipse.)

    {The C language makes a distinction between an object and a reference to that object.
    Java lacks this distinction in C. This can be confusing.
    (References are also called pointers, links, handles, addresses, and more.)
    An assignment like storing object center into a list does not store the object into a list.
    It stores a reference to the object. The object exists elsewhere on its own and the list has only a reference to it.
    At the same time we can store object center into a MyEllipse object.
    Again, the object exists separately and the MyEllipse has only a reference to it.
    So if a mouse move finds center in the list and changes its coordinate values x and y,
    then when paintComponent() paints the MyEllipse object that refers to center
    it will paint the ellipse at the new location.}
    Last edited by zweibieren; 11-22-2009 at 02:57 AM.

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

    Default

    But my ellips is moving..it doesnt mean that it take coordinates correct?

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

    Default

    Is the ellipse moving or are new ellipses being created?

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

    Default

    Elips moving .I add jar fıle of my project ..you can see that ıf two ellipses connected ,then I can move each of them and the line moving too.But when I connect new ellips with one of thoose ellipses then ony moves new ellips and connected ellips with line..Also I tryed like creating new clas name MyEllips
    Java Code:
    public class MyEllips extends JPanel {
    
        int posX;
        int posY;
        int width;
        int height;
    
        public MyEllips(Point c) {
            setBorder(BorderFactory.createLineBorder(Color.RED));
            posX = c.x;
            posY = c.y;
    
        }
    
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
    
            g.setColor(Color.RED);
            g.fillRect(posX, posY, width, height);
            g.setColor(Color.RED);
            g.drawRect(posX, posY, width, height);
    
        }
    }
    And im my Shape control panel instead of adding ellipsList new Ellips2d ,I add like ellipsList.add(new MyEllips(c)); C is point . But now My main paintComponent on MyPanel doesnt draw this ellips :(
    Java Code:
    public class MainPanel extends JPanel implements ActionListener {
    
        private JButton XvalueButton = new JButton("Circle");
        private JButton YvalueButton = new JButton("Draw");
        private JButton ZvalueButton = new JButton("Z");
        private ShapeControl shape = new ShapeControl(this);
        public boolean circleSelected, drawlineSelected, clicked;
        double LineCenter;
        double a, b;
    
        public MainPanel() {
    
            circleSelected = false;
            drawlineSelected = false;
    
            XvalueButton.addActionListener(this);
            YvalueButton.addActionListener(this);
            ZvalueButton.addActionListener(this);
    
    
            add(XvalueButton);
            add(YvalueButton);
            add(ZvalueButton);
            addMouseMotionListener(shape);
            addMouseListener(shape);
        }
    
        public void actionPerformed(ActionEvent e) {
    
            if (e.getSource() == XvalueButton) {
                circleSelected = true;
                //shape.initCircle(shape.p);
                clicked = true;
            }
            if (e.getSource() == YvalueButton) {
    
                drawlineSelected = true;
    
            }
        }
    
        @Override
        protected void paintComponent(Graphics g) {
    
    
            if (circleSelected) {
    
                for (int i = 0; i < shape.ellipsList.size(); i++) {
                    MyEllips circle = (MyEllips) shape.ellipsList.get(i);
                    circle.getGraphics();  //HERE IT MUS GET GRAPHICS OR DRAW MY ELLIPS
                    repaint();
                }
            }
            if (drawlineSelected) {
    
                for (int i = 0; i < shape.lineList.size(); i++) {
                    //  g2.draw((Shape)shape.lineList.get(i));
                }
            }
    
            repaint();
        }
    }

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

    Default

    Sorry site doesnt allow to send file :( I could not send it .

  8. #28
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    It will allow you to send text files and zip files. If it is java code, Jar it (with source code!) then change the extension to zip and upload.

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

    Default

    I added. With source codes and jar file name Paint.1.0.0
    Attached Files Attached Files

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

    Default

    Note that MyEllips is NOT storing Point c. It is storing 4 integers.
    Instead store c and extract the integers at paint time.
    Java Code:
    public class MyEllips extends JPanel {
        [COLOR="SeaGreen"]Point c;[/COLOR]
        int width, height;    // Not initialized ???
        public MyEllips(Point center) {
            setBorder(BorderFactory.createLineBorder(Color.RED));
            c = center;
        }
    
        protected void paintComponent(Graphics g) {
            [COLOR="SeaGreen"]int posX = c.x, posY = c.y;[/COLOR]
    
            super.paintComponent(g);
            g.setColor(Color.RED);
            g.fillRect(posX, posY, width, height);
            g.setColor(Color.RED);
            g.drawRect(posX, posY, width, height);
        }
    }
    
    int downX, downY;
    Point movingPoint = null;
    
    mousePressed(MouseEvent e){
       if (...mouse is near Point [I]target[/I]...) {
          downX = e.getX();
          downY = e.getY();
          movingPoint = target;
       }
       . . . 
    }
    mouseReleased(MouseEvent e) {
       if (movingPoint != null) {
    [COLOR="Blue"]      movingPoint.x += e.getX() - downX;
          movingPoint.y += e.getY() - downY;[/COLOR]
          movingPoint = null;
       }
       . . . 
    }
    Now when the point c changes by dragging it, the ellipse will be painted in its new location.

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

    Default

    I understood.But I use mouse presed and released ın ShapeControl class.They will mix?or I must create in ShapeControl class new object of thıs class ? or I mus write this implementatin intead of my ShapeClass?I confuzed :((

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

    Default

    The code I wrote is notional. Adapt it for your own use.
    Probably it should be part of the existing mouse event handlers.
    The important concept is to capture the mouse down location
    and then adjust movingPoint with the blue code.
    Last edited by zweibieren; 11-22-2009 at 05:13 PM.

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

    Default

    As I understood I must write same as for lines too.

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

    Default

    As I understood I must write same as for lines too.
    If lines are to move with their Points,
    then the lines code must be similar to the Ellipse code.
    There only needs to be one set of code for moving Points.

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

    Default

    Moving point I have done .But now in MyEllips class I have paintComponent. and my mouselistener on Shape Control ..When mouse clicked I wnat to draw ellips .. I have done like this
    Java Code:
      public void mouseClicked(MouseEvent e) {
            Point p = e.getPoint();
            if (selectablePanel.circleSelected) {
                if (selectablePanel.clicked) {
                    Point center = new Point(e.getX(), e.getY());
    
                    pointList.add(center);
                    initCircle(center); 
                    selectablePanel.clicked = false;
    
                }
            }
    And init circle

    Java Code:
       public void initCircle(Point c) {
         
            
            
            ellipsList.add(new MyEllips(c));
              
            
    
        }
    I must add some where
    Java Code:
      for (int i = 0; i < shape.ellipsList.size(); i++) {
                    MyEllips circle = (MyEllips) shape.ellipsList.get(i);
                  circle.getGrapgichs();  // 
                   repaint();
                }
    Like this? I couldent draw on secreen ellips. it creads and add it too list and on screen when I clik it founds this point and moves but there no ellips ...Where mus I have use this paintComponent?

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

    Default

    call repaint()
    see Tutorial

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

    Default

    I done my initcircle funtion like this
    Java Code:
     public void initCircle(Point c) {
         
            MyEllips myellips = new MyEllips(c);
          
            ellipsList.add(myellips);
           myellips.setBackground(Color.red);
           selectablePanel.add(myellips);
           myellips.setVisible(true);
    
        }
    and used repaint() methot
    Java Code:
     public void actionPerformed(ActionEvent e) {
    
            if (e.getSource() == XvalueButton) {
                circleSelected = true;
            
                clicked = true;
                 for (int i = 0; i < shape.ellipsList.size(); i++) {
                    MyEllips circle = (MyEllips) shape.ellipsList.get(i);
            
                  repaint();
                }
    }
    It creates lıttle red square near buttons .and I cant dragg or do some thıng .It coordınates doesnt belong to my Poınt..and if I change in MyEllips class width, height ş it doesnt do any sence too..What is wrong here?

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

    Default

    As written, this.repaint() is called shape.ellipsList.size() times.
    No MyEllips object is repaint()ed.

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

    Default

    It must be circle.repaint(); ?but it doesnt work ..Am I have to create BorderLayout for each panels?

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

    Default

    It doesnt work :((( I am cerating new object form MyEllips class.It extends JPanel. In my ShapeCOntrol class in InitEllips function it creates that object on point c which is point of mousePressed location .and adding thise eliips object to ellipslist and then adding this obejt to slectedPanel onject wich object of MyPanel class.it contains buttons.I think it must be correct.I used repaint method in initCircle when it created.but is doesnt show any thing .When I add some border it creates that border near buttons.WHne i wrote myellips.setSize(60,60) ; it created in such dimetinon square.but it is not my object wich I defined in MyEllips class on paintComponent(); I know paintCOmponent() works.I added System.out.print("PAINTING") on that funtion and when I resize or click mouse it writes this.I looked tuttorial form sun but I think I have done the same thing.BUt it doenst work :(((

Page 2 of 3 FirstFirst 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
  •