user3556152
user3556152

Reputation: 105

How to drawing on JFrame

public class MouseTracker extends JPanel{
static List<Double> xL = new ArrayList<Double>();
static List<Double> yL = new ArrayList<Double>();

public static void main(String args[]) throws InterruptedException{
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(1920,1080);
    frame.setVisible(true);

    Timer time = new Timer();
    time.schedule(new TimerTask(){
        public void run(){
            String coords = getCoords();
            System.out.println(coords);
        }
    }, 0, 250);
}


public void paintComponent(Graphics g){
    super.paintComponent(g);

    List<Integer> x = new ArrayList<Integer>();
    List<Integer> y = new ArrayList<Integer>();

    x.add(xL.get(xL.size() - 1).intValue());
    x.add(xL.get(xL.size() - 2).intValue());

    y.add(yL.get(yL.size() - 1).intValue());
    y.add(yL.get(yL.size() - 2).intValue());

    Graphics2D g2d = (Graphics2D) g;

    g2d.setColor(Color.red);
    g2d.drawLine(x.get(0), y.get(0), x.get(1), y.get(1));
}


public static String getCoords(){
    double xCoord = MouseInfo.getPointerInfo().getLocation().getX();
    double yCoord = MouseInfo.getPointerInfo().getLocation().getY();

    xL.add(xCoord);
    yL.add(yCoord);

    String coords = Double.toString(xCoord) + " : " + Double.toString(yCoord);
    return coords;
}
}

Im trying to write a program to create a visualization of mouse movement.

The above code is grabing the mouse coords fine the problem is I am not sure as to how to invoke the paintComponent in order to draw on the frame.

Any help would be appreciated.

Upvotes: 0

Views: 331

Answers (1)

Lukas Rotter
Lukas Rotter

Reputation: 4188

First you have to add a new instance of MouseTracker to the contentpane:

JFrame frame = new JFrame();

frame.getContentPane().add(new MouseTracker());

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1920, 1080);
frame.setVisible(true);

Then call repaint in the timer:

Timer time = new Timer();
time.schedule(new TimerTask() {
    public void run() {
        String coords = getCoords();
        System.out.println(coords);
        frame.getContentPane().repaint();
    }
}, 0, 250);

So the whole code will look like this (where the lines are persistent):

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.MouseInfo;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class MouseTracker extends JPanel {
    static ArrayList<Double> xL = new ArrayList<Double>();
    static ArrayList<Double> yL = new ArrayList<Double>();

    public static void main(String args[]) throws InterruptedException {
        JFrame frame = new JFrame();

        frame.getContentPane().add(new MouseTracker());

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(1920, 1080);
        frame.setVisible(true);

        Timer time = new Timer();
        time.schedule(new TimerTask() {
            public void run() {
                String coords = getCoords();
                System.out.println(coords);
                frame.getContentPane().repaint();
            }
        }, 0, 250);
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        ArrayList<Integer> x = new ArrayList<Integer>();
        ArrayList<Integer> y = new ArrayList<Integer>();

        for (int i = 0; i < xL.size(); i++) {

            x.add(xL.get(i).intValue());
            y.add(yL.get(i).intValue());

        }

        Graphics2D g2d = (Graphics2D) g;

        g2d.setColor(Color.red);

        for (int i = 0; i < xL.size() - 1; i++) {

            g2d.drawLine(x.get(i), y.get(i), x.get(i + 1), y.get(i + 1));

        }

    }

    public static String getCoords() {
        double xCoord = MouseInfo.getPointerInfo().getLocation().getX();
        double yCoord = MouseInfo.getPointerInfo().getLocation().getY();

        xL.add(xCoord);
        yL.add(yCoord);

        String coords = Double.toString(xCoord) + " : "
                + Double.toString(yCoord);
        return coords;
    }
}

Upvotes: 1

Related Questions