我正在使用Graphics2D
,Point
和Line2D
(对于BrushStroke)来创建一个免费的手绘程序(用于教育目的)。我的问题是,我可以使用鼠标拖动来绘制线条,但它始于JPanel
的左上角到我释放鼠标按钮的位置。我希望能够使用Point
(E.G Point startPoint,endPoint)能够在移动鼠标时绘制,在释放时停止,并在再次按下鼠标时开始绘制新点。有人可以帮助我使用鼠标监听器进行编码(mousedragged,... press,... release等)?
public class GraphicsPanel extends JPanel implements MouseMotionListener, MouseListener {
private Color color;
private JColorChooser cc;
private JComboBox paintToolBox;
Point pointStart = null;
Point pointEnd = null;
GraphicsPanel(JColorChooser cc, Color color, JComboBox paintToolBox) {
this.cc = cc;
this.color = color;
this.paintToolBox = paintToolBox;
addMouseMotionListener(this);
}
@Override
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(cc.getColor());
Line2D line2D = new Line2D.Double(pointStart, pointEnd);
if (pointStart != null || pointEnd != null) {
g2d.draw(line2D);
}
}
@Override
public void mouseDragged(MouseEvent e) {
}
@Override
public void mouseMoved(MouseEvent e) {
}
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
}
答案 0 :(得分:0)
建议:
ArrayList<Line2D>
作为实例字段