動態溫度顯示——Java Swing 應用案例
在文章《溫度顯示介面——Java Swing 應用簡介》中,我們通過溫度顯示介面的應用來演示Java Swing 各個部分的用法。本節我們在此基礎之上,編寫動態溫度顯示介面,程式碼如下:
package damuchacha.window; import java.awt.Color; import java.awt.Font; import javax.swing.JLabel; /* * 繪製標籤類 */ public class textLabel extends JLabel { private String str; //字串 private Color color; //標籤顏色 private int fontSize; //字型大小 public textLabel(){ } public textLabel(String s, Color c, int f){ this.str = s; this.color = c; this.fontSize = f; setText(str); setFont(new Font(null,Font.PLAIN,fontSize)); setBackground(Color.BLACK); setForeground(color); } }
package damuchacha.window; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.EventQueue; import java.awt.Font; import java.awt.Graphics; import java.awt.GridLayout; import java.awt.Toolkit; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; import java.text.DateFormat; import java.util.Date; import java.util.Random; import java.util.Timer; import java.util.TimerTask; public class WindowMenu extends JFrame { private static final int frameWidth = 1000; //frame寬度 常量 private static final int frameHeight = 1000; //frame高度 常量 private static final int MAX_SAMPLES = 1000; //溫度曲線長度 private int time = 0; private int[] highTemp = new int[MAX_SAMPLES]; //儲存最高溫度數值 private int[] lowTemp = new int[MAX_SAMPLES]; //儲存最低溫度數值 private static JLabel highTempValue; //顯示溫度數值 private static JLabel lowTempValue; private textLabel tempName1; //顯示溫度名稱 private textLabel tempUnit1; //顯示溫度單位 private textLabel tempName2; private textLabel tempUnit2; private JPanel highTempValuePanel; //最高氣溫Panel private JPanel lowTempValuePanel; //最低氣溫Panel private JPanel tempValuePanel; //溫度Panel private JPanel curveTempPanel; //溫度曲線Panel public WindowMenu() { initComponents(); } public void addData(int hT,int lT) { time = time + 1; highTemp[time] = hT; //新增最高溫度 lowTemp[time] = lT; //新增最低溫度 if(time>9){ //如果溫度超過了七天,則將第一天的溫度覆蓋 for (int i = 0;i<time;i++){ highTemp[i] = highTemp[i+1]; lowTemp[i] = lowTemp[i+1]; } } repaint(); } public void paint(Graphics g) { super.paint(g); int left = curveTempPanel.getX() + 30; int top = curveTempPanel.getY() + 30; int right = left + curveTempPanel.getWidth() - 30; int bottom = top + curveTempPanel.getHeight() - 30; int y0 = bottom - 20; int yn = top; int x0 = left + 33; int xn = right; int yScale = (int) ((yn - y0) /40.0); // y軸比例尺,最大數值 40 int xScale = (int) (xn - x0)/10; // x軸比例尺,最大數值10 /* * 繪製x,y座標軸 */ g.setColor(Color.BLACK); //座標軸顏色 g.drawLine(x0, yn, x0, y0); //y座標軸 g.drawLine(x0, y0, xn, y0); //x座標軸 /* * 繪製x座標軸,刻度,數值 */ for (int xt = x0 + xScale; xt < xn; xt += xScale) { g.drawLine(xt, y0 + 1, xt, y0 - 1); int min = (xt - x0) / (xScale); g.drawString(Integer.toString(min), xt - (min < 10 ? 3 : 7) , y0 + 20); } /* * 繪製y座標軸,刻度,數值 */ for (int vt = 40; vt > 0; vt -= 5) { // tick every 200 int v = y0 + (int)(vt * yScale); g.drawLine(x0 - 1, v, x0 + 1, v); //y座標軸刻度 g.drawString(Integer.toString(vt), x0 - 38 , v + 5); //y座標軸數值 } /* * 繪製溫度變化曲線 * */ int xp = -1; int yp1 = -1; int yp2 = -1; if(time>9){ time = 9; } for (int i = 0; i < time; i++) { int x = x0 + (int)(i * xScale); int v1 = y0 + (int)(highTemp[i] * yScale); int v2 = y0 + (int)(lowTemp[i] * yScale); if (xp > 0) { g.setColor(Color.RED); g.drawLine(xp, yp1, x, v1); g.setColor(Color.BLUE); g.drawLine(xp, yp2, x, v2); } xp = x; yp1 = v1; yp2 = v2; } } private void initComponents() { this.setTitle("溫度顯示介面"); Toolkit kit = Toolkit.getDefaultToolkit(); //定義工具包 Dimension screenSize = kit.getScreenSize(); //獲取螢幕的尺寸 int screenWidth = screenSize.width; //獲取螢幕的寬 int screenHeight = screenSize.height; //獲取螢幕的高 this.setLocation(screenWidth/2-frameWidth/2, screenHeight/2-frameHeight/2);//設定視窗居中顯示 this.setResizable(false); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setSize(this.frameWidth,this.frameHeight); this.setVisible(true); /* * Panel1 格式設定 */ tempName1 = new textLabel(" 當天最高溫度",Color.RED,40); tempUnit1 = new textLabel(" °C",Color.RED,40); highTempValue = new JLabel(); highTempValue.setFont(new Font(null,Font.PLAIN,50)); highTempValue.setForeground(Color.RED); highTempValuePanel = new JPanel(); highTempValuePanel.setLayout(new GridLayout(1,3,1,1)); highTempValuePanel.add(tempName1); highTempValuePanel.add(highTempValue); highTempValuePanel.add(tempUnit1); /* * Panel2 格式設定 */ tempName2 = new textLabel(" 當天最低溫度",Color.BLUE,40); tempUnit2 = new textLabel(" °C",Color.BLUE,40); lowTempValue = new JLabel(); lowTempValue.setFont(new Font(null,Font.PLAIN,50)); lowTempValue.setForeground(Color.BLUE); lowTempValuePanel = new JPanel(); lowTempValuePanel.setLayout(new GridLayout(1,2,1,1)); lowTempValuePanel.add(tempName2); lowTempValuePanel.add(lowTempValue); lowTempValuePanel.add(tempUnit2); /* * Panel3格式設定 * 將Panel1和Panel2放在一起構成Panel3 */ tempValuePanel = new JPanel(); tempValuePanel.setLayout(new GridLayout(2,1,1,1)); tempValuePanel.add(highTempValuePanel); tempValuePanel.add(lowTempValuePanel); /* * 溫度曲線 */ curveTempPanel = new JPanel(); curveTempPanel.setMinimumSize(new Dimension(400, 250)); curveTempPanel.setPreferredSize(new Dimension(400, 250)); this.setLayout(new GridLayout(0,1,5,5)); this.add(tempValuePanel); this.add(curveTempPanel); pack(); } public static void main (String args[]) { WindowMenu dw = new WindowMenu(); EventQueue.invokeLater(new Runnable() { public void run() { dw.setVisible(true); } } ); Thread thread = new Thread() { public void run() { while (true) { int hT = new Random().nextInt(40); //獲得實時最高溫度 int lT = new Random().nextInt(40); //獲得實時最低溫度 highTempValue.setText(" "+hT); lowTempValue.setText(" "+lT); dw.addData(hT,lT); // 將溫度數值新增到畫板上 dw.repaint(); try { Thread.sleep(2000L); } catch (InterruptedException e) { e.printStackTrace(); } } } }; thread.run(); } }
動態顯示結果如下:
完
2018年10月2日