Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.imageio.ImageIO;
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.image.BufferedImage;
- import java.io.File;
- import java.io.IOException;
- import java.util.ArrayList;
- public class UkladSloneczny {
- public static void main(String[] args) {
- new UkladSloneczny();
- }
- public UkladSloneczny() {
- EventQueue.invokeLater(new Runnable() {
- @Override
- public void run() {
- try {
- UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
- } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
- ex.printStackTrace();
- }
- JFrame frame = new JFrame("Planety i Księżyce");
- frame.setLayout(new BorderLayout());
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- PanelSloneczny panelSloneczny = new PanelSloneczny();
- frame.add(panelSloneczny, BorderLayout.CENTER);
- JToolBar toolBar = new JToolBar();
- JButton day = new JButton("1s = 1 dzien");
- JButton week = new JButton("1s = 1 tydzien");
- JButton month = new JButton("1s = 1 miesiąc");
- JButton year = new JButton("1s = 1 rok");
- JButton slower = new JButton("2x wolniej");
- JButton faster = new JButton("2x szybciej");
- JButton pause = new JButton("pauza");
- day.addActionListener(e -> panelSloneczny.speed = 1);
- week.addActionListener(e -> panelSloneczny.speed = 7);
- month.addActionListener(e -> panelSloneczny.speed = 30);
- year.addActionListener(e -> panelSloneczny.speed = 365);
- slower.addActionListener(e -> panelSloneczny.speed *= 0.5);
- faster.addActionListener(e -> panelSloneczny.speed *= 2);
- pause.addActionListener(e -> panelSloneczny.pause = !panelSloneczny.pause);
- toolBar.add(day);
- toolBar.add(week);
- toolBar.add(month);
- toolBar.add(year);
- toolBar.add(slower);
- toolBar.add(faster);
- toolBar.add(pause);
- frame.add(toolBar, BorderLayout.SOUTH);
- frame.pack();
- frame.setResizable(false);
- frame.setVisible(true);
- }
- });
- }
- public class PanelSloneczny extends JPanel {
- private BufferedImage currentFrame;
- private int frame;
- ArrayList<Cialo> list = new ArrayList<Cialo>();
- BufferedImage background = null;
- double angle = 0;
- double speed = 1;
- double way = -1;
- boolean pause = true;
- public PanelSloneczny() {
- try {
- background = ImageIO.read(new File("E:\\Media\\Documents\\java\\LAB12\\graphics\\starry_sky_stock_by_budgie.jpg"));
- } catch (IOException e) {
- e.printStackTrace();
- }
- list.add(Cialo.Sun);
- list.add(Cialo.Mercury);
- list.add(Cialo.Venus);
- list.add(Cialo.Earth);
- list.add(Cialo.Moon);
- list.add(Cialo.Mars);
- list.add(Cialo.Eris);
- list.add(Cialo.Jupiter);
- list.add(Cialo.Saturn);
- list.add(Cialo.Uran);
- list.add(Cialo.Neptun);
- list.add(Cialo.Pluto);
- list.add(Cialo.Ceres);
- Timer timer = new Timer(16, new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- repaint();
- }
- });
- timer.start();
- }
- @Override
- public Dimension getPreferredSize() {
- return new Dimension(1000, 1000);
- }
- @Override
- protected void paintComponent(Graphics g) {
- super.paintComponent(g);
- if (pause)
- angle += Math.PI * 2 * way * speed * 0.016;
- if (background != null) {
- Graphics2D g2d = (Graphics2D) g.create();
- g2d.drawImage(background, 0, 0, this);
- g2d.setPaint(Color.GRAY);
- for (int i = 1; i < list.size(); i++) {
- list.get(i).calcPosition(angle);
- g2d.drawOval(list.get(i).center.getX() - (int) list.get(i).orbit, list.get(i).center.getY() - (int) list.get(i).orbit,
- (int) list.get(i).orbit * 2, (int) list.get(i).orbit * 2);
- }
- for (int i = 0; i < list.size(); i++) {
- g2d.fillOval(list.get(i).getX() - (int) list.get(i).radius, list.get(i).getY() - (int) list.get(i).radius,
- (int) list.get(i).radius * 2, (int) list.get(i).radius * 2);
- g2d.setPaint(Color.LIGHT_GRAY);
- g2d.drawString(list.get(i).toString(), list.get(i).getX(), list.get(i).getY());
- }
- g2d.dispose();
- }
- }
- }
- public enum Cialo {
- Sun(new Point(500, 500), 696342),
- Mercury(Sun, 57909036.f, 2439, 0.241),
- Venus(Sun, 108208925.f, 6051, 0.615),
- Earth(Sun, 149597871.f, 6371, 1),
- Moon(Earth, 384400.f, 1736, 0.07480132796),
- Mars(Sun, 227936637.f, 3389, 1.881),
- Jupiter(Sun, 778412028.f, 70000, 11.862),
- Saturn(Sun, 1473838225.f, 60000, 29.457),
- Uran(Sun, 2872428721.f, 25362, 84.011),
- Neptun(Sun, 4494967229.f, 24662, 164.69),
- Ceres(Sun, 414082773.f, 945, 4.605479452),
- Pluto(Sun, 5906423142.f, 2370, 247.6),
- Haumea(Sun, 6483571729.f, 1379, 285.3),
- Eris(Sun, 10120295973.f, 2326, 556.4),
- Makemake(Sun, 6845598576.f, 1500, 309.57);
- Point position;
- boolean stationary;
- Cialo center;
- double orbit;
- double radius;
- double period;
- Cialo(Cialo center, double orbit, double radius, double period) {
- this.center = center;
- this.position = new Point();//center.getX()+orbit, center.getY());
- stationary = false;
- this.orbit = Math.log10(orbit)*30;
- this.radius = Math.sqrt(radius) / 5;
- this.period = period * 365.256363004;
- System.out.println(this.orbit);
- }
- Cialo(Point position, double radius) {
- this.position = position;
- this.stationary = true;
- this.radius = Math.sqrt(radius) / 20;
- }
- void calcPosition(double angle) {
- if (stationary) return;
- angle = angle / period;
- position.setLocation(center.getX() + orbit * Math.sin(angle),
- center.getY() + orbit * Math.cos(angle));
- }
- public int getX() {
- return (int) position.getX();
- }
- public int getY() {
- return (int) position.getY();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement