Advertisement
Guest User

Jeronimus

a guest
May 29th, 2016
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 15.51 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.util.ArrayList;
  3. import java.awt.*;
  4. import java.awt.image.BufferStrategy;
  5. import static java.lang.Math.*;
  6. import java.awt.event.*;
  7. import java.awt.image.BufferedImage;
  8.  
  9. class Spacetime {
  10.  
  11.   public static void main(String[] args) {
  12.    
  13.     MainClass mainClass = new MainClass();
  14.     mainClass.start();
  15.    
  16.   }
  17. }
  18.  
  19.  
  20. class MainClass {
  21.  
  22.   public MainClass() {
  23.    
  24.   }  
  25.  
  26.   public void start(){
  27.    
  28.     createFrameAndCanvases();
  29.     generateEvents();
  30.     generateEvents_p();
  31.    
  32.     render();
  33.     jFrame.paintComponents(g2DjFrame);
  34.     jFrame.repaint();
  35.    
  36.     mainLoop();
  37.   }
  38.  
  39.  
  40.   public void mainLoop() {
  41.    
  42.    
  43.     do{
  44.       updateState();
  45.       render();
  46.       try{Thread.sleep(20);} catch (Exception e){};
  47.     } while(true);
  48.   }
  49.  
  50.   int stage=1;
  51.   boolean deletePaths=false;
  52.  
  53.   public void updateState(){
  54.    
  55.     here:
  56.      
  57.       if(!lock) {
  58.      
  59.       if (deletePaths==true){
  60.         g2Dimg.setBackground(new Color(0,0,0,0));
  61.         g2Dimg.clearRect(0,0,501,501);
  62.         deletePaths=false;
  63.         if (stage<6){stage+=1;}
  64.       }
  65.      
  66.       if (stage==1){
  67.        
  68.         if ((round(v_rel*10000)/10000.0)<0.5) { accelerate();
  69.         } else {
  70.           lock=true;
  71.           deletePaths=true;
  72.           break here;
  73.         }
  74.       }
  75.      
  76.       if (stage==2) {
  77.        
  78.         if ((round(timePassed*10000)/10000.0)<10){letSomeTimePass();
  79.         } else {
  80.           turnAroundTime = timePassed;
  81.           turnAroundPosition = v_rel*timePassed;
  82.           lock=true;
  83.           deletePaths=true;
  84.           break here;
  85.         }
  86.       }
  87.      
  88.       if (stage==3) {
  89.        
  90.         if ((round(v_rel*10000)/10000.0)>0) {deccelerate();
  91.         } else {
  92.           lock=true;
  93.           deletePaths=true;
  94.           break here;
  95.         }
  96.       }
  97.      
  98.      
  99.       if (stage==4) {
  100.        
  101.         if ((round(v_rel*10000)/10000.0)>-0.5) {deccelerate();
  102.         } else {
  103.           lock=true;
  104.           deletePaths=true;
  105.           break here;
  106.         }
  107.       }
  108.      
  109.      
  110.       if (stage==5) {
  111.        
  112.         if((round(timePassed*10000)/10000.0)<20) {letSomeTimePass();
  113.         } else {
  114.           lock=true;
  115.           deletePaths=true;
  116.           break here;
  117.         }
  118.       }
  119.      
  120.       if (stage==6) {
  121.        
  122.         if ((round(v_rel*10000)/10000.0)<0) {accelerate();
  123.         } else {
  124.           lock=true;
  125.           deletePaths=true;
  126.           break here;
  127.         }
  128.       }
  129.     }
  130.   }
  131.  
  132.  
  133.   public void accelerate() {
  134.     v_rel+=0.002;
  135.   }
  136.  
  137.   public void deccelerate() {
  138.     v_rel-=0.002;
  139.   }
  140.  
  141.  
  142.   public void letSomeTimePass() {
  143.     timePassed +=0.02;
  144.   }
  145.  
  146.  
  147.   public void render(){
  148.    
  149.     g2Dcanvas1 = (Graphics2D) strategyCanvas1.getDrawGraphics();
  150.     g2Dcanvas2 = (Graphics2D) strategyCanvas2.getDrawGraphics();
  151.     g2DjFrame = (Graphics2D) strategyJFrame.getDrawGraphics();
  152.    
  153.     g2Dcanvas1.setColor(backgroundColor);
  154.     g2Dcanvas2.setColor(backgroundColor);
  155.     g2DjFrame.setColor(backgroundColor);
  156.    
  157.     g2Dcanvas1.fillRect(0,0,501,501);
  158.     g2Dcanvas2.fillRect(0,0,501,501);
  159.     g2DjFrame.fillRect(0,0,1280,720);
  160.    
  161.     g2Dcanvas1.setColor(Color.WHITE);
  162.     g2Dcanvas1.fillOval((int)(traveller_x*25-10+251),(int)(traveller_t*25-10+251),20,20);
  163.    
  164.     g2Dcanvas2.setColor(Color.BLUE);
  165.     g2Dcanvas2.fillOval((int)(not_traveller_x_p*25-10+251),(int)(not_traveller_t_p*25-10+251),20,20);
  166.    
  167.     g2Dcanvas1.setColor(Color.BLUE);
  168.     g2Dcanvas1.fillOval((int)(xPos*25-10+251),(int)(-tPos*25-10+251),20,20);
  169.    
  170.     if (showPaths) {
  171.       g2Dcanvas2.drawImage(bufferedImage,0,0,501,501,null);}
  172.    
  173.    
  174.     g2Dcanvas1.setStroke(new BasicStroke(2));
  175.     g2Dcanvas2.setStroke(new BasicStroke(2));
  176.     plotLightbeams();
  177.    
  178.     g2Dcanvas1.setStroke(new BasicStroke(1));
  179.     g2Dcanvas2.setStroke(new BasicStroke(1));
  180.     plotDiagrams();
  181.    
  182.     g2Dcanvas1.setStroke(new BasicStroke(3));
  183.     g2Dcanvas2.setStroke(new BasicStroke(3));
  184.     plotEvents();
  185.    
  186.    
  187.     jFrame.paintComponents(g2DjFrame);
  188.    
  189.     g2DjFrame.setColor(Color.BLACK);
  190.     g2DjFrame.setFont(stringFont3);
  191.    
  192.     g2DjFrame.drawString("Vrel "+(Double.toString(round(v_rel*10000)/10000.0)+"c"),610,100);
  193.     g2DjFrame.setColor(Color.BLACK);
  194.     g2DjFrame.drawString("stage "+stage,615,150);
  195.    
  196.     g2DjFrame.setColor(Color.BLUE);
  197.     g2DjFrame.drawString("Observer clock count "+(Double.toString(round(timePassed*10000)/10000.0))+"s",50,570);
  198.     g2DjFrame.drawString("x-Pos / t-Pos: 0ls / 0s",50,600);
  199.     g2DjFrame.setColor(Color.WHITE);
  200.     g2DjFrame.drawString("White clock count "+(Double.toString(round(timePassed_p*10000)/10000.0))+"s",330,570);
  201.     g2DjFrame.drawString("x Position "+(Double.toString(round(traveller_x*10000)/10000.0))+"ls",330,600);
  202.     g2DjFrame.drawString("t Position 0s",330,620);
  203.    
  204.     g2DjFrame.setColor(Color.WHITE);;
  205.     g2DjFrame.drawString("Observer clock count "+(Double.toString(round(timePassed_p*10000)/10000.0))+"s",700,570);
  206.     g2DjFrame.drawString("x-Pos / t-Pos: 0ls / 0s ",700,600);
  207.     g2DjFrame.setColor(Color.BLUE);
  208.     g2DjFrame.drawString("Blue clock count "+(Double.toString(round(clockTime*10000)/10000.0))+"s",1000,570);
  209.     g2DjFrame.drawString("x Position "+(Double.toString(round(not_traveller_x_p*10000)/10000.0))+"ls",1000,600);
  210.     g2DjFrame.drawString("t Position 0s",1000,620);
  211.    
  212.     g2DjFrame.drawString("Blue clock count "+(Double.toString(round(clockTime*10000)/10000.0))+"s",50,640);
  213.     g2DjFrame.drawString("x Position 0ls",50,670);
  214.     g2DjFrame.drawString("t Position "+(Double.toString(round(tPos*10000)/10000.0))+"s",50,690);
  215.    
  216.    
  217.     strategyCanvas2.show();
  218.     strategyCanvas1.show();
  219.     strategyJFrame.show();
  220.    
  221.     g2Dcanvas1.dispose();
  222.     g2Dcanvas2.dispose();
  223.     g2DjFrame.dispose();
  224.   }
  225.  
  226.  
  227.   public void plotLightbeams() {
  228.    
  229.     g2Dcanvas1.setColor(Color.YELLOW);
  230.     g2Dcanvas2.setColor(Color.YELLOW);
  231.    
  232.     g2Dcanvas1.drawLine(0,0,501,501);
  233.     g2Dcanvas1.drawLine(0,501,501,0);
  234.    
  235.     g2Dcanvas2.drawLine(0,0,501,501);
  236.     g2Dcanvas2.drawLine(0,501,501,0);
  237.   }
  238.  
  239.  
  240.   public void plotEvents( ){
  241.    
  242.     g2Dcanvas1.setFont(stringFont);
  243.     g2Dcanvas2.setFont(stringFont);
  244.    
  245.     max_gamma = 1/(sqrt(1-pow(0.5,2)));
  246.     temporary_gamma = 1/(sqrt(1-pow(v_rel,2)));
  247.    
  248.     timePassed_p = timePassed/max_gamma;
  249.    
  250.    
  251.     if (stage==1 || stage == 2 ) { traveller_x = v_rel*timePassed; clockTime = timePassed_p/max_gamma;}
  252.    
  253.     if (stage==3 || stage==4 || stage==5) { traveller_x = v_rel*(timePassed - turnAroundTime) + turnAroundPosition;}
  254.    
  255.     if (stage==2) { not_traveller_x_p = - v_rel*timePassed_p; rest_length =- v_rel*timePassed_p*max_gamma;  }
  256.    
  257.     if (stage==3 || stage==4) {
  258.      
  259.       not_traveller_x_p = rest_length/temporary_gamma;
  260.       turnAroundPosition_p = not_traveller_x_p;
  261.       turnAroundTime_p = timePassed_p;
  262.      
  263.     }
  264.    
  265.     if (stage==5) {      not_traveller_x_p = -v_rel*(timePassed_p - turnAroundTime_p) + turnAroundPosition_p;}
  266.    
  267.    
  268.     xPos = 0;
  269.     tPos = temporary_gamma*v_rel*not_traveller_x_p;
  270.    
  271.     clockTime = tPos + timePassed;
  272.    
  273.     for (int i=0; i<eventsArraylist.size();i++){
  274.      
  275.       x=eventsArraylist.get(i).xPosInLightSeconds;
  276.       t=eventsArraylist.get(i).timeInSeconds-timePassed;
  277.      
  278.       clockCounter = eventsArraylist.get(i).clockCounter;
  279.      
  280.      
  281.       x_p = temporary_gamma*(x-traveller_x - v_rel * t);  
  282.       t_p = temporary_gamma*(t - (v_rel*(x-traveller_x)));
  283.      
  284.       if ((x<11 && x >-11) && (t <11 && t>-11)){
  285.         g2Dcanvas1.setColor(Color.BLACK);  
  286.         if (x==5 || x==-5) { g2Dcanvas1.setColor(Color.GREEN);}
  287.         if (x==0 ) { g2Dcanvas1.setColor(Color.BLUE);}  
  288.        
  289.         g2Dcanvas1.drawString(Integer.toString((int)clockCounter),(int)(x*25+251-4),(int)(251+4-t*25));
  290.        
  291.         g2Dimg.setColor(Color.BLACK);
  292.         if (x==5 || x==-5) { g2Dcanvas2.setColor(Color.GREEN); g2Dimg.setColor(Color.GREEN);}
  293.         if (x==0 ) { g2Dcanvas2.setColor(Color.BLUE); g2Dimg.setColor(Color.BLUE);}
  294.        
  295.        
  296.         g2Dcanvas2.drawString(Integer.toString((int)clockCounter),(int)(x_p*25+251-4),(int)(251+4-t_p*25));
  297.         g2Dcanvas2.setColor(Color.BLACK);
  298.        
  299.         g2Dimg.setStroke(new BasicStroke(2));
  300.         g2Dimg.drawLine((int)(x_p*25+251),(int)(251-t_p*25),(int)(x_p*25+251),(int)(251-t_p*25));
  301.       }
  302.     }
  303.    
  304.     for (int i=0; i<events_pArraylist.size();i++){
  305.      
  306.       x=events_pArraylist.get(i).xPosInLightSeconds;
  307.       t=events_pArraylist.get(i).timeInSeconds-timePassed_p;
  308.      
  309.       if ((x<11 && x >-11) && (t <11 && t>-11) && (showWorldline==true)){
  310.         clockCounter = events_pArraylist.get(i).clockCounter;
  311.        
  312.         g2Dcanvas2.setColor(Color.WHITE);
  313.         g2Dcanvas2.drawString(Integer.toString((int)clockCounter),(int)(x*25+251-4),(int)(251+4-t*25));
  314.        
  315.         x_p = temporary_gamma*(x + v_rel * t)+traveller_x;  
  316.         t_p = temporary_gamma*(t + v_rel*x);
  317.        
  318.         g2Dcanvas1.setColor(Color.WHITE);
  319.         g2Dcanvas1.drawString(Integer.toString((int)clockCounter),(int)(x_p*25+251-4),(int)(251+4-t_p*25));
  320.       }
  321.     }
  322.   }
  323.  
  324.  
  325.   public void createFrameAndCanvases(){
  326.    
  327.     jFrame = new JFrame();
  328.     jFrame.setLayout(null);
  329.     jFrame.setSize(1280,720);
  330.     jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  331.     jFrame.getContentPane().setBackground(new Color(110,110,111));
  332.     jFrame.setResizable(true);
  333.     jFrame.setIgnoreRepaint(true);
  334.    
  335.     button1 = new JButton("start/stop");
  336.     button1.setBounds(500,580,100,100);
  337.     button1.addActionListener(actionListener);
  338.     button1.setActionCommand("start/stop");
  339.     jFrame.add(button1);
  340.    
  341.     button2 = new JButton("reset");
  342.     button2.setBounds(620,580,100,100);
  343.     button2.addActionListener(actionListener);
  344.     button2.setActionCommand("reset");
  345.     button2.setVisible(true);
  346.     jFrame.add(button2);
  347.    
  348.     canvas1 = new Canvas();
  349.     canvas1.setSize(501,501);
  350.     canvas1.setLocation(75,0);
  351.     canvas1.setIgnoreRepaint(true);
  352.     canvas1.setBackground(new Color(222,222,222));
  353.     jFrame.add(canvas1);
  354.    
  355.     canvas2 = new Canvas();
  356.     canvas2.setSize(501,501);
  357.     canvas2.setLocation(1280-501-75,0);
  358.     canvas2.setIgnoreRepaint(true);
  359.     canvas2.setBackground(new Color(222,222,222));
  360.     jFrame.add(canvas2);
  361.    
  362.     checkBox = new JCheckBox("show/hide paths",false);
  363.     checkBox.addActionListener(actionListener);
  364.     checkBox.setActionCommand("show/hide paths");
  365.     checkBox.setBounds(750,580,200,20);
  366.     jFrame.add(checkBox);
  367.    
  368.     checkBox2 = new JCheckBox("white clock (est.)worldline",true);
  369.     checkBox2.addActionListener(actionListener);
  370.     checkBox2.setActionCommand("white clock (est.)worldline");
  371.     checkBox2.setBounds(750,600,200,20);
  372.     jFrame.add(checkBox2);
  373.    
  374.    
  375.     jFrame.setVisible(true);
  376.    
  377.     jFrame.createBufferStrategy(2);
  378.     canvas1.createBufferStrategy(2);
  379.     canvas2.createBufferStrategy(2);
  380.    
  381.     strategyCanvas1 = canvas1.getBufferStrategy();
  382.     strategyCanvas2 = canvas2.getBufferStrategy();
  383.    
  384.     strategyJFrame = jFrame.getBufferStrategy();
  385.    
  386.     g2DjFrame = (Graphics2D) strategyJFrame.getDrawGraphics();
  387.    
  388.     g2Dcanvas1 = (Graphics2D) strategyCanvas1.getDrawGraphics();
  389.     g2Dcanvas2 = (Graphics2D) strategyCanvas2.getDrawGraphics();
  390.   }
  391.  
  392.  
  393.   public void plotDiagrams() {
  394.    
  395.     g2Dcanvas1.setFont(stringFont2);
  396.     g2Dcanvas2.setFont(stringFont2);
  397.    
  398.     g2Dcanvas1.setColor(Color.BLACK);
  399.     g2Dcanvas2.setColor(Color.BLACK);
  400.    
  401.     g2Dcanvas1.drawLine(0,250,500,250);
  402.     g2Dcanvas1.drawLine(250,0,250,500);
  403.    
  404.     g2Dcanvas2.drawLine(0,250,500,250);
  405.     g2Dcanvas2.drawLine(250,0,250,500);
  406.    
  407.     int x,t;
  408.     for (x=0; x<21;x++){
  409.       if (x!=10) {
  410.         g2Dcanvas1.drawLine(x*(500/20),245, x*(500/20),255);
  411.         g2Dcanvas1.drawString(Integer.toString(x-10),x*(500/20)-5,268);
  412.         g2Dcanvas2.drawLine(x*(500/20),245, x*(500/20),255);
  413.         g2Dcanvas2.drawString(Integer.toString(x-10),x*(500/20)-5,268);
  414.       }
  415.     }
  416.    
  417.     for (t=0; t<21;t++){
  418.       if (t!=10) {
  419.         g2Dcanvas1.drawLine(245,t*(500/20), 255,t*(500/20));
  420.         g2Dcanvas1.drawString(Integer.toString(10-t),258,t*(500/20)+5);
  421.         g2Dcanvas2.drawLine(245,t*(500/20), 255,t*(500/20));
  422.         g2Dcanvas2.drawString(Integer.toString(10-t),258,t*(500/20)+5);
  423.       }
  424.     }
  425.   }
  426.  
  427.  
  428.   public void generateEvents(){
  429.     int i=0;
  430.     int x,t,clockCounter;
  431.     for (x=-40; x<41;x++){
  432.       for (t=-40; t<41;t++){
  433.         clockCounter = t;
  434.         eventsArraylist.add( new Event(x,t,clockCounter));
  435.        
  436.       }
  437.     }
  438.    
  439.   }
  440.  
  441.   public void generateEvents_p(){
  442.     int i=0;
  443.     int x,t,clockCounter;
  444.     x=0;
  445.     for (t=-40; t<41;t++){
  446.       clockCounter = t;
  447.       events_pArraylist.add( new Event_p(x,t,clockCounter));
  448.     }
  449.   }
  450.  
  451.  
  452.   class Event_p {
  453.    
  454.     double xPosInLightSeconds;
  455.     double timeInSeconds;
  456.     double clockCounter;
  457.    
  458.     public Event_p(int x, int t, int cc) {
  459.      
  460.       xPosInLightSeconds = x;
  461.       timeInSeconds = t;
  462.       clockCounter = cc;
  463.      
  464.     }
  465.   }
  466.  
  467.  
  468.   class Event {
  469.    
  470.     double xPosInLightSeconds;
  471.     double timeInSeconds;
  472.     double clockCounter;
  473.    
  474.     public Event(int x, int t, int cc) {
  475.      
  476.       xPosInLightSeconds = x;
  477.       timeInSeconds = t;
  478.       clockCounter = cc;
  479.      
  480.     }
  481.   }
  482.  
  483.   public void resetAll(){
  484.    
  485.     v_rel=0;
  486.     timePassed=0;
  487.     not_traveller_x_p = 0;
  488.     traveller_x=0;
  489.     deletePaths=false;
  490.     stage = 1;
  491.    
  492.     g2Dimg.setBackground(new Color(0,0,0,0));
  493.     g2Dimg.clearRect(0,0,501,501);
  494.    
  495.     lock = true;
  496.   }
  497.  
  498.  
  499.   JFrame jFrame;
  500.   Canvas canvas1,canvas2;
  501.  
  502.   double turnAroundTime, turnAroundPosition;
  503.   double traveller_x,traveller_t, not_traveller_x_p, not_traveller_t_p;
  504.   double x,t,x_p,t_p,clockCounter;
  505.   double rest_length;
  506.   double timePassed_p, turnAroundPosition_p,turnAroundTime_p;
  507.   double max_gamma, clockTime;
  508.   double xPos, tPos;
  509.  
  510.   boolean lock=true, showPaths=false, showWorldline=true;
  511.  
  512.   BufferedImage bufferedImage = new BufferedImage(501, 501,BufferedImage.TYPE_INT_ARGB);
  513.  
  514.   Color backgroundColor = new Color(211,222,222);
  515.   BufferStrategy strategyCanvas1,strategyCanvas2,strategyJFrame;
  516.   Graphics2D g2Dcanvas1, g2Dcanvas2, g2DjFrame, g2Dimg = bufferedImage.createGraphics();;
  517.  
  518.   JButton button1,button2;
  519.   JCheckBox checkBox, checkBox2;
  520.  
  521.   ArrayList<Event> eventsArraylist = new ArrayList<Event>();
  522.   ArrayList<Event_p> events_pArraylist = new ArrayList<Event_p>();
  523.  
  524.   double temporary_gamma,v_rel,timePassed;
  525.  
  526.   Font stringFont = new Font( "SansSerif", Font.BOLD, 14 );
  527.   Font stringFont2 = new Font( "SansSerif", Font.PLAIN, 10 );
  528.   Font stringFont3 = new Font( "SansSerif", Font.PLAIN, 20 );
  529.  
  530.   ActionListener actionListener = new ActionListener() {
  531.    
  532.     public void actionPerformed(ActionEvent e) {
  533.       String str=e.getActionCommand();
  534.      
  535.       if (str.equals("show/hide paths")) {
  536.        
  537.         if (showPaths) {showPaths=false;} else {showPaths=true;};
  538.       }
  539.      
  540.       if (str.equals("white clock (est.)worldline")) {
  541.        
  542.         if (showWorldline) {showWorldline=false;} else {showWorldline=true;};
  543.       }
  544.      
  545.       if (str.equals("reset")) {
  546.        
  547.         resetAll();
  548.       }
  549.      
  550.       if (str.equals("start/stop")){
  551.         if (lock){lock=false;} else {lock=true;};
  552.       }
  553.     }
  554.   };
  555. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement