简体   繁体   English

如何通过拖动鼠标绘制上下颠倒的正方形?

[英]How to draw an upsidedown square by dragging mouse?

How can I draw an inverted rectangle in Java using swing? 如何使用Swing在Java中绘制一个倒置的矩形?

Using the 'g.drawRect(x,y,width,height)' method, Creating a rectangle through mouse drag was successful, but there was a slight error. 使用'g.drawRect(x,y,width,height)'方法,通过鼠标拖动成功创建矩形,但是存在一些错误。

If you drag to a point (x2,y2| x2>x1 && y2>y1) larger than the first point(x,y), It will operate normally. 如果拖动到比第一个点(x,y)大的点(x2,y2 | x2> x1 && y2> y1),它将正常运行。

However, in the opposite case, if the coordinates of the drag point are smaller than the coordinates of the first click point, it is drawn in the opposite direction, not to the drag point direction. 但是,在相反的情况下,如果拖动点的坐标小于第一次单击点的坐标,则会沿相反的方向而不是拖动点的方向进行绘制。

Even if I tried to draw reverse it through if(), I couldn't figure out what to do. 即使我试图通过if()将其反转,我也无法弄清楚该怎么做。

The direction I want is like a dragging box in Window, but It's a little hard to me. 我想要的方向就像Window中的拖动框,但对我来说有点难。 Please give me some hint to overcome this hard trip. 请给我一些提示,以克服这一艰难的旅程。

↓Here is my code ↓这是我的代码

   class Rect {
            int x, y, w, h;
   }

   public class Rectangle extends JPanel{
            int i = 0;
       int x, y = 0;
       Rect [] ary = new Rect[100];
       public Rectangle() {
           addMouseListener(new MouseListener() {
               public void mouseClicked(MouseEvent e) {}
               public void mouseEntered(MouseEvent e) {}
               public void mouseExited(MouseEvent e) {}
               public void mousePressed(MouseEvent e) {
                   if(i>100) return;
                   ary[i] = new Rect();
                   ary[i].x = e.getX(); ary[i].y = e.getY();
                   x= ary[i].x; y = ary[i].y;
               }

               @Override
               public void mouseReleased(MouseEvent e) {
                   ary[i].w = Math.abs(ary[i].x-e.getX()); 
                        ary[i].h = Math.abs(ary[i].y- e.getY());
                   i++;
                   repaint();
               }
           });

           addMouseMotionListener(new MouseMotionListener() {
               @Override
               public void mouseDragged(MouseEvent e) {
                   ary[i].w = Math.abs(ary[i].x-e.getX()); 
                   ary[i].h = Math.abs(ary[i].y- e.getY());
                   repaint();
               }
               public void mouseMoved(MouseEvent e) {
               }
           });
       }

       public void paintComponent(Graphics g) {
           super.paintComponent(g);
           for(Rect r:ary){
               if(r !=null) {
                   g.setColor(Color.BLACK);
                   g.drawRect(r.x, r.y, r.w, r.h);
               }
           }
       }
   }

please help me 请帮我

Problem image 问题图片

because ary[i].x and ary[i].y must be the minimum among the press and release coordinates: 因为ary[i].xary[i].y必须是按下和释放坐标中的最小值:

           @Override
           public void mouseReleased(MouseEvent e) {
               ary[i].w = Math.abs(ary[i].x-e.getX()); 
               ary[i].h = Math.abs(ary[i].y- e.getY());
               // upper left point
               ary[i].x = Math.min(ary[i].x,e.getX()); 
               ary[i].y = Math.min(ary[i].y,e.getY()); 
               i++;
               repaint();
           }

First of all don't use an Array to store the Rectangle objects you want to paint. 首先,不要使用数组存储要绘制的Rectangle对象。 Use an ArrayList it will grow dynamically as you want to paint more rectangles. 使用ArrayList,它将随着您想要绘制更多矩形而动态增长。

The problem with your current code is that you are attempting to update the x/y/width/height values in 3 separate methods. 当前代码的问题在于,您试图通过3种单独的方法更新x / y / width / height值。 This should only be done in the mouseDragged method. 这只能在mouseDragged方法中完成。

The basic steps are: 基本步骤是:

  1. in mousePressed you save the initial mouse point. mousePressed保存初始鼠标点。 Create an empty Rectangle object to used to draw the rectangle 创建一个空的Rectangle对象以绘制矩形
  2. in mouseDragged you compare the initial mouse point with the current mouse point and determine the minimum x/y values. mouseDragged您将初始鼠标点与当前鼠标点进行比较,并确定最小x / y值。 You then get the absolute value for the x and y values separately so you know the width/height of the rectangle. 然后,您分别获得x和y值的绝对值,因此您知道矩形的宽度/高度。 Update the x/y/width/height values of your Rectangle object and repaint the Rectangle 更新Rectangle对象的x / y / width / height值并重新绘制Rectangle
  3. in mouseReleased you add the Rectangle object to your ArrayList mouseReleased ,将Rectangle对象添加到ArrayList中

See the DrawOnComponent example found in Custom Painting Approaches for a working example that implements the above suggestion. 有关实现上述建议的工作示例,请参见“ 自定义绘画方法”中DrawOnComponent示例。

The MouseInputAdapter implementation from the above example is as follows. 上面示例中的MouseInputAdapter实现如下。 It shows how most of the logic is in the mouseDragged method: 它显示了大多数逻辑在mouseDragged方法中:

class MyMouseListener extends MouseInputAdapter
{
    private Point startPoint;

    public void mousePressed(MouseEvent e)
    {
        startPoint = e.getPoint();
        shape = new Rectangle();
    }

    public void mouseDragged(MouseEvent e)
    {
        int x = Math.min(startPoint.x, e.getX());
        int y = Math.min(startPoint.y, e.getY());
        int width = Math.abs(startPoint.x - e.getX());
        int height = Math.abs(startPoint.y - e.getY());

        shape.setBounds(x, y, width, height);
        repaint();
    }

    public void mouseReleased(MouseEvent e)
    {
        if (shape.width != 0 || shape.height != 0)
        {
            addRectangle(shape, e.getComponent().getForeground());
        }

        shape = null;
    }
}

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

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