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;
// 玻璃外壳长宽与起始坐标
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.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);
// 右侧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();
}
}
上一篇: PrepareStatement插入时间 JavaSQLJDK
下一篇: JVM 堆 JVM