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;
/**
* Analog Sphygmomanometer type, high pressure, low pressure
*
* @author Hung Junbin
* @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;
Long Canvas width
Private final int canvas_width = 400;
Private final int canvas_height = 800;
Glass housing width and starting coordinates
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;
Frame size and starting coordinates
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 horizontal ordinate and length of tick mark line
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;
Input of high pressure, low voltage
private int highpressinput, lowpressinput;
Dynamic height of high and low pressure mercury column
int highpressheight = 0;
int lowpressheight = 0;
int startlow = blood_y;
High and low Mercury timer
Timer Highpresstimer, Lowpresstimer;
Public Blood () {
Super ("Custom Sphygmomanometer model-freedoman");
This.setdefaultcloseoperation (Exit_on_close);
This.setbounds (DM, canvas_width, canvas_height + 20);
Add control to frame Northern zone
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 () {
@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) {
Increase height by 1 pixel/0.01s, only to meet input requirements, stop timing
Highpressheight + 1;
Bloodcanvas.repaint ();
if (highpressheight = = Highpressinput * 2) {
Highpresstimer.stop ();
Low-pressure Mercury-column timer is embedded in the high-pressure timer inside, in a sequential order (high pressure first go, after low pressure)
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 (ten, Lowpresstaskperformer);
Lowpresstimer.start ();
}
}
};
Defines an event listener that executes once every 0.01 seconds
Highpresstimer = new Timer (ten, Highpresstaskperformer);
Highpresstimer.start ();
}
});
Toppanel.add (New JLabel ("High Pressure value", jlabel.center));
Toppanel.add (Highpresstext);
Toppanel.add (New JLabel ("Low pressure value", jlabel.center));
Toppanel.add (Lowpresstext);
Toppanel.add (New JLabel ("heart rate", jlabel.center));
Toppanel.add (Pressbutton);
Add Canvas to Central area
This.add (Bloodcanvas, Borderlayout.center);
This.setresizable (FALSE);
This.setvisible (TRUE);
}
/**
* Canvas Redraw Sphygmomanometer
*/
Class MyCanvas extends Canvas {
public void Paint (Graphics g) {
Draw a border
G.setcolor (Color.Black);
G.draw3drect (frame_x, frame_y, Frame_width, Frame_height, true);
Draw a glass shell
G.setcolor (Color.orange);
G.fill3drect (blood_x, blood_y, Blood_width, Blood_height, true);
High Pressure Mercury Column
G.setcolor (color.red);
G.fill3drect (blood_x, Zoreline_y-highpressheight, Blood_width,
Highpressheight, True);
Low pressure high-pressure mercury column
G.setcolor (Color.orange);
G.fill3drect (blood_x, Startlow, Blood_width, Lowpressheight, true);
Draw the bottom of the mercury ball
G.setcolor (color.red);
G.filloval (CANVAS_WIDTH/2-zoreline_y-5, 60, 60);
Right 0 tick mark start tick and coordinate (tick mark ordinate to line_y gradient)
int rightstartdegree = 0;
int line_y = zoreline_y;
for (; line_y > blood_y; line_y-= 2) {
2 pixel points for a minimum score of 1 degrees
G.setcolor (Color.Black);
G.drawline (zoreline_x, line_y, zoreline_x + line_length, line_y);
Draw 10-degree tick marks per 10 min. degree
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;
}
}
Left 0 tick mark start tick and coordinate (tick mark ordinate to line_y gradient)
int leftstartdegree = 0;
int leftline_y = zoreline_y;
for (; leftline_y > blood_y; leftline_y-= 6) {
6 pixel points for a minimum score of 1 degrees
G.setcolor (Color.Black);
G.drawline (blood_x, leftline_y, Blood_x-line_length,
LEFTLINE_Y);
Draw 10-degree tick marks per 10 min. degree
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;
}
}
}
/**
* Double buffering technology: Complex calculation speed is slower than screen display, with buffering to solve the problem of screen flicker
*/
@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) {
Set the appearance of the interface for system appearance
try {
Uimanager.setlookandfeel (Uimanager.getsystemlookandfeelclassname ());
catch (Exception e) {
E.printstacktrace ();
}
New Blood ();
}
}