Advertisement
dgulczynski

Lab12

Jan 21st, 2018
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.24 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 week = new JButton("1s = 1 tydzien");
  36.                 JButton month = new JButton("1s = 1 miesiąc");
  37.                 JButton year = new JButton("1s = 1 rok");
  38.                 JButton slower = new JButton("2x wolniej");
  39.                 JButton faster = new JButton("2x szybciej");
  40.                 JButton pause = new JButton("pauza");
  41.                 day.addActionListener(e -> panelSloneczny.speed = 1);
  42.                 week.addActionListener(e -> panelSloneczny.speed = 7);
  43.                 month.addActionListener(e -> panelSloneczny.speed = 30);
  44.                 year.addActionListener(e -> panelSloneczny.speed = 365);
  45.                 slower.addActionListener(e -> panelSloneczny.speed *= 0.5);
  46.                 faster.addActionListener(e -> panelSloneczny.speed *= 2);
  47.                 pause.addActionListener(e -> panelSloneczny.pause = !panelSloneczny.pause);
  48.                 toolBar.add(day);
  49.                 toolBar.add(week);
  50.                 toolBar.add(month);
  51.                 toolBar.add(year);
  52.                 toolBar.add(slower);
  53.                 toolBar.add(faster);
  54.                 toolBar.add(pause);
  55.                
  56.                 frame.add(toolBar, BorderLayout.SOUTH);
  57.  
  58.                 frame.pack();
  59.                 frame.setResizable(false);
  60.                 frame.setVisible(true);
  61.             }
  62.         });
  63.     }
  64.  
  65.     public class PanelSloneczny extends JPanel {
  66.  
  67.         private BufferedImage currentFrame;
  68.         private int frame;
  69.         ArrayList<Cialo> list = new ArrayList<Cialo>();
  70.         BufferedImage background = null;
  71.         double angle = 0;
  72.         double speed = 1;
  73.         double way = -1;
  74.         boolean pause = true;
  75.  
  76.         public PanelSloneczny() {
  77.             try {
  78.                 background = ImageIO.read(new File("E:\\Media\\Documents\\java\\LAB12\\graphics\\starry_sky_stock_by_budgie.jpg"));
  79.             } catch (IOException e) {
  80.                 e.printStackTrace();
  81.             }
  82.  
  83.             list.add(Cialo.Sun);
  84.             list.add(Cialo.Mercury);
  85.             list.add(Cialo.Venus);
  86.             list.add(Cialo.Earth);
  87.             list.add(Cialo.Moon);
  88.             list.add(Cialo.Mars);
  89.             list.add(Cialo.Eris);
  90.             list.add(Cialo.Jupiter);
  91.             list.add(Cialo.Saturn);
  92.             list.add(Cialo.Uran);
  93.             list.add(Cialo.Neptun);
  94.             list.add(Cialo.Pluto);
  95.             list.add(Cialo.Ceres);
  96.  
  97.             Timer timer = new Timer(16, new ActionListener() {
  98.                 @Override
  99.                 public void actionPerformed(ActionEvent e) {
  100.                     repaint();
  101.                 }
  102.             });
  103.             timer.start();
  104.         }
  105.  
  106.         @Override
  107.         public Dimension getPreferredSize() {
  108.             return new Dimension(1000, 1000);
  109.         }
  110.  
  111.         @Override
  112.         protected void paintComponent(Graphics g) {
  113.             super.paintComponent(g);
  114.             if (pause)
  115.                 angle += Math.PI * 2 * way * speed * 0.016;
  116.             if (background != null) {
  117.                 Graphics2D g2d = (Graphics2D) g.create();
  118.                 g2d.drawImage(background, 0, 0, this);
  119.                 g2d.setPaint(Color.GRAY);
  120.                 for (int i = 1; i < list.size(); i++) {
  121.                     list.get(i).calcPosition(angle);
  122.                     g2d.drawOval(list.get(i).center.getX() - (int) list.get(i).orbit, list.get(i).center.getY() - (int) list.get(i).orbit,
  123.                             (int) list.get(i).orbit * 2, (int) list.get(i).orbit * 2);
  124.  
  125.                 }
  126.  
  127.                 for (int i = 0; i < list.size(); i++) {
  128.                     g2d.fillOval(list.get(i).getX() - (int) list.get(i).radius, list.get(i).getY() - (int) list.get(i).radius,
  129.                             (int) list.get(i).radius * 2, (int) list.get(i).radius * 2);
  130.                     g2d.setPaint(Color.LIGHT_GRAY);
  131.                     g2d.drawString(list.get(i).toString(), list.get(i).getX(), list.get(i).getY());
  132.                 }
  133.                 g2d.dispose();
  134.             }
  135.  
  136.         }
  137.  
  138.     }
  139.  
  140.     public enum Cialo {
  141.         Sun(new Point(500, 500), 696342),
  142.         Mercury(Sun, 57909036.f, 2439, 0.241),
  143.         Venus(Sun, 108208925.f, 6051, 0.615),
  144.         Earth(Sun, 149597871.f, 6371, 1),
  145.         Moon(Earth, 384400.f, 1736, 0.07480132796),
  146.         Mars(Sun, 227936637.f, 3389, 1.881),
  147.         Jupiter(Sun, 778412028.f, 70000, 11.862),
  148.         Saturn(Sun, 1473838225.f, 60000, 29.457),
  149.         Uran(Sun, 2872428721.f, 25362, 84.011),
  150.         Neptun(Sun, 4494967229.f, 24662, 164.69),
  151.         Ceres(Sun, 414082773.f, 945, 4.605479452),
  152.         Pluto(Sun, 5906423142.f, 2370, 247.6),
  153.         Haumea(Sun, 6483571729.f, 1379, 285.3),
  154.         Eris(Sun, 10120295973.f, 2326, 556.4),
  155.         Makemake(Sun, 6845598576.f, 1500, 309.57);
  156.  
  157.         Point position;
  158.         boolean stationary;
  159.         Cialo center;
  160.         double orbit;
  161.         double radius;
  162.         double period;
  163.        
  164.         Cialo(Cialo center, double orbit, double radius, double period) {
  165.             this.center = center;
  166.             this.position = new Point();//center.getX()+orbit, center.getY());
  167.             stationary = false;
  168.             this.orbit = Math.log10(orbit)*30;
  169.             this.radius = Math.sqrt(radius) / 5;
  170.             this.period = period * 365.256363004;
  171.             System.out.println(this.orbit);
  172.         }
  173.  
  174.         Cialo(Point position, double radius) {
  175.             this.position = position;
  176.             this.stationary = true;
  177.             this.radius = Math.sqrt(radius) / 20;
  178.         }
  179.  
  180.         void calcPosition(double angle) {
  181.             if (stationary) return;
  182.                 angle = angle / period;
  183.                 position.setLocation(center.getX() + orbit * Math.sin(angle),
  184.                         center.getY() + orbit * Math.cos(angle));
  185.         }
  186.  
  187.         public int getX() {
  188.             return (int) position.getX();
  189.         }
  190.  
  191.         public int getY() {
  192.             return (int) position.getY();
  193.         }
  194.     }
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement