java code to simulate a cardio monitor with heartbeat curve on screen
- User
java code to simulate a cardio monitor with heartbeat curve on screen
- Assistant
- User
no heart beat on screen
- Assistant
I apologize for the confusion. Here's an updated code that generates a heartbeat curve on the screen:
import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class CardioMonitor extends JPanel { private int currentX = 0; // Current x-coordinate for drawing the curve private int currentY; // Current y-coordinate for drawing the curve public CardioMonitor() { setPreferredSize(new Dimension(800, 300)); setOpaque(true); setBackground(Color.WHITE); // Start animating the heartbeat curve Timer timer = new Timer(30, new ActionListener() { @Override public void actionPerformed(ActionEvent e) { currentX++; currentY = (int) (getHeight() / 2 + Math.sin(Math.toRadians(currentX * 0.5)) * (getHeight() / 4)); repaint(); } }); timer.start(); } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D) g; g2d.setColor(Color.RED); g2d.setStroke(new BasicStroke(2)); // Draw the heartbeat curve g2d.drawLine(currentX - 1, currentY, currentX, currentY); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { JFrame frame = new JFrame("Cardio Monitor"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(new CardioMonitor()); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } }); } } - User
make sinewave look like flatline with pulses: public void actionPerformed(ActionEvent e) { currentX++; currentY = (int) (getHeight() / 2 + Math.sin(Math.toRadians(currentX * 0.5)) * (getHeight() / 4)); repaint();
- Assistant
To make the sine wave look like a flatline with periodic pulses, you can modify the calculation of
currentYbased on a pulse function. Here's an updated version of theactionPerformedmethod:public void actionPerformed(ActionEvent e) { currentX++; int amplitude = getHeight() / 4; // Amplitude of the pulse int pulseDuration = 20; // Duration of each pulse (in frames) int pulseGap = 80; // Gap between each pulse (in frames) // Calculate currentY based on the pulse function if (currentX % (pulseDuration + pulseGap) < pulseDuration) { currentY = getHeight() / 2 + amplitude; } else { currentY = getHeight() / 2; } repaint(); }In this updated code, we introduce
amplitude,pulseDuration, andpulseGapvariables.amplitudedetermines the vertical displacement of the pulse from the flatline. indicates the number of frames that each pulse lasts, and defines the number of frames between each pulse. - User
dot is not moving
- Assistant
- User
works fine,but pulse goes down , not up ,show only line you alter
- Assistant
- User
can the dot behave like a pencil draww
- Assistant