Search code examples
javaapplet

Drawing dynamic lines in java applets using JAVA AWT mousemotionlistener?


So, here is the thing. My lecturer taught us how to get mouse coordinates by clicking on an applet. Now, i was thinking that may be i can try drawing dynamic lines using mousemotionlistener class using the mousedrag method and the mouse coordinates. I tried it but couldn't really make it work. Can anyone please suggest me where i made the error. Thanks a lot.

import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.applet.*;

public class drawing extends Applet
implements MouseMotionListener {  

    int mx,my;
    Graphics2D g2;

    public void init()
    {
        this.addMouseMotionListener(this);

    }

    public void paint(Graphics g)
    {
        this.setSize(800,600);

         g2 = (Graphics2D) g;
         g2.setStroke(new BasicStroke(10));
         g2.drawLine(40,50,200,150); // I was experimenting to draw static lines using the drawLine() method. 
    }

    @Override
    public void mouseDragged(MouseEvent e) {
        // TODO Auto-generated method stub

        mx = e.getX();
        my = e.getY();
        System.out.println(" "+ mx + " "+ my);
        g2.setStroke(new BasicStroke(10));
        g2.drawLine(mx,my,mx,my); 



    }

    @Override
    public void mouseMoved(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }



}

Solution

  • In order to draw a line, you need to know the start point (which would be the point the user clicked) and the end point (the point where the user dragged to), from there is a simple matter to simple use Graphics#drawLine.

    Start by taking a look at How to Write a Mouse Listener, 2D Graphics and Painting in AWT and Swing for more details

    import java.applet.Applet;
    import java.awt.Graphics;
    import java.awt.Point;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.awt.event.MouseMotionListener;
    
    public class Drawing extends Applet
                    implements MouseMotionListener, MouseListener {
    
        private Point startPoint, endPoint;
    
        public void init() {
            this.addMouseMotionListener(this);
            addMouseListener(this);
        }
    
        @Override
        public void paint(Graphics g) {
            super.paint(g);
            if (startPoint != null && endPoint != null) {
                g.drawLine(startPoint.x, startPoint.y, endPoint.x, endPoint.y);
            }
        }
    
        @Override
        public void mouseDragged(MouseEvent e) {
            endPoint = e.getPoint();
            repaint();
        }
    
        @Override
        public void mouseMoved(MouseEvent arg0) {
    
        }
    
        @Override
        public void mouseClicked(MouseEvent e) {
        }
    
        @Override
        public void mousePressed(MouseEvent e) {
            startPoint = e.getPoint();
            endPoint = e.getPoint();
            repaint();
        }
    
        @Override
        public void mouseReleased(MouseEvent e) {
        }
    
        @Override
        public void mouseEntered(MouseEvent e) {
        }
    
        @Override
        public void mouseExited(MouseEvent e) {
        }
    
    }
    

    Now, my advice, don't use Applet or AWT directly. Instead, start with a JPanel and override it's paintComponent method and use it perform your custom painting. Then add this panel to a JFrame to display it on the screen.

    AWT is more then 15 years out of date and applets have way to many gotchas which you just don't need to deal with right now.

    Start by having a look at Creating a GUI With JFC/Swing or even do some research into JavaFX