我有一個繪畫程序,我已經完成了所有按鈕和滑塊,但是我對實際繪畫本身有問題。當我將光標拖過屏幕而不是一條完整的線時,我幾乎得到了一條我不想要的虛線。下面是在JPanel
和BufferedImage
爲MouseListener
代碼:.drawLine()問題和緩衝圖像
public void mouseDragged(MouseEvent e) {
Graphics g=buffered.getGraphics();
g.setColor(mycol);
Graphics2D graph=(Graphics2D)g;
BasicStroke stroke=new BasicStroke(30);
graph.setStroke(stroke);
// g.fillRect(xcor, ycor, 20, 20);
//varx=e.getX();
ycor=e.getY();
xcor=e.getX();
int bad=xcor;
int good=ycor;
graph.drawLine(xcor, ycor, bad, good);
// buffered.setRGB(xcor, ycor, mycol.getRGB());
repaint();
// g.drawLine(xcor, ycor, x, x)
repaint();
}
我看來,你提供了錯誤的參數給'的drawLine()'方法,因爲值x1,y1是一樣X2,Y2。因此,點就是你的情況,你應該做的是'mouseClick()'存儲x1,y1,然後拖動get x2,y2。然後通過這兩個'drawLine(xClicked,yClicked,xDragged,yDragged)',這將使該行出現在你的情況:-) – 2012-08-09 18:33:13