Draw varying size rectangle with different orientation using rectangle 2D

后端 未结 2 1057
北荒
北荒 2020-12-11 23:40

I want to draw a rectangle in a java application. I have used rectangle2d to draw a rectangle. I need the rectangle to vary size based on mouse drag. i.e. The size of the re

相关标签:
2条回答
  • 2020-12-11 23:55

    Assuming you are using two sets of Points gained from the mousePressed and the mouseDragged MouseEvent, Here something to consider.

    Break it down into smaller pieces. Look at it in terms of quadrants (The O in the center being the initial Point gathered from the mousePressed

               Quadrants
    +--------------+---------------+
    |              |               |
    |              |               |
    |      I       |       II      |
    |              |               |
    |              |               |
    +--------------O---------------+
    |              |               |
    |              |               |
    |     IV       |      III      |
    |              |               |
    |              |               |
    +--------------+---------------+
    

    When you drag the mouse, the second Point obtained from the mouseDragged will be either in quadrant I, II, III, or IV.

    So again I'll say.. Break it down into smaller pieces.

    How would you draw the rectangle if the the second point is in quadrant I?

    • Point 2 would then become the initial point to draw from. So you would have to switch the drawing points using

      // original
      setRect(p1.x, p1.y, p2.x - p1.x, p2.y - p1.y);
      
      // change to
      setRect(p2.x, p2.y, p1.x - p2.x, p1.y - p2.y);
      

      You can use logic to check which quadrant point is in, such as

      public boolean isPointTwoInQuadOne(Point p1, Point p2) {
          return p1.x >= p2.x && p1.y >= p2.y;
      }
      

    Hope that helps you out, or at least helps you see the problem from a different perspective :)


    Here's a running example. I figured out the quadrant I for you, and you seem to already know the quadrant III, so I'll leave it to you, to figure II and IV ;-)

    enter image description here

    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Point;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseMotionAdapter;
    import java.awt.geom.Rectangle2D;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    
    public class RectangleDrawWithDrag extends JPanel{
        private static final int D_W = 500;
        private static final int D_H = 500;
    
        private Point p1;
        private Point p2;
        private Rectangle2D rectangle;
    
        public RectangleDrawWithDrag() {
            addMouseListener(new MouseAdapter(){
                public void mousePressed(MouseEvent e) {
                    p1 = e.getPoint();
                    rectangle = new Rectangle2D.Double(p1.x, p1.y, p1.x - p1.x, p1.y - p1.y);
                }
            });
            addMouseMotionListener(new MouseMotionAdapter(){
                public void mouseDragged(MouseEvent e) {
                    p2 = e.getPoint();
                    if (isPointTwoInQuadOne(p1, p2)) {
                        rectangle.setRect(p2.x, p2.y, p1.x - p2.x, p1.y - p2.y);
                    } else {
                        rectangle.setRect(p1.x, p1.y, p2.x - p1.x, p2.y - p1.y);
                    }
    
                    repaint();
                }
            });
        }
    
        public boolean isPointTwoInQuadOne(Point p1, Point p2) {
            return p1.x >= p2.x && p1.y >= p2.y;
        }
    
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D)g;
            if (rectangle != null) {
                g2.fill(rectangle);
            }
        }
    
        @Override
        public Dimension getPreferredSize() {
            return new Dimension(D_W, D_H);
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    JFrame frame = new JFrame();
                    frame.add(new RectangleDrawWithDrag());
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    }
    
    0 讨论(0)
  • 2020-12-12 00:01

    Check out Custom Painting Approaches for the two common ways to do painting:

    1. from an ArrayList of objects
    2. on a BufferedImage

    The example show how to draw multiple Rectangles of any size and varying colors.

    0 讨论(0)
提交回复
热议问题