Untitled

 avatar
unknown
plain_text
3 days ago
4.9 kB
2
Indexable
***********************************************/

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

public class BouncingBall2 extends JPanel implements ActionListener {
    private static final int TIMER_DELAY = 20;
    private double ballX = 0;
    private double ballY = 0;
    private double ballVelocityY = 0;
    private boolean isBouncing = false;
    private Timer timer;
    private double gravity = 0.5;
    private double speedMultiplier = 1.0;
    private ArrayList<double[]> path;
    private JLabel speedLabel;
    private JLabel positionLabel;

    public BouncingBall2() {
        timer = new Timer(TIMER_DELAY, this);
        setPreferredSize(new Dimension(800, 600));
        setBackground(Color.LIGHT_GRAY);
        path = new ArrayList<>();
        speedLabel = new JLabel("Speed: 1.00");
        positionLabel = new JLabel("Position: (0.00, 0.00)");
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        drawBall(g);
        drawPath(g);
    }

    private void drawBall(Graphics g) {
        int ballSize = getBallSize();
        int drawX = (int) (ballX * getWidth());
        int drawY = (int) (ballY * getHeight());
        g.setColor(Color.RED);
        g.fillOval(drawX, drawY, ballSize, ballSize);
    }

    private void drawPath(Graphics g) {
        g.setColor(Color.BLACK);
        for (int i = 1; i < path.size(); i++) {
            double[] prevLocation = path.get(i - 1);
            double[] currentLocation = path.get(i);
            g.drawLine((int) (prevLocation[0] * getWidth()), (int) (prevLocation[1] * getHeight()),
                    (int) (currentLocation[0] * getWidth()), (int) (currentLocation[1] * getHeight()));
        }
    }

    private int getBallSize() {
        return (int) (getHeight() * 0.05);
    }

    public void startBouncing() {
        isBouncing = true;
        path.clear();
        timer.start();
    }

    public void stopBouncing() {
        isBouncing = false;
        timer.stop();
    }

    public void continueBouncing() {
        isBouncing = true;
        timer.start();
    }

    public void increaseSpeed() {
        speedMultiplier = Math.min(2.0, speedMultiplier + 0.25);
        updateSpeedLabel();
    }

    public void decreaseSpeed() {
        speedMultiplier = Math.max(0.25, speedMultiplier - 0.25);
        updateSpeedLabel();
    }

    public void resetBall() {
        ballX = 0;
        ballY = 0;
        ballVelocityY = 0;
        path.clear();
        repaint();
    }

    private void updateSpeedLabel() {
        speedLabel.setText("Speed: " + String.format("%.2f", speedMultiplier));
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (isBouncing) {
            updateBallPosition();
            repaint();
        }
    }

    private void updateBallPosition() {
        ballVelocityY += gravity * speedMultiplier;
        ballY += ballVelocityY * speedMultiplier / getHeight();

        int ballSize = getBallSize();

        if (ballY * getHeight() + ballSize > getHeight()) {
            ballY = (getHeight() - ballSize) / (double) getHeight();
            ballVelocityY = -ballVelocityY * 0.8;
        }

        ballX += 2 * speedMultiplier / getWidth();

        if (ballX * getWidth() + ballSize > getWidth()) {
            stopBouncing();
        }

        double centerX = ballX + (ballSize / (double) getWidth()) / 2;
        double centerY = ballY + (ballSize / (double) getHeight()) / 2;
        path.add(new double[]{centerX, centerY});

        positionLabel.setText(String.format("Position: (%.2f, %.2f)", ballX, ballY));
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("Bouncing Ball Simulation");
        frame.setMinimumSize(new Dimension(700, 300));
        BouncingBall2 bouncingBall = new BouncingBall2();

        JButton startButton = new JButton("Start");
        JButton stopButton = new JButton("Stop");
        JButton continueButton = new JButton("Continue");
        JButton fastButton = new JButton("Fast");
        JButton slowButton = new JButton("Slow");
        JButton resetButton = new JButton("Reset");

        startButton.addActionListener(e -> bouncingBall.startBouncing());
        stopButton.addActionListener(e -> bouncingBall.stopBouncing());
        continueButton.addActionListener(e -> bouncingBall.continueBouncing());
        fastButton.addActionListener(e -> bouncingBall.increaseSpeed());
        slowButton.addActionListener(e -> bouncingBall.decreaseSpeed());
        resetButton.addActionListener(e -> bouncingBall.resetBall());

        JPanel buttonPanel = new JPanel();
        buttonPanel.add(startButton);
        buttonPanel.add(stopButton);
        buttonPanel.add}}
Editor is loading...
Leave a Comment