java實(shí)現(xiàn)模擬進(jìn)度計(jì)量器
本文實(shí)例為大家分享了java模擬進(jìn)度計(jì)量器的具體代碼,供大家參考,具體內(nèi)容如下
一、程序說(shuō)明
1、自定義模擬血壓計(jì)外觀圖class MyCanvas 繼承 Canvas類,重寫(xiě)其中的public void paint(Graphics g)方法,里邊繪制詳細(xì)的界面組件外觀,包括邊框、玻璃外殼、高壓水銀柱、低壓高壓水銀柱、底部水銀圓球、左右側(cè)0刻度線、刻度線等。
2、主框架類Blood 繼承 JFrame,設(shè)定布局,添加文本框和輸入框用于為高、低壓賦值。實(shí)例化MyCanvas類產(chǎn)生對(duì)象bloodCanvas,并將bloodCanvas添加到框架中央?yún)^(qū)。
3、高壓、低壓計(jì)時(shí)器highPressTimer, lowPressTimer用于每隔一定的時(shí)間去執(zhí)行特定任務(wù),高壓與低壓更新任務(wù)highPressTaskPerformer,lowPressTaskPerformer用于完成進(jìn)度條更新。
4、程序中的重點(diǎn):
(1)、繪制動(dòng)態(tài)高壓、低壓進(jìn)度條,其中低壓水銀柱計(jì)時(shí)器嵌套于高壓計(jì)時(shí)器內(nèi)部,有先后順序,高壓先上升,后低壓下降。
(2)、繪制刻度線算法。
二、運(yùn)行效果

三、源代碼
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;
/**
* 模擬血壓計(jì)類,高壓、低壓
*
* @author Freedoman
* @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;
// 畫(huà)布長(zhǎng)寬
private final int CANVAS_WIDTH = 400;
private final int CANVAS_HEIGHT = 800;
// 玻璃外殼長(zhǎng)寬與起始坐標(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)與長(zhǎng)度
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;
// 高、低壓水銀柱的動(dòng)態(tài)高度
int highPressHeight = 0;
int lowPressHeight = 0;
int startLow = BLOOD_Y;
// 高、低水銀計(jì)時(shí)器
Timer highPressTimer, lowPressTimer;
public Blood() {
super("自定義血壓計(jì)模型-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,只到滿足輸入的要求,停止計(jì)時(shí)
highPressHeight += 1;
bloodCanvas.repaint();
if (highPressHeight == highPressInput * 2) {
highPressTimer.stop();
// 低壓水銀柱計(jì)時(shí)器嵌套于高壓計(jì)時(shí)器內(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)聽(tīng)器
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);
// 添加畫(huà)布到中央?yún)^(qū)
this.add(bloodCanvas, BorderLayout.CENTER);
this.setResizable(false);
this.setVisible(true);
}
/**
* 畫(huà)布重繪血壓計(jì)
*/
class MyCanvas extends Canvas {
public void paint(Graphics g) {
// 畫(huà)邊框
g.setColor(Color.BLUE);
g.draw3DRect(FRAME_X, FRAME_Y, FRAME_WIDTH, FRAME_HEIGHT, true);
// 畫(huà)玻璃外殼
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);
// 畫(huà)底部水銀圓球
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個(gè)像素點(diǎn)為一個(gè)最小分度 1度
g.setColor(Color.BLACK);
g.drawLine(ZORELINE_X, line_y, ZORELINE_X + LINE_LENGTH, line_y);
// 每隔10最小分度個(gè)畫(huà)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個(gè)像素點(diǎn)為一個(gè)最小分度 1度
g.setColor(Color.BLACK);
g.drawLine(BLOOD_X, leftLine_y, BLOOD_X - LINE_LENGTH,
leftLine_y);
// 每隔10最小分度個(gè)畫(huà)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ù)雜的計(jì)算速度慢于屏幕顯示,用緩沖解決屏幕閃爍問(wèn)題
*/
@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();
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- java讀取文件顯示進(jìn)度條的實(shí)現(xiàn)方法
- java控制臺(tái)輸出百分比進(jìn)度條示例
- Java上傳文件進(jìn)度條的實(shí)現(xiàn)方法(附demo源碼下載)
- java實(shí)現(xiàn)在復(fù)制文件時(shí)使用進(jìn)度條(java實(shí)現(xiàn)進(jìn)度條)
- JavaWeb項(xiàng)目實(shí)現(xiàn)文件上傳動(dòng)態(tài)顯示進(jìn)度實(shí)例
- Java動(dòng)態(tài)顯示文件上傳進(jìn)度實(shí)現(xiàn)代碼
- 基于Retrofit+Rxjava實(shí)現(xiàn)帶進(jìn)度顯示的下載文件
- Java Swing組件實(shí)現(xiàn)進(jìn)度監(jiān)視功能示例
- Java Swing JProgressBar進(jìn)度條的實(shí)現(xiàn)示例
相關(guān)文章
Java中的System.getenv()和System.getProperty()使用詳解
文章介紹了Java中用于讀取環(huán)境配置信息的兩種方法:System.getenv()和System.getProperty(),前者讀取系統(tǒng)環(huán)境變量,返回一個(gè)不可修改的Map;后者獲取JVM環(huán)境變量值,可以通過(guò)-D參數(shù)設(shè)置,文章還提到,通過(guò)這兩種方法可以簡(jiǎn)化配置,不需要修改代碼2024-11-11
Java利用位運(yùn)算實(shí)現(xiàn)加減乘除的方法詳解
我們經(jīng)常使用的加減乘除,我們所看到的只是表面的效果,那么加減乘除在底層究竟是怎么實(shí)現(xiàn)的?今天就讓我們一探究竟2022-08-08
MyBatis中#{}?和?${}?的區(qū)別和動(dòng)態(tài)?SQL詳解
這篇文章主要介紹了MyBatis中#{}和${}的區(qū)別,包括參數(shù)傳遞、安全性、性能等方面,然后詳細(xì)介紹了如何使用#{}和${}進(jìn)行排序、模糊查詢、動(dòng)態(tài)SQL、數(shù)據(jù)庫(kù)連接池等操作,最后,總結(jié)了注解方式的動(dòng)態(tài)SQL,感興趣的朋友跟隨小編一起看看吧2024-11-11
使用工具類-java精確到小數(shù)點(diǎn)后6位
這篇文章主要介紹了使用工具類-java精確到小數(shù)點(diǎn)后6位,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10

