java圖形界面編程之模擬血壓計
package GraphicsCanvas;
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.Timer;
import javax.swing.UIManager;
/**
* 模擬血壓計類,高壓、低壓
*
* @author 樊俊彬
* @Time 2013-12-10
*/
public class Blood extends JFrame {
private static final long serialVersionUID = 1L;
private Image iBuffer;
private MyCanvas bloodCanvas = new MyCanvas();
private JTextField highPressText, lowPressText;
// 畫布長寬
private final int CANVAS_WIDTH = 400;
private final int CANVAS_HEIGHT = 800;
// 玻璃外殼長寬與起始坐標(biāo)
private final int BLOOD_WIDTH = 30;
private final int BLOOD_HEIGHT = 650;
private final int BLOOD_X = CANVAS_WIDTH / 2 - BLOOD_WIDTH / 2;
private final int BLOOD_Y = 50;
// 框架大小與起始坐標(biāo)
private final int FRAME_WIDTH = 120;
private final int FRAME_HEIGHT = 720;
private final int FRAME_X = CANVAS_WIDTH / 2 - FRAME_WIDTH / 2;
private final int FRAME_Y = BLOOD_Y - 20;
// 0刻度線的橫縱坐標(biāo)與長度
private final int ZORELINE_Y = BLOOD_Y + BLOOD_HEIGHT - 10;
private final int ZORELINE_X = CANVAS_WIDTH / 2 + BLOOD_WIDTH / 2;
private final int LINE_LENGTH = 8;
// 輸入的高壓、低壓
private int highPressInput, lowPressInput;
// 高、低壓水銀柱的動態(tài)高度
int highPressHeight = 0;
int lowPressHeight = 0;
int startLow = BLOOD_Y;
// 高、低水銀計時器
Timer highPressTimer, lowPressTimer;
public Blood() {
super("自定義血壓計模型-FreeDoman");
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setBounds(300, 50, CANVAS_WIDTH, CANVAS_HEIGHT + 20);
// 添加控制到框架北部區(qū)
JPanel topPanel = new JPanel();
this.add(topPanel, BorderLayout.NORTH);
highPressText = new JTextField(5);
lowPressText = new JTextField(5);
JButton pressButton = new JButton("顯示");
pressButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
highPressInput = Integer.parseInt(highPressText.getText());
lowPressInput = Integer.parseInt(lowPressText.getText());
ActionListener highPressTaskPerformer = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
// 高度增加 1像素/0.01s,只到滿足輸入的要求,停止計時
highPressHeight += 1;
bloodCanvas.repaint();
if (highPressHeight == highPressInput * 2) {
highPressTimer.stop();
// 低壓水銀柱計時器嵌套于高壓計時器內(nèi)部,有先后順序(高壓先走,后低壓)
startLow = ZORELINE_Y - highPressHeight;
ActionListener lowPressTaskPerformer = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
lowPressHeight += 1;
bloodCanvas.repaint();
if (lowPressHeight == ZORELINE_Y
- lowPressInput * 2 - startLow)
lowPressTimer.stop();
}
};
lowPressTimer = new Timer(10, lowPressTaskPerformer);
lowPressTimer.start();
}
}
};
// 定義每0.01秒執(zhí)行一次的事件監(jiān)聽器
highPressTimer = new Timer(10, highPressTaskPerformer);
highPressTimer.start();
}
});
topPanel.add(new JLabel("高壓值", JLabel.CENTER));
topPanel.add(highPressText);
topPanel.add(new JLabel("低壓值", JLabel.CENTER));
topPanel.add(lowPressText);
// topPanel.add(new JLabel("心率", JLabel.CENTER));
topPanel.add(pressButton);
// 添加畫布到中央?yún)^(qū)
this.add(bloodCanvas, BorderLayout.CENTER);
this.setResizable(false);
this.setVisible(true);
}
/**
* 畫布重繪血壓計
*/
class MyCanvas extends Canvas {
public void paint(Graphics g) {
// 畫邊框
g.setColor(Color.BLACK);
g.draw3DRect(FRAME_X, FRAME_Y, FRAME_WIDTH, FRAME_HEIGHT, true);
// 畫玻璃外殼
g.setColor(Color.ORANGE);
g.fill3DRect(BLOOD_X, BLOOD_Y, BLOOD_WIDTH, BLOOD_HEIGHT, true);
// 高壓水銀柱
g.setColor(Color.RED);
g.fill3DRect(BLOOD_X, ZORELINE_Y - highPressHeight, BLOOD_WIDTH,
highPressHeight, true);
// 低壓高壓水銀柱
g.setColor(Color.ORANGE);
g.fill3DRect(BLOOD_X, startLow, BLOOD_WIDTH, lowPressHeight, true);
// 畫底部水銀圓球
g.setColor(Color.RED);
g.fillOval(CANVAS_WIDTH / 2 - 30, ZORELINE_Y - 5, 60, 60);
// 右側(cè)0刻度線起始刻度與坐標(biāo)(刻度線縱坐標(biāo)以line_y漸變)
int rightStartDegree = 0;
int line_y = ZORELINE_Y;
for (; line_y > BLOOD_Y; line_y -= 2) {
// 2個像素點為一個最小分度 1度
g.setColor(Color.BLACK);
g.drawLine(ZORELINE_X, line_y, ZORELINE_X + LINE_LENGTH, line_y);
// 每隔10最小分度個畫10度刻度線
if (line_y % 20 == 10) {
g.setColor(Color.BLUE);
g.drawLine(ZORELINE_X, line_y,
ZORELINE_X + LINE_LENGTH * 2, line_y);
g.drawString(rightStartDegree + "", ZORELINE_X
+ LINE_LENGTH * 3, line_y + 4);
rightStartDegree += 10;
}
}
// 左側(cè)0刻度線起始刻度與坐標(biāo)(刻度線縱坐標(biāo)以line_y漸變)
int leftStartDegree = 0;
int leftLine_y = ZORELINE_Y;
for (; leftLine_y > BLOOD_Y; leftLine_y -= 6) {
// 6個像素點為一個最小分度 1度
g.setColor(Color.BLACK);
g.drawLine(BLOOD_X, leftLine_y, BLOOD_X - LINE_LENGTH,
leftLine_y);
// 每隔10最小分度個畫10度刻度線
if (leftLine_y % 20 == 10) {
g.setColor(Color.BLUE);
g.drawLine(BLOOD_X, leftLine_y, BLOOD_X - LINE_LENGTH * 2,
leftLine_y);
g.drawString(leftStartDegree + "", BLOOD_X - LINE_LENGTH
* 4, leftLine_y + 4);
leftStartDegree += 10;
}
}
}
/**
* 雙緩沖技術(shù):復(fù)雜的計算速度慢于屏幕顯示,用緩沖解決屏幕閃爍問題
*/
@Override
public void update(Graphics g) {
if (iBuffer == null) {
iBuffer = createImage(this.getSize().width,
this.getSize().height);
}
Graphics gBuffer = iBuffer.getGraphics();
gBuffer.setColor(getBackground());
gBuffer.fillRect(0, 0, this.getSize().width, this.getSize().height);
paint(gBuffer);
gBuffer.dispose();
g.drawImage(iBuffer, 0, 0, this);
}
}
public static void main(String[] args) {
// 設(shè)置界面的外觀,為系統(tǒng)外觀
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
new Blood();
}
}
相關(guān)文章
Java并發(fā)編程之ConcurrentLinkedQueue解讀
這篇文章主要介紹了Java并發(fā)編程之ConcurrentLinkedQueue解讀,非阻塞的實現(xiàn)方式則可以使用循環(huán)CAS的方式來實現(xiàn),而ConcurrentLinkedQueue就是juc包中自帶的經(jīng)典非堵塞方式實現(xiàn)的工具類,需要的朋友可以參考下2023-12-12feign調(diào)用中文參數(shù)被encode編譯的問題
這篇文章主要介紹了feign調(diào)用中文參數(shù)被encode編譯的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03mybatis調(diào)用mysql存儲過程(返回參數(shù),單結(jié)果集,多結(jié)果集)
本文主要介紹了mybatis調(diào)用mysql存儲過程(返回參數(shù),單結(jié)果集,多結(jié)果集),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-01-01詳解SpringSecurity如何實現(xiàn)前后端分離
這篇文章主要為大家介紹了詳解SpringSecurity如何實現(xiàn)前后端分離,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03Java使用agent實現(xiàn)main方法之前的實例詳解
這篇文章主要介紹了Java使用agent實現(xiàn)main方法之前的實例詳解的相關(guān)資料,希望通過本文能幫助到大家,讓大家理解這部分內(nèi)容,需要的朋友可以參考下2017-10-10Java WebSocket客戶端接收大量數(shù)據(jù)的三種方案
WebSocket是一種基于TCP協(xié)議的全雙工通信協(xié)議,它能夠在客戶端和服務(wù)器之間建立一個持久連接,實現(xiàn)實時的雙向數(shù)據(jù)傳輸,在實際應(yīng)用中,有時候我們需要處理大量的數(shù)據(jù),所以本文將介紹如何使用 Java WebSocket 客戶端接收大量數(shù)據(jù),并提供一些優(yōu)化方案2023-11-11SpringBoot核心@SpringBootApplication使用介紹
這篇文章主要介紹了SpringBoot核心@SpringBootApplication的使用,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03