Java graphical interface programming analog blood pressure monitor

  • 2020-04-01 02:53:21
  • OfStack


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;

public class Blood extends JFrame {
 private static final long serialVersionUID = 1L;
 private Image iBuffer;
 private MyCanvas bloodCanvas = new MyCanvas();
 private JTextField highPressText, lowPressText;
 //The canvas width
 private final int CANVAS_WIDTH = 400;
 private final int CANVAS_HEIGHT = 800;
 //Length and width of the glass shell with 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 the length of the 0 scale
 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 pressure, low pressure
 private int highPressInput, lowPressInput;
 //Dynamic heights 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 sphygmomanometer model -FreeDoman");
  this.setDefaultCloseOperation(EXIT_ON_CLOSE);
  this.setBounds(300, 50, CANVAS_WIDTH, CANVAS_HEIGHT + 20);
  //Add control to the northern part of the framework
  JPanel topPanel = new JPanel();
  this.add(topPanel, BorderLayout.NORTH);
  highPressText = new JTextField(5);
  lowPressText = new JTextField(5);
  JButton pressButton = new JButton(" According to ");
  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) {
      //Height increased by 1 pixel /0.01s, only to meet the requirements of the input, stop timing
      highPressHeight += 1;
      bloodCanvas.repaint();
      if (highPressHeight == highPressInput * 2) {
       highPressTimer.stop();
       //The low pressure mercury column timer is nested inside the high voltage timer and has a sequence (high voltage goes first, then low voltage).
       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();
      }
     }
    };
    //Defines an event listener that executes every 0.01 seconds
    highPressTimer = new Timer(10, highPressTaskPerformer);
    highPressTimer.start();
   }
  });
  topPanel.add(new JLabel(" High pressure 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 the canvas to the central area
  this.add(bloodCanvas, BorderLayout.CENTER);
  this.setResizable(false);
  this.setVisible(true);
 }
 
 class MyCanvas extends Canvas {
  public void paint(Graphics g) {
   //Picture frame
   g.setColor(Color.BLACK);
   g.draw3DRect(FRAME_X, FRAME_Y, FRAME_WIDTH, FRAME_HEIGHT, true);
   //Painted 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 scale and coordinate of the scale line 0 on the right (the vertical coordinate of the scale line is graded with line_y)
   int rightStartDegree = 0;

   int line_y = ZORELINE_Y;
   for (; line_y > BLOOD_Y; line_y -= 2) {
    //Two pixels are a minimum index of 1 degree
    g.setColor(Color.BLACK);
    g.drawLine(ZORELINE_X, line_y, ZORELINE_X + LINE_LENGTH, line_y);
    //Mark 10 degrees at 10 min intervals
    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 scale start scale and coordinate (the ordinate of the scale line is graded with line_y)
   int leftStartDegree = 0;
   int leftLine_y = ZORELINE_Y;
   for (; leftLine_y > BLOOD_Y; leftLine_y -= 6) {
    //Six pixels are a minimum index of 1 degree
    g.setColor(Color.BLACK);
    g.drawLine(BLOOD_X, leftLine_y, BLOOD_X - LINE_LENGTH,
      leftLine_y);
    //Mark 10 degrees at 10 min intervals
    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) {
  //Set the appearance of the interface as the system appearance
  try {
   UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
  } catch (Exception e) {
   e.printStackTrace();
  }
  new Blood();
 }
}


Related articles: