Uso de JFrame con una secuencia de entrada continua
Estoy tratando de implementar un código que lea desde el puerto serie de mi redboard y, según lo que lea, repinte un círculo. El objetivo final de esto es usar la clase Robot para lograr el control real del cursor, pero primero quiero aprender más sobre Java en el camino y por eso estoy tratando de lograrlo primero con algunos gráficos básicos.
Para resumir mi problema, no sé cómo usar JFrame con una entrada que cambia continuamente desde un método estático.
El puerto serie que accede a JAR se puede encontrar enhttp://fazecast.github.io/jSerialComm/
El Arduino escribe continuamente en el serial basado en un sistema de acelerómetro FPGA en la forma "UpLeft", "Up", "UpRight", "Left", "Center", "Right", "DownLeft", "Down", " Completamente". El programa Java debería tomar esto y volver a pintar un círculo en consecuencia.
Puedo abrir COMM3 e imprimir la dirección correcta recibida de mi hardware, pero cada vez que intento aplicar JFrame me pierdo. He encontrado muchos tutoriales de ActionListener, pero esta implementación debe ser continua y no depender de eventos de mouse o teclado. Por lo tanto, no sé cómo usar los métodos paintComponent () y painter () ya que el método principal es estático.
¡Muchas gracias por tu tiempo!
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) {};
}
}
}