好吧,所以我正在尝试制作一个油漆项目,至今我都很成功。我有一个“画布”来绘制,我有一个带有颜色选择的工具栏菜单。我用drawLine()
制作了一个按钮和方法,它通过鼠标移动侦听器和我的结束坐标获取我当前的坐标,基本上在画布上随时随地绘制线条。现在我想添加一个按钮,单击它时,将在我的画布上绘制一个椭圆形/圆形。这里出现问题。第一个问题 - 我可以让它在我的鼠标坐标上绘制椭圆,但在释放鼠标按钮并实际绘制它之前,我无法拖动它以改变其大小(就像在Microsoft绘制中那样)。如何正确使用drawOval()方法?
第二个问题 - 当我选择了我的“线条”按钮来调用我的画线方法时,我可以画线条,但是当我点击“椭圆形”按钮时,它将绘制椭圆形,当我点击鼠标时(我假设当我选择椭圆时我需要禁用鼠标移动侦听器)。相反也是如此,如果我之前选择了“椭圆”按钮并绘制了椭圆,则点击“线”按钮,它会绘制一条线,而且每次点击开始绘制线条时都会放一个椭圆。
这是我简单的程序看起来像: Image
下面是有关的drawLine,drawOval和我协调收集方法的代码,因为其他一切都如预期运行的一部分:
// Image in which we're drawing.
private Image image;
// Graphics2D object which we used to draw on.
private Graphics2D g2;
// Mouse coordinates
private int currentX, currentY, oldX, oldY;
public DrawArea(){
setDoubleBuffered(false);
addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e){
//save coordinates x,y when mouse is pressed.
oldX=e.getX();
oldY=e.getY();
}});
}
protected void paintComponent(Graphics g){
if (image==null){
//image to draw null ==> we create
image = createImage(getSize().width, getSize().height);
g2 = (Graphics2D) image.getGraphics();
//enable antialiasing.
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
//clear draw area
clear();
}
g.drawImage(image, 0, 0, null);
}
public void clear(){
g2.setPaint(Color.white);
//draw white on entire draw are to clear it.
g2.fillRect(0, 0, getSize().width, getSize().height);
g2.setPaint(Color.black);
repaint();
}
public void Line(){
addMouseMotionListener(new MouseMotionAdapter(){
public void mouseDragged(MouseEvent e){
//coordinates x,y when dragging mouse.
currentX=e.getX();
currentY=e.getY();
if (g2 != null){
g2.drawLine(oldX, oldY, currentX, currentY);
repaint();
oldX = currentX;
oldY = currentY;
}
}
});
}
public void Oval(){
addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e){
currentX=e.getX();
currentY=e.getY();
if (g2 != null){
g2.drawOval(oldX, oldY, currentX, currentY);
repaint();
}
}
});
}
谢谢您的输入。问题更加突出,例如当我选择Oval并选择Line时,每当我按下画线时,它也会弹出一个椭圆,反之亦然。当我按下按钮时它没有画任何东西,因为我已经在检查它了,所以这至少不是问题。我只需要弄清楚如何让mouselistener一次只能使用一个“形状”。 – Sick
@Sick你也可以删除以前添加的侦听器,如果有的话,但我会建议尝试只使用一个。毕竟,听众的任务是收集某些事件的位置并通知其他组件,但不会执行实际绘制(请参阅单一职责原则)。因此,尝试使用另一个类(或更好的层次结构)进行渲染 - 我将添加一个示例。 – Thomas