Results 1 to 2 of 2
  1. #1
    bobbytown is offline Member
    Join Date
    Jan 2014
    Posts
    5
    Rep Power
    0

    Default Resize objects from an ArrayList

    Hello, I have a problem with my program. My aim is to display in a JPanel, 2 rectangles, and resize them by clicking. I want to give the user the ability to change the height of the rectangles (independently) by moving the top and/or bottom edges. Currently, it displays the 2 rectangles, but impossible to resize and I can not find the solution.
    Someone could help me fix my program ? Thank you very much.


    Here is my code :

    Java Code:
    import java.awt.*;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.util.ArrayList;
    import javax.swing.*;
    
     
    
    public class Resizing extends JPanel {
    
     
    
        private static final int PREF_W = 600;
        private static final int PREF_H = 400;
        private static final Color RECT_COLOR = Color.blue;
        java.awt.List myGuiList = new java.awt.List();
        java.util.List<MyRectangle> rectangleList = new ArrayList<MyRectangle>();
    
     
    
        public Resizing() {
          rectangleList.add(new MyRectangle(100, 100, 150, 150, "Rect 1"));
          rectangleList.add(new MyRectangle(300, 100, 150, 150, "Rect 2"));
        }
    
     
    
        public java.util.List<MyRectangle> getRectangleList() {
          return rectangleList;
        }
    
     
    
        @Override
        protected void paintComponent(Graphics g) {
          super.paintComponent(g);
          Graphics2D g2 = (Graphics2D) g;
          g2.setColor(RECT_COLOR);
          for (MyRectangle rect : rectangleList) {
             rect.fill(g2);
          }
        }
    
     
    
        public Dimension getPreferredSize() {
          return new Dimension(PREF_W, PREF_H);
        }
    
     
    
        public static void main(String[] args) {
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
     
    
            Resizing essai = new Resizing();
            Resizer rectangle = new Resizer(essai);
            essai.addMouseListener(rectangle);
            essai.addMouseMotionListener(rectangle);
            f.add(essai);
    
     
    
            f.setSize(600,400);
            f.setLocation(100,100);
            f.setVisible(true);
        }
    }
    
     
    
    class Resizer extends MouseAdapter {
        Resizing component;
        private Rectangle rect;
    
     
    
        boolean dragging = false;
        // Give user some leeway for selections.
        final int PROX_DIST = 3;
    
     
    
        public Resizer(Resizing r) {
            component = r;
        }
    
     
    
    @Override
        public void mousePressed(MouseEvent e) {
            if(component.getCursor() != Cursor.getDefaultCursor()) {
                // If cursor is set for resizing, allow dragging.
                dragging = true;
            }
        }
    @Override
        public void mouseReleased(MouseEvent e) {
            dragging = false;
        }
    @Override
        public void mouseDragged(MouseEvent e) {
            if(dragging){
                Point p = e.getPoint();
                Rectangle r = rect;
                int type = component.getCursor().getType();
                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.S_RESIZE_CURSOR:
                        height = dy;
                        r.setRect(r.x, r.y, r.width, height);
                        break;
                    default:
                        System.out.println("unexpected type: " + type);
                }
                component.repaint();
            }
        }
    
     
    
    public int getOutcode(Point p) {
            Rectangle r = (Rectangle)rect.clone();
            r.grow(-PROX_DIST, -PROX_DIST);
            return r.outcode(p.x, p.y);
       }
    
     
    
       public boolean isOverRect(Point p) {
            Rectangle r = (Rectangle)rect.clone();
            r.grow(PROX_DIST, PROX_DIST);
            return r.contains(p);
       }
    
     
    
    
    @Override
        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 = 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_BOTTOM:
                    if(Math.abs(p.y - (r.y+r.height)) < PROX_DIST) {
                        component.setCursor(Cursor.getPredefinedCursor(
                                            Cursor.S_RESIZE_CURSOR));
                    }
                    break;
                default:    // center
                    component.setCursor(Cursor.getDefaultCursor());
            }
        }
    }
    
     
    
    class MyRectangle {
       private Rectangle rect;
       private String name;
       final int PROX_DIST = 3;
    
     
    
       public MyRectangle(int x, int y, int width, int height, String name) {
          rect = new Rectangle(x, y, width, height);
          this.name = name;
       }
    
     
    
       public void fill(Graphics2D g2) {
          g2.fill(rect);
       }
    
     
    
      
       public String getName() {
          return name;
       }
    
     
    
       @Override
       public String toString() {
          return name + " " + rect.toString();
       }
    }

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default Re: Resize objects from an ArrayList

    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

Similar Threads

  1. Printing out arraylist of objects?
    By pilik in forum New To Java
    Replies: 6
    Last Post: 03-13-2013, 01:27 PM
  2. Arraylist-Objects
    By ztomtomz in forum New To Java
    Replies: 5
    Last Post: 11-18-2012, 01:28 PM
  3. Help with objects in ArrayList
    By BiteMuncher in forum New To Java
    Replies: 7
    Last Post: 02-21-2011, 11:55 AM
  4. ArrayList with different objects? Help
    By xtrmi in forum New To Java
    Replies: 4
    Last Post: 02-27-2009, 08:51 PM

Tags for this Thread

Posting Permissions

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