在鼠标拖动上绘制形状

时间:2017-11-02 04:11:33

标签: java swing user-interface mouseevent mouselistener

class MyPanel extends JPanel implements Observer, MouseMotionListener, MouseListener {
private MyModel model;
private View view; 
private String mode; 
private Rectangle rectangle;
private Square square;

public MyPanel(MyModel model, View view) {
    this.setBackground(Color.black);
    this.setPreferredSize(new Dimension(300, 300));
    this.addMouseListener(this);
    this.addMouseMotionListener(this);
    this.model = model;
    this.model.addObserver(this);

}

public void paintComponent(Graphics g) {

    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g;

    ArrayList<Rectangle> rectangles = this.model.getRectangles();
    for (Rectangle r : getRectangles()) {

        int width = Math.abs(r.getStartPoint().getX() - r.getEndPoint().getX());
        int height = Math.abs(r.getStartPoint().getY() - r.getEndPoint().getY());
    }

    ArrayList<Square> squares = this.model.getSquares();

    for (Square sqr : getSquares()) {

        int xPosition = Math.min(sqr.getStartPoint().getX(), sqr.getEndPoint().getX());
        int yPosition = Math.min(sqr.getStartPoint().getY(), sqr.getEndPoint().getY());
        int width = Math.abs(sqr.getStartPoint().getX() - sqr.getEndPoint().getX());
        int height = Math.abs(sqr.getStartPoint().getY() - sqr.getEndPoint().getY());       
    }

    g2d.dispose();
}

public void update(Observable o, Object arg) {
    this.repaint();
}

@Override
public void mouseDragged(MouseEvent e) {

    if (this.mode.equals("Rectangle")) {

        this.rectangle.setEndPoint(e.getX(), e.getY());
        this.model.addRectangle(this.rectangle);
    }

    else if (this.mode.equals("Square")) {


        // What code should I add here?

        this.model.addSquare(this.square);

    }

}

// MouseListener below
@Override
public void mouseClicked(MouseEvent e) {}

@Override
public void mousePressed(MouseEvent e) {

    if (this.mode.equals("Rectangle")) {
        this.rectangle = new Rectangle(e.getX(), e.getY());
    }

    else if (this.mode.equals("Square")) {
        this.square = new Square(e.getX(), e.getY());

    }
}

@Override
public void mouseReleased(MouseEvent e) {

    if (this.mode.equals("Rectangle")) {

        this.rectangle.setEndPoint(e.getX(), e.getY());
        this.model.addRectangle(this.rectangle);
        this.rectangle = null;
    }

    else if (this.mode.equals("Square")) {
        this.model.addSquare(this.square);
        this.square = null;
    }

}

@Override
public void mouseEntered(MouseEvent e) {}
@Override
public void mouseExited(MouseEvent e) {}

}

用户选择模式,矩形或方形。然后他们可以用鼠标绘制正方形或矩形(显示实时反馈)。这是我的绘图面板类。我成功地实现了矩形模式。用户可以绘制一个矩形,当他们移动鼠标时,矩形显示在中间构造中。我想对方形模式做同样的事情。出于某种原因,我很难做到这一点。当用户移动鼠标时,我将如何在中间构造中显示完美的正方形?一旦释放,我将如何绘制它?我应该在paintComponent方法,mouseDragged,mousePressed和mouseReleased方法中添加什么代码来执行此操作?对于一个矩形很容易,因为没有约束,但我不知道如何使用我当前的实现来做一个正方形。

1 个答案:

答案 0 :(得分:1)

    int width = Math.abs(r.getStartPoint().getX() - r.getEndPoint().getX());
    int height = Math.abs(r.getStartPoint().getY() - r.getEndPoint().getY());

我猜这个&#34;尺寸&#34;正方形将是上述两个值的最大值。

然后我认为你会使用:

r.drawStyle(g2d, xPosition, yPosition, size, size);