簡體   English   中英

自制油漆應用程序中的繪圖錯誤

[英]drawing bug in self-made paint application

在Java開發的類似於Paint的應用程序中,我找不到一個愚蠢的錯誤。 現在的問題是,當我繪制形狀(比如說矩形)時,矩形應該在所有4個方向上繪制(無論以何種方式拖動),但是現在它的繪制只在右下方。 我將其編碼為全方位。 在找出錯誤的同時,我意識到當您向左拖動時,初始x和y會取x2的值y2。 我不知道為什么會這樣,因為我沒有這樣做的代碼。 請幫助,我整夜都在處理此錯誤((

此類負責所有形狀

public abstract class DrawShapes implements DrawListenerInterface {

private final Canvas canvas;

protected int x;
protected int y;
protected int x2;
protected int y2;
protected int w;
private boolean prev;

protected int h;

public DrawShapes(Canvas canvas) {
    this.canvas = canvas;
}

@Override
public void preview(Graphics2D g2) {
    g2.setColor(Color.black);
    if (prev) {
        draw(g2);
    }
}

@Override
public void draw(Graphics2D g2) {

    System.out.println("before " + x + " " + y + " " + x2 + " " + y2);
    x = Math.min(x, x2);
    y = Math.min(y, y2);
    w = Math.abs(x - x2);
    h = Math.abs(y - y2);
    //
    System.out.println("after " + x + " " + y + " " + x2 + " " + y2);

}

@Override
public void mousePressed(MouseEvent e) {
    prev = true;

    this.x = e.getX();
    this.y = e.getY();
    this.x2 = e.getX();
    this.y2 = e.getY();
    System.out.println("PRESSED " + x + " " + x2 + " " + y + " " + y2);

    canvas.repaint();
}

@Override
public void mouseReleased(MouseEvent e) {

    // this.x2 = e.getX();
    // this.y2 = e.getY();
    final Graphics2D g2 = (Graphics2D) canvas.getImage().getGraphics();
    canvas.defaultSettings(g2);
    prev = false;
    draw(g2);
    // canvas.repaint();

}

@Override
public void mouseDragged(MouseEvent e) {

    this.x2 = e.getX();
    this.y2 = e.getY();
    System.out.println("dragging " + x2 + " " + y2);
    this.canvas.repaint();

}

此類是DrawShapes的子類,它正在繪制自己的形狀

public class SquareListener extends DrawShapes {



@Override
public void draw(Graphics2D g2) {
    // TODO Auto-generated method stub

    super.draw(g2);
        g2.drawRect(x, y, w, h);
// System.out.println(x + " " + y + " " + x2 + " " + y2);
}

您必須將X1與X2和Y1與Y2進行比較,以防萬一* 1> * 2-將它們交換為drawRect()。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM