java实现模拟进度计量器
作者:Freedoman1990 时间:2023-05-26 12:05:03
本文实例为大家分享了java模拟进度计量器的具体代码,供大家参考,具体内容如下
一、程序说明
1、自定义模拟血压计外观图class MyCanvas 继承 Canvas类,重写其中的public void paint(Graphics g)方法,里边绘制详细的界面组件外观,包括边框、玻璃外壳、高压水银柱、低压高压水银柱、底部水银圆球、左右侧0刻度线、刻度线等。
2、主框架类Blood 继承 JFrame,设定布局,添加文本框和输入框用于为高、低压赋值。实例化MyCanvas类产生对象bloodCanvas,并将bloodCanvas添加到框架中央区。
3、高压、低压计时器highPressTimer, lowPressTimer用于每隔一定的时间去执行特定任务,高压与低压更新任务highPressTaskPerformer,lowPressTaskPerformer用于完成进度条更新。
4、程序中的重点:
(1)、绘制动态高压、低压进度条,其中低压水银柱计时器嵌套于高压计时器内部,有先后顺序,高压先上升,后低压下降。
(2)、绘制刻度线算法。
二、运行效果
三、源代码
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 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;
// 画布长宽
private final int CANVAS_WIDTH = 400;
private final int CANVAS_HEIGHT = 800;
// 玻璃外壳长宽与起始坐标
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;
// 框架大小与起始坐标
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刻度线的横纵坐标与长度
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;
// 高、低压水银柱的动态高度
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);
// 添加控制到框架北部区
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();
// 低压水银柱计时器嵌套于高压计时器内部,有先后顺序(高压先走,后低压)
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秒执行一次的事件 *
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);
// 添加画布到中央区
this.add(bloodCanvas, BorderLayout.CENTER);
this.setResizable(false);
this.setVisible(true);
}
/**
* 画布重绘血压计
*/
class MyCanvas extends Canvas {
public void paint(Graphics g) {
// 画边框
g.setColor(Color.BLUE);
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);
// 右侧0刻度线起始刻度与坐标(刻度线纵坐标以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;
}
}
// 左侧0刻度线起始刻度与坐标(刻度线纵坐标以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;
}
}
}
/**
* 双缓冲技术:复杂的计算速度慢于屏幕显示,用缓冲解决屏幕闪烁问题
*/
@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) {
// 设置界面的外观,为系统外观
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
new Blood();
}
}
来源:https://blog.csdn.net/oFanJunBin/article/details/20289097
标签:java,进度,计量器
0
投稿
猜你喜欢
c# 字符串操作总结
2022-03-16 05:59:25
一文总结Java获取文件后缀名的所有方法
2022-10-18 16:06:40
Android中获得手机屏幕大小实现代码
2022-10-24 13:41:13
java 中锁的性能提高办法
2021-07-28 10:50:28
Flutter封装组动画混合动画AnimatedGroup示例详解
2022-12-29 17:17:05
C#中四步轻松使用log4net记录本地日志的方法
2021-12-22 02:23:16
Java8 CompletableFuture runAsync学习总结submit() execute()等
2023-05-25 04:13:43
JavaWeb中上传和下载文件实例代码
2023-11-04 10:24:00
android中使用SharedPreferences进行数据存储的操作方法
2023-06-16 17:37:42
Java中Equals使用方法汇总
2022-09-14 23:08:28
解决IDEA无法下载maven依赖的问题
2023-12-05 17:06:59
java实现文件上传下载功能
2021-11-26 17:19:05
SpringBoot整合Mybatis-plus案例及用法实例
2022-03-31 12:02:44
C++实现softmax函数的面试经验
2023-06-16 02:07:47
SpringCloud微服务基础简介
2022-09-01 23:16:24
java实现学籍管理系统
2023-04-03 00:32:19
WPF+SkiaSharp实现自绘弹幕效果
2022-09-30 09:52:38
使用java实现telnet-client工具分享
2023-10-18 10:56:59
详解SpringMVC和MyBatis框架开发环境搭建和简单实用
2022-03-11 13:54:51
java项目中的多线程实践记录
2023-10-16 16:12:03