نسخة الكود كما يلي:
حزمة 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
* @الوقت 2013-12-10
*/
دم الطب العام يمتد JFrame {
خاص ثابت نهائي طويل المسبانيويد = 1L ؛
صورة خاصة ibuffer ؛
mycanvas ploodcanvas الخاصة = new mycanvas () ؛
private JTextfield HighpressText ، LowPressText ؛
// طول القماش وعرضه
نهائي خاص int canvas_width = 400 ؛
نهائي خاص int canvas_height = 800 ؛
// طول وعرض القشرة الزجاجية وإحداثيات البداية
نهائي خاص int Blood_width = 30 ؛
نهائي خاص int Blood_height = 650 ؛
نهائي خاص int Blood_x = canvas_width / 2 - Blood_width / 2 ؛
نهائي خاص int Blood_y = 50 ؛
// حجم الإطار وإحداثيات البدء
Private Final int Frame_width = 120 ؛
Private Final int Frame_Height = 720 ؛
Frame int Frame_x = canvas_width / 2 - frame_width / 2 ؛
Private Final int Frame_y = Blood_y - 20 ؛
// الإحداثيات الأفقية والرأسية وطول خط المقياس 0
نهائي خاص int zoreline_y = Blood_y + Blood_height - 10 ؛
نهائي خاص int zoreline_x = canvas_width / 2 + Blood_width / 2 ؛
Final Final int line_length = 8 ؛
// إدخال الجهد العالي والجهد المنخفض
Private Int HighpressInput ، lowpressinput ؛
// الارتفاع الديناميكي لأعمدة الزئبق عالية الضغط والمنخفضة
int Highpressheight = 0 ؛
int lowpressheight = 0 ؛
int startLow = Blood_y ؛
// مرتفعة ومنخفضة الزئبق
Timer Highpresstimer ، lowpresstimer ؛
الدم العام () {
Super ("مخصص مراقبة ضغط الدم النموذجية") ؛
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 (New ActionListener () {
@تجاوز
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.01 ، ووقف التوقيت حتى يتم استيفاء متطلبات الإدخال.
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 () ؛
إذا (lowpressheight == Zoreline_Y
- LowPressInput * 2 - StartLow)
lowpresstimer.stop () ؛
}
} ؛
lowpresstimer = توقيت جديد (10 ، lowpressTaskPerformer) ؛
lowpresstimer.start () ؛
}
}
} ؛
// تحديد مستمع الحدث الذي ينفذ كل 0.01 ثانية
HighpressTimer = 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.setResible (false) ؛
this.setVisible (صحيح) ؛
}
/**
* قماش إعادة رسم مقياس ضغط الدم
*/
فئة mycanvas يمتد قماش {
طلاء باطل عام (رسومات ز) {
// رسم الحدود
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 ، صحيح) ؛
// عمود الزئبق منخفض الضغط منخفض الضغط
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) ؛
.
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 فهارس أدنى
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 ؛
}
}
.
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 فهارس أدنى
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 ؛
}
}
}
/**
* تقنية التخزين المؤقت المزدوجة: سرعة الحساب المعقدة أبطأ من عرض الشاشة ، ويتم استخدام التخزين المؤقت لحل مشكلة الخفقان على الشاشة
*/
@تجاوز
تحديث الفراغ العام (الرسومات ز) {
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.getSystemandFeelClassName ()) ؛
} catch (استثناء e) {
E.PrintStackTrace () ؛
}
دم جديد () ؛
}
}