2014-03-04 42 views
-1

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

我的当前代码已被修改尽可能多的我可以从在Performing Custom Painting上的java演示程序,因为如何使用repaint方法来更新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); 

    } 
} 

我也被赋予一个Rectangle类,类似于java.awt.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); 
    } 


} 
+0

看看[这个提问/回答(http://stackoverflow.com/questions/22160245/intersection-of-two-rectangles-with-paintcomponent/22160470# 22160470),因为它基本上是相同的代码。一般的答案是,你需要维护n可以在“paintComponent”方法中绘制的“矩形”的列表 – MadProgrammer

+0

显然缺少一个问题。 –

+0

*请不要破坏你自己的问题 –

回答

3

不知道为什么要扩展一个JLabel做风俗画刷油漆。本教程向您展示了如何使用JPanel。

对于两种常见的方式做增量的油漆,退房Custom Painting Approaches

  1. 使用列表来跟踪矩形的(这可能是你想要的,因为你希望能够以测试交叉口。
  2. 使用的BufferedImage。
+0

奇怪的是,我链接的问题(基本上是相同的代码)也使用了“JLabel”,我想他们可能已经被赋予了一个“模板”来填写...... – MadProgrammer