0
所以我在java中做了一个画图应用程序。然而,不要点击并按住,然后拖动来画东西。相反,我想点击激活我的画笔/笔,它将绘制到我带上鼠标的位置,然后再次单击以取消激活画笔/笔。点击激活笔的Java画图
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;
}
}
});