我正在尝试实现从我的红板的串行端口读取的代码,并根据读取的内容重新绘制一个圆圈。最终目的是使用Robot类来实现实际的光标控制,但是我首先想学习更多有关Java的知识,因此我想首先通过一些基本图形来实现它。
总结一下我的问题,我不知道如何在静态方法的不断变化的输入中使用JFrame。
可以在http://fazecast.github.io/jSerialComm/中找到访问JAR的串行端口
Arduino连续基于FPGA加速度计系统以“ UpLeft”,“ Up”,“ UpRight”,“ Left”,“ Center”,“ Right”,“ DownLeft”,“ Down”,“彻头彻尾”。然后,Java程序应抓住它并相应地重画一个圆圈。
我能够打开COMM3并打印从硬件接收到的正确方向,但是每当尝试应用JFrame时,我都会迷路。我已经找到了许多ActionListener教程,但是此实现应该是连续的,并且不依赖于鼠标或键盘事件。因此,由于主要方法是静态的,因此我不知道如何使用paintComponent()和painter()方法。
非常感谢您的宝贵时间!
import java.awt.Color;
import java.awt.Graphics;
import java.util.Scanner;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSlider;
import com.fazecast.jSerialComm.*;
public class Main extends JPanel{
public void paintComponent(Graphics g, int x, int y) {
super.paintComponent(g);
g.setColor(Color.MAGENTA);
g.fillOval(x, y, 20, 20);
}
public void painter(int x, int y, int velx, int vely){
x = x + velx;
y = y + vely;
repaint();
}
public static void main(String[] args) {
int x = 0, y = 0, velx = 0, vely = 0;
SerialPort ports[] = SerialPort.getCommPorts();
System.out.println("Select a Port:");
SerialPort port = ports[1];
Graphics g;
if(port.openPort()){
System.out.println("Successfully opened the port.");
} else {
System.out.println("Unable to open the port.");
}
port.setComPortTimeouts(SerialPort.TIMEOUT_SCANNER, 0, 0);
JFrame jf = new JFrame();
Main main = new Main();
jf.setTitle("Window");
jf.setSize(600, 400);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.add(main);
Scanner data = new Scanner(port.getInputStream());
while(data.hasNextLine()) {
System.out.println(data.nextLine());
try{String dir = data.nextLine();
if(dir.equals("UpLeft")) {
velx = -1;
vely = -1;
}
if(dir.equals("Up")) {
velx = 0;
vely = -1;
}
if(dir.equals("UpRight")) {
velx = 1;
vely = -1;
}
if(dir.equals("Left")) {
velx = -1;
vely = 0;
}
if(dir.equals("Center")) {
velx = 0;
vely = 0;
}
if(dir.equals("Right")) {
velx = 1;
vely = 0;
}
if(dir.equals("DownLeft")) {
velx = -1;
vely = 1;
}
if(dir.equals("Down")) {
velx = 0;
vely = 1;
}
if(dir.equals("DownRight")) {
velx = 1;
vely = 1;
}
System.out.println(velx);
System.out.println(vely);
}
catch(Exception e) {};
}
}
}
最佳答案
意见建议:
从静态main方法中读取InputStream并将其读取到其自己的类中。
此类应扩展SwingWorker<Void, String>
。您必须阅读Concurrency in Swing教程,以全面了解如何使用此类。它将帮助您在后台线程中进行长时间运行的流读取,然后将获取的数据安全地传递到Swing事件线程上的Swing GUI中。
流中的所有读取都应在此类的doInBackground()
方法中完成。从扫描程序/流获取字符串,然后调用工作程序的publish(...)
方法,将字符串传递到此方法中。
为您的Swing GUI提供公共方法,例如public void getPortText(String text)
在Worker中使用protected void process(List<String> chunks)
方法替代。通过for循环遍历块List<String>
,通过调用GUI的getPortText(...)
方法将数据传递到Swing GUI中。
在GUI的getPortText方法中,更改GUI类的字段的状态,velX和velY字段将起作用,然后调用repaint()
在GUI(JPanel的)paintComponent方法重写中,使用velX和velY字段更改绘制的内容。
当然,工作人员需要引用可视化的GUI才能正常工作(所有这些都需要正确地“接线”)。
您的main方法中包含一个Graphics变量。摆脱它,因为在那个位置很危险。避免在paintComponent方法之外或在paintComponent中调用的方法之外使用Graphics对象。避免给您的班图形字段。主要的例外情况是,如果您使用的是从BufferedImage中提取的Graphics对象。
成功