简体   繁体   English

使用矩形2D绘制具有不同方向的不同尺寸矩形

[英]Draw varying size rectangle with different orientation using rectangle 2D

I want to draw a rectangle in a java application. 我想在java应用程序中绘制一个矩形。 I have used rectangle2d to draw a rectangle. 我用rectangle2d绘制了一个矩形。 I need the rectangle to vary size based on mouse drag. 我需要矩形来根据鼠标拖动来改变大小。 ie The size of the rectangle varies as I drag the mouse. 即拖动鼠标时矩形的大小会有所不同。 I am currently able to draw only one type of the rectangle, ie When i drag the mouse towards down right of the screen. 我目前只能绘制一种类型的矩形,即当我将鼠标向下拖动到屏幕右侧时。 But i am unable to draw the other rectangles.for example. 但我无法绘制其他矩形。例如。 when the mouse is dragged top right of the screen. 当鼠标被拖动到屏幕的右上方时。 I am using a method called setRect that takes upper left x and y coordinates of the rectangle. 我正在使用一个名为setRect的方法,它采用矩形的左上角x和y坐标。 But since when I drag the mouse top left, my upper left and upper right change and my rectangle distorts. 但是当我向左拖动鼠标时,我的左上角和右上角发生了变化,我的矩形变形了。

I have tried my best to explain this in words. 我尽力用语言解释这一点。 If you have any doubts on the question, open up MS paint application or any ther drawing application, select a rectangle and move the mouse in all directions and see the 4 different orientations of rectangle when the mouse is dragged up left, up right, down left and down right. 如果您对此问题有任何疑问,请打开MS绘图应用程序或任何其他绘图应用程序,选择一个矩形并向所有方向移动鼠标,并在向左,向右,向下拖动鼠标时查看矩形的4个不同方向向左和向右。 Out of these I am able to draw only one when up left coordinates remain the same. 其中,当左上坐标保持不变时,我只能绘制一个。 is there any function that I can use to draw the rest of the three rectangle orientations 是否有任何功能,我可以用来绘制三个矩形方向的其余部分

Assuming you are using two sets of Points gained from the mousePressed and the mouseDragged MouseEvent , Here something to consider. 假设您正在使用从mousePressedmouseDragged MouseEvent获得的两组Points ,请注意以下事项。

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 用象限来看它(中心的O是从mousePressed收集的初始Point

           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. 拖动鼠标时,从mouseDragged获得的第二个Point将位于象限I,II,III或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? 如果第二个点位于象限I,你将如何绘制矩形?

  • Point 2 would then become the initial point to draw from. 然后,第2点将成为绘制的起始点。 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 ;-) 我找到了你的象限,你似乎已经知道了象限III,所以我会把它留给你,想象一下II和IV ;-)

在此输入图像描述

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);
            }
        });
    }
}

Check out Custom Painting Approaches for the two common ways to do painting: 查看自定义绘画方法 ,了解两种常见的绘画方法

  1. from an ArrayList of objects 来自对象的ArrayList
  2. on a BufferedImage 在BufferedImage上

The example show how to draw multiple Rectangles of any size and varying colors. 该示例显示如何绘制任意大小和不同颜色的多个矩形。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM