Añadir archivo
This commit is contained in:
parent
278147c0ee
commit
a7439c6ffe
|
@ -0,0 +1,168 @@
|
||||||
|
//package Graphics.SolarSystem;
|
||||||
|
|
||||||
|
import java.awt.*;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import javax.swing.*;
|
||||||
|
|
||||||
|
public class SolarSystemAnimation extends JFrame {
|
||||||
|
private SolarPanel panel;
|
||||||
|
|
||||||
|
public SolarSystemAnimation() {
|
||||||
|
super("Sistema Solar ASCII");
|
||||||
|
setSize(800, 800);
|
||||||
|
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
|
panel = new SolarPanel();
|
||||||
|
add(panel);
|
||||||
|
setVisible(true);
|
||||||
|
|
||||||
|
// Hacer que la ventana se ajuste al tamaño de la pantalla
|
||||||
|
setResizable(true); // Esto permite que la ventana se redimensione
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
panel.updatePositions();
|
||||||
|
panel.repaint();
|
||||||
|
try {
|
||||||
|
Thread.sleep(50);
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
new SolarSystemAnimation();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class SolarPanel extends JPanel {
|
||||||
|
private ArrayList<Planet> planets;
|
||||||
|
private int centerX;
|
||||||
|
private int centerY;
|
||||||
|
private final String sunASCII = "*";
|
||||||
|
|
||||||
|
public SolarPanel() {
|
||||||
|
planets = new ArrayList<>();
|
||||||
|
planets.add(new Planet("Mercurio", 50, 2, "(o)", Color.GRAY));
|
||||||
|
planets.add(new Planet("Venus", 80, 1.5, "(O)", Color.ORANGE));
|
||||||
|
planets.add(new Planet("Tierra", 120, 1, "[O]", Color.green));
|
||||||
|
planets.add(new Planet("Marte", 160, 0.8, "(M)", Color.RED));
|
||||||
|
|
||||||
|
String Jupiter =
|
||||||
|
" .-'-.\n" +
|
||||||
|
" /%$$%%$%%\\\n" +
|
||||||
|
" |##$$%%%$#$|\n" +
|
||||||
|
" \\$%$$%$%%/\n" +
|
||||||
|
" '-.-'";
|
||||||
|
|
||||||
|
planets.add(new Planet("Jupiter", 220, 0.5, Jupiter, Color.ORANGE));
|
||||||
|
String saturnoASCII =
|
||||||
|
" .::.\n" +
|
||||||
|
" .:' .:\n" +
|
||||||
|
" ,MMM8&&&.:' .:'\n" +
|
||||||
|
" MMMMM88&&&& .:'\n" +
|
||||||
|
" MMMMM88&&&&&&:'\n" +
|
||||||
|
" MMMMM88&&&&&&\n" +
|
||||||
|
" .:MMMMM88&&&&&&\n" +
|
||||||
|
" .:' MMMMM88&&&&\n" +
|
||||||
|
" .:' .:'MMM8&&&'\n" +
|
||||||
|
":'. .:' \n" + //
|
||||||
|
"'::'";
|
||||||
|
planets.add(new Planet("Saturno", 280, 0.3, saturnoASCII, Color.YELLOW));
|
||||||
|
|
||||||
|
String uranoASCII =
|
||||||
|
" .-'-.\n" +
|
||||||
|
" / \\\n" +
|
||||||
|
" | ◉ |\n" +
|
||||||
|
" \\ /\n" +
|
||||||
|
" '-.-'";
|
||||||
|
|
||||||
|
planets.add(new Planet("Urano", 340, 0.2, uranoASCII, Color.CYAN));
|
||||||
|
|
||||||
|
String neptunoASCII =
|
||||||
|
" OOOOO \n" +
|
||||||
|
" OOOOOOO \n" +
|
||||||
|
" OOOOOOO \n" +
|
||||||
|
" OOOOO ";
|
||||||
|
|
||||||
|
planets.add(new Planet("Neptuno", 400, 0.1,neptunoASCII, Color.magenta));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void updatePositions() {
|
||||||
|
for (Planet planet : planets) {
|
||||||
|
planet.updatePosition();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void paintComponent(Graphics g) {
|
||||||
|
super.paintComponent(g);
|
||||||
|
int width = getWidth();
|
||||||
|
int height = getHeight();
|
||||||
|
|
||||||
|
// mantener el Sol y los planetas centrados
|
||||||
|
centerX = width / 2;
|
||||||
|
centerY = height / 2;
|
||||||
|
|
||||||
|
g.setColor(Color.BLACK);
|
||||||
|
g.fillRect(0, 0, width, height);
|
||||||
|
|
||||||
|
g.setColor(Color.YELLOW);
|
||||||
|
|
||||||
|
String[] sunLines = sunASCII.split("\n");
|
||||||
|
g.setFont(new Font("Courier New", Font.PLAIN, 11));
|
||||||
|
|
||||||
|
for (int i = 0; i < sunLines.length; i++) {
|
||||||
|
g.drawString(sunLines[i], centerX - sunLines[i].length() * 3, centerY + i * 20);
|
||||||
|
}
|
||||||
|
|
||||||
|
g.setColor(Color.WHITE);
|
||||||
|
for (Planet planet : planets) {
|
||||||
|
int x = centerX + (int) (planet.getOrbitRadius() * Math.cos(planet.getAngle()));
|
||||||
|
int y = centerY + (int) (planet.getOrbitRadius() * Math.sin(planet.getAngle()));
|
||||||
|
|
||||||
|
g.setColor(planet.getColor());
|
||||||
|
String[] planetLines = planet.getAscii().split("\n");
|
||||||
|
for (int i = 0; i < planetLines.length; i++) {
|
||||||
|
g.drawString(planetLines[i], x - planetLines[i].length() * 3, y + i * 20);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class Planet {
|
||||||
|
private String name;
|
||||||
|
private int orbitRadius;
|
||||||
|
private double angularVelocity;
|
||||||
|
private double angle;
|
||||||
|
private String ascii;
|
||||||
|
private Color color;
|
||||||
|
|
||||||
|
public Planet(String name, int orbitRadius, double angularVelocity, String ascii, Color color) {
|
||||||
|
this.name = name;
|
||||||
|
this.orbitRadius = orbitRadius;
|
||||||
|
this.angularVelocity = angularVelocity;
|
||||||
|
this.angle = 0;
|
||||||
|
this.ascii = ascii;
|
||||||
|
this.color = color;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void updatePosition() {
|
||||||
|
angle += angularVelocity * Math.PI / 180;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getOrbitRadius() {
|
||||||
|
return orbitRadius;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double getAngle() {
|
||||||
|
return angle;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getAscii() {
|
||||||
|
return ascii;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Color getColor() {
|
||||||
|
return color;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue