所以我在java中创建了一个paint应用程序。然而,而不是单击并按住然后拖动来绘制一些东西。相反,我想点击激活我的画笔/笔,它将绘制到我带鼠标的位置,然后再次单击以停用画笔/笔。
public class DrawArea extends JComponent {
private Image image;
private Graphics2D g2;
// Mouse coordinates
private int currentX, currentY, oldX, oldY;
public DrawArea() {
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
// save coord x,y when mouse is pressed
oldX = e.getX();
oldY = e.getY();
}
});
addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent e) {
// coord x,y when drag mouse
currentX = e.getX();
currentY = e.getY();
if (g2 != null) {
// draw oval if g2 context not null
g2.drawOval(oldX, oldY, 20, 20);
g2.fillOval(oldX, oldY, 20, 20);
// refresh draw area to repaint
repaint();
// store current coords x,y as olds x,y
oldX = currentX;
oldY = currentY;
}
}
});
答案 0 :(得分:0)
创建一个在您单击时激活的变量,运行直到再次单击,将变量设置为false。这就是我的工作。