Java swing,需要使用鼠标绘制矩形,而不会丢失重叠的前一个矩形

时间:2014-03-04 02:50:58

标签: java swing

在这个我扩展JLabel的类中我需要能够使用鼠标左键单击,然后向下和/或向右拖动以创建一个矩形并能够重复该过程以绘制多个矩形而不会丢失任何前面的和重叠的绘图框,以及能够找到由所有矩形的并集like this

制作的矩形

我当前的代码尽可能地从Performing Custom Painting上的java演示中进行了调整,程序似乎以奇怪的方式表现,因为重绘方法用于更新JLabel,但我不知道如何解决它

JLabel类

import javax.swing.*;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class JLabelx extends JLabel {
    private int squareX = 0;
    private int squareY = 0;
    private int squareW = 0;
    private int squareH = 0;

public JLabelx() {

    addMouseListener(new MouseAdapter() {
        public void mousePressed(MouseEvent e) {
            squareX = e.getX();
            squareY = e.getY();
            //set coordinates of next rectangle 
        }
    });

    addMouseMotionListener(new MouseAdapter() {
        public void mouseDragged(MouseEvent e) {

            newDraw(e.getX(),e.getY());
            //find length and width of next rectangle
        }
    });

    }
protected void newDraw(int x, int y) {
    int OFFSET = 1;
    if ((squareX!=x) || (squareY!=y)) {
    //  repaint(squareX,squareY,squareW+OFFSET,squareH+OFFSET);
        squareW=x-squareX;
        squareH=y-squareY;
        repaint(squareX,squareY,squareW+OFFSET,squareH+OFFSET);
    } 
}
protected void paintComponent(Graphics g) {
    super.paintComponents(g);       

    g.setColor(Color.GREEN);
    g.fillRect(squareX,squareY,squareW,squareH);
    g.setColor(Color.BLACK);
    g.drawRect(squareX,squareY,squareW,squareH);

    }
}

我也得到了一个类似于java.awt.Rectangle的Rectangle类,它有一些方法可以找到由重叠构成的矩形和由所有矩形的并集构成的矩形,但我不知道如何创建使用鼠标移动的矩形对象,然后在此JLabel中绘制它们

public class Rectangle {
private int x,y,width,height;

public Rectangle(int x,int y,int width,int height)
{
    this.x = x;
    this.y = y;
    this.width = width;
    this.height = height;
}

public Rectangle(Rectangle a)
{
    this.x = a.x;
    this.y = a.y;
    this.width = a.width;
    this.height = a.height;
}

public String toString()
{
    return "Start: ("+x+","+y+"), Width: "+width+", Height: "+height+"\n";
}

public int getX()
{
    return x;
}

public int getY()
{
    return y;
}

public int getWidth()
{
    return width;
}

public int getHeight()
{
    return height;
}

public void setX(int x)
{
    this.x = x;
}

public void setY(int y)
{
    this.y = y;
}

public void setWidth(int width)
{
    this.width = width;
}

public void setHeight(int height)
{
    this.height = height;
}

public int area()
{
    return width*height;
}

public boolean overlaps(Rectangle a)
{
    if ((x>a.x+a.width) || (a.x>x+width) || (y>a.y+a.height) || (a.y>y+height))
    {
        return false;
    }
    return true;
}

public Rectangle intersect(Rectangle a)
{
    if (!overlaps(a))
        return null;

    int left,right,top,bottom;

    if (x<a.x)
        left = a.x;
    else
        left = x;

    if (y<a.y)
        bottom = a.y;
    else
        bottom = y;

    if ((x+width)<(a.x+a.width))
        right = x+width;
    else
        right = a.x+a.width;

    if ((y+height)<(a.y+a.height))
        top = y+height;
    else
        top = a.y+a.height;

    return new Rectangle(left,bottom,right-left,top-bottom);
}

public Rectangle union(Rectangle a)
{
    int left,right,top,bottom;

    if (x<a.x)
        left = x;
    else
        left = a.x;

    if (y<a.y)
        bottom = y;
    else
        bottom = a.y;

    if ((x+width)<(a.x+a.width))
        right = a.x+a.width;
    else
        right = x+width;

    if ((y+height)<(a.y+a.height))
        top = a.y+a.height;
    else
        top = y+height;

    return new Rectangle(left,bottom,right-left,top-bottom);
    }


 }

1 个答案:

答案 0 :(得分:3)

不确定为什么要扩展JLabel来进行自定义绘制。教程向您展示了如何使用JPanel。

有关增量绘画的两种常用方法,请查看Custom Painting Approaches

  1. 使用列表跟踪矩形(这可能是您想要的,因为您希望能够测试交叉点。
  2. 使用BufferedImage。