Advertisement
Guest User

Jeronimus

a guest
Jun 4th, 2016
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 32.42 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. import java.awt.geom.Rectangle2D;
  10. import javax.swing.event.*;
  11.  
  12. public class Spacetime {
  13.  
  14.   public static void main(String[] args) {
  15.    
  16.     new Spacetime().start();
  17.    
  18.   }
  19.  
  20.   double v_rel_MAX = 0.5;
  21.   double totalTravelTime = 20;
  22.  
  23.   Font stringFont = new Font("Monospace", Font.BOLD, 15);
  24.   Font stringFont2 = new Font("Monospace", Font.PLAIN, 10);
  25.   Font stringFont3 = new Font("Monospace", Font.PLAIN, 18);
  26.  
  27.   public Spacetime() {
  28.    
  29.   }
  30.  
  31.   public void start() {
  32.    
  33.     createFrameAndCanvases();
  34.     generateEvents();
  35.     generateEvents_p();
  36.    
  37.     mainLoop();
  38.   }
  39.  
  40.   public void mainLoop() {
  41.     do {
  42.      
  43.       updateState();
  44.       render();
  45.       try { Thread.sleep(1);}catch (Exception e){};
  46.      
  47.     } while (!exitApp);
  48.    
  49.     jFrame.dispose();
  50.     System.exit(0);
  51.   }
  52.  
  53.   public void updateState() {
  54.    
  55.     here:
  56.      
  57.       if (!lock) {
  58.      
  59.       if (nextStage == true) {
  60.         g2Dimg.setBackground(new Color(0, 0, 0, 0));
  61.         g2Dimg.clearRect(0, 0, 501, 501);
  62.         nextStage = false;
  63.         if (stage < 6) {
  64.           stage += 1;
  65.         }
  66.       }
  67.      
  68.       if (stage == 0) {
  69.         nextStage = true;
  70.       }
  71.      
  72.       if (stage == 1) {
  73.        
  74.         if ((round(v_rel * 10000) / 10000.0) < v_rel_MAX) {
  75.           accelerate();
  76.         } else {
  77.           lock = true;
  78.           nextStage = true;
  79.           break here;
  80.         }
  81.       }
  82.      
  83.       if (stage == 2) {
  84.         if ((round(timePassed * 10000) / 10000.0) < totalTravelTime / 2d) {
  85.           letSomeTimePass();
  86.         } else {
  87.           turnAroundTime = timePassed;
  88.           turnAroundPosition = v_rel * timePassed;
  89.           lock = true;
  90.           nextStage = true;
  91.           break here;
  92.         }
  93.       }
  94.      
  95.       if (stage == 3) {
  96.         if ((round(v_rel * 10000) / 10000.0) > 0) {
  97.           deccelerate();
  98.         } else {
  99.           lock = true;
  100.           nextStage = true;
  101.           break here;
  102.         }
  103.       }
  104.      
  105.       if (stage == 4) {
  106.        
  107.         if ((round(v_rel * 10000) / 10000.0) > -v_rel_MAX) {
  108.           deccelerate();
  109.         } else {
  110.           lock = true;
  111.           nextStage = true;
  112.           break here;
  113.         }
  114.       }
  115.      
  116.       if (stage == 5) {
  117.         if ((round(timePassed * 10000) / 10000.0) < totalTravelTime) {
  118.           letSomeTimePass();
  119.         } else {
  120.           lock = true;
  121.           nextStage = true;
  122.           break here;
  123.         }
  124.       }
  125.      
  126.       if (stage == 6) {
  127.         if ((round(v_rel * 10000) / 10000.0) < 0) {
  128.           accelerate();
  129.         } else {
  130.           lock = true;
  131.           nextStage = true;
  132.           break here;
  133.         }
  134.       }
  135.     }
  136.      
  137.       max_gamma = 1 / (sqrt(1 - pow(v_rel_MAX, 2)));
  138.       temporary_gamma = 1 / (sqrt(1 - pow(v_rel, 2)));
  139.      
  140.       timePassed_p = timePassed / max_gamma;
  141.       if (showSymmetry) {
  142.         timeMoved = timePassed - timePassed_p;
  143.       } else {
  144.         timeMoved = 0;
  145.       };
  146.      
  147.       if (stage == 1 || stage == 2) {
  148.         traveller_x = v_rel * timePassed;
  149.         blue_ClockCounter = timePassed_p / max_gamma;
  150.       }
  151.      
  152.       if (stage == 3 || stage == 4 || stage == 5) {
  153.         traveller_x = v_rel * (timePassed - turnAroundTime) + turnAroundPosition;
  154.       }
  155.      
  156.       if (stage == 2) {
  157.         not_traveller_x_p = -v_rel * timePassed_p;
  158.         rest_length = not_traveller_x_p * max_gamma;
  159.       }
  160.      
  161.       if (stage == 3 || stage == 4) {
  162.         not_traveller_x_p = rest_length / temporary_gamma;
  163.         turnAroundPosition_p = not_traveller_x_p;
  164.         turnAroundTime_p = timePassed_p;
  165.       }
  166.      
  167.       if (stage == 5) {
  168.         not_traveller_x_p = -v_rel * (timePassed_p - turnAroundTime_p) + turnAroundPosition_p;
  169.       }
  170.      
  171.       xPos = 0;
  172.       tPos = temporary_gamma * v_rel * not_traveller_x_p;
  173.      
  174.       blue_ClockCounter = tPos + timePassed;
  175.       not_traveller_t_p = temporary_gamma * (tPos - v_rel * -traveller_x) + timeMoved;
  176.   }
  177.  
  178.   public void accelerate() {
  179.     if (stage == 1) {
  180.       if (v_rel + 0.0001 * animationSpeed < v_rel_MAX) {
  181.         v_rel += 0.0001 * animationSpeed;
  182.       } else {
  183.         v_rel = v_rel_MAX;
  184.       }
  185.     }
  186.    
  187.     if (stage == 6) {
  188.       if (v_rel + 0.0001 * animationSpeed < 0) {
  189.         v_rel += 0.0001 * animationSpeed;
  190.       } else {
  191.         v_rel = 0;
  192.       }
  193.      
  194.     }
  195.   }
  196.  
  197.   public void deccelerate() {
  198.     if (stage == 3) {
  199.       if (v_rel - 0.0001 * animationSpeed > 0) {
  200.         v_rel -= 0.0001 * animationSpeed;
  201.       } else {
  202.         v_rel = 0;
  203.       }
  204.     }
  205.    
  206.     if (stage == 4) {
  207.       if (v_rel - 0.0001 * animationSpeed > -v_rel_MAX) {
  208.         v_rel -= 0.0001 * animationSpeed;
  209.       } else {
  210.         v_rel = -v_rel_MAX;
  211.       }
  212.     }
  213.   }
  214.  
  215.   public void letSomeTimePass() {
  216.    
  217.     if (stage == 2) {
  218.       if (timePassed + 0.001 * animationSpeed < totalTravelTime / 2d) {
  219.         timePassed += 0.001 * animationSpeed;
  220.       } else {
  221.         timePassed = totalTravelTime / 2d;
  222.       }
  223.     }
  224.    
  225.     if (stage == 5) {
  226.       if (timePassed + 0.001 * animationSpeed < totalTravelTime) {
  227.         timePassed += 0.001 * animationSpeed;
  228.       } else {
  229.         timePassed = totalTravelTime;
  230.       }
  231.     }
  232.   }
  233.  
  234.   public void plotLightGrid() {
  235.    
  236.     for (int i = 0; i < eventsArraylist.size(); i++) {
  237.      
  238.       x = eventsArraylist.get(i).xPosInLightSeconds;
  239.       t = eventsArraylist.get(i).timeInSeconds - timePassed;
  240.      
  241.       if ((x < 11 && x > -11) && (t < 11 && t > -11)) {
  242.         g2Dcanvas1.setColor(Color.YELLOW);
  243.         g2Dcanvas1.setStroke(new BasicStroke(1));
  244.         g2Dcanvas1.drawLine((int) ((x - 0.5) * 25 + 251), (int) (251 - (t - 0.5) * 25), (int) ((x + 0.5) * 25 + 251), (int) (251 - (t + 0.5) * 25));
  245.         g2Dcanvas1.drawLine((int) ((x - 0.5) * 25 + 251), (int) (251 - (t + 0.5) * 25), (int) ((x + 0.5) * 25 + 251), (int) (251 - (t - 0.5) * 25));
  246.       }
  247.      
  248.       if ((x < 11 + traveller_x && x > -11 + traveller_x) && (t < 11 && t > -11)) {
  249.         clockCounter = eventsArraylist.get(i).clockCounter;
  250.        
  251.         x_p = temporary_gamma * (x - traveller_x - v_rel * t);
  252.         t_p = temporary_gamma * (t - (v_rel * (x - traveller_x)));
  253.        
  254.         g2Dcanvas2.setColor(Color.YELLOW);
  255.         g2Dcanvas2.setStroke(new BasicStroke(1));
  256.        
  257.         if (v_rel > 0) {
  258.           factor = pow(temporary_gamma, 4);
  259.         } else {
  260.           factor = 1 / pow(temporary_gamma, 4);
  261.         }
  262.        
  263.         g2Dcanvas2.drawLine((int) ((x_p - 0.5 / factor) * 25 + 251), (int) (251 - (t_p - 0.5 / factor) * 25), (int) ((x_p + 0.5 / factor) * 25 + 251), (int) (251 - (t_p + 0.5 / factor) * 25));
  264.         g2Dcanvas2.drawLine((int) ((x_p - 0.5 * factor) * 25 + 251), (int) (251 - (t_p + 0.5 * factor) * 25), (int) ((x_p + 0.5 * factor) * 25 + 251), (int) (251 - (t_p - 0.5 * factor) * 25));
  265.       }
  266.     }
  267.   }
  268.  
  269.   public void plotEvents() {
  270.     g2Dcanvas1.setFont(stringFont);
  271.     g2Dcanvas2.setFont(stringFont);
  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.       x_p = temporary_gamma * (x - traveller_x - v_rel * t);
  281.       t_p = temporary_gamma * (t - (v_rel * (x - traveller_x)));
  282.      
  283.       message = Integer.toString((int) clockCounter);
  284.       midWidth = mW(message, stringFont);
  285.       midHeight = mH(message, stringFont);
  286.      
  287.       if ((x < 11 && x > -11) && (t < 11 && t > -11)) {
  288.        
  289.         g2Dcanvas1.setColor(Color.BLACK);
  290.        
  291.         if (x == 5 || x == -5) {
  292.           g2Dcanvas1.setColor(Color.GREEN);
  293.           g2Dcanvas1.drawString(message, (int) (x * 25 + 251 - midWidth), (int) (251 + midHeight - t * 25));
  294.         }
  295.        
  296.         if (x == 0) {
  297.           g2Dcanvas1.setColor(Color.BLUE);
  298.           g2Dcanvas1.drawString(message, (int) (x * 25 + 251 - midWidth), (int) (251 + midHeight - t * 25));
  299.         }
  300.        
  301.         if (showBlackCounters) {
  302.           g2Dcanvas1.drawString(message, (int) (x * 25 + 251 - midWidth), (int) (251 + midHeight - t * 25));
  303.         }
  304.        
  305.       }
  306.      
  307.       if ((x < 11 + traveller_x && x > -11 + traveller_x) && (t < 11 && t > -11)) {
  308.        
  309.         g2Dimg.setColor(Color.BLACK);
  310.         g2Dcanvas2.setColor(Color.BLACK);
  311.        
  312.         if (showBlackCounters) {
  313.           g2Dcanvas2.drawString(message, (int) (x_p * 25 + 251 - midWidth), (int) (251 + midHeight - t_p * 25));
  314.         }
  315.        
  316.         if (x == 5 || x == -5) {
  317.           g2Dcanvas2.setColor(Color.GREEN);
  318.           g2Dimg.setColor(Color.GREEN);
  319.           g2Dcanvas2.drawString(message, (int) (x_p * 25 + 251 - midWidth), (int) (251 + midHeight - (t_p - timeMoved) * 25));
  320.         }
  321.         if (x == 0) {
  322.           g2Dcanvas2.setColor(Color.BLUE);
  323.           g2Dimg.setColor(Color.BLUE);
  324.           g2Dcanvas2.drawString(message, (int) (x_p * 25 + 251 - midWidth), (int) (251 + midHeight - (t_p - timeMoved) * 25));
  325.         }
  326.        
  327.         x_p0 = -temporary_gamma * traveller_x;
  328.         t_p0 = temporary_gamma * (v_rel * traveller_x);
  329.        
  330.         g2Dimg.setStroke(new BasicStroke(2));
  331.         g2Dimg.drawLine((int) (x_p * 25 + 251), (int) (251 - t_p * 25), (int) (x_p * 25 + 251), (int) (251 - t_p * 25));
  332.       }
  333.     }
  334.    
  335.     for (int i = 0; i < events_pArraylist.size(); i++) {
  336.       x = events_pArraylist.get(i).xPosInLightSeconds;
  337.       t = events_pArraylist.get(i).timeInSeconds - timePassed_p;
  338.      
  339.       if ((x < 11 && x > -11) && (t < 11 && t > -11) && (showWorldline == true)) {
  340.        
  341.         clockCounter = events_pArraylist.get(i).clockCounter;
  342.        
  343.         message = Integer.toString((int) clockCounter);
  344.         midWidth = mW(message, stringFont);
  345.         midHeight = mH(message, stringFont);
  346.        
  347.         x_p = temporary_gamma * (x + v_rel * t) + traveller_x;
  348.         t_p = temporary_gamma * (t + v_rel * x);
  349.        
  350.         g2Dcanvas1.setColor(Color.WHITE);
  351.         /*  if ((x==5 || x ==-5) && showSymmetry==true ) {//g2Dcanvas1.setColor(new Color(55,155,55));}
  352.          if (x==0 && showSymmetry==true ) {//g2Dcanvas1.setColor(new Color(100,115,211));}*/
  353.         g2Dcanvas1.drawString(message, (int) (x_p * 25 + 251 - midWidth), (int) (251 + midHeight - t_p * 25));
  354.        
  355.         g2Dcanvas2.setColor(Color.WHITE);
  356.         /* if ((x==5 || x ==-5) && showSymmetry==true ) {g2Dcanvas2.setColor(new Color(55,155,55));}
  357.          if (x==0 && showSymmetry==true ) {g2Dcanvas2.setColor(new Color(100,115,211));}*/
  358.        
  359.         g2Dcanvas2.drawString(message, (int) (x * 25 + 251 - midWidth), (int) (251 + midHeight - (t - timeMoved) * 25));
  360.       }
  361.     }
  362.   }
  363.  
  364.   public int mH(String message, Font whichFont) {
  365.     int fontHeight;
  366.     bounds = whichFont.getStringBounds(message, g2Dcanvas1.getFontRenderContext());
  367.     fontHeight = (int) bounds.getHeight();
  368.     return fontHeight / 4;
  369.   }
  370.  
  371.   public int mW(String message, Font whichFont) {
  372.     int fontWidth;
  373.     bounds = whichFont.getStringBounds(message, g2Dcanvas1.getFontRenderContext());
  374.     fontWidth = (int) bounds.getWidth();
  375.     return fontWidth / 2;
  376.   }
  377.  
  378.   public void render() {
  379.    
  380.     g2Dcanvas1 = (Graphics2D) strategyCanvas1.getDrawGraphics();
  381.     g2Dcanvas2 = (Graphics2D) strategyCanvas2.getDrawGraphics();
  382.     g2Dcanvas3 = (Graphics2D) strategyCanvas3.getDrawGraphics();
  383.    
  384.     g2Dcanvas1.scale(scaleFactor, scaleFactor);
  385.     g2Dcanvas2.scale(scaleFactor, scaleFactor);
  386.     g2Dcanvas3.scale(scaleFactor, scaleFactor);
  387.    
  388.     /*g2Dimg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
  389.      RenderingHints.VALUE_ANTIALIAS_ON);
  390.      
  391.      g2Dcanvas1.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
  392.      RenderingHints.VALUE_ANTIALIAS_ON);
  393.      
  394.      g2Dcanvas2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
  395.      RenderingHints.VALUE_ANTIALIAS_ON);
  396.      
  397.      g2Dcanvas3.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
  398.      RenderingHints.VALUE_ANTIALIAS_ON);
  399.      */
  400.     g2Dcanvas1.setColor(backgroundColor);
  401.     g2Dcanvas2.setColor(backgroundColor);
  402.     g2Dcanvas3.setColor(darkGrey);
  403.    
  404.     g2Dcanvas1.fillRect(0, 0, 501, 501);
  405.     g2Dcanvas2.fillRect(0, 0, 501, 501);
  406.     g2Dcanvas3.fillRect(0, 0, 1280, 720);
  407.    
  408.     if (setClearPaths) {
  409.       g2Dimg.setBackground(new Color(0, 0, 0, 0));
  410.       g2Dimg.clearRect(0, 0, 501, 501);
  411.       setClearPaths = false;
  412.     }
  413.     if (showGrid) {
  414.       plotLightGrid();
  415.     }
  416.    
  417.     plotMovingClocks();
  418.    
  419.     if (showPaths) {
  420.       g2Dcanvas2.drawImage(bufferedImage, 0, 0, 501, 501, null);
  421.     }
  422.    
  423.     g2Dcanvas1.setStroke(new BasicStroke(2));
  424.     g2Dcanvas2.setStroke(new BasicStroke(2));
  425.     plotLightbeams();
  426.    
  427.     g2Dcanvas1.setStroke(new BasicStroke(1));
  428.     g2Dcanvas2.setStroke(new BasicStroke(1));
  429.     plotDiagrams();
  430.    
  431.     g2Dcanvas1.setStroke(new BasicStroke(3));
  432.     g2Dcanvas2.setStroke(new BasicStroke(3));
  433.     plotEvents();
  434.    
  435.     plotValues();
  436.    
  437.     strategyCanvas1.show();
  438.     strategyCanvas2.show();
  439.     strategyCanvas3.show();
  440.    
  441.     g2Dcanvas1.dispose();
  442.     g2Dcanvas2.dispose();
  443.     g2Dcanvas3.dispose();
  444.    
  445.     jFrame.repaint();
  446.   }
  447.  
  448.   public void plotValues() {
  449.    
  450.     int offsetX = (int) (canvas2.getBounds().x / scaleFactor);
  451.     int offsetX2 = (int) (canvas1.getBounds().x / scaleFactor);
  452.    
  453.     g2Dcanvas3.setColor(Color.BLACK);
  454.     g2Dcanvas3.setFont(stringFont3);
  455.    
  456.     g2Dcanvas3.drawString("Vrel " + (Double.toString(round(v_rel * 10000) / 10000.0) + "c"), 590, 100);
  457.     g2Dcanvas3.setColor(Color.MAGENTA);
  458.     g2Dcanvas3.drawString("stage " + stage, 600, 150);
  459.    
  460.     g2Dcanvas3.setColor(Color.CYAN);
  461.     g2Dcanvas3.drawString("Observer clock count " + (Double.toString(round(timePassed * 10000) / 10000.0)) + "s", offsetX2, 520);
  462.     g2Dcanvas3.drawString("x-Pos / t-Pos: 0ls / 0s", offsetX2, 550);
  463.     g2Dcanvas3.setColor(whiteish);
  464.     g2Dcanvas3.drawString("White clock count " + (Double.toString(round(timePassed_p * 10000) / 10000.0)) + "s", offsetX2 + 300, 520);
  465.     g2Dcanvas3.drawString("x Position " + (Double.toString(round(traveller_x * 10000) / 10000.0)) + "ls", offsetX2 + 300, 550);
  466.     g2Dcanvas3.drawString("t Position 0s", offsetX2 + 300, 570);
  467.    
  468.     g2Dcanvas3.setColor(whiteish);;
  469.     g2Dcanvas3.drawString("Observer clock count " + (Double.toString(round(timePassed_p * 10000) / 10000.0)) + "s", offsetX, 520);
  470.     g2Dcanvas3.drawString("x-Pos / t-Pos: 0ls / 0s ", offsetX, 550);
  471.     g2Dcanvas3.setColor(Color.BLUE);
  472.     g2Dcanvas3.drawString("Blue clock count " + (Double.toString(round(blue_ClockCounter * 10000) / 10000.0)) + "s", offsetX + 300, 520);
  473.     g2Dcanvas3.drawString("x Position " + (Double.toString(round(not_traveller_x_p * 10000) / 10000.0)) + "ls", offsetX + 300, 550);
  474.     g2Dcanvas3.drawString("t Position 0s", offsetX + 300, 570);
  475.    
  476.     g2Dcanvas3.drawString("Blue clock count " + (Double.toString(round(blue_ClockCounter * 10000) / 10000.0)) + "s", offsetX2, 590);
  477.     g2Dcanvas3.drawString("x Position 0ls", offsetX2, 620);
  478.     g2Dcanvas3.drawString("t Position " + (Double.toString(round(tPos * 10000) / 10000.0)) + "s", offsetX2, 640);
  479.    
  480.   }
  481.  
  482.   public void plotMovingClocks() {
  483.     if (addEvents == true) {
  484.       if (showSymmetry == true) {
  485.         offsetTime = timeMoved;
  486.       } else {
  487.         offsetTime = 0;
  488.       }
  489.       if (showSymmetry == false) {
  490.         offsetTime2 = timePassed - timePassed_p;
  491.         offsetTime = offsetTime2;
  492.       } else {
  493.         offsetTime2 = 0;
  494.       }
  495.      
  496.       g2Dcanvas2.setColor(orangeish);
  497.       g2Dcanvas2.fillOval((int) (-xPos * 25 - 12 + 251), (int) ((-tPos - offsetTime2) * 25 - 12 + 251), 24, 24);
  498.      
  499.       x = temporary_gamma * (xPos + v_rel * (tPos + offsetTime)) + traveller_x;
  500.       t = temporary_gamma * (tPos + offsetTime - (-v_rel * xPos));
  501.      
  502.       g2Dcanvas1.setColor(orangeish);
  503.       g2Dcanvas1.fillOval((int) (x * 25 - 12 + 251), (int) (-t * 25 - 12 + 251), 24, 24);
  504.      
  505.       g2Dcanvas2.setColor(redish);
  506.       g2Dcanvas2.fillOval((int) (-traveller_x * 25 - 11 + 251), (int) ((traveller_t - offsetTime2) * 25 - 11 + 251), 22, 22);
  507.      
  508.       x = temporary_gamma * (-traveller_x + v_rel * (-traveller_t + offsetTime)) + traveller_x;
  509.       t = temporary_gamma * (-traveller_t + offsetTime - (-v_rel * -traveller_x));
  510.      
  511.       g2Dcanvas1.setColor(redish);
  512.       g2Dcanvas1.fillOval((int) (x * 25 - 11 + 251), (int) ((-t) * 25 - 11 + 251), 22, 22);
  513.      
  514.       g2Dcanvas2.setColor(violetish);
  515.       g2Dcanvas2.fillOval(251 - 10, (int) (251 - 10 - offsetTime2 * 25), 20, 20);
  516.      
  517.       x = temporary_gamma * (0 + v_rel * (0 + offsetTime)) + traveller_x;
  518.       t = temporary_gamma * (0 + offsetTime - (-v_rel * 0));
  519.      
  520.       g2Dcanvas1.setColor(violetish);
  521.       g2Dcanvas1.fillOval((int) (x * 25 - 10 + 251), (int) (-t * 25 - 10 + 251), 20, 20);
  522.      
  523.     }
  524.    
  525.     g2Dcanvas1.setColor(blueish);
  526.     g2Dcanvas1.fillOval((int) (xPos * 25 - 12 + 251), (int) (-tPos * 25 - 12 + 251), 24, 24);
  527.    
  528.     g2Dcanvas1.setColor(whiteish);
  529.     g2Dcanvas1.fillOval((int) (traveller_x * 25 - 11 + 251), (int) (traveller_t * 25 - 12 + 251), 22, 22);
  530.    
  531.     g2Dcanvas1.setColor(Color.CYAN);
  532.     g2Dcanvas1.fillOval(251 - 10, 251 - 10, 20, 20);
  533.    
  534.     g2Dcanvas2.setColor(blueish);
  535.     g2Dcanvas2.fillOval((int) (not_traveller_x_p * 25 - 12 + 251), (int) ((not_traveller_t_p) * 25 - 12 + 251), 24, 24);
  536.    
  537.     g2Dcanvas2.setColor(Color.CYAN);
  538.     g2Dcanvas2.fillOval((int) (x_p0 * 25 + 251 - 11), (int) ((-t_p0 + timeMoved) * 25 + 251 - 11), 22, 22);
  539.    
  540.     g2Dcanvas2.setColor(whiteish);
  541.     g2Dcanvas2.fillOval(251 - 10, (int) (251 - 10 + timeMoved * 25), 20, 20);
  542.   }
  543.  
  544.   public void plotLightbeams() {
  545.     g2Dcanvas1.setColor(Color.YELLOW);
  546.     g2Dcanvas2.setColor(Color.YELLOW);
  547.    
  548.     g2Dcanvas1.drawLine(0, 0, 501, 501);
  549.     g2Dcanvas1.drawLine(0, 501, 501, 0);
  550.    
  551.     g2Dcanvas2.drawLine(0, 0, 501, 501);
  552.     g2Dcanvas2.drawLine(0, 501, 501, 0);
  553.   }
  554.  
  555.   public void createFrameAndCanvases() {
  556.    
  557.     jFrame = new JFrame();
  558.     jFrame.setLayout(null);
  559.     jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  560.     jFrame.getContentPane().setBackground(darkGrey);
  561.     jFrame.setResizable(true);
  562.     jFrame.setIgnoreRepaint(false);
  563.    
  564.     myPanel = new JPanel();
  565.     myPanel.setSize(1280, 720);
  566.     myPanel.setLayout(null);
  567.     myPanel.setIgnoreRepaint(false);
  568.     myPanel.setBackground(darkGrey);
  569.    
  570.     guiPanel = new JPanel();
  571.     guiPanel.setSize(640, 100);
  572.     guiPanel.setLocation(1280 - 640 - 320, 502 + 80);
  573.     guiPanel.setLayout(null);
  574.     guiPanel.setIgnoreRepaint(true);
  575.     guiPanel.setBackground(new Color(110, 110, 111));
  576.     myPanel.add(guiPanel);
  577.    
  578.     button1 = new JButton("start/stop");
  579.     button1.setBounds(210, 0, 100, 100);
  580.     button1.addActionListener(actionListener);
  581.     button1.setActionCommand("start/stop");
  582.     guiPanel.add(button1);
  583.    
  584.     button2 = new JButton("reset");
  585.     button2.setBounds(330, 0, 100, 100);
  586.     button2.addActionListener(actionListener);
  587.     button2.setActionCommand("reset");
  588.     button2.setVisible(true);
  589.     guiPanel.add(button2);
  590.    
  591.     button3 = new JButton("exit app");
  592.     button3.setBounds(100, 80, 100, 20);
  593.     button3.addActionListener(actionListener);
  594.     button3.setActionCommand("exit");
  595.     button3.setVisible(true);
  596.     guiPanel.add(button3);
  597.    
  598.     canvas1 = new Canvas();
  599.     canvas1.setSize(501, 501);
  600.     canvas1.setLocation(75, 0);
  601.     canvas1.setIgnoreRepaint(true);
  602.     canvas1.setBackground(new Color(222, 222, 222));
  603.     myPanel.add(canvas1);
  604.    
  605.     canvas2 = new Canvas();
  606.     canvas2.setSize(501, 501);
  607.     canvas2.setLocation(1280 - 501 - 75, 0);
  608.     canvas2.setIgnoreRepaint(true);
  609.     canvas2.setBackground(new Color(222, 222, 222));
  610.     myPanel.add(canvas2);
  611.    
  612.     canvas3 = new Canvas();
  613.     canvas3.setSize(1280, 720);
  614.     canvas3.setLocation(0, 0);
  615.     canvas3.setIgnoreRepaint(true);
  616.     canvas3.setBackground(new Color(222, 222, 222));
  617.     myPanel.add(canvas3);
  618.    
  619.     checkBox = new JCheckBox("show/hide paths", showPaths);
  620.     checkBox.addActionListener(actionListener);
  621.     checkBox.setActionCommand("show/hide paths");
  622.     checkBox.setBounds(440, 0, 200, 20);
  623.     guiPanel.add(checkBox);
  624.    
  625.     checkBox1 = new JCheckBox("show symmetry (<=stage2)", showSymmetry);
  626.     checkBox1.addActionListener(actionListener);
  627.     checkBox1.setActionCommand("show the symmetry");
  628.     checkBox1.setBounds(440, 20, 200, 20);
  629.     guiPanel.add(checkBox1);
  630.    
  631.     checkBox2 = new JCheckBox("white clock (est.)worldline", showWorldline);
  632.     checkBox2.addActionListener(actionListener);
  633.     checkBox2.setActionCommand("white clock (est.)worldline");
  634.     checkBox2.setBounds(440, 40, 200, 20);
  635.     guiPanel.add(checkBox2);
  636.    
  637.     checkBox3 = new JCheckBox("show light grid", showGrid);
  638.     checkBox3.addActionListener(actionListener);
  639.     checkBox3.setActionCommand("show light grid");
  640.     checkBox3.setBounds(0, 20, 200, 20);
  641.     guiPanel.add(checkBox3);
  642.    
  643.     checkBox4 = new JCheckBox("add more events", addEvents);
  644.     checkBox4.addActionListener(actionListener);
  645.     checkBox4.setActionCommand("add more events");
  646.     checkBox4.setBounds(0, 40, 200, 20);
  647.     guiPanel.add(checkBox4);
  648.    
  649.     checkBox5 = new JCheckBox("show black clock counters", showBlackCounters);
  650.     checkBox5.addActionListener(actionListener);
  651.     checkBox5.setActionCommand("show black clock counters");
  652.     checkBox5.setBounds(0, 60, 200, 20);
  653.     guiPanel.add(checkBox5);
  654.    
  655.     checkBox6 = new JCheckBox("fullscreen(borderless window)", isFullscreen);
  656.     checkBox6.addActionListener(actionListener);
  657.     checkBox6.setActionCommand("fake Fullscreen");
  658.     checkBox6.setBounds(0, 0, 200, 20);
  659.     guiPanel.add(checkBox6);
  660.    
  661.     checkBox7 = new JCheckBox("bold font", boldFont);
  662.     checkBox7.addActionListener(actionListener);
  663.     checkBox7.setActionCommand("bold font");
  664.     checkBox7.setBounds(0, 80, 90, 20);
  665.     guiPanel.add(checkBox7);
  666.    
  667.     slider = new JSlider(0, 100, 40);
  668.     slider.addChangeListener(changeListener);
  669.     slider.setBounds(440, 60, 200, 40);
  670.     slider.setMinorTickSpacing(2);
  671.     slider.setMajorTickSpacing(20);
  672.     slider.setPaintTicks(true);
  673.     slider.setPaintLabels(true);
  674.     guiPanel.add(slider);
  675.    
  676.     jFrame.add(myPanel);
  677.    
  678.     if (isFullscreen) {
  679.       jFrame.setUndecorated(true);
  680.       jFrame.setSize((int)Toolkit.getDefaultToolkit().getScreenSize().getWidth(), (int) Toolkit.getDefaultToolkit().getScreenSize().getHeight());
  681.     } else {
  682.       jFrame.setSize(1280, 720);
  683.       jFrame.setUndecorated(false);
  684.     }
  685.    
  686.     jFrame.setVisible(true);
  687.    
  688.     canvas1.createBufferStrategy(2);
  689.     canvas2.createBufferStrategy(2);
  690.     canvas3.createBufferStrategy(2);
  691.    
  692.     strategyCanvas1 = canvas1.getBufferStrategy();
  693.     strategyCanvas2 = canvas2.getBufferStrategy();
  694.     strategyCanvas3 = canvas3.getBufferStrategy();
  695.    
  696.     g2Dcanvas1 = (Graphics2D) strategyCanvas1.getDrawGraphics();
  697.     g2Dcanvas2 = (Graphics2D) strategyCanvas2.getDrawGraphics();
  698.     g2Dcanvas3 = (Graphics2D) strategyCanvas2.getDrawGraphics();
  699.    
  700.     jFrame.addComponentListener(new ComponentAdapter() {
  701.       public void componentResized(ComponentEvent e) {
  702.        
  703.         if (jFrame.getWidth() < 1300 || jFrame.getHeight() < 740) {
  704.           scaleFactor = 1;
  705.           if (canvas1.getWidth() > 501) {
  706.             canvas1.setSize(501, 501);
  707.             canvas2.setSize(501, 501);
  708.           }
  709.           myPanel.setSize(1280, 720);
  710.           canvas2.setLocation(1280 - 501 - 75, 0);
  711.           guiPanel.setLocation(1280 - 640 - 320, 502 + 80);
  712.           jFrame.repaint();
  713.         } else {
  714.           myPanel.setSize(jFrame.getWidth(), jFrame.getHeight());
  715.           canvas1.setSize((int) (jFrame.getWidth() * 501 / 1280.0), (int) (jFrame.getWidth() * 501 / 1280.0));
  716.          
  717.           canvas2.setSize((int) (jFrame.getWidth() * 501 / 1280.0), (int) (jFrame.getWidth() * 501 / 1280.0));
  718.           canvas2.setLocation(jFrame.getWidth() - canvas2.getWidth() - 75, 0);
  719.          
  720.           canvas3.setSize((int) (jFrame.getWidth()), (int) (jFrame.getHeight()));
  721.          
  722.           scaleFactor = jFrame.getWidth() / 1280.0;
  723.           guiPanel.setLocation((int) (myPanel.getWidth() / 2.0 - 320), (int) (myPanel.getHeight() - 100 - 40));
  724.           jFrame.repaint();
  725.         }
  726.        
  727.       }
  728.     });
  729.    
  730.   }
  731.  
  732.   public void plotDiagrams() {
  733.     g2Dcanvas1.setFont(stringFont2);
  734.     g2Dcanvas2.setFont(stringFont2);
  735.    
  736.     g2Dcanvas1.setColor(Color.BLACK);
  737.     g2Dcanvas2.setColor(Color.BLACK);
  738.    
  739.     g2Dcanvas1.drawLine(0, 250, 500, 250);
  740.     g2Dcanvas1.drawLine(250, 0, 250, 500);
  741.    
  742.     g2Dcanvas2.drawLine(0, 250, 500, 250);
  743.     g2Dcanvas2.drawLine(250, 0, 250, 500);
  744.    
  745.     int x, t;
  746.     for (x = 0; x < 21; x++) {
  747.       if (x != 10) {
  748.        
  749.         message = Integer.toString(x - 10);
  750.         midWidth = mW(message, stringFont2);
  751.         midHeight = mH(message, stringFont2);
  752.        
  753.         g2Dcanvas1.drawLine(x * (500 / 20), 245, x * (500 / 20), 255);
  754.         g2Dcanvas1.drawString(message, x * (500 / 20) - midWidth, 251 + midHeight + 10);
  755.         g2Dcanvas2.drawLine(x * (500 / 20), 245, x * (500 / 20), 255);
  756.         g2Dcanvas2.drawString(message, x * (500 / 20) - midWidth, 251 + midHeight + 10);
  757.       }
  758.     }
  759.    
  760.     for (t = 0; t < 21; t++) {
  761.       if (t != 10) {
  762.        
  763.         message = Integer.toString(10 - t);
  764.         midWidth = mW(message, stringFont2);
  765.         midHeight = mH(message, stringFont2);
  766.        
  767.         g2Dcanvas1.drawLine(245, t * (500 / 20), 255, t * (500 / 20));
  768.         g2Dcanvas1.drawString(message, 251 + midWidth + 5, t * (500 / 20) + midHeight);
  769.         g2Dcanvas2.drawLine(245, t * (500 / 20), 255, t * (500 / 20));
  770.         g2Dcanvas2.drawString(message, 251 + midWidth + 5, t * (500 / 20) + midHeight);
  771.       }
  772.     }
  773.   }
  774.  
  775.   public void generateEvents() {
  776.     int i = 0;
  777.     int x, t, clockCounter;
  778.    
  779.     for (x = -(int) totalTravelTime; x < (int) totalTravelTime + 21; x++) {
  780.       for (t = -(int) totalTravelTime; t < (int) totalTravelTime + 21; t++) {
  781.         clockCounter = t;
  782.         eventsArraylist.add(new Event(x, t, clockCounter));
  783.        
  784.         if (x == 0 && t == 0) {
  785.           zeroEvent = i;
  786.         }
  787.         i++;
  788.       }
  789.     }
  790.   }
  791.  
  792.   public void generateEvents_p() {
  793.     int x, t, clockCounter;
  794.     x = 0;
  795.     for (t = -(int) totalTravelTime; t < (int) totalTravelTime + 21; t++) {
  796.       clockCounter = t;
  797.       events_pArraylist.add(new Event_p(x, t, clockCounter));
  798.     }
  799.    
  800.     x = -5;
  801.     for (t = -(int) totalTravelTime; t < (int) totalTravelTime + 21; t++) {
  802.       clockCounter = t;
  803.       events_pArraylist.add(new Event_p(x, t, clockCounter));
  804.     }
  805.    
  806.     x = 5;
  807.     for (t = -(int) totalTravelTime + 10; t < (int) totalTravelTime + 11; t++) {
  808.       clockCounter = t;
  809.       events_pArraylist.add(new Event_p(x, t, clockCounter));
  810.     }
  811.   }
  812.  
  813.   class Event_p {
  814.    
  815.     double xPosInLightSeconds;
  816.     double timeInSeconds;
  817.     double clockCounter;
  818.    
  819.     public Event_p(int x, int t, int cc) {
  820.       xPosInLightSeconds = x;
  821.       timeInSeconds = t;
  822.       clockCounter = cc;
  823.     }
  824.   }
  825.  
  826.   class Event {
  827.    
  828.     double xPosInLightSeconds;
  829.     double timeInSeconds;
  830.     double clockCounter;
  831.    
  832.     public Event(int x, int t, int cc) {
  833.       xPosInLightSeconds = x;
  834.       timeInSeconds = t;
  835.       clockCounter = cc;
  836.     }
  837.   }
  838.  
  839.   public void resetAll() {
  840.     v_rel = 0;
  841.     timePassed = 0;
  842.     not_traveller_x_p = 0;
  843.     traveller_x = 0;
  844.     nextStage = false;
  845.     stage = 0;
  846.     setClearPaths = true;
  847.     lock = true;
  848.   }
  849.  
  850.   JFrame jFrame;
  851.   JPanel guiPanel, myPanel;
  852.  
  853.   JSlider slider;
  854.   Canvas canvas1, canvas2, canvas3;
  855.  
  856.   BufferedImage bufferedImage = new BufferedImage(501, 501, BufferedImage.TYPE_INT_ARGB);
  857.   BufferedImage bufferedImageClock;
  858.   BufferStrategy strategyCanvas1, strategyCanvas2, strategyCanvas3;
  859.   Graphics2D g2Dcanvas1, g2Dcanvas2, g2Dcanvas3, g2Dimg = bufferedImage.createGraphics(), g2DimgClock;
  860.  
  861.   JButton button1, button2, button3;
  862.   JCheckBox checkBox, checkBox1, checkBox2, checkBox3, checkBox4, checkBox5, checkBox6, checkBox7;
  863.  
  864.   double turnAroundTime, turnAroundPosition;
  865.   double traveller_x, traveller_t, not_traveller_x_p, not_traveller_t_p;
  866.   double x, t, x_p, t_p, clockCounter;
  867.   double rest_length;
  868.   double timePassed_p, turnAroundPosition_p, turnAroundTime_p;
  869.   double max_gamma, blue_ClockCounter;
  870.   double temporary_gamma, v_rel, timePassed, timeMoved;
  871.  
  872.   double xPos, tPos;
  873.   double x_p0, t_p0;
  874.  
  875.   double factor = 1;
  876.   double offsetTime, offsetTime2, scaleFactor = 1;
  877.  
  878.   int zeroEvent;
  879.   int animationSpeed = 40;
  880.  
  881.   int midWidth, midHeight;
  882.   String message;
  883.   Rectangle2D bounds;
  884.  
  885.   int stage = 0;
  886.   int tickUPS;
  887.   int tickFPS;
  888.   long delta = System.nanoTime();
  889.  
  890.   long deltaNanoTime1, deltaNanoTime2, timeStampA = 0, timeStampB = 0;
  891.   double UPS, FPS;
  892.   long delta2 = System.nanoTime();
  893.  
  894.   boolean lock = true, showPaths = false, showWorldline = true, showSymmetry = false, setClearPaths = false, firstRun = true, boldFont = true;
  895.   boolean showBlackCounters = true, nextStage = false, showGrid = false, addEvents = false, isFullscreen = false, exitApp = false;
  896.   Color backgroundColor = new Color(170, 180, 180);
  897.   Color whiteish = new Color(232, 222, 212);
  898.   Color blueish = new Color(100, 115, 245);
  899.   Color orangeish = new Color(222, 160, 100);
  900.   Color redish = new Color(222, 106, 100);
  901.   Color violetish = new Color(184, 116, 190);
  902.   Color darkGrey = new Color(110, 110, 111);
  903.  
  904.   ArrayList<Event> eventsArraylist = new ArrayList<Event>();
  905.   ArrayList<Event_p> events_pArraylist = new ArrayList<Event_p>();
  906.  
  907.   ActionListener actionListener = new ActionListener() {
  908.    
  909.     public void actionPerformed(ActionEvent e) {
  910.       String str = e.getActionCommand();
  911.      
  912.       if (str.equals("show/hide paths")) {
  913.         if (showPaths) {
  914.           showPaths = false;
  915.         } else {
  916.           showPaths = true;
  917.         };
  918.       }
  919.      
  920.       if (str.equals("show the symmetry")) {
  921.         if (showSymmetry) {
  922.           showSymmetry = false;
  923.         } else {
  924.           showSymmetry = true;
  925.         };
  926.       }
  927.      
  928.       if (str.equals("white clock (est.)worldline")) {
  929.         if (showWorldline) {
  930.           showWorldline = false;
  931.         } else {
  932.           showWorldline = true;
  933.         };
  934.       }
  935.      
  936.       if (str.equals("reset")) {
  937.         resetAll();
  938.       }
  939.      
  940.       if (str.equals("start/stop")) {
  941.         if (lock) {
  942.           lock = false;
  943.         } else {
  944.           lock = true;
  945.         };
  946.       }
  947.      
  948.       if (str.equals("show light grid")) {
  949.         if (showGrid) {
  950.           showGrid = false;
  951.         } else {
  952.           showGrid = true;
  953.         };
  954.       }
  955.      
  956.       if (str.equals("add more events")) {
  957.         if (addEvents) {
  958.           addEvents = false;
  959.         } else {
  960.           addEvents = true;
  961.         };
  962.       }
  963.      
  964.       if (str.equals("show black clock counters")) {
  965.         if (showBlackCounters) {
  966.           showBlackCounters = false;
  967.         } else {
  968.           showBlackCounters = true;
  969.         };
  970.       }
  971.      
  972.       if (str.equals("exit")) {
  973.         if (exitApp) {
  974.           exitApp = false;
  975.         } else {
  976.           exitApp = true;
  977.         };
  978.       }
  979.      
  980.       if (str.equals("bold font")) {
  981.         if (boldFont) {
  982.           boldFont = false;
  983.           stringFont = new Font("Monospace", Font.PLAIN, 13);
  984.         } else {
  985.           boldFont = true;
  986.           stringFont = new Font("Monospace", Font.BOLD, 15);
  987.         };
  988.       }
  989.      
  990.       if (str.equals("fake Fullscreen")) {
  991.         if (isFullscreen == true) {
  992.           isFullscreen = false;
  993.           JFrame _oldFrame = jFrame;
  994.           createFrameAndCanvases();
  995.           _oldFrame.dispose();
  996.         } else {
  997.           isFullscreen = true;
  998.           JFrame _oldFrame = jFrame;
  999.           createFrameAndCanvases();
  1000.           _oldFrame.dispose();
  1001.         }
  1002.       }
  1003.     }
  1004.   };
  1005.  
  1006.   ChangeListener changeListener = new ChangeListener() {
  1007.     public void stateChanged(ChangeEvent e) {
  1008.       animationSpeed = ((JSlider) e.getSource()).getValue();
  1009.     }
  1010.   };
  1011. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement