Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7509795
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T23:01:42+00:00 2026-05-29T23:01:42+00:00

I’m new at using actionListener and mouselistner . I want to track the Mouse

  • 0

I’m new at using actionListener and mouselistner. I want to track the Mouse movements and connect the dots as I go. So it will be one continuous line. I am using the MouseMotionListener every time I move the mouse across the screen and get a new set of points. I am not using mousePressed or mouseReleased.

Everytime I move the mouse, I can get every point of where my mouse’s position is on my JPanel to show up on my results window, but in order to draw the line between two points, I’m not sure how to decipher between each different set of points so I can have a beginning and ending set of points to use for drawing a line? I checked through here and the API and I’m stuck.

I was asked to put more code and so I gave you my code.

I am basically creating a Jpane that tracks mouse movement when the Track Mouse button or Draw Track Radio button is pressed
These results will print out as (x,y) on the output screen

FYI: You can also draw circles once the Draw circles RadioButton is checked

Jpanel
Output

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;

public class MouseTest {

private JFrame frame;
private boolean tracking;
private boolean drawing;
private boolean connectdots;
private int xstart;             
private int ystart;             
private int xend;   
private int y;              
private int x;  
private int yend;               
private int borderWidth = 5;    
String drawCircles = "Draw Circles";
String drawTrack = "Draw Track";

public static void main (String [] arg) {
    MouseTest first = new MouseTest(); 
}//main

$

public MouseTest() {
    tracking = false; 
    frame = new JFrame(); 
    frame.setBounds(250,98,600,480);
    frame.setTitle("Window number three");
    Container cp = frame.getContentPane();
    JButton track = new JButton("Track Mouse");
    track.addActionListener(new ActionListener() {    

        public void actionPerformed(ActionEvent ae) {
            trackMouse();
        }
    });

    JPanel top = new JPanel();
    top.add(track);
    cp.add(top,BorderLayout.NORTH);

    MyPanel pane = new MyPanel();

    cp.add(pane,BorderLayout.CENTER);
    pane.addMouseListener(new MouseAdapter() {
        public void mousePressed(MouseEvent e) {
            xstart = e.getX();
            ystart = e.getY();
        }
        public void mouseReleased(MouseEvent e) {
            xend = e.getX();
            yend = e.getY();
            if (xend < xstart) { int tmp = xstart; xstart = xend; xend = tmp; } 
            if (yend < ystart) { int tmp = ystart; ystart = yend; yend = tmp; } 
            frame.repaint();
        }
    });
    pane.addMouseMotionListener(new MouseMotionAdapter() { 
        public void mouseMoved(MouseEvent e) { 
            if (tracking) { 
                x = e.getX();  
                y = e.getY();
                msg("(" + x + ", " + y + ")");
            }
        }
    });

    JRadioButton circleButton = new JRadioButton(drawCircles);
    circleButton.setMnemonic(KeyEvent.VK_B);
    circleButton.setActionCommand(drawCircles);
    circleButton.setSelected(false);

    JRadioButton trackButton = new JRadioButton(drawTrack);
    trackButton.setMnemonic(KeyEvent.VK_C);

    ButtonGroup group = new ButtonGroup();
    group.add(circleButton);
    group.add(trackButton);

    JPanel radioPanel = new JPanel(new GridLayout(2,0)); 
    radioPanel.add(circleButton);
    radioPanel.add(trackButton);    
    cp.add(radioPanel,BorderLayout.EAST);
    drawing = false;
    connectdots = false;

    circleButton.addActionListener(new ActionListener() { //Set drawing to true when the button is clicked
        public void actionPerformed(ActionEvent ae) {
            tracking = !tracking;
            drawCircles();
        }
    });

    trackButton.addActionListener(new ActionListener() { //Set drawing to true when the button is clicked
        public void actionPerformed(ActionEvent ae) {
            drawing = false;
            tracking = true;
            connectDots();
        }   
    });

    frame.setVisible(true);
}//constructor

$

public void trackMouse() {  
        tracking = !tracking;
}//trackMouse

public void msg(String s) { //new method to simplify the system.out.print method
    System.out.println(s);
}

public void drawCircles() { 
    drawing = true;

}//Allow Drawing of Circles

public void connectDots() { 
    connectdots = !connectdots;

}//Trying to use for connecting the mouse motion listener points when tracking

$

public class MyPanel extends JPanel {        

    ArrayList<Circle> circles = new ArrayList<Circle>();


public void paintComponent(Graphics g) {

    int width  = this.getWidth();
    int height = this.getHeight();

    msg("H = " + height + ",  w = " + width);
    g.setColor(Color.BLACK);
    Circle c = new Circle(xstart, ystart);   

    circles.add(c);
    if (drawing){  
        for(int k=0; k<circles.size(); k++){
            circles.get(k).draw(g);
        }
    }           // draw the circle

    if (connectdots && tracking) { //trying to use for drawing the lines
        g.drawLine(x,y,x,y);
    }

    for (int delta = 0; delta < borderWidth; delta++) {

        g.drawRect(delta,delta,width-(2*delta),height-(2*delta));

    }
    if (xstart != xend || ystart != yend) {    
        int red     = (int)(256*Math.random());
        int green   = (int)(256*Math.random());
        int blue    = (int)(256*Math.random());
        g.setColor(new Color(red, green, blue));
        msg("Colors are:  " + red + " - " + green + " - " + blue );
        msg("Drawing from: (" + xstart + ", " + ystart + ") to (" + xend + ", " + yend + ")");
        msg("Width is " + (xend-xstart) + " -  Height is " + (yend-ystart));
        g.fillRect(xstart,ystart,xend-xstart,yend-ystart);
    }

}
}
}

$

public class Circle {

// instance variables:
int radius;  //random radius
int x, y;  // coords of the center point
private Graphics g;


public Circle(int xIn, int yIn) {
    radius = (int)(127*Math.random());
    x = xIn;
    y = yIn;

}    

public void draw(Graphics g){
    g.drawOval(x-radius, y-radius, radius, radius);
    g.fillOval(x-radius, y-radius, radius, radius);
    int red     = (int)(256*Math.random());
    int green   = (int)(256*Math.random());
    int blue    = (int)(256*Math.random());
    g.setColor(new Color(red, green, blue));
}

public int getX(int xstart) {
    // TODO Auto-generated method stub
    return x;
}

public int getY(int ystart) {
    // TODO Auto-generated method stub
    return y;
}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T23:01:47+00:00Added an answer on May 29, 2026 at 11:01 pm

    You just have to save x and y in some attribute for your class (like lastX and lastY.) When you get the event the second time, your first point coordinates are saved in your attributes and you can then draw your line. This new point has to be saved too to serve as your new lastX and lastY.

    Even better: since you’ll probably need to redraw your lines each frame (if you want a set of lines and not just a single line to be drawn each frame), use Point to save your coordinates and some kind of list like ArrayList. Save the full set of tracked points in an ArrayList<Point> and then draw each frame iterating over this list.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I am reading a book about Javascript and jQuery and using one of the
I want use html5's new tag to play a wav file (currently only supported
I'm making a simple page using Google Maps API 3. My first. One marker
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.