2014-03-04 41 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