我试图进行太阳系模拟 我在jPanel中绘制了行星和太阳,在jPanel中我添加了内部框架来添加来自pallete的东西,但是当它运行时,它只有太阳,按钮,标签等行星,它不存在
这是我在主类
中的代码import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
/**
*
*
*/
public class UAS_PBOL_2014130005 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
SwingUtilities.invokeLater(new Runnable() { //berguna untuk mendaftarkan sebuat objek Runnale kepada thread Event Dispatch, utk nantinya dikerjakan oleh thread tersebut
public void run() {
createAndShowGUI(); //tugas" yang dikerjakan adalah uang ada pada method run(), pada kasus ini adalah memanggil methid createAndSHowGUI()
}
});
}
private static void createAndShowGUI() {
JFrame f = new JFrame("Solar System Simulation"); //membuat objek JFrame dengan judul window ("Swing..");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Secara default, pas tombol "X" ditekan, program tidak dihentikan. Kode ini membuat program berhenti ketika tombol "X" ditekan
f.add(new SolarSystem());
f.setSize(700, 600);
f.pack();
f.setVisible(true);
}
}
来自SolarSystem类
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.geom.AffineTransform;
import java.awt.geom.Ellipse2D;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.JPanel;
import javax.swing.JSlider;
import uas_pbol_2014130005.Planet;
import uas_pbol_2014130005.TimePanel;
public class SolarSystem extends JPanel {
public SolarSystem() {
super();
initComponents();
init();
}
static final int WIDTH = 700;
static final int HEIGHT = 600;
ArrayList<Planet> planets = new ArrayList<>();
float sun = 50;
//final JSlider daySlider = new JSlider(0, SolarSystemModel.DAYS_PER_EARTH_REVOLUTION_AROUND_SUN);
private void init() {
Mercury();
Venus();
Earth();
Mars();
Jupiter();
Saturn();
Uranus();
Neptune();
}
public void Mercury() {
Planet mercury = new Planet(60, 0.1f, 5, Color.YELLOW);
mercury.name("Mercury");
addPlanet(mercury);
}
public void Venus() {
Planet venus = new Planet(80, 0.07f, 7, Color.ORANGE);
venus.name("Venus");
addPlanet(venus);
}
public void Earth() {
Planet earth = new Planet(95, 0.05f, 8, Color.BLUE);
earth.satelit(1, new String[]{"Moon"});
earth.name("Earth");
addPlanet(earth);
}
public void Mars() {
Planet mars = new Planet(110, 0.03f, 8, Color.CYAN);
//mars.satelit(1, new String[] {"Moon"});
mars.name("Mars");
addPlanet(mars);
}
public void Jupiter() {
Planet jupiter = new Planet(140, 0.025f, 17, Color.DARK_GRAY);
//mars.satelit(1, new String[] {"Moon"});
jupiter.name("Jupiter");
addPlanet(jupiter);
}
public void Saturn() {
Planet saturn = new Planet(170, 0.015f, 15, Color.GREEN);
//mars.satelit(1, new String[] {"Moon"});
saturn.name("Saturn");
addPlanet(saturn);
}
public void Uranus() {
Planet uranus = new Planet(205, 0.02f, 13, Color.MAGENTA);
//mars.satelit(1, new String[] {"Moon"});
uranus.name("Uranus");
addPlanet(uranus);
}
public void Neptune() {
Planet neptune = new Planet(230, 0.0145f, 10, Color.ORANGE);
//mars.satelit(1, new String[] {"Moon"});
neptune.name("Neptune");
addPlanet(neptune);
}
public void addPlanet(Planet planet) {
planets.add(planet);
}
private void draw1(Graphics2D g) {
g.translate(WIDTH / 2, HEIGHT / 2);
//draw sun
Shape shape = new Ellipse2D.Float(-sun, -sun, sun * 2, sun * 2);
g.setColor(Color.red);
g.fill(shape);
g.setColor(Color.YELLOW);
g.drawString("Sun", 0, 0);
//draw planets
for (Planet planet : planets) {
planet.rotate();
planet.draw(g);
}
g.translate(WIDTH, HEIGHT);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.BLACK);
g2d.fillRect(0, 0, WIDTH, HEIGHT);
draw1(g2d);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
repaint();
}
任何想法? 先谢谢