search
HomeJavajavaTutorialJava graphical interface programming for analog blood pressure monitor

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;
/**
 * 模拟血压计类,高压、低压
 * 
 * @author 樊俊彬
 * @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;
 // 画布长宽
 private final int CANVAS_WIDTH = 400;
 private final int CANVAS_HEIGHT = 800;
 // 玻璃外壳长宽与起始坐标
 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;
 // 框架大小与起始坐标
 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刻度线的横纵坐标与长度
 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;
 // 输入的高压、低压
 private int highPressInput, lowPressInput;
 // 高、低压水银柱的动态高度
 int highPressHeight = 0;
 int lowPressHeight = 0;
 int startLow = BLOOD_Y;
 // 高、低水银计时器
 Timer highPressTimer, lowPressTimer;
 public Blood() {
  super("自定义血压计模型-FreeDoman");
  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("显示");
  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) {
      // 高度增加 1像素/0.01s,只到满足输入的要求,停止计时
      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();
         if (lowPressHeight == ZORELINE_Y
           - lowPressInput * 2 - startLow)
          lowPressTimer.stop();
        }
       };
       lowPressTimer = new Timer(10, lowPressTaskPerformer);
       lowPressTimer.start();
      }
     }
    };
    // 定义每0.01秒执行一次的事件监听器
    highPressTimer = new 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.setResizable(false);
  this.setVisible(true);
 }
 /**
  * 画布重绘血压计
  */
 class MyCanvas extends Canvas {
  public void paint(Graphics 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;
   for (; 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;
    }
   }
   // 左侧0刻度线起始刻度与坐标(刻度线纵坐标以line_y渐变)
   int leftStartDegree = 0;
   int leftLine_y = ZORELINE_Y;
   for (; 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;
    }
   }
  }
  /**
   * 双缓冲技术:复杂的计算速度慢于屏幕显示,用缓冲解决屏幕闪烁问题
   */
  @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) {
  // 设置界面的外观,为系统外观
  try {
   UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
  } catch (Exception e) {
   e.printStackTrace();
  }
  new Blood();
 }
}

For more articles related to java graphical interface programming and analog blood pressure monitor, please pay attention to the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
JVM performance vs other languagesJVM performance vs other languagesMay 14, 2025 am 12:16 AM

JVM'sperformanceiscompetitivewithotherruntimes,offeringabalanceofspeed,safety,andproductivity.1)JVMusesJITcompilationfordynamicoptimizations.2)C offersnativeperformancebutlacksJVM'ssafetyfeatures.3)Pythonisslowerbuteasiertouse.4)JavaScript'sJITisles

Java Platform Independence: Examples of useJava Platform Independence: Examples of useMay 14, 2025 am 12:14 AM

JavaachievesplatformindependencethroughtheJavaVirtualMachine(JVM),allowingcodetorunonanyplatformwithaJVM.1)Codeiscompiledintobytecode,notmachine-specificcode.2)BytecodeisinterpretedbytheJVM,enablingcross-platformexecution.3)Developersshouldtestacross

JVM Architecture: A Deep Dive into the Java Virtual MachineJVM Architecture: A Deep Dive into the Java Virtual MachineMay 14, 2025 am 12:12 AM

TheJVMisanabstractcomputingmachinecrucialforrunningJavaprogramsduetoitsplatform-independentarchitecture.Itincludes:1)ClassLoaderforloadingclasses,2)RuntimeDataAreafordatastorage,3)ExecutionEnginewithInterpreter,JITCompiler,andGarbageCollectorforbytec

JVM: Is JVM related to the OS?JVM: Is JVM related to the OS?May 14, 2025 am 12:11 AM

JVMhasacloserelationshipwiththeOSasittranslatesJavabytecodeintomachine-specificinstructions,managesmemory,andhandlesgarbagecollection.ThisrelationshipallowsJavatorunonvariousOSenvironments,butitalsopresentschallengeslikedifferentJVMbehaviorsandOS-spe

Java: Write Once, Run Anywhere (WORA) - A Deep Dive into Platform IndependenceJava: Write Once, Run Anywhere (WORA) - A Deep Dive into Platform IndependenceMay 14, 2025 am 12:05 AM

Java implementation "write once, run everywhere" is compiled into bytecode and run on a Java virtual machine (JVM). 1) Write Java code and compile it into bytecode. 2) Bytecode runs on any platform with JVM installed. 3) Use Java native interface (JNI) to handle platform-specific functions. Despite challenges such as JVM consistency and the use of platform-specific libraries, WORA greatly improves development efficiency and deployment flexibility.

Java Platform Independence: Compatibility with different OSJava Platform Independence: Compatibility with different OSMay 13, 2025 am 12:11 AM

JavaachievesplatformindependencethroughtheJavaVirtualMachine(JVM),allowingcodetorunondifferentoperatingsystemswithoutmodification.TheJVMcompilesJavacodeintoplatform-independentbytecode,whichittheninterpretsandexecutesonthespecificOS,abstractingawayOS

What features make java still powerfulWhat features make java still powerfulMay 13, 2025 am 12:05 AM

Javaispowerfulduetoitsplatformindependence,object-orientednature,richstandardlibrary,performancecapabilities,andstrongsecurityfeatures.1)PlatformindependenceallowsapplicationstorunonanydevicesupportingJava.2)Object-orientedprogrammingpromotesmodulara

Top Java Features: A Comprehensive Guide for DevelopersTop Java Features: A Comprehensive Guide for DevelopersMay 13, 2025 am 12:04 AM

The top Java functions include: 1) object-oriented programming, supporting polymorphism, improving code flexibility and maintainability; 2) exception handling mechanism, improving code robustness through try-catch-finally blocks; 3) garbage collection, simplifying memory management; 4) generics, enhancing type safety; 5) ambda expressions and functional programming to make the code more concise and expressive; 6) rich standard libraries, providing optimized data structures and algorithms.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)