gpt4 book ai didi

java - 如何在JFrame上绘图

转载 作者:行者123 更新时间:2023-12-01 11:10:58 26 4
gpt4 key购买 nike

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

我正在尝试编写一个程序来创建鼠标移动的可视化。

上面的代码很好地获取了鼠标坐标,问题是我不确定如何调用paintComponent以便在框架上绘制。

如有任何帮助,我们将不胜感激。

最佳答案

首先,您必须将 MouseTracker 的新实例添加到内容 Pane :

JFrame frame = new JFrame();

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

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

然后在定时器中调用repaint:

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

关于java - 如何在JFrame上绘图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32378740/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com