重绘不会在连续事件

时间:2016-03-08 09:12:28

标签: java swing serial-port paintcomponent thread-sleep

我正在尝试绘制一个形状,根据PC(Eclipse IDE)中的串行端口输入。我正在使用jSSC和Swing。使用传入数据成功调用串行端口事件,但重绘(@processAngleInfo)根本不调用paintComponent。

你能检查我犯了什么错误吗?

import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.Point;

import java.util.Enumeration;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.event.MouseInputListener;

import jssc.SerialPort;
import jssc.SerialPortEvent;
import jssc.SerialPortEventListener;
import jssc.SerialPortException;

public class SerialTest {

    static SerialPort serialPort;

    static double fir=0;
    static double sec=0;
    static double thi=0;
    static String []rofl=new String[4];

    private Point clickPoint, cursorPoint;

    private void buildUI(Container container) {
        container.setLayout(new BoxLayout(container, BoxLayout.PAGE_AXIS));

        SerialPortReader coordinateArea = new SerialPortReader();
        container.add(coordinateArea);

        coordinateArea.setAlignmentX(Component.LEFT_ALIGNMENT);
    }

    public static void main(String[] args) throws Exception {
        // Initialize Serial Port
        serialPort = new SerialPort("COM8"); 
        try {
            serialPort.openPort();
            serialPort.setParams(9600, 8, 1, 0);
            //Preparing a mask. In a mask, we need to specify the types of events that we want to track.
            //Well, for example, we need to know what came some data, thus in the mask must have the
            //following value: MASK_RXCHAR. If we, for example, still need to know about changes in states 
            //of lines CTS and DSR, the mask has to look like this: SerialPort.MASK_RXCHAR + SerialPort.MASK_CTS + SerialPort.MASK_DSR
            int mask = SerialPort.MASK_RXCHAR;
            //Set the prepared mask
            serialPort.setEventsMask(mask);
            //Add an interface through which we will receive information about events
            serialPort.addEventListener(new SerialPortReader());
        }
        catch (SerialPortException ex) {
            System.out.println(ex);
        }

        Thread t=new Thread() {
            public void run() {
                //the following line will keep this app alive for 1000 seconds,
                //waiting for events to occur and responding to them (printing incoming messages to console).
                try {Thread.sleep(1000000);} catch (InterruptedException ie) {}
            }
        };
        t.start();
        System.out.println("Started");

        JFrame frame = new JFrame("Smart Shoes");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        SerialTest controller = new SerialTest();
        controller.buildUI(frame.getContentPane());

        frame.pack();
        frame.setVisible(true);
    }

    public static class SerialPortReader extends JComponent implements SerialPortEventListener{
        Point point = null;

        SerialTest controller;

        Dimension preferredSize = new Dimension(1000, 500);

        Color gridColor;

        public void processAngleInfo(String inputLine) {
                rofl=inputLine.split("  ");
            fir=Double.parseDouble(rofl[1]);
            //sec=Double.parseDouble(rofl[2]);
            //thi=Double.parseDouble(rofl[3]);
            System.out.print("ypr   c ");               
            System.out.println(rofl[1]);
            //  System.out.print(rofl[2]+"  z ");   
            //  System.out.println(rofl[3]);
            repaint();
        }

        /**
         * Handle an event on the serial port. Read the data and print it.
         */
        StringBuilder message = new StringBuilder();
        public void serialEvent(SerialPortEvent event) {
                if(event.isRXCHAR() && event.getEventValue() > 0){
                    try {
                        byte buffer[] = serialPort.readBytes();
                        for (byte b: buffer) {
                                if ( (b == '\r' || b == '\n') && message.length() > 0) {
                                    String toProcess = message.toString();
                                    processAngleInfo(toProcess);
                                    //System.out.print(toProcess);
                                    message.setLength(0);
                                   // repaint();
                                   // System.out.println("after_repaint");
                                }
                                else {
                                    message.append((char)b);
                                }
                        }                
                    }
                    catch (SerialPortException ex) {
                        System.out.println(ex);
                        System.out.println("serialEvent");
                    }
                }
        }

        public SerialPortReader() {             
            setBackground(Color.WHITE);
            setOpaque(true);
        }

        public Dimension getPreferredSize() {
            return preferredSize;
        }

        protected void paintComponent(Graphics g) {

            // Paint background if we're opaque.
            if (isOpaque()) {
                g.setColor(getBackground());
                g.fillRect(0, 0, getWidth(), getHeight());
            }

            System.out.print("fir : ");
            System.out.println(fir);
            if(fir<0) { fir = fir * -1; }

                g.setColor(Color.BLACK);
                g.drawLine(500, 500, 700, 700);
                        g.drawLine((int) (fir*100), (int) (fir*100)+30, (int) (fir*100), (int) (fir*100));
                g.drawLine((int) (fir*100)+5, (int) (fir*100)+5, (int) (fir*100), (int) (fir*100));
                g.drawLine((int) (fir*100)-5, (int) (fir*100)+5, (int) (fir*100), (int) (fir*100));



        }

    }

    }

而且,我在这里根据示例代码编写了这个程序:

http://www.java2s.com/Tutorial/Java/0261__2D-Graphics/Drawsasmalldotwheretheuserclicksthemouse.htm

这会调用鼠标事件的重绘。我将非常感谢你的帮助。

0 个答案:

没有答案