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);
        }

    }
}

Recommended Answers

All 6 Replies

Because you add the triangle to triangles every time mouseDragged is called, so you end up with it repeated many times.
You can do something like:

mouse pressed: create a new Triangle object
mouse dragged: update the Triangle object
mouse released: add the Triangle object to triangles

If you want to see the triangle change shape as you drag the mouse, then use drawPolygon to draw it in your paint method.

ps: overriding paint is not recommended, override paintComponent instead (the code is the same) . See https://docs.oracle.com/javase/tutorial/uiswing/painting/closer.html

if i remove add triangles(to list) logic from mouse dragged event, and place it in mouse release,

then again triangle did not show till mouse release.

I'm confused. Do you want the triangle to show while you are still moving the mouse? If so see my previous post. You need to add a statement to draw your temporary triangle in your paintComponent.

Please if you provide modified version of my code, it will help me alot. Because I am also confused and having hard to understand the logic. Thanks !

Sorry, but that's now we work here. We will help you to learn and help you to write your own code, butif we do your homework for you then nobody benefits.

Try to get the logic straight in your head, or on a piece of paper, before trying to write any code.

Usually people want to show the shape being drawn as the mouse moves, so they write the code in a pattern like this:

create the list of shapes
declare a temporary shape, initially null

mousepressed:  create new shape in the temporary variable
mouse dragged: update the temporary shape, call repaint
mouse released: add the temporary shape to the list, set the temporary variable to null, call repaint.

paintComponent: 
    draw all the shapes in the List
   if the temporary shape variable is not null draw the temporary shape.

 

That pattern means there's a temporary shape that keeps being updated and re-drawn as long as the mouse is moving. When the mouse is released the final shape is added to the list and the temporary shape is nulled.

you are absolutely right ! Problem solved thanks :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.