/*
*
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 quadcoptergui;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class QuadcopterAnimation extends
JPanel implements ActionListener {
private double rotationAngle = 0;
private final int ROTOR_RADIUS = 30;
private final int BODY_WIDTH = 100;
private final int BODY_HEIGHT = 100;
public QuadcopterAnimation() {
// Timer fires every 20ms (~50
FPS)
Timer timer = new Timer(20, this);
timer.start();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
// Enable Antialiasing for smooth lines
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
int centerX = getWidth() / 2;
int centerY = getHeight() / 2;
// 1. Draw Main Body (Rectangle)
g2d.setColor(Color.ORANGE);
g2d.fillRect(centerX - BODY_WIDTH / 2, centerY - BODY_HEIGHT / 2,
BODY_WIDTH, BODY_HEIGHT);
// 2. Draw 4 Rotors
drawRotor(g2d, centerX - 50, centerY - 50); // Top Left
drawRotor(g2d, centerX + 50, centerY - 50); // Top Right
drawRotor(g2d, centerX - 50, centerY + 50); // Bottom Left
drawRotor(g2d, centerX + 50, centerY + 50); // Bottom Right
}
private void drawRotor(Graphics2D g2d, int x, int y) {
g2d.setColor(Color.BLACK);
// Draw the outer circle
g2d.drawOval(x - ROTOR_RADIUS, y - ROTOR_RADIUS, ROTOR_RADIUS * 2,
ROTOR_RADIUS * 2);
// Draw 4 Spokes
for (int i = 0; i < 4; i++) {
// Space spokes 90 degrees
apart (PI/2 radians)
double currentSpokeAngle = rotationAngle + (i * Math.PI / 2);
int endX = (int) (x + Math.cos(currentSpokeAngle) * ROTOR_RADIUS);
int endY = (int) (y + Math.sin(currentSpokeAngle) * ROTOR_RADIUS);
g2d.drawLine(x, y, endX, endY);
}
}
@Override
public void actionPerformed(ActionEvent e) {
// Increase angle for rotation
rotationAngle += 0.15;
repaint(); // Redraw the panel
}
public static void main(String[] args) {
JFrame frame = new JFrame("Quadcopter GUI Animation");
QuadcopterAnimation panel = new QuadcopterAnimation();
frame.add(panel);
frame.setSize(400, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}

Tidak ada komentar:
Posting Komentar