การคัดลอกรหัสมีดังนี้:
Package GraphicsCanvas;
นำเข้า Java.awt.BorderLayout;
นำเข้า java.awt.canvas;
นำเข้า Java.awt.Color;
นำเข้า java.awt.graphics;
นำเข้า java.awt.image;
นำเข้า java.awt.event.actionEvent;
นำเข้า java.awt.event.actionListener;
นำเข้า Javax.swing.jbutton;
นำเข้า Javax.swing.jframe;
นำเข้า Javax.swing.jlabel;
นำเข้า Javax.swing.jpanel;
นำเข้า Javax.swing.jtextfield;
นำเข้า Javax.swing.timer;
นำเข้า Javax.swing.uimanager;
-
* เครื่องวัดความดันโลหิตจำลองแรงดันสูงความดันต่ำ
-
* @author Fan Junbin
* @time 2013-12-10
-
เลือดระดับสาธารณะขยาย JFrame {
ส่วนตัวคงที่สุดท้าย Long SerialVersionUid = 1L;
ภาพส่วนตัว Ibuffer;
mycanvas ส่วนตัว bloodcanvas = mycanvas ใหม่ ();
ส่วนตัว Jtextfield HighpressText, LowpressText;
// Canvas ความยาวและความกว้าง
INT สุดท้ายส่วนตัว CANVAS_WIDTH = 400;
ส่วนตัว int สุดท้าย canvas_height = 800;
// ความยาวและความกว้างของเปลือกแก้วและพิกัดเริ่มต้น
int สุดท้าย int blood_width = 30;
int สุดท้าย int blood_height = 650;
int สุดท้าย int blood_x = canvas_width / 2 - Blood_width / 2;
int สุดท้าย int blood_y = 50;
// ขนาดเฟรมและพิกัดเริ่มต้น
INT FINAL FINAL FRAME_WIDTH = 120;
INT FINAL FINAL FRAME_HEIGHT = 720;
INT FINAL FINAL FRAME_X = CANVAS_WIDTH / 2 - FRAME_WIDTH / 2;
INT FINAL FINAL FRAME_Y = Blood_Y - 20;
// พิกัดแนวนอนและแนวตั้งและความยาวของเส้นสเกล 0
ส่วนตัวสุดท้าย int zoreline_y = blood_y + blood_height - 10;
ส่วนตัวสุดท้าย int zoreline_x = canvas_width / 2 + Blood_width / 2;
int สุดท้าย int สุดท้าย line_length = 8;
// อินพุตแรงดันสูงและแรงดันไฟฟ้าต่ำ
INT ส่วนตัวสูง highpressinput, lowpressinput;
// ความสูงแบบไดนามิกของคอลัมน์ปรอทแรงดันสูงและต่ำ
int highpressheight = 0;
int lowpressheight = 0;
int startlow = blood_y;
// ตัวจับเวลาปรอทสูงและต่ำ
จับเวลา HighpressTimer, LowpressTimer;
เลือดสาธารณะ () {
Super ("Model-Freedoman Monitor");
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 ("Show");
pressbutton.addactionListener (ใหม่ actionListener () {
@Override
โมฆะสาธารณะ ActionPerformed (ActionEvent Arg0) {
HighPressInput = Integer.ParseInt (HighpressText.getText ());
lowpressInput = integer.parseInt (lowpressText.getText ());
ActionListener HighpressTaskPerformer = new ActionListener () {
โมฆะสาธารณะ ActionPerformed (ActionEvent EVT) {
// เพิ่มความสูง 1 พิกเซล/0.01s และหยุดเวลาจนกว่าจะตรงตามข้อกำหนดของอินพุต
Highpressheight += 1;
Bloodcanvas.Repaint ();
ถ้า (HighpressHeight == HighpressInput * 2) {
HighpressTimer.stop ();
// ตัวจับเวลาคอลัมน์ปรอทความดันต่ำนั้นซ้อนอยู่ภายในตัวจับเวลาแรงดันสูงตามลำดับ (แรงดันสูงก่อนจากนั้นแรงดันต่ำ)
startlow = zoreline_y - Highpressheight;
actionListener lowpressTaskperformer = new ActionListener () {
โมฆะสาธารณะ ActionPerformed (ActionEvent EVT) {
LowPressHeight += 1;
Bloodcanvas.Repaint ();
ถ้า (lowpressHeight == ZORELINE_Y
- LowPressInput * 2 - startlow)
lowpressTimer.stop ();
-
-
LowPressTimer = ตัวจับเวลาใหม่ (10, LowPressTaskPerformer);
lowpresstimer.start ();
-
-
-
// กำหนดผู้ฟังเหตุการณ์ที่ดำเนินการทุก ๆ 0.01 วินาที
HighpressTimer = ตัวจับเวลาใหม่ (10, HighpressTaskPerformer);
HighpressTimer.start ();
-
-
toppanel.add (ใหม่ Jlabel ("ค่าแรงดันสูงสูง", jlabel.center));
toppanel.add (HighpressText);
toppanel.add (ใหม่ Jlabel ("ค่าแรงดันไฟฟ้าต่ำ", jlabel.center));
toppanel.add (LowpressText);
// toppanel.add (New Jlabel ("อัตราการเต้นของหัวใจ", jlabel.center));
toppanel.add (PressButton);
// เพิ่มผืนผ้าใบลงในพื้นที่ส่วนกลาง
this.add (Bloodcanvas, Borderlayout.center);
this.setResizable (เท็จ);
this.setVisible (จริง);
-
-
* ผืนผ้าใบวาดใหม่ของเครื่องวัดความดันโลหิต
-
คลาส Mycanvas ขยาย Canvas {
สีโมฆะสาธารณะ (กราฟิก 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;
สำหรับ (; 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 ดัชนีขั้นต่ำ 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;
สำหรับ (; 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 ดัชนีขั้นต่ำ 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
การอัปเดตโมฆะสาธารณะ (กราฟิก G) {
if (ibuffer == null) {
ibuffer = createimage (this.getSize (). ความกว้าง
this.getSize (). ความสูง);
-
กราฟิก gbuffer = ibuffer.getGraphics ();
gbuffer.setColor (getBackground ());
gbuffer.fillrect (0, 0, this.getSize (). ความกว้าง, this.getSize (). ความสูง);
สี (gbuffer);
gbuffer.dispose ();
G.Drawimage (Ibuffer, 0, 0, นี้);
-
-
โมฆะคงที่สาธารณะหลัก (สตริง [] args) {
// ตั้งค่าลักษณะที่ปรากฏของอินเทอร์เฟซเป็นลักษณะที่ปรากฏของระบบ
พยายาม {
uimanager.setlookandfeel (uimanager.getSystemLookandFeelClassName ());
} catch (Exception e) {
E.PrintStackTrace ();
-
เลือดใหม่ ();
-
-