The code copy is as follows:
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;
/**
* Simulated blood pressure meter, high pressure, low pressure
*
* @author Fan 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;
// Canvas length and width
private final int CANVAS_WIDTH = 400;
private final int CANVAS_HEIGHT = 800;
// The length and width of the glass shell and the 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;
// The horizontal and vertical coordinates and length of the 0 scale 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 high voltage and low voltage
private int highPressInput, lowPressInput;
// Dynamic height of high and low pressure mercury columns
int highPressHeight = 0;
int lowPressHeight = 0;
int startLow = BLOOD_Y;
// High and low mercury timer
Timer highPressTimer, lowPressTimer;
public Blood() {
super("Custom blood pressure monitor model-FreeDoman");
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setBounds(300, 50, CANVAS_WIDTH, CANVAS_HEIGHT + 20);
// Add control to the north of the framework
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 the height by 1 pixel/0.01s, and stop the timing until the input requirements are met.
highPressHeight += 1;
bloodCanvas.repaint();
if (highPressHeight == highPressInput * 2) {
highPressTimer.stop();
// The low-pressure mercury column timer is nested inside the high-pressure timer, in sequence (high-pressure first, then 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(10, lowPressTaskPerformer);
lowPressTimer.start();
}
}
};
// Define event listener that executes every 0.01 seconds
highPressTimer = new Timer(10, highPressTaskPerformer);
highPressTimer.start();
}
});
topPanel.add(new JLabel("High Voltage Value", JLabel.CENTER));
topPanel.add(highPressText);
topPanel.add(new JLabel("low voltage value", JLabel.CENTER));
topPanel.add(lowPressText);
// topPanel.add(new JLabel("Heart Rate", JLabel.CENTER));
topPanel.add(pressButton);
// Add canvas to the central area
this.add(bloodCanvas, BorderLayout.CENTER);
this.setResizable(false);
this.setVisible(true);
}
/**
* Canvas redrawing of blood pressure meter
*/
class MyCanvas extends Canvas {
public void paint(Graphics g) {
// Draw borders
g.setColor(Color.BLACK);
g.draw3DRect(FRAME_X, FRAME_Y, FRAME_WIDTH, FRAME_HEIGHT, true);
// Draw 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 mercury ball at the bottom
g.setColor(Color.RED);
g.fillOval(CANVAS_WIDTH / 2 - 30, ZORELINE_Y - 5, 60, 60);
// The starting tick and coordinates of the 0 scale line on the right (the vertical coordinate of the scale line is gradually changed by line_y)
int rightStartDegree = 0;
int line_y = ZORELINE_Y;
for (; line_y > BLOOD_Y; line_y -= 2) {
// 2 pixels are a minimum index of 1 degree
g.setColor(Color.BLACK);
g.drawLine(ZORELINE_X, line_y, ZORELINE_X + LINE_LENGTH, line_y);
// Draw 10-degree ticks every 10 minimum indexes
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;
}
}
// The starting tick and coordinates of the 0 scale line on the left (the vertical coordinate of the scale line is gradually changed by line_y)
int leftStartDegree = 0;
int leftLine_y = ZORELINE_Y;
for (; leftLine_y > BLOOD_Y; leftLine_y -= 6) {
// 6 pixel points are a minimum index of 1 degree
g.setColor(Color.BLACK);
g.drawLine(BLOOD_X, leftLine_y, BLOOD_X - LINE_LENGTH,
leftLine_y);
// Draw 10-degree ticks every 10 minimum indexes
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, and buffering is used to solve the problem of screen flickering
*/
@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 to the system appearance
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
new Blood();
}
}