Advertisement
dgulczynski

big basia

Jan 22nd, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.41 KB | None | 0 0
  1. import javax.imageio.ImageIO;
  2. import javax.swing.*;
  3. import java.awt.*;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6. import java.awt.image.BufferedImage;
  7. import java.io.File;
  8. import java.io.IOException;
  9. import java.util.ArrayList;
  10.  
  11. public class UkladSloneczny {
  12.  
  13.     public static void main(String[] args) {
  14.         new UkladSloneczny();
  15.     }
  16.  
  17.     public UkladSloneczny() {
  18.         EventQueue.invokeLater(new Runnable() {
  19.             @Override
  20.             public void run() {
  21.                 try {
  22.                     UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
  23.                 } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
  24.                     ex.printStackTrace();
  25.                 }
  26.  
  27.                 JFrame frame = new JFrame("Planety i Księżyce");
  28.                 frame.setLayout(new BorderLayout());
  29.                 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  30.                 PanelSloneczny panelSloneczny = new PanelSloneczny();
  31.                 frame.add(panelSloneczny, BorderLayout.CENTER);
  32.  
  33.                 JToolBar toolBar = new JToolBar();
  34.                 JButton day = new JButton("1s = 1 dzien");
  35.                 JButton month = new JButton("1s = 1 miesiąc");
  36.                 JButton slower = new JButton("2x wolniej");
  37.                 JButton faster = new JButton("2x szybciej");
  38.                 JButton pause = new JButton("pauza");
  39.                 JButton way = new JButton("zawróć");
  40.                 day.addActionListener(e -> panelSloneczny.speed = 1);
  41.                 month.addActionListener(e -> panelSloneczny.speed = 30);
  42.                 slower.addActionListener(e -> panelSloneczny.speed *= 0.5);
  43.                 faster.addActionListener(e -> panelSloneczny.speed *= 2);
  44.                 pause.addActionListener(e -> panelSloneczny.pause = !panelSloneczny.pause);
  45.                 way.addActionListener(e -> panelSloneczny.way *= -1);
  46.                 toolBar.add(day);
  47.                 toolBar.add(month);
  48.                 toolBar.add(slower);
  49.                 toolBar.add(faster);
  50.                 toolBar.add(way);
  51.                 toolBar.add(pause);
  52.  
  53.                 frame.add(toolBar, BorderLayout.SOUTH);
  54.  
  55.                 frame.pack();
  56.                 //frame.setResizable(false);
  57.                 frame.setVisible(true);
  58.             }
  59.         });
  60.     }
  61.  
  62.     public class PanelSloneczny extends JPanel {
  63.  
  64.         private BufferedImage currentFrame;
  65.         private int frame;
  66.         ArrayList<Cialo> list = new ArrayList<Cialo>();
  67.         BufferedImage background = null;
  68.         double angle = 0;
  69.         double speed = 1;
  70.         double way = -1;
  71.         boolean pause = true;
  72.  
  73.         public PanelSloneczny() {
  74.             try {
  75.                 background = ImageIO.read(new File("E:\\Media\\Documents\\java\\LAB12\\graphics\\starry_sky_stock_by_budgie.jpg"));
  76.             } catch (IOException e) {
  77.                 e.printStackTrace();
  78.             }
  79.  
  80.             list.add(Cialo.Słońce);
  81.             list.add(Cialo.Merkury);
  82.             list.add(Cialo.Wenus);
  83.             list.add(Cialo.Ziemia);
  84.             list.add(Cialo.Księżyc);
  85.             list.add(Cialo.Mars);
  86.             list.add(Cialo.Eris);
  87.             list.add(Cialo.Jowisz);
  88.             list.add(Cialo.Saturn);
  89.             list.add(Cialo.Uran);
  90.             list.add(Cialo.Neptun);
  91.             list.add(Cialo.Pluto);
  92.             list.add(Cialo.Ceres);
  93.             list.add(Cialo.Haumea);
  94.             list.add(Cialo.Makemake);
  95.             list.add(Cialo.Titan);
  96.             list.add(Cialo.Ganymede);
  97.             list.add(Cialo.Callisto);
  98.             list.add(Cialo.Io);
  99.  
  100.             Timer timer = new Timer(16, new ActionListener() {
  101.                 @Override
  102.                 public void actionPerformed(ActionEvent e) {
  103.                     repaint();
  104.                 }
  105.             });
  106.             timer.start();
  107.         }
  108.  
  109.         @Override
  110.         public Dimension getPreferredSize() {
  111.             return new Dimension(1200, 1000);
  112.         }
  113.  
  114.         @Override
  115.         protected void paintComponent(Graphics g) {
  116.             super.paintComponent(g);
  117.             if (pause)
  118.                 angle += Math.PI * 2 * way * speed * 0.016;
  119.             if (background != null) {
  120.                 Graphics2D g2d = (Graphics2D) g.create();
  121.                 g2d.drawImage(background, 0, 0, this);
  122.                 g2d.setPaint(Color.GRAY);
  123.                 for (int i = 1; i < list.size(); i++) {
  124.                     list.get(i).calcPosition(angle);
  125.                     g2d.drawOval(list.get(i).center.getX() - (int) list.get(i).orbit, list.get(i).center.getY() - (int) list.get(i).orbit,
  126.                             (int) list.get(i).orbit * 2, (int) list.get(i).orbit * 2);
  127.                 }
  128.  
  129.                 for(int i = 0; i < list.size(); i++) {
  130.                     g2d.setPaint(list.get(i).color);
  131.                     g2d.fillOval(list.get(i).getX() - (int) list.get(i).radius, list.get(i).getY() - (int) list.get(i).radius,
  132.                             (int) list.get(i).radius * 2, (int) list.get(i).radius * 2);
  133.                 }
  134.                 g2d.setPaint(Color.WHITE);
  135.                 for(int i = 0; i < list.size(); i++) {
  136.                     g2d.drawString(list.get(i).toString(), list.get(i).getX() + (int) list.get(i).radius, list.get(i).getY() + (int) list.get(i).radius);
  137.                 }
  138.                 g2d.drawString("1s to: "+speed+"dni = "+speed/365+" lat.", 10, 990);
  139.                 g2d.dispose();
  140.             }
  141.  
  142.         }
  143.  
  144.     }
  145.  
  146.     public enum Cialo {
  147.         Słońce(new Point(600, 500), 696342, new Color(255, 200, 0)),
  148.         Merkury(Słońce, 57909036.f, 2439, 0.241, new Color(225, 190, 150)),
  149.         Wenus(Słońce, 108208925.f, 6051, 0.615, new Color(178, 118, 50)),
  150.         Ziemia(Słońce, 149597871.f, 6371, 1, new Color(60, 180, 200)),
  151.         Księżyc(Ziemia, 384400.f, 1736, 0.0748, Color.LIGHT_GRAY),
  152.         Mars(Słońce, 227936637.f, 3389, 1.881, new Color(182, 144, 109)),
  153.         Jowisz(Słońce, 778412028.f, 70000, 11.862, new Color(226, 203, 159)),
  154.         Saturn(Słońce, 1473838225.f, 60000, 29.457, new Color(248, 243, 172)),
  155.         Uran(Słońce, 2872428721.f, 25362, 84.011, new Color(151, 188, 208)),
  156.         Neptun(Słońce, 4494967229.f, 24662, 164.69, new Color(97, 138, 211)),
  157.         Ceres(Słońce, 414082773.f, 945, 4.6054, new Color(158, 151, 156)),
  158.         Pluto(Słońce, 5906423142.f, 2370, 247.6, new Color(165, 126, 103)),
  159.         Haumea(Słońce, 6483571729.f, 1379, 285.3, new Color(203, 203, 203)),
  160.         Eris(Słońce, 10120295973.f, 2326, 556.4, new Color(214, 212, 225)),
  161.         Makemake(Słońce, 6845598576.f, 1500, 309.57, new Color(100, 50, 40)),
  162.         Titan(Saturn, 1221870.f, 2575, 0.0438, new Color(220, 220, 110)),
  163.         Io(Jowisz, 421700.f, 1821, 0.00466, new Color(220, 220, 110)),
  164.         Ganymede(Jowisz, 1070400.f, 2634, 0.0192, Color.LIGHT_GRAY),
  165.         Callisto(Jowisz, 1882700.f, 2410, 0.0457, Color.LIGHT_GRAY);
  166.  
  167.         Point position;
  168.         boolean stationary;
  169.         Cialo center;
  170.         double orbit;
  171.         double radius;
  172.         double period;
  173.         Color color;
  174.  
  175.         Cialo(Cialo center, double orbit, double radius, double period, Color color) {
  176.             this.center = center;
  177.             this.position = new Point();//center.getX()+orbit, center.getY());
  178.             stationary = false;
  179.             this.color = color;
  180.             this.radius = Math.sqrt(radius) / 15;
  181.             //this.orbit = (Math.log10(orbit)-5.5)*100+this.radius;
  182.             this.orbit = (Math.sqrt(orbit) / 50 + (Math.log10(orbit) - 5) * 50) * 0.195 + center.radius;
  183.             this.period = period * 365.256363004;
  184.         }
  185.  
  186.         Cialo(Point position, double radius, Color color) {
  187.             this.position = position;
  188.             this.stationary = true;
  189.             this.color = color;
  190.             this.radius = Math.sqrt(radius) / 15;
  191.         }
  192.  
  193.         void calcPosition(double angle) {
  194.             if (stationary) return;
  195.             angle = angle / period;
  196.             position.setLocation(center.getX() + orbit * Math.sin(angle),
  197.                     center.getY() + orbit * Math.cos(angle));
  198.         }
  199.  
  200.         public int getX() {
  201.             return (int) position.getX();
  202.         }
  203.  
  204.         public int getY() {
  205.             return (int) position.getY();
  206.         }
  207.     }
  208. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement