Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ukladSloneczny;
- import java.awt.image.BufferedImage;
- public class Planeta
- {
- String nazwa;
- Planeta centrumObiegu;
- int szerokosc;
- double r;
- double omega;
- double masa;
- int x,y;
- int paintX,paintY;
- double fi;
- BufferedImage image;
- public Planeta(String name, Planeta center, double orbit,int radius, double omega, double mass,double f,BufferedImage img)
- {
- this.nazwa=name;
- this.centrumObiegu=center;
- this.r=orbit;
- this.szerokosc=radius;
- this.omega=omega;
- this.masa=mass;
- this.fi=f;
- this.image=img;
- }
- public void setLocation()
- {
- if(centrumObiegu==null)
- return;
- x=(int) (r*Math.sin(omega*MyFrame.currentTime+fi)+centrumObiegu.x);
- y=(int) (r*Math.cos(omega*MyFrame.currentTime+fi)+centrumObiegu.y);
- paintX=x-szerokosc;
- paintY=y-szerokosc;
- if(nazwa=="Saturn")
- {
- paintX=x-72;
- paintY=y-30;
- }
- }
- public void setXY(int iks,int igrek)
- {
- this.x=iks;
- this.y=igrek;
- this.paintX=this.x-szerokosc;
- this.paintY=this.y-szerokosc;
- }
- }
- package ukladSloneczny;
- import java.awt.BorderLayout;
- import java.awt.Dimension;
- import java.awt.GridLayout;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.image.BufferedImage;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.util.Timer;
- import javax.imageio.ImageIO;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JToolBar;
- public class MyFrame extends JFrame
- {
- JLabel label = new JLabel("1s = 2 dni");
- JButton increase = new JButton("+");
- JButton decrease = new JButton("-");
- JToolBar toolBar = new JToolBar();
- private static final long serialVersionUID = 1L;
- public static double currentTime=0;
- int width=1400,height=1050;
- private BufferedImage background;
- public static KosmosPanel kosmosPanel;
- public static Planeta[] planets= new Planeta[20];
- private Timer myTimer=new Timer();
- public static double dt= 0.003;
- int days=2;
- private void makePlanets()
- { //(String name, Planeta center, double orbit,int radius, double omega, double mass,double f,BufferedImage img)
- BufferedImage img=null;
- try { img = ImageIO.read(new File("C:\\Users\\kucha\\eclipse-workspace\\zad12\\src\\ukladSloneczny\\sun2.png"));
- } catch (IOException e) {System.out.println("Nie ualo sie zalaowac grafiki");}
- planets[0] = new Planeta("Słońce",null,0,37,0,332950,0,img);
- planets[0].setXY(width/2, height/2);
- try { img = ImageIO.read(new File("C:\\Users\\kucha\\eclipse-workspace\\zad12\\src\\ukladSloneczny\\mercury2.png"));
- } catch (IOException e) {System.out.println("Nie ualo sie zalaowac grafiki");}
- planets[1] = new Planeta("Merkury",planets[0],57,8,4.2,1,5,img);
- try { img = ImageIO.read(new File("C:\\Users\\kucha\\eclipse-workspace\\zad12\\src\\ukladSloneczny\\venus2.png"));
- } catch (IOException e) {System.out.println("Nie ualo sie zalaowac grafiki");}
- planets[2] = new Planeta("Wenus",planets[0],95,12,1.64,1,0,img);
- try { img = ImageIO.read(new File("C:\\Users\\kucha\\eclipse-workspace\\zad12\\src\\ukladSloneczny\\earth2.png"));
- } catch (IOException e) {System.out.println("Nie ualo sie zalaowac grafiki");}
- planets[3] = new Planeta("Ziemia",planets[0],138,13,0.53,1,2,img);
- try { img = ImageIO.read(new File("C:\\Users\\kucha\\eclipse-workspace\\zad12\\src\\ukladSloneczny\\mars2.png"));
- } catch (IOException e) {System.out.println("Nie ualo sie zalaowac grafiki");}
- planets[4] = new Planeta("Mars",planets[0],182,10,0.3,1,9,img);
- try { img = ImageIO.read(new File("C:\\Users\\kucha\\eclipse-workspace\\zad12\\src\\ukladSloneczny\\jowisz2.png"));
- } catch (IOException e) {System.out.println("Nie ualo sie zalaowac grafiki");}
- planets[5] = new Planeta("Jowisz",planets[0],283,30,0.084,1,1,img);
- try { img = ImageIO.read(new File("C:\\Users\\kucha\\eclipse-workspace\\zad12\\src\\ukladSloneczny\\saturn2.png"));
- } catch (IOException e) {System.out.println("Nie ualo sie zalaowac grafiki");}
- planets[6] = new Planeta("Saturn",planets[0],346,24,0.034,1,0,img);
- try { img = ImageIO.read(new File("C:\\Users\\kucha\\eclipse-workspace\\zad12\\src\\ukladSloneczny\\uran2.png"));
- } catch (IOException e) {System.out.println("Nie ualo sie zalaowac grafiki");}
- planets[7] = new Planeta("Uran",planets[0],401,20,0.011,1,5,img);
- try { img = ImageIO.read(new File("C:\\Users\\kucha\\eclipse-workspace\\zad12\\src\\ukladSloneczny\\neptun2.png"));
- } catch (IOException e) {System.out.println("Nie ualo sie zalaowac grafiki");}
- planets[8] = new Planeta("Neptun",planets[0],448,20,0.006,1,4,img);
- try { img = ImageIO.read(new File("C:\\Users\\kucha\\eclipse-workspace\\zad12\\src\\ukladSloneczny\\pluton2.png"));
- } catch (IOException e) {System.out.println("Nie ualo sie zalaowac grafiki");}
- planets[9] = new Planeta("Pluton",planets[0],476,8,0.004,1,2,img);
- try { img = ImageIO.read(new File("C:\\Users\\kucha\\eclipse-workspace\\zad12\\src\\ukladSloneczny\\moon2.png"));
- } catch (IOException e) {System.out.println("Nie ualo sie zalaowac grafiki");}
- planets[10] = new Planeta("Księżyc",planets[3],22,5,12,1,0,img);
- try { img = ImageIO.read(new File("C:\\Users\\kucha\\eclipse-workspace\\zad12\\src\\ukladSloneczny\\haumea2.png"));
- } catch (IOException e) {System.out.println("Nie ualo sie zalaowac grafiki");}
- planets[11] = new Planeta("Haumea",planets[0],485,4,0.0039,0.003,7,img);
- try { img = ImageIO.read(new File("C:\\Users\\kucha\\eclipse-workspace\\zad12\\src\\ukladSloneczny\\makemake2.png"));
- } catch (IOException e) {System.out.println("Nie ualo sie zalaowac grafiki");}
- planets[12] = new Planeta("Makemake",planets[0],495,5,0.003,0.003,9,img);
- try { img = ImageIO.read(new File("C:\\Users\\kucha\\eclipse-workspace\\zad12\\src\\ukladSloneczny\\eris2.png"));
- } catch (IOException e) {System.out.println("Nie ualo sie zalaowac grafiki");}
- planets[13] = new Planeta("Eris",planets[0],512,9,0.0017,0.003,3,img);
- try { img = ImageIO.read(new File("C:\\Users\\kucha\\eclipse-workspace\\zad12\\src\\ukladSloneczny\\ceres2.png"));
- } catch (IOException e) {System.out.println("Nie ualo sie zalaowac grafiki");}
- planets[14] = new Planeta("Ceres",planets[0],227,3,0.2,0.2,4,img);
- try { img = ImageIO.read(new File("C:\\Users\\kucha\\eclipse-workspace\\zad12\\src\\ukladSloneczny\\fobdeim.png"));
- } catch (IOException e) {System.out.println("Nie ualo sie zalaowac grafiki");}
- planets[15] = new Planeta("Fobos",planets[4],12,1,200,0.2,0,img);
- try { img = ImageIO.read(new File("C:\\Users\\kucha\\eclipse-workspace\\zad12\\src\\ukladSloneczny\\fobdeim.png"));
- } catch (IOException e) {System.out.println("Nie ualo sie zalaowac grafiki");}
- planets[16] = new Planeta("Deimos",planets[4],14,1,60,0.2,0,img);
- }
- public MyFrame()
- {
- super("Układ Słoneczny");
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setVisible(true);
- setSize(new Dimension(width+70, height));
- setLocation(150, 10);
- setResizable(false);
- toolBar.setLayout(new GridLayout(3, 1));
- toolBar.add(increase);
- toolBar.add(decrease);
- toolBar.add(label);
- add(toolBar,BorderLayout.EAST);
- background = null;
- try {
- background = ImageIO.read(new File("C:\\Users\\kucha\\eclipse-workspace\\zad12\\src\\ukladSloneczny\\background.png"));
- } catch (IOException e) {System.out.println("Nie ualo sie zalaowac grafiki");
- }
- kosmosPanel= new KosmosPanel(background);
- makePlanets();
- add(kosmosPanel);
- myTimer.schedule( new MyTimerTask(),0,40);
- increase.addActionListener(new ActionListener()
- {
- @Override
- public void actionPerformed(ActionEvent e)
- {
- if(days<Integer.MAX_VALUE/2)
- {
- dt=dt*2;
- days=days*2;
- label.setText("1s = "+Integer.toString(days)+" dni");
- }
- }
- });
- decrease.addActionListener(new ActionListener()
- {
- @Override
- public void actionPerformed(ActionEvent e)
- {
- if(days>2)
- {
- dt=dt/2;
- days=days/2;
- label.setText("1s = "+Integer.toString(days)+" dni");
- }
- else if(days==2)
- {
- dt=dt/2;
- days=days/2;
- label.setText("1s = "+Integer.toString(days)+" dzień");
- }
- }
- });
- repaint();
- }
- }
- package ukladSloneczny;
- import java.awt.Dimension;
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import java.awt.image.BufferedImage;
- import javax.swing.JPanel;
- public class KosmosPanel extends JPanel
- {
- private static final long serialVersionUID = 1L;
- private BufferedImage background;
- public KosmosPanel(BufferedImage img)
- {
- super();
- background=img;
- this.setSize(new Dimension(1400,1050));
- }
- public void paintComponent(Graphics g)
- {
- Graphics2D g2d = (Graphics2D) g;
- g2d.drawImage(background, 0, 0, this);
- for(int i=0;i<17;i++)
- {
- g2d.drawImage(MyFrame.planets[i].image, MyFrame.planets[i].paintX, MyFrame.planets[i].paintY, this);
- }
- }
- }
- package ukladSloneczny;
- import java.util.TimerTask;
- public class MyTimerTask extends TimerTask
- {
- @Override
- public void run()
- {
- MyFrame.currentTime+=MyFrame.dt;
- for(int i=0;i<17;i++)
- {
- MyFrame.planets[i].setLocation();
- }
- MyFrame.kosmosPanel.repaint();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement