Advertisement
Guest User

Untitled

a guest
Jan 21st, 2018
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.70 KB | None | 0 0
  1. package ukladSloneczny;
  2.  
  3. import java.awt.image.BufferedImage;
  4.  
  5. public class Planeta
  6. {
  7. String nazwa;
  8. Planeta centrumObiegu;
  9. int szerokosc;
  10. double r;
  11. double omega;
  12. double masa;
  13. int x,y;
  14. int paintX,paintY;
  15. double fi;
  16. BufferedImage image;
  17. public Planeta(String name, Planeta center, double orbit,int radius, double omega, double mass,double f,BufferedImage img)
  18. {
  19. this.nazwa=name;
  20. this.centrumObiegu=center;
  21. this.r=orbit;
  22. this.szerokosc=radius;
  23. this.omega=omega;
  24. this.masa=mass;
  25. this.fi=f;
  26. this.image=img;
  27. }
  28. public void setLocation()
  29. {
  30. if(centrumObiegu==null)
  31. return;
  32. x=(int) (r*Math.sin(omega*MyFrame.currentTime+fi)+centrumObiegu.x);
  33. y=(int) (r*Math.cos(omega*MyFrame.currentTime+fi)+centrumObiegu.y);
  34. paintX=x-szerokosc;
  35. paintY=y-szerokosc;
  36. if(nazwa=="Saturn")
  37. {
  38. paintX=x-72;
  39. paintY=y-30;
  40. }
  41. }
  42. public void setXY(int iks,int igrek)
  43. {
  44. this.x=iks;
  45. this.y=igrek;
  46. this.paintX=this.x-szerokosc;
  47. this.paintY=this.y-szerokosc;
  48. }
  49. }
  50. package ukladSloneczny;
  51.  
  52. import java.awt.BorderLayout;
  53. import java.awt.Dimension;
  54. import java.awt.GridLayout;
  55. import java.awt.event.ActionEvent;
  56. import java.awt.event.ActionListener;
  57. import java.awt.image.BufferedImage;
  58. import java.io.File;
  59. import java.io.FileInputStream;
  60. import java.io.FileNotFoundException;
  61. import java.io.FileOutputStream;
  62. import java.io.IOException;
  63. import java.util.Timer;
  64.  
  65. import javax.imageio.ImageIO;
  66. import javax.swing.JButton;
  67. import javax.swing.JFrame;
  68. import javax.swing.JLabel;
  69. import javax.swing.JToolBar;
  70.  
  71.  
  72. public class MyFrame extends JFrame
  73. {
  74. JLabel label = new JLabel("1s = 2 dni");
  75. JButton increase = new JButton("+");
  76. JButton decrease = new JButton("-");
  77. JToolBar toolBar = new JToolBar();
  78. private static final long serialVersionUID = 1L;
  79. public static double currentTime=0;
  80. int width=1400,height=1050;
  81. private BufferedImage background;
  82. public static KosmosPanel kosmosPanel;
  83. public static Planeta[] planets= new Planeta[20];
  84. private Timer myTimer=new Timer();
  85. public static double dt= 0.003;
  86. int days=2;
  87. private void makePlanets()
  88. { //(String name, Planeta center, double orbit,int radius, double omega, double mass,double f,BufferedImage img)
  89.  
  90. BufferedImage img=null;
  91. try { img = ImageIO.read(new File("C:\\Users\\kucha\\eclipse-workspace\\zad12\\src\\ukladSloneczny\\sun2.png"));
  92. } catch (IOException e) {System.out.println("Nie ualo sie zalaowac grafiki");}
  93. planets[0] = new Planeta("Słońce",null,0,37,0,332950,0,img);
  94. planets[0].setXY(width/2, height/2);
  95. try { img = ImageIO.read(new File("C:\\Users\\kucha\\eclipse-workspace\\zad12\\src\\ukladSloneczny\\mercury2.png"));
  96. } catch (IOException e) {System.out.println("Nie ualo sie zalaowac grafiki");}
  97. planets[1] = new Planeta("Merkury",planets[0],57,8,4.2,1,5,img);
  98. try { img = ImageIO.read(new File("C:\\Users\\kucha\\eclipse-workspace\\zad12\\src\\ukladSloneczny\\venus2.png"));
  99. } catch (IOException e) {System.out.println("Nie ualo sie zalaowac grafiki");}
  100. planets[2] = new Planeta("Wenus",planets[0],95,12,1.64,1,0,img);
  101. try { img = ImageIO.read(new File("C:\\Users\\kucha\\eclipse-workspace\\zad12\\src\\ukladSloneczny\\earth2.png"));
  102. } catch (IOException e) {System.out.println("Nie ualo sie zalaowac grafiki");}
  103. planets[3] = new Planeta("Ziemia",planets[0],138,13,0.53,1,2,img);
  104. try { img = ImageIO.read(new File("C:\\Users\\kucha\\eclipse-workspace\\zad12\\src\\ukladSloneczny\\mars2.png"));
  105. } catch (IOException e) {System.out.println("Nie ualo sie zalaowac grafiki");}
  106. planets[4] = new Planeta("Mars",planets[0],182,10,0.3,1,9,img);
  107. try { img = ImageIO.read(new File("C:\\Users\\kucha\\eclipse-workspace\\zad12\\src\\ukladSloneczny\\jowisz2.png"));
  108. } catch (IOException e) {System.out.println("Nie ualo sie zalaowac grafiki");}
  109. planets[5] = new Planeta("Jowisz",planets[0],283,30,0.084,1,1,img);
  110. try { img = ImageIO.read(new File("C:\\Users\\kucha\\eclipse-workspace\\zad12\\src\\ukladSloneczny\\saturn2.png"));
  111. } catch (IOException e) {System.out.println("Nie ualo sie zalaowac grafiki");}
  112. planets[6] = new Planeta("Saturn",planets[0],346,24,0.034,1,0,img);
  113. try { img = ImageIO.read(new File("C:\\Users\\kucha\\eclipse-workspace\\zad12\\src\\ukladSloneczny\\uran2.png"));
  114. } catch (IOException e) {System.out.println("Nie ualo sie zalaowac grafiki");}
  115. planets[7] = new Planeta("Uran",planets[0],401,20,0.011,1,5,img);
  116. try { img = ImageIO.read(new File("C:\\Users\\kucha\\eclipse-workspace\\zad12\\src\\ukladSloneczny\\neptun2.png"));
  117. } catch (IOException e) {System.out.println("Nie ualo sie zalaowac grafiki");}
  118. planets[8] = new Planeta("Neptun",planets[0],448,20,0.006,1,4,img);
  119. try { img = ImageIO.read(new File("C:\\Users\\kucha\\eclipse-workspace\\zad12\\src\\ukladSloneczny\\pluton2.png"));
  120. } catch (IOException e) {System.out.println("Nie ualo sie zalaowac grafiki");}
  121. planets[9] = new Planeta("Pluton",planets[0],476,8,0.004,1,2,img);
  122. try { img = ImageIO.read(new File("C:\\Users\\kucha\\eclipse-workspace\\zad12\\src\\ukladSloneczny\\moon2.png"));
  123. } catch (IOException e) {System.out.println("Nie ualo sie zalaowac grafiki");}
  124. planets[10] = new Planeta("Księżyc",planets[3],22,5,12,1,0,img);
  125. try { img = ImageIO.read(new File("C:\\Users\\kucha\\eclipse-workspace\\zad12\\src\\ukladSloneczny\\haumea2.png"));
  126. } catch (IOException e) {System.out.println("Nie ualo sie zalaowac grafiki");}
  127. planets[11] = new Planeta("Haumea",planets[0],485,4,0.0039,0.003,7,img);
  128. try { img = ImageIO.read(new File("C:\\Users\\kucha\\eclipse-workspace\\zad12\\src\\ukladSloneczny\\makemake2.png"));
  129. } catch (IOException e) {System.out.println("Nie ualo sie zalaowac grafiki");}
  130. planets[12] = new Planeta("Makemake",planets[0],495,5,0.003,0.003,9,img);
  131. try { img = ImageIO.read(new File("C:\\Users\\kucha\\eclipse-workspace\\zad12\\src\\ukladSloneczny\\eris2.png"));
  132. } catch (IOException e) {System.out.println("Nie ualo sie zalaowac grafiki");}
  133. planets[13] = new Planeta("Eris",planets[0],512,9,0.0017,0.003,3,img);
  134. try { img = ImageIO.read(new File("C:\\Users\\kucha\\eclipse-workspace\\zad12\\src\\ukladSloneczny\\ceres2.png"));
  135. } catch (IOException e) {System.out.println("Nie ualo sie zalaowac grafiki");}
  136. planets[14] = new Planeta("Ceres",planets[0],227,3,0.2,0.2,4,img);
  137. try { img = ImageIO.read(new File("C:\\Users\\kucha\\eclipse-workspace\\zad12\\src\\ukladSloneczny\\fobdeim.png"));
  138. } catch (IOException e) {System.out.println("Nie ualo sie zalaowac grafiki");}
  139. planets[15] = new Planeta("Fobos",planets[4],12,1,200,0.2,0,img);
  140. try { img = ImageIO.read(new File("C:\\Users\\kucha\\eclipse-workspace\\zad12\\src\\ukladSloneczny\\fobdeim.png"));
  141. } catch (IOException e) {System.out.println("Nie ualo sie zalaowac grafiki");}
  142. planets[16] = new Planeta("Deimos",planets[4],14,1,60,0.2,0,img);
  143.  
  144. }
  145. public MyFrame()
  146. {
  147. super("Układ Słoneczny");
  148. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  149. setVisible(true);
  150. setSize(new Dimension(width+70, height));
  151. setLocation(150, 10);
  152. setResizable(false);
  153.  
  154. toolBar.setLayout(new GridLayout(3, 1));
  155. toolBar.add(increase);
  156. toolBar.add(decrease);
  157. toolBar.add(label);
  158. add(toolBar,BorderLayout.EAST);
  159. background = null;
  160. try {
  161. background = ImageIO.read(new File("C:\\Users\\kucha\\eclipse-workspace\\zad12\\src\\ukladSloneczny\\background.png"));
  162. } catch (IOException e) {System.out.println("Nie ualo sie zalaowac grafiki");
  163. }
  164. kosmosPanel= new KosmosPanel(background);
  165. makePlanets();
  166. add(kosmosPanel);
  167. myTimer.schedule( new MyTimerTask(),0,40);
  168. increase.addActionListener(new ActionListener()
  169. {
  170. @Override
  171. public void actionPerformed(ActionEvent e)
  172. {
  173. if(days<Integer.MAX_VALUE/2)
  174. {
  175. dt=dt*2;
  176. days=days*2;
  177. label.setText("1s = "+Integer.toString(days)+" dni");
  178. }
  179. }
  180. });
  181. decrease.addActionListener(new ActionListener()
  182. {
  183. @Override
  184. public void actionPerformed(ActionEvent e)
  185. {
  186. if(days>2)
  187. {
  188. dt=dt/2;
  189. days=days/2;
  190. label.setText("1s = "+Integer.toString(days)+" dni");
  191. }
  192. else if(days==2)
  193. {
  194. dt=dt/2;
  195. days=days/2;
  196. label.setText("1s = "+Integer.toString(days)+" dzień");
  197. }
  198. }
  199. });
  200. repaint();
  201. }
  202. }
  203. package ukladSloneczny;
  204.  
  205. import java.awt.Dimension;
  206. import java.awt.Graphics;
  207. import java.awt.Graphics2D;
  208. import java.awt.image.BufferedImage;
  209. import javax.swing.JPanel;
  210.  
  211. public class KosmosPanel extends JPanel
  212. {
  213. private static final long serialVersionUID = 1L;
  214. private BufferedImage background;
  215. public KosmosPanel(BufferedImage img)
  216. {
  217. super();
  218. background=img;
  219. this.setSize(new Dimension(1400,1050));
  220. }
  221. public void paintComponent(Graphics g)
  222. {
  223. Graphics2D g2d = (Graphics2D) g;
  224. g2d.drawImage(background, 0, 0, this);
  225. for(int i=0;i<17;i++)
  226. {
  227. g2d.drawImage(MyFrame.planets[i].image, MyFrame.planets[i].paintX, MyFrame.planets[i].paintY, this);
  228. }
  229. }
  230. }
  231. package ukladSloneczny;
  232.  
  233. import java.util.TimerTask;
  234.  
  235. public class MyTimerTask extends TimerTask
  236. {
  237. @Override
  238. public void run()
  239. {
  240. MyFrame.currentTime+=MyFrame.dt;
  241. for(int i=0;i<17;i++)
  242. {
  243. MyFrame.planets[i].setLocation();
  244. }
  245. MyFrame.kosmosPanel.repaint();
  246. }
  247.  
  248. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement