Java Swing绘制正弦/余弦波形图

下面的Java代码实现了正弦、余弦波形图的绘制,main方法位于DataWindow.java中。

DataPanel.java

import java.awt.Color;
import java.awt.Graphics;
import java.text.DateFormat;
import java.util.ArrayList;

import javax.swing.JPanel;

public class DataPanel extends JPanel {

    private static final long serialVersionUID = -9039511286331044798L;

    private int index = 0;
    private ArrayList<Long> time = new ArrayList<>();
    private ArrayList<Integer> val = new ArrayList<>();
    DateFormat fmt = DateFormat.getDateTimeInstance();

    public DataPanel() {
    }
    
    public void addData(long t, int v) {
        time.add(t);
        val.add(v);
        index++;
        repaint();
    }
    
    // Graph the sensor values in the dataPanel JPanel
    public void paint(Graphics g) {
        super.paint(g);
        int left = getX() + 10;       // get size of pane
        int top = 10;
        int right = left + getWidth() - 20;
        int bottom = getHeight() - 20;
        
        int y0 = bottom - 20;                   // leave some room for margins
        int yn = top;
        int x0 = left + 33;
        int xn = right;
        double vscale = (yn - y0) / 120.0;      // light values range from 0 to 800
        double tscale = 1.0 / 2000.0;           // 1 pixel = 2 seconds = 2000 milliseconds
        
        // draw X axis = time
        g.setColor(Color.BLACK);
        g.drawLine(x0, yn, x0, y0);
        g.drawLine(x0, y0, xn, y0);
        int tickInt = 60 / 2;
        for (int xt = x0 + tickInt; xt < xn; xt += tickInt) {   // tick every 1 minute
            g.drawLine(xt, y0 + 5, xt, y0 - 5);
            int min = (xt - x0) / (60 / 2);
            g.drawString(Integer.toString(min), xt - (min < 10 ? 3 : 7) , y0 + 20);
        }
        
        // draw Y axis = sensor reading
        g.setColor(Color.BLUE);
        for (int vt = 120; vt > 0; vt -= 20) {         // tick every 200
            int v = y0 + (int)(vt * vscale);
            g.drawLine(x0 - 5, v, x0 + 5, v);
            g.drawString(Integer.toString(vt), x0 - 38 , v + 5);
        }

        // graph sensor values
        int xp = -1;
        int vp = -1;
        for (int i = 0; i < index; i++) {
            int x = x0 + (int)((time.get(i) - time.get(0)) * tscale);
            int v = y0 + (int)(val.get(i) * vscale);
            if (xp > 0) {
                g.drawLine(xp, vp, x, v);
            }
            xp = x;
            vp = v;
        }
    }

}

DataWindow.java

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.text.DateFormat;
import java.util.Date;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ScrollPaneConstants;

public class DataWindow extends JFrame {

    private static final long serialVersionUID = -5628586708228044760L;

    DateFormat fmt = DateFormat.getDateTimeInstance();

    /** Creates new form DataWindow */
    public DataWindow() {
        initComponents();
    }

    public DataWindow(String ieee) {
        initComponents();
        setTitle(ieee);
    }

    public void addData(long t, int v1, int v2) {
        dataPanelTop.addData(t, v1);
        dataPanelBottom.addData(t, v2);
        dataTextArea.append(fmt.format(new Date(t)) + " v1, v2 = " + v1 + ", " + v2 + "\n");
        dataTextArea.setCaretPosition(dataTextArea.getText().length());
    }

    public static void main(String args[]) {
        DataWindow dw = new DataWindow("Dynamic Line Chart Demo");
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                dw.setVisible(true);
            }
        });
        Thread thread = new Thread() {
            public void run() {
                while (true) {
                    long t = System.currentTimeMillis();
                    int v1 = (int)(Math.sin(t / 1000 * Math.PI / 30) * 50) + 50;
                    int v2 = (int)(Math.cos(t / 1000 * Math.PI / 30) * 50) + 50;
                    dw.addData(t, v1, v2);
                    try {
                        Thread.sleep(1000L);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
        };
        thread.run();
    }

    private void initComponents() {
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        
        dataPanelTop = new DataPanel();
        dataPanelTop.setMinimumSize(new Dimension(600, 220));
        dataPanelTop.setPreferredSize(new Dimension(600, 220));
        getContentPane().add(dataPanelTop, BorderLayout.NORTH);

        dataPanelBottom = new DataPanel();
        dataPanelBottom.setMinimumSize(new Dimension(600, 220));
        dataPanelBottom.setPreferredSize(new Dimension(600, 220));
        getContentPane().add(dataPanelBottom, BorderLayout.SOUTH);

        jScrollPaneCenter = new javax.swing.JScrollPane();
        dataTextArea = new javax.swing.JTextArea();

        jScrollPaneCenter.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        jScrollPaneCenter.setMinimumSize(new Dimension(600, 160));
        jScrollPaneCenter.setPreferredSize(new Dimension(600, 160));

        dataTextArea.setColumns(20);
        dataTextArea.setEditable(false);
        dataTextArea.setRows(4);
        dataTextArea.setBackground(Color.LIGHT_GRAY);
        jScrollPaneCenter.setViewportView(dataTextArea);

        getContentPane().add(jScrollPaneCenter, BorderLayout.CENTER);

        pack();
    }

    private DataPanel dataPanelTop;
    private DataPanel dataPanelBottom;
    private JTextArea dataTextArea;
    private JScrollPane jScrollPaneCenter;

}

 

本文链接:http://bookshadow.com/weblog/2016/10/20/java-swing-sin-cos-wave/
请尊重作者的劳动成果,转载请注明出处!书影博客保留对文章的所有权利。

如果您喜欢这篇博文,欢迎您捐赠书影博客: ,查看支付宝二维码