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;
}
}
我正在尝试编写一个程序来创建鼠标移动的可视化。
上面的代码正在抓住鼠标coords很好问题是我不确定如何调用paintComponent以便在框架上绘制。
任何帮助都将不胜感激。
答案 0 :(得分:1)
首先,您必须在contentpane中添加MouseTracker
的新实例:
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);
所以整个代码看起来像这样(行是持久的):
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;
}
}