2015-06-19 75 views
1

我在試圖從一個點向負方向繪製一個矩形問題點擊屏幕上。我有以下類模擬屏幕捕捉軟件,如Gyazo:前面已經提到問題負方向繪製矩形 - Java的

class DrawSquare extends JPanel implements MouseListener, MouseMotionListener { 

// Components 
public JDialog frame; 
public Rectangle rectangle; 
public BufferedImage bufferedImage; 
public Point start, end; 

// Variables 
public String capturedImage; 

public DrawSquare(JDialog frame) { 

    this.frame = frame; 

    // Read in crosshair image to replace mouse icon 
    Toolkit tool = Toolkit.getDefaultToolkit(); 
    Image newImage = getToolkit().getImage("components/cursor.png"); 
    Cursor cursor = tool.createCustomCursor(newImage, new Point (this.frame.getX(), this.frame.getY()), "img"); 

    this.frame.setCursor(cursor); 
    this.frame.addMouseListener(this); 
    this.frame.addMouseMotionListener(this); 

} 

@Override 
public void paintComponent(Graphics g) { 
    super.paintComponent(g); 

    Graphics2D g2d = (Graphics2D) g.create(); 
    // draw background overlay 
    g2d.drawImage(bufferedImage, WIDTH, 0, this); 
    if (rectangle != null) { 
     //g2d.setColor(new Color(225, 225, 255, 128)); 
     frame.setOpacity(0.6f); 
     //g2d.fill(rectangle); 
     System.out.println(rectangle); 
     g2d.setColor(new Color(72,119,205)); 
     g2d.draw(rectangle); 
    } 
    g2d.dispose(); 
} 

@Override 
public void mouseDragged(MouseEvent e) { 

    this.end = e.getPoint(); 
    int width = end.x - start.x; 
    int height = end.y - start.y; 

    rectangle.setSize(new Dimension(width, height)); 
    frame.validate(); 
    frame.repaint(); 
} 

@Override 
public void mouseMoved(MouseEvent e) { 
    this.start = e.getPoint(); 
    frame.validate(); 
    frame.repaint(); 
} 


@Override 
public void mousePressed(MouseEvent e) { 
    // Get the X and Y point from the mouse pressed 
    rectangle = new Rectangle(start); 
    System.out.println(rectangle); 
    // Repaint the screen 
    frame.validate(); 
    frame.repaint(); 
} 

@Override 
public void mouseReleased(MouseEvent e) {} 

@Override 
public void mouseClicked(MouseEvent e) {} 

@Override 
public void mouseEntered(MouseEvent e) {} 

@Override 
public void mouseExited(MouseEvent e) {} 

}

現在對於這個問題的原因是,當我試圖繪製矩形框的反對或點擊屏幕上的點的負方向,它不會畫,矩形信息看起來像這樣這樣的嘗試中:

java.awt.Rectangle[x=635,y=395,width=-316,height=-194]

然而,當我拖動rectnagle在正DIR撓度它的工作原理,因爲它應該是:

enter image description here

我想知道的是我怎麼能這樣使用負值的寬度/高度,或完全在做另一種方式解決。

回答

3

實際上你應該有2點 - 拖動起點和當前拖動點。

矩形計算方法是:由什麼保證金

x=min(dragStartPoint.x, dragCurrentPoint.x) 
y=min(dragStartPoint.y, dragCurrentPoint.y) 
width=abs(dragStartPoint.x - dragCurrentPoint.x) 
height=abs(dragStartPoint.y - dragCurrentPoint.y) 
+0

感謝您的回覆,我提到了兩點,起點和終點。添加上面概述的解決方案會產生相同的結果。你可能更具體一些嗎?乾杯 – Zy0n

2

增加的寬度和高度都還好。 當它們減少並達到0時,需要減少x和y。

或以其他方式看到:矩形介於兩個對角線點之間和右下角之間。對於一個座標(x或y)它們交叉時,這些角色會改變。


正面:你拖動右下角的點(我猜)。 兩個座標零點:TL和BT cooincide,W和H零點。 兩個座標負:你離開右下角點左右位置,拖動矩形的左上角點,W和H從0

最簡單的增加是:

  • 按住鼠標向下點from
  • TRAG鼠標拖動點to

計算它們之間的矩形:

Point min = new Point(Math.min(from.x, to.x), Math.min(from.y, to.y)); 
Point max = new Point(Math.max(from.x, to.x), Math.max(from.y, to.y)); 

然後矩形很容易計算。

+0

當你說降低x和y?由當前的W * H繪製? – Zy0n

+0

我在答案中澄清了; @斯坦尼斯拉夫直到現在都提供了最好的答案。 –