Results 1 to 2 of 2
  1. #1
    New to JAVA is offline Member
    Join Date
    Dec 2008
    Posts
    4
    Rep Power
    0

    Unhappy how to resize a shape on a jpanel, using mouse

    I have written a program for my java project on, "my paint application". i have attached the code here. i have a jpanel on a jframe. i have used the panel as a canvas. i have created buttons, when the user clicks these , a shape is drawn. i am able to move a shape with the mouse. i want to resize the shapes when they are dragged. i have written the code for it, it is compiled successfully, but the shape is not resized.

    can anyone please help me?

    one more problem, is with the cursors. the cursor changes currectly, when the mouse touches the top and left borders of the shape, but nothing happens when it touches the other two sides.

    Any help will be appreciated.

    Thank you.
    Attached Files Attached Files

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Here's an exploration of the resizing part of your app.
    Note the use of a small range allowed for selection of cursor for a line/corner, ie, the cursor position does not have to be exactly on the line/corner.
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class Resizing extends JPanel {
        Rectangle rect = new Rectangle(100,100,150,150);
    
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D)g;
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                                RenderingHints.VALUE_ANTIALIAS_ON);
            g2.setPaint(Color.blue);
            g2.draw(rect);
        }
    
        public static void main(String[] args) {
            Resizing test = new Resizing();
            Resizer resizer = new Resizer(test);
            test.addMouseListener(resizer);
            test.addMouseMotionListener(resizer);
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(test);
            f.setSize(400,400);
            f.setLocation(100,100);
            f.setVisible(true);
        }
    }
    
    /**
     * MouseAdapter okay for j2se 1.6+
     * Use MouseInputAdapter for j2se 1.5-
     */
    class Resizer extends MouseAdapter {
        Resizing component;
        boolean dragging = false;
        // Give user some leeway for selections.
        final int PROX_DIST = 3;
    
        public Resizer(Resizing r) {
            component = r;
        }
    
        public void mousePressed(MouseEvent e) {
            if(component.getCursor() != Cursor.getDefaultCursor()) {
                // If cursor is set for resizing, allow dragging.
                dragging = true;
            }
        }
    
        public void mouseReleased(MouseEvent e) {
            dragging = false;
        }
    
        public void mouseDragged(MouseEvent e) {
            if(dragging) {
                Point p = e.getPoint();
                Rectangle r = component.rect;
                int type = component.getCursor().getType();
                int dx = p.x - r.x;
                int dy = p.y - r.y;
                switch(type) {
                    case Cursor.N_RESIZE_CURSOR:
                        int height = r.height - dy;
                        r.setRect(r.x, r.y+dy, r.width, height);
                        break;
                    case Cursor.NW_RESIZE_CURSOR:
                        int width = r.width - dx;
                        height = r.height - dy;
                        r.setRect(r.x+dx, r.y+dy, width, height);
                        break;
                    case Cursor.W_RESIZE_CURSOR:
                        width = r.width - dx;
                        r.setRect(r.x+dx, r.y, width, r.height);
                        break;
                    case Cursor.SW_RESIZE_CURSOR:
                        width = r.width - dx;
                        height = dy;
                        r.setRect(r.x+dx, r.y, width, height);
                        break;
                    case Cursor.S_RESIZE_CURSOR:
                        height = dy;
                        r.setRect(r.x, r.y, r.width, height);
                        break;
                    case Cursor.SE_RESIZE_CURSOR:
                        width = dx;
                        height = dy;
                        r.setRect(r.x, r.y, width, height);
                        break;
                    case Cursor.E_RESIZE_CURSOR:
                        width = dx;
                        r.setRect(r.x, r.y, width, r.height);
                        break;
                    case Cursor.NE_RESIZE_CURSOR:
                        width = dx;
                        height = r.height - dy;
                        r.setRect(r.x, r.y+dy, width, height);
                        break;
                    default:
                        System.out.println("unexpected type: " + type);
                }
                component.repaint();
            }
        }
    
        public void mouseMoved(MouseEvent e) {
            Point p = e.getPoint();
            if(!isOverRect(p)) {
                if(component.getCursor() != Cursor.getDefaultCursor()) {
                    // If cursor is not over rect reset it to the default.
                    component.setCursor(Cursor.getDefaultCursor());
                }
                return;
            }
            // Locate cursor relative to center of rect.
            int outcode = getOutcode(p);
            Rectangle r = component.rect;
            switch(outcode) {
                case Rectangle.OUT_TOP:
                    if(Math.abs(p.y - r.y) < PROX_DIST) {
                        component.setCursor(Cursor.getPredefinedCursor(
                                            Cursor.N_RESIZE_CURSOR));
                    }
                    break;
                case Rectangle.OUT_TOP + Rectangle.OUT_LEFT:
                    if(Math.abs(p.y - r.y) < PROX_DIST &&
                       Math.abs(p.x - r.x) < PROX_DIST) {
                        component.setCursor(Cursor.getPredefinedCursor(
                                            Cursor.NW_RESIZE_CURSOR));
                    }
                    break;
                case Rectangle.OUT_LEFT:
                    if(Math.abs(p.x - r.x) < PROX_DIST) {
                        component.setCursor(Cursor.getPredefinedCursor(
                                            Cursor.W_RESIZE_CURSOR));
                    }
                    break;
                case Rectangle.OUT_LEFT + Rectangle.OUT_BOTTOM:
                    if(Math.abs(p.x - r.x) < PROX_DIST &&
                       Math.abs(p.y - (r.y+r.height)) < PROX_DIST) {
                        component.setCursor(Cursor.getPredefinedCursor(
                                            Cursor.SW_RESIZE_CURSOR));
                    }
                    break;
                case Rectangle.OUT_BOTTOM:
                    if(Math.abs(p.y - (r.y+r.height)) < PROX_DIST) {
                        component.setCursor(Cursor.getPredefinedCursor(
                                            Cursor.S_RESIZE_CURSOR));
                    }
                    break;
                case Rectangle.OUT_BOTTOM + Rectangle.OUT_RIGHT:
                    if(Math.abs(p.x - (r.x+r.width)) < PROX_DIST &&
                       Math.abs(p.y - (r.y+r.height)) < PROX_DIST) {
                        component.setCursor(Cursor.getPredefinedCursor(
                                            Cursor.SE_RESIZE_CURSOR));
                    }
                    break;
                case Rectangle.OUT_RIGHT:
                    if(Math.abs(p.x - (r.x+r.width)) < PROX_DIST) {
                        component.setCursor(Cursor.getPredefinedCursor(
                                            Cursor.E_RESIZE_CURSOR));
                    }
                    break;
                case Rectangle.OUT_RIGHT + Rectangle.OUT_TOP:
                    if(Math.abs(p.x - (r.x+r.width)) < PROX_DIST &&
                       Math.abs(p.y - r.y) < PROX_DIST) {
                        component.setCursor(Cursor.getPredefinedCursor(
                                            Cursor.NE_RESIZE_CURSOR));
                    }
                    break;
                default:    // center
                    component.setCursor(Cursor.getDefaultCursor());
            }
        }
    
        /**
         * Make a smaller Rectangle and use it to locate the
         * cursor relative to the Rectangle center.
         */
        private int getOutcode(Point p) {
            Rectangle r = (Rectangle)component.rect.clone();
            r.grow(-PROX_DIST, -PROX_DIST);
            return r.outcode(p.x, p.y);        
        }
    
        /**
         * Make a larger Rectangle and check to see if the
         * cursor is over it.
         */
        private boolean isOverRect(Point p) {
            Rectangle r = (Rectangle)component.rect.clone();
            r.grow(PROX_DIST, PROX_DIST);
            return r.contains(p);
        }
    }

Similar Threads

  1. Help with Move Shape
    By romina in forum AWT / Swing
    Replies: 2
    Last Post: 12-09-2010, 03:25 AM
  2. Exponential Shape Program
    By checkmylongboarding in forum New To Java
    Replies: 3
    Last Post: 09-23-2008, 03:31 AM
  3. implementing shape
    By sidkdbl07 in forum Java 2D
    Replies: 1
    Last Post: 01-12-2008, 06:42 PM
  4. weird looking shape JButton is it possible?
    By unhurt in forum AWT / Swing
    Replies: 8
    Last Post: 11-03-2007, 09:10 AM
  5. How to change shape of JButton
    By FaRuK in forum AWT / Swing
    Replies: 1
    Last Post: 05-19-2007, 12:56 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
  •