Sabtu, 02 Mei 2026

JAVA - Quadcopter Game - Bonus Code

 



/*

 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license

 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Main.java to edit this template

 */

package quadcopteranimation4;

import javax.swing.*;

import java.awt.*;

import java.awt.geom.*;

import java.util.Random;


public class QuadcopterAnimation4 extends JPanel {

    // Quadcopter position and movement

    private double x = 100, y = 100;

    private double dx = 2, dy = 2; // Velocity

    private double rotationAngle = 0; // Rotor rotation

    

    private final int BODY_WIDTH = 60;

    private final int BODY_HEIGHT = 50;

    private final int ROTOR_SIZE = 30;

    private final Random rand = new Random();


    public QuadcopterAnimation4() {

        // Animation Timer: ~60 FPS (16ms delay)

        Timer timer = new Timer(16, e -> {

            updatePosition();

            rotationAngle += 0.5; // Speed of rotor spin

            repaint();

        });

        timer.start();

    }


    private void updatePosition() {

        x += dx;

        y += dy;


        // Bounce off walls and pick a random new direction

        if (x < 0 || x > getWidth() - BODY_WIDTH - ROTOR_SIZE) {

            dx = -dx + (rand.nextDouble() - 0.5); 

            x = Math.max(0, Math.min(x, getWidth() - BODY_WIDTH));

        }

        if (y < 0 || y > getHeight() - BODY_HEIGHT - ROTOR_SIZE) {

            dy = -dy + (rand.nextDouble() - 0.5);

            y = Math.max(0, Math.min(y, getHeight() - BODY_HEIGHT));

        }

    }


    @Override

    protected void paintComponent(Graphics g) {

        super.paintComponent(g);

        Graphics2D g2d = (Graphics2D) g;

        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);


        // 1. Draw Body

        g2d.setColor(Color.ORANGE);

        g2d.fillRect((int)x, (int)y, BODY_WIDTH, BODY_HEIGHT);


        // 2. Draw 4 Rotors (Corners)

        drawRotor(g2d, x, y); // Top Left

        drawRotor(g2d, x + BODY_WIDTH, y); // Top Right

        drawRotor(g2d, x, y + BODY_HEIGHT); // Bottom Left

        drawRotor(g2d, x + BODY_WIDTH, y + BODY_HEIGHT); // Bottom Right

    }


    private void drawRotor(Graphics2D g2d, double cx, double cy) {

        g2d.setColor(Color.BLACK);

        // Draw the outer circle

        Ellipse2D circle = new Ellipse2D.Double(cx - ROTOR_SIZE/2.0, cy - ROTOR_SIZE/2.0, ROTOR_SIZE, ROTOR_SIZE);

        g2d.draw(circle);


        // Draw 4 Rotating Spokes

        for (int i = 0; i < 4; i++) {

            double angle = rotationAngle + (i * Math.PI / 2);

            int x2 = (int) (cx + Math.cos(angle) * (ROTOR_SIZE / 2.0));

            int y2 = (int) (cy + Math.sin(angle) * (ROTOR_SIZE / 2.0));

            g2d.drawLine((int)cx, (int)cy, x2, y2);

        }

    }


    public static void main(String[] args) {

        JFrame frame = new JFrame("Quadcopter Random Flight");

        frame.add(new QuadcopterAnimation4());

        frame.setSize(800, 600);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.setLocationRelativeTo(null);

        frame.setVisible(true);

    }

}



Tidak ada komentar: