Skip to content

Instantly share code, notes, and snippets.

@etemesi254
Created July 15, 2024 09:36
Show Gist options
  • Save etemesi254/11fc90b12e71fd8822131572670d8ad6 to your computer and use it in GitHub Desktop.
Save etemesi254/11fc90b12e71fd8822131572670d8ad6 to your computer and use it in GitHub Desktop.
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Ellipse2D;
public class SpaceAnimationMain {
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
JFrame frame = new JFrame("Space Animation");
SpaceAnimationPanel animationPanel = new SpaceAnimationPanel();
frame.add(animationPanel);
frame.setSize(800, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
Timer timer = new Timer(50, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
animationPanel.updateAnimation();
animationPanel.repaint();
}
});
timer.start();
});
}
}
class SpaceAnimationPanel extends JPanel {
private int width, height;
private SpaceAnimation.Sun sun;
private SpaceAnimation.Earth earth;
private SpaceAnimation.Satellite satellite1, satellite2;
private double time;
public SpaceAnimationPanel() {
setBackground(Color.BLACK);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
width = getWidth();
height = getHeight();
if (sun == null || earth == null || satellite1 == null || satellite2 == null) {
initializeObjects();
}
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
sun.draw(g2d);
earth.draw(g2d);
satellite1.draw(g2d);
satellite2.draw(g2d);
}
public void initializeObjects() {
sun = new SpaceAnimation.Sun((double) width / 2, (double) height / 2, 50);
earth = new SpaceAnimation.Earth((double) width * 3 / 4, (double) height / 2, 20);
satellite1 = new SpaceAnimation.Satellite((double) width * 3 / 4, (double) height / 2, 5, 1.5);
satellite2 = new SpaceAnimation.Satellite((double) width * 3 / 4, (double) height / 2, 5, 2.0);
}
public void updateAnimation() {
var newWidth = getWidth();
var newHeight = getHeight();
initializeObjects();
time += 0.05;
earth.update(newWidth, newHeight, time);
satellite1.update(newWidth, newHeight, time);
satellite2.update(newWidth, newHeight, time);
}
static class SpaceAnimation {
static class CelestialBody {
protected double x, y, radius;
protected Color color;
public CelestialBody(double x, double y, double radius) {
this.x = x;
this.y = y;
this.radius = radius;
this.color = Color.WHITE;
}
public void draw(Graphics2D g2d) {
g2d.setColor(color);
g2d.fill(new Ellipse2D.Double(x - radius, y - radius, 2 * radius, 2 * radius));
}
}
static class Sun extends CelestialBody {
public Sun(double x, double y, double radius) {
super(x, y, radius);
this.color = Color.YELLOW;
}
}
static class Earth extends CelestialBody {
private double orbitRadius = 200;
private double angularSpeed = 0.5;
public Earth(double centerX, double centerY, double radius) {
super(centerX, centerY, radius);
this.color = Color.BLUE;
}
public void update(double width, double height, double time) {
x = (double) width / 2 + orbitRadius * Math.cos(angularSpeed * time);
y = (double) height / 2 + orbitRadius * Math.sin(angularSpeed * time);
// Color variation based on position
float hue = (float) ((Math.sin(angularSpeed * time) + 1) / 2);
color = Color.getHSBColor(hue, 1.0f, 1.0f);
// Radius variation based on position
radius = 20 + 5 * Math.sin(angularSpeed * time);
}
}
static class Satellite extends CelestialBody {
private double orbitRadius = 50;
private double angularSpeed;
private double earthAngle;
public Satellite(double centerX, double centerY, double radius, double speed) {
super(centerX, centerY, radius);
this.angularSpeed = speed;
this.color = Color.RED;
}
public void update(double width, double height, double time) {
earthAngle = 0.5 * time;
double satelliteAngle = angularSpeed * time;
// Elliptical orbit
double a = orbitRadius;
double b = orbitRadius * 0.6;
double satelliteX = a * Math.cos(satelliteAngle);
double satelliteY = b * Math.sin(satelliteAngle);
// Rotate around Earth's position
double earthX = width / 2 + 200 * Math.cos(earthAngle);
double earthY = height / 2 + 200 * Math.sin(earthAngle);
x = earthX + satelliteX * Math.cos(earthAngle) - satelliteY * Math.sin(earthAngle);
y = earthY + satelliteX * Math.sin(earthAngle) + satelliteY * Math.cos(earthAngle);
// Color variation based on position
float hue = (float) ((Math.sin(satelliteAngle) + 1) / 2);
color = Color.getHSBColor(hue, 1.0f, 1.0f);
// Radius variation based on position
radius = 5 + 2 * Math.sin(satelliteAngle);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment