如何用新的散点更新绘图区?
how to update the drawing area with new scatter points?
我编写了一个连接到 GPS 接收器并从中提取坐标的程序。我想创建一个散点图,我将在上面绘制从接收器中提取的位置点,每次有新的观察可用时都应更新此图。
她是我的串行端口 reader class,它允许我从接收器中提取数据:
class SerialPortReader implements SerialPortEventListener {
public void serialEvent(SerialPortEvent event) {
if (event.isRXCHAR() && event.getEventValue() > 0) {
try {
String tmp = SP.readString();
String[] msgs = tmp.split("\r\n");
for (int j = 0; j < msgs.length; j++){
if (msgs[j].contains("$GPGGA") && msgs[j].length() > 60){
System.out.println(msgs[j]);
NMEA nmea = new NMEA();
nmea.parse(msgs[j]);
System.out.println(nmea.position.toString());
writer.write(nmea.position.toString() + "\n");
}
}
}
catch (SerialPortException ex) {System.out.println(ex);}
catch (IOException ex) {System.out.println(ex);}
}
}
}
这是我的端口连接器方法:
public void openport(){
try {
SP.openPort();
SP.setParams(SerialPort.BAUDRATE_115200, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
SP.addEventListener(new SerialPortReader(),SerialPort.MASK_RXCHAR);
String dir = System.getProperty("user.dir");
String file = dir+"\NMEAout\nmeaClean.txt";
writer = new BufferedWriter(new FileWriter(file));
Canvas C = new Canvas();
C.setBackground(Color.green);
frame = new JFrame("canvas");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setLocation(500, 500);
frame.setSize(1000, 1000);
frame.add(C);
}
catch (SerialPortException ex) {System.out.println(ex);}
catch (IOException ex) {System.out.println(ex);}
}
这是我的 Canvas class 应该允许我在屏幕上绘制点。这还没有完成,这是我需要帮助的部分:
class Canvas extends JPanel{
int x = 100, y = 100;
public void newParam (int xnew, int ynew){
x = xnew;
y = ynew;
}
public void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(Color.red);
g.fillOval(x, y, 5, 5);
repaint();
}
}
frame
是在我的主 class 顶部声明的 JFrame
对象。
问题是我不明白如何在通过事件侦听器获得 Canvas 绘图新点后获得它们。我还想在不删除旧点的情况下绘制新点。
有人可以帮忙吗?任何帮助,将不胜感激。谢谢。
the problem is that i do not understand how to get the Canvas plot new points after i obtain them through the event listener
您的自定义绘画 class 中需要一个 API 允许您添加要绘画的新点。
增量绘画有两种常用方法:
保留一个要绘制的点列表,然后每次遍历列表并绘制点
将每个点绘制到 BufferedImage 上,然后只绘制 BufferedImage。
查看 Custom Painting Approaches 以了解这两种方法的工作示例。两者都试一下,看看你更喜欢哪个。
我编写了一个连接到 GPS 接收器并从中提取坐标的程序。我想创建一个散点图,我将在上面绘制从接收器中提取的位置点,每次有新的观察可用时都应更新此图。 她是我的串行端口 reader class,它允许我从接收器中提取数据:
class SerialPortReader implements SerialPortEventListener {
public void serialEvent(SerialPortEvent event) {
if (event.isRXCHAR() && event.getEventValue() > 0) {
try {
String tmp = SP.readString();
String[] msgs = tmp.split("\r\n");
for (int j = 0; j < msgs.length; j++){
if (msgs[j].contains("$GPGGA") && msgs[j].length() > 60){
System.out.println(msgs[j]);
NMEA nmea = new NMEA();
nmea.parse(msgs[j]);
System.out.println(nmea.position.toString());
writer.write(nmea.position.toString() + "\n");
}
}
}
catch (SerialPortException ex) {System.out.println(ex);}
catch (IOException ex) {System.out.println(ex);}
}
}
}
这是我的端口连接器方法:
public void openport(){
try {
SP.openPort();
SP.setParams(SerialPort.BAUDRATE_115200, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
SP.addEventListener(new SerialPortReader(),SerialPort.MASK_RXCHAR);
String dir = System.getProperty("user.dir");
String file = dir+"\NMEAout\nmeaClean.txt";
writer = new BufferedWriter(new FileWriter(file));
Canvas C = new Canvas();
C.setBackground(Color.green);
frame = new JFrame("canvas");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setLocation(500, 500);
frame.setSize(1000, 1000);
frame.add(C);
}
catch (SerialPortException ex) {System.out.println(ex);}
catch (IOException ex) {System.out.println(ex);}
}
这是我的 Canvas class 应该允许我在屏幕上绘制点。这还没有完成,这是我需要帮助的部分:
class Canvas extends JPanel{
int x = 100, y = 100;
public void newParam (int xnew, int ynew){
x = xnew;
y = ynew;
}
public void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(Color.red);
g.fillOval(x, y, 5, 5);
repaint();
}
}
frame
是在我的主 class 顶部声明的 JFrame
对象。
问题是我不明白如何在通过事件侦听器获得 Canvas 绘图新点后获得它们。我还想在不删除旧点的情况下绘制新点。
有人可以帮忙吗?任何帮助,将不胜感激。谢谢。
the problem is that i do not understand how to get the Canvas plot new points after i obtain them through the event listener
您的自定义绘画 class 中需要一个 API 允许您添加要绘画的新点。
增量绘画有两种常用方法:
保留一个要绘制的点列表,然后每次遍历列表并绘制点
将每个点绘制到 BufferedImage 上,然后只绘制 BufferedImage。
查看 Custom Painting Approaches 以了解这两种方法的工作示例。两者都试一下,看看你更喜欢哪个。