Hello eveyone,
Please tell me why triangle shape is redrawing on mouse move. It want triangle shape draw only once until mouse release.
Here is my code below. Thank you !
public class Triangle extends JFrame {
Point startDrag, endDrag, midPoint;
private java.util.List<Polygon> triangles = new LinkedList<Polygon>();
Polygon triangle;
public static void main(String[] args)
{
Triangle t = new Triangle();
}
Triangle()
{
addMouseListener(mouseListener);
addMouseMotionListener(mouseMotionListener);
this.setSize(600, 500);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
MouseListener mouseListener = new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
startDrag = new Point(e.getX(), e.getY());
endDrag = startDrag;
if (startDrag.x > endDrag.x)
midPoint = new Point((endDrag.x +(Math.abs(startDrag.x - endDrag.x)/2)),e.getY());
else
midPoint = new Point((endDrag.x -(Math.abs(startDrag.x - endDrag.x)/2)),e.getY());
int[] xs = { startDrag.x, endDrag.x, midPoint.x };
int[] ys = { startDrag.y, startDrag.y, midPoint.y };
triangle = new Polygon(xs, ys, 3);
repaint();
}
@Override
public void mouseReleased(MouseEvent e) {
startDrag = null;
endDrag = null;
repaint();
}
};
MouseMotionListener mouseMotionListener = new MouseMotionAdapter() {
@Override
public void mouseDragged(MouseEvent e) {
endDrag = new Point(e.getX(), e.getY());
if (startDrag.x > endDrag.x)
midPoint = new Point((endDrag.x +(Math.abs(startDrag.x - endDrag.x)/2)),e.getY());
else
midPoint = new Point((endDrag.x -(Math.abs(startDrag.x - endDrag.x)/2)),e.getY());
int[] xs = { startDrag.x, endDrag.x, midPoint.x };
int[] ys = { startDrag.y, startDrag.y, midPoint.y };
triangles.add(new Polygon(xs, ys, 3));
repaint();
}
};
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
for (Polygon triangle : triangles) {
g2.drawPolygon(triangle);
}
}
}