Results 1 to 6 of 6
  1. #1
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default swing and mouse dragging

    -The first program is copied from the Cor Java book (an excellent text book when learning Java)
    -The second I did after studying the first sample
    It is not bad for a student I believe, but the mouse dragging is not working as well, why?




    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    package willemlearningjava;
    
    import java.awt.*;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseMotionListener;
    import java.awt.geom.*;
    import java.util.ArrayList;
    import javax.swing.*;
    
    /**
     *
     * @author willem
     */
    public class ClickSquares {
    
        public static void main(String[] args) { // main of the public class
            SetMouseFrame frame = new SetMouseFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
        }
    }
    
    class SetMouseFrame extends JFrame {
        private int WposX=100, WposY=100, WX=1200, WY=600;
    
        public SetMouseFrame() {
            setTitle("Click Squares");
            setSize(WX, WY);
            setLocation(WposX, WposY);
    
            SetMousePanel panel = new SetMousePanel();
            Container contentPane = getContentPane();
            contentPane.add(panel);
        }  
    }
    
    class SetMousePanel extends JPanel {
           
           private static final int SQ_SIZE = 10;
           private ArrayList squares;
    
           public SetMousePanel()  {
                squares = new ArrayList();
                addMouseListener(new MouseHandler());
                addMouseMotionListener(new MouseDragHandler());
           }
    
           public void paintComponent(Graphics g) {
           super.paintComponent(g);
           Graphics2D g2 = (Graphics2D)g;
           
           for (int i = 0; i < squares.size(); i++) {
               g2.draw((Rectangle2D)squares.get(i));
           }
    
           }
    
           public void AddSquares(Point2D p) {
    
               Rectangle2D sq = new Rectangle2D.Double(
                       p.getX(), p.getY(), SQ_SIZE, SQ_SIZE);
               squares.add(sq); // creates rect obj and adds to array
           }
    
           public void RemoveSquares(Rectangle2D r)  {     
                squares.remove(r);
                repaint();
           }
    
           public void DragSquare()  {
    
           }
    
           public Rectangle2D InsideSquare(Point2D p) { // if outside return null
               
               for (int i = 0; i < squares.size(); i++)  {
                   Rectangle2D r = (Rectangle2D)squares.get(i);
                    if (r.contains(p))return r; // has the pointed rect
               }
               return null;
           }
    
    
    
           private class MouseHandler extends MouseAdapter {
               public void mousePressed(MouseEvent event) {
                   if  (InsideSquare(event.getPoint()) != null);
                   else // if inside square do nothing
                   AddSquares(event.getPoint());
                   repaint();
               }
               public void mouseClicked(MouseEvent event) {
    
                   if ((event.getClickCount() >= 2) &&
                           (InsideSquare(event.getPoint()) != null)) {
                         RemoveSquares(InsideSquare(event.getPoint()));
                   }
               }
           }
    
           private class MouseDragHandler implements MouseMotionListener {
    
                public void mouseDragged(MouseEvent me) {
                    Rectangle2D sq = InsideSquare(me.getPoint());
                    if (sq != null) {
                        int x = me.getX();
                        int y = me.getY();
                        x = x - SQ_SIZE / 2;
                        y = y - SQ_SIZE / 2;
    
                        InsideSquare(me.getPoint()).setFrame(x , y ,
                                SQ_SIZE, SQ_SIZE);
                      //  System.out.println("drag");
                        repaint();
    
                    }
                }
                public void mouseMoved(MouseEvent me) {}
           }
    }

    ++++++++++++++++++++++++++++++++++


    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    package willemlearningjava;
    
    /**
     *
     * @author willem
     */
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    import java.awt.geom.*;
    import javax.swing.*;
    
    public class MouseTest
    {
       public static void main(String[] args)
       {
          MouseFrame frame = new MouseFrame();
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.show();
       }
    }
    
    /**
       A frame containing a panel for testing mouse operations
    */
    class MouseFrame extends JFrame
    {
       public MouseFrame()
       {
          setTitle("MouseTest");
          setSize(300, 200);
    
          // add panel to frame
    
          MousePanel panel = new MousePanel();
          Container contentPane = getContentPane();
          contentPane.add(panel);
       }
    
       public static final int WIDTH = 300;
       public static final int HEIGHT = 200;
    }
    
    /**
       A panel with mouse operations for adding and removing squares.
    */
    class MousePanel extends JPanel
    {
       public MousePanel()
       {
          squares = new ArrayList();
          current = null;
    
          addMouseListener(new MouseHandler());
          addMouseMotionListener(new MouseMotionHandler());
       }
    
       public void paintComponent(Graphics g)
       {
          super.paintComponent(g);
          Graphics2D g2 = (Graphics2D)g;
    
          // draw all squares
          for (int i = 0; i < squares.size(); i++)
             g2.draw((Rectangle2D)squares.get(i));
       }
    
       /**
          Finds the first square containing a point.
          @param p a point
          @return the index of the first square that contains p
       */
       public Rectangle2D find(Point2D p)
       {
          for (int i = 0; i < squares.size(); i++)
          {
             Rectangle2D r = (Rectangle2D)squares.get(i);
             if (r.contains(p)) return r;
          }
    
          return null;
       }
    
       /**
          Adds a square to the collection.
          @param p the center of the square
       */
       public void add(Point2D p)
       {
          double x = p.getX();
          double y = p.getY();
    
          current = new Rectangle2D.Double(
             x - SIDELENGTH / 2,
             y - SIDELENGTH / 2,
             SIDELENGTH,
             SIDELENGTH);
          squares.add(current);
          repaint();
       }
    
       /**
          Removes a square from the collection.
          @param s the square to remove
       */
       public void remove(Rectangle2D s)
       {
          if (s == null) return;
          if (s == current) current = null;
          squares.remove(s);
          repaint();
       }
    
    
       private static final int SIDELENGTH = 10;
       private ArrayList squares;
       private Rectangle2D current;
       // the square containing the mouse cursor
    
       private class MouseHandler extends MouseAdapter
       {
          public void mousePressed(MouseEvent event)
          {
             // add a new square if the cursor isn't inside a square
             current = find(event.getPoint());
             if (current == null)
                add(event.getPoint());
          } //current is eithr a created or a pointed rectangle or zero (see find)
    
          public void mouseClicked(MouseEvent event)
          {
             // remove the current square if double clicked
             current = find(event.getPoint());
             if (current != null && event.getClickCount() >= 2)
                remove(current);
          }
       }
    
       private class MouseMotionHandler
          implements MouseMotionListener
       {
          public void mouseMoved(MouseEvent event)
          {
             // set the mouse cursor to cross hairs if it is inside
             // a rectangle
    
             if (find(event.getPoint()) == null)
                setCursor(Cursor.getDefaultCursor());
             else
                setCursor(Cursor.getPredefinedCursor
                   (Cursor.CROSSHAIR_CURSOR));
          }
    
          public void mouseDragged(MouseEvent event)
          {
             if (current != null)
             {
                int x = event.getX();
                int y = event.getY();
    
                // drag the current rectangle to center it at (x, y)
                current.setFrame(
                   x - SIDELENGTH / 2,
                   y - SIDELENGTH / 2,
                   SIDELENGTH,
                   SIDELENGTH);
                repaint();
             }
          }
       }
    }

  2. #2
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: swing and mouse dragging

    Sorry it is the other way around
    first is mine
    second is of corejava

  3. #3
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: swing and mouse dragging

    ok there was a repaint() missing at AddSquares

  4. #4
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: swing and mouse dragging

    Let me elaborate this stuff a little more:
    The program is very straight forward, each mouse click creates a square each double click
    takes the square of the screen.
    So each new created square gets added to an array list and the double click takes that
    particular square of the list...... so far fine (the paint comp simply paints the array content)

    the problem occurs when dragging a square
    Last edited by willemjav; 03-05-2013 at 07:38 AM.

  5. #5
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: swing and mouse dragging

    It is not the first time I got in trouble using the paint comp!
    You see repaint() should call the paint comp, but infect
    it is the system that calls the comp and you never really know when.

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,197
    Rep Power
    19

    Default Re: swing and mouse dragging

    Which is why painting method overrides should never contain business logic. More generally, when overriding an inherited method, don't try to repurpose it.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Drawing with the mouse in the same frame as swing components
    By StillCantPlay in forum AWT / Swing
    Replies: 2
    Last Post: 04-07-2012, 12:12 AM
  2. Moving a ball by dragging the mouse
    By stevo812 in forum Java Applets
    Replies: 5
    Last Post: 03-31-2012, 12:49 AM
  3. Draw rectangle by dragging mouse
    By Tyler in forum SWT / JFace
    Replies: 11
    Last Post: 08-09-2010, 07:50 PM
  4. drawing Line by dragging mouse !!!
    By h9h in forum Java 2D
    Replies: 14
    Last Post: 10-23-2009, 05:10 AM
  5. Scaling-ache and mouse dragging
    By willemjav in forum Java Applets
    Replies: 19
    Last Post: 07-19-2008, 12:17 AM

Posting Permissions

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