使用矩形 2D 绘制不同方向的不同大小的矩形 [英] Draw varying size rectangle with different orientation using rectangle 2D

查看:36
本文介绍了使用矩形 2D 绘制不同方向的不同大小的矩形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 Java 应用程序中绘制一个矩形.我已经使用 rectangle2d 绘制了一个矩形.我需要矩形根据鼠标拖动改变大小.即矩形的大小随着我拖动鼠标而变化.我目前只能绘制一种类型的矩形,即当我向屏幕右下方拖动鼠标时.但我无法绘制其他矩形.例如.当鼠标被拖动到屏幕右上角时.我正在使用一种名为 setRect 的方法,它采用矩形的左上角 x 和 y 坐标.但是因为当我拖动鼠标左上角时,我的左上角和右上角会发生变化并且我的矩形会扭曲.

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 rectangle varies as I drag the mouse. I am currently able to draw only one type of the rectangle, i.e. 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. But since when I drag the mouse top left, my upper left and upper right change and my rectangle distorts.

我已经尽力用文字解释了这一点.如果你对这个问题有任何疑问,打开MS Paint应用程序或任何其他绘图应用程序,选择一个矩形并在各个方向移动鼠标,当鼠标向上左,向右,向下拖动时,可以看到矩形的4个不同方向左下右下.其中,当左上坐标保持不变时,我只能绘制一个.有什么函数可以用来绘制其余的三个矩形方向

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. 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

推荐答案

假设您正在使用从 mousePressedmouseDragged<获得的两组 Points/code> MouseEvent,这里需要考虑一下.

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

把它分解成更小的部分.从象限的角度来看它(中心的 O 是从 mousePressed

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      |
|              |               |
|              |               |
+--------------+---------------+

当您拖动鼠标时,从 mouseDragged 获得的第二个 Point 将在象限 I、II、III 或 IV.

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.

如果第二个点在象限 I 中,你将如何绘制矩形?

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

  • 点 2 将成为绘制的初始点.所以你必须使用

  • 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 :)

这是一个运行示例.我为你计算了象限 I,你似乎已经知道象限 III,所以我把它留给你,图 II 和 IV ;-)

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

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

这篇关于使用矩形 2D 绘制不同方向的不同大小的矩形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆