Results 1 to 9 of 9
  1. #1
    BeijingDuck is offline Member
    Join Date
    Nov 2010
    Posts
    23
    Rep Power
    0

    Default How to fix this code to move this shape?

    Hi guys

    Below code can draw a polygon whenever the mouse is pressed, but it is also supposed to drag the polygon to move to anywhere the mouse is released. But the polygon doesn't move at all.

    Does anyone know how to fix it?

    I believe it must be something wrong in OctagonPanel class, please see my OctagonPanel class below:
    (public class Octagon extends Polygon {....} and public class Main {...} are omitted here.)

    Java Code:
    public class OctagonPanel extends JPanel
    {
        private Octagon octagon;
        private Octagon selectedOctagon = null;
        private ArrayList<Octagon> lstOctagon = new ArrayList<Octagon>();
        
        static Random generator = new Random();
        int oldx, oldy, newx, newy;
        boolean dragging = false;
    
        public OctagonPanel()
        {
          addMouseListener (new OctagonsListener());
          addMouseMotionListener (new OctagonsListener());
        }
    
       public void paintComponent(Graphics g)
        {
            super.paintComponent(g);
            for(Octagon o : lstOctagon)
            {
                o.draw(g);
            }
        }
    
         private class OctagonsListener implements MouseListener, MouseMotionListener
         {
             public void mousePressed (MouseEvent e)
             {
                oldx = e.getX();
                oldy = e.getY();
    
                for(Octagon o : lstOctagon)
                {
                   if(o.contains(oldx, oldy))
                   {
                       selectedOctagon = o;
                       dragging = true;
                   }
                }
                if(selectedOctagon == null)
                {
                    int n = generator.nextInt(80);
                    int[] newx = {oldx, oldx + n, oldx + n, oldx, oldx - 2*n, oldx - 3*n, oldx - 3*n, oldx - 2*n};
                    int[] newy = {oldy, oldy + n, oldy + 2*n, oldy + 3*n, oldy + 3*n, oldy + 2*n, oldy + n, oldy};
                    octagon = new Octagon(newx, newy, 8);
    
                    lstOctagon.add(octagon);
                    repaint();
                }
              }
    
             public void mouseReleased(MouseEvent e)
             {
                 if(dragging = true)
                 {
                     repaint();
                 }
                 selectedOctagon = null;
             }
             public void mouseClicked(MouseEvent e)
             {
             }
             public void mouseMoved(MouseEvent e) {}
             public void mouseDragged(MouseEvent e)
             {
                newx = e.getX();
                newy = e.getY();
                int xtrans = newx - oldx;
                int ytrans = newy - oldy;
                selectedOctagon.translate(xtrans,ytrans);
                repaint();
    
             }
             public void mouseEntered(MouseEvent e) {}
             public void mouseExited(MouseEvent e) {}
         }
    
     }
    Thanks heaps
    Last edited by BeijingDuck; 12-09-2010 at 12:46 AM.

  2. #2
    BeijingDuck is offline Member
    Join Date
    Nov 2010
    Posts
    23
    Rep Power
    0

    Default

    Hi guys, no one wants to say something yet? :)

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default

    if(dragging = true)
    Do you know what that line does?

    Also, you have two different OctagonListeners added as MouseListener and MouseMotionListener. The one instance doesn't automagically have the same field values as the other.

    And we have no idea what the methods in your Octagon class do.

    db

  4. #4
    BeijingDuck is offline Member
    Join Date
    Nov 2010
    Posts
    23
    Rep Power
    0

    Default

    db, thanks for a say. I wanted to make
    if(dragging = true)
    show the particular shape is selected, then it can be moved. If that is not a right way to do, what should it look like?
    I tried - it doesn't make a difference if just "MouseListener" is added or both "MouseListener and MouseMotionListener" added.
    I am surprised that you said it might have something to do with Octagon class, as I thought it must be wrong in the methods of mousePressed, mouseReleased, mouseDragged. Ok, here we go:

    import java.awt.*;
    import java.util.Random;

    public class Octagon extends Polygon // so polygon's methods can be used on octagon
    {
    private int[] x; //= {150, 175, 175, 150, 100, 75, 75, 100};
    private int[] y; // = {75, 100, 150, 175, 175, 150, 100, 75};
    private Color color;
    static Random generator = new Random();

    public Octagon(int[]x, int[]y, int n)
    {
    this.x = x;
    this.y = y;
    color = new Color(Math.abs(generator.nextInt())% 16777216);
    }

    void draw(Graphics g)
    {
    g.setColor(color);
    g.fillPolygon(x, y, 8);
    }

    }
    Thanks guys, please keep talking here.

  5. #5
    BeijingDuck is offline Member
    Join Date
    Nov 2010
    Posts
    23
    Rep Power
    0

    Default Your say

    Hi guys, don't keep silent, I need your say here. :)

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Java Code:
    if (dragging = true)
    will set dragging to true. You were probably wanting to do this instead:


    Java Code:
    if (dragging == true)

    But even better, to prevent risk of this nonsense is to just simply do this:

    Java Code:
    if (dragging)

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    In addition, you need to do some debugging which can sometimes be simply done by adding print statements in your code. For example check out the results from these changes:
    Java Code:
       private class OctagonsListener implements MouseListener, MouseMotionListener {
          public void mousePressed(MouseEvent e) {
             oldx = e.getX();
             oldy = e.getY();
    
             for (Octagon o : lstOctagon) {
                
                //!! added:  Show the bounds of all Octagons in the list.
                System.out.printf("rect: %s%n", o.getBounds().toString()); 
                
                
                if (o.contains(oldx, oldy)) {
                   
                 //!! added: see if we've selected one.
                   System.out.printf("YES! This rect has it: %s%n", o.getBounds().toString());
                   
                   
                   selectedOctagon = o;
                   dragging = true;
                }
             }
             System.out.println(); //!! added
             System.out.println();
    Are you finding all your Octagons have 0 size and 0 location? Perhaps you need to call a super constructor in your Octagon constructor so that the parent class can register the points?

  8. #8
    BeijingDuck is offline Member
    Join Date
    Nov 2010
    Posts
    23
    Rep Power
    0

    Default

    Thank you Fubarable for your suggestion, now I think I have found out a big error here:
    selectedOctagon never got selected in the methods private Octagon hitTest(MouseEvent e) and public void mousePressed (MouseEvent e).
    (Because 1., mouseClicked and mouseDragged can't act properly.)
    So how to fix that part? ..Thank you so much!
    Java Code:
    import java.awt.event.*;
    import java.awt.*;
    import javax.swing.JPanel;
    import java.util.ArrayList;
    import java.util.Random;
    
    public class OctagonPanel extends JPanel
    {
        private Octagon octagon;
        private Octagon selectedOctagon = null;
        private ArrayList<Octagon> lstOctagon = new ArrayList<Octagon>();
        static Random generator = new Random();
        int oldx, oldy, difx, dify, newx, newy;
    
        public OctagonPanel()
        {
             addMouseListener(new OctagonsListener());
             addMouseMotionListener(new OctagonsListener());
         }
    
        public void paintComponent(Graphics g)
        {
            super.paintComponent(g); //optional here
            for(Octagon o : lstOctagon)
            {
                o.draw(g);
            }
    
        }
    
         private class OctagonsListener implements MouseListener, MouseMotionListener
         {
            int n;
    
            private Octagon hitTest(MouseEvent e)
            {
                Octagon hitO = null;
                for (Octagon o : lstOctagon)
                {
                    if (o.contains(e.getX(), e.getY()))
                    {
                        hitO = o;
                        break;
                    }
                }
                return hitO;
            }
    
             public void mousePressed (MouseEvent e)
             {
                selectedOctagon = hitTest(e);
             }
             public void mouseClicked(MouseEvent e)
             {
                oldx = e.getX();
                oldy = e.getY();
    
                if(selectedOctagon == null)
                {
                    System.out.println(oldx);
                    n = generator.nextInt(50);
                    int[]x = {oldx, oldx + n, oldx + n, oldx, oldx - 2*n, oldx - 3*n, oldx - 3*n, oldx - 2*n};
                    int[]y = {oldy, oldy + n, oldy + 2*n, oldy + 3*n, oldy + 3*n, oldy + 2*n, oldy + n, oldy};
    
                    octagon = new Octagon(x, y, 8);
                    lstOctagon.add(octagon);
    
                    repaint();
                }
    
             }
             public void mouseReleased(MouseEvent e)
             {
                //selectedOctagon = null; 
                repaint();
             }
    
             public void mouseMoved(MouseEvent e)
             {
             }
             public void mouseDragged(MouseEvent e)
             {
                  newx = e.getX(); newy = e.getY();
                  if(selectedOctagon != null)
                  {
                      difx = newx - oldx; dify = newy - oldy;
                      selectedOctagon.translate(difx, dify);
                      repaint();
                }
    
             }
             public void mouseEntered(MouseEvent e) {}
             public void mouseExited(MouseEvent e) {}
         }
    
    }

  9. #9
    BeijingDuck is offline Member
    Join Date
    Nov 2010
    Posts
    23
    Rep Power
    0

Similar Threads

  1. Help with Move Shape
    By romina in forum AWT / Swing
    Replies: 2
    Last Post: 12-09-2010, 04:25 AM
  2. Replies: 1
    Last Post: 04-02-2010, 01:48 PM
  3. rezise shape
    By frankenstein in forum Java 2D
    Replies: 5
    Last Post: 07-30-2009, 01:44 AM
  4. Random Shape generator
    By scheng12 in forum New To Java
    Replies: 1
    Last Post: 03-09-2009, 03:06 AM
  5. implementing shape
    By sidkdbl07 in forum Java 2D
    Replies: 1
    Last Post: 01-12-2008, 07:42 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
  •