Advertisement
Guest User

Jeronimus

a guest
May 27th, 2016
73
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.     new MainClass();
  14.    
  15.   }
  16. }
  17.  
  18.  
  19. class MainClass {
  20.  
  21.   public MainClass() {
  22.    
  23.     createFrameAndCanvases();
  24.     generateEvents();
  25.    
  26.     mainLoop();
  27.   }  
  28.  
  29.   public void mainLoop(){
  30.    
  31.     render();
  32.     jFrame.paintComponents(g2DjFrame);
  33.     jFrame.repaint();
  34.    
  35.     do{
  36.       updateState();
  37.       render();
  38.       try{Thread.sleep(20);} catch (Exception e){};
  39.     } while(true);
  40.   }
  41.  
  42.   public void updateState(){
  43.    
  44.     if(!lock) {
  45.       if (v_rel<1) {
  46.         v_rel+=0.001;
  47.       }
  48.     }
  49.   }
  50.  
  51.   public void render(){
  52.    
  53.     g2Dcanvas1 = (Graphics2D) strategyCanvas1.getDrawGraphics();
  54.     g2Dcanvas2 = (Graphics2D) strategyCanvas2.getDrawGraphics();
  55.     g2DjFrame = (Graphics2D) strategyJFrame.getDrawGraphics();
  56.    
  57.     g2Dcanvas1.setStroke(new BasicStroke(3));
  58.     g2Dcanvas2.setStroke(new BasicStroke(3));
  59.    
  60.     g2Dcanvas1.setColor(backgroundColor);
  61.     g2Dcanvas2.setColor(backgroundColor);
  62.     g2DjFrame.setColor(backgroundColor);
  63.    
  64.     g2Dcanvas1.fillRect(0,0,501,501);
  65.     g2Dcanvas2.fillRect(0,0,501,501);
  66.     g2DjFrame.fillRect(0,0,1280,720);
  67.    
  68.     plotDiagrams();
  69.     plotLightbeams();
  70.     plotEvents(v_rel);
  71.    
  72.     if (showPaths) {
  73.       g2Dcanvas2.drawImage(bufferedImage,0,0,501,501,null);}
  74.    
  75.     jFrame.paintComponents(g2DjFrame);
  76.    
  77.     g2DjFrame.setColor(Color.RED);
  78.     g2DjFrame.setFont(stringFont3);
  79.     g2DjFrame.drawString("Vrel "+(Double.toString(round(v_rel*10000)/10000.0)),600,600);
  80.    
  81.     strategyCanvas2.show();
  82.     strategyCanvas1.show();
  83.     strategyJFrame.show();
  84.    
  85.     g2Dcanvas1.dispose();
  86.     g2Dcanvas2.dispose();
  87.     g2DjFrame.dispose();
  88.   }
  89.  
  90.  
  91.   public void plotLightbeams() {
  92.    
  93.     g2Dcanvas1.setColor(Color.YELLOW);
  94.     g2Dcanvas2.setColor(Color.YELLOW);
  95.    
  96.     g2Dcanvas1.drawLine(0,0,501,501);
  97.     g2Dcanvas1.drawLine(0,501,501,0);
  98.    
  99.     g2Dcanvas2.drawLine(0,0,501,501);
  100.     g2Dcanvas2.drawLine(0,501,501,0);
  101.   }
  102.  
  103.  
  104.   public void plotEvents(double v_rel){
  105.    
  106.     g2Dcanvas1.setFont(stringFont);
  107.     g2Dcanvas2.setFont(stringFont);
  108.    
  109.     double x,t,x_p,t_p;
  110.     vrel = v_rel;
  111.    
  112.     gamma = 1/(sqrt(1-pow(vrel,2))); // c^2 = 1 when vrel is multiple of c  
  113.    
  114.     for (int i=0; i<eventsArraylist.size();i++){
  115.       x=eventsArraylist.get(i).xPosInLightSeconds;
  116.       t=eventsArraylist.get(i).timeInSeconds;
  117.       g2Dcanvas1.setColor(Color.RED);
  118.       if (x==8 && t==4) { g2Dcanvas1.setColor(Color.GREEN);}
  119.       if (x==4 || x==-4) { g2Dcanvas1.setColor(Color.GREEN);}
  120.       if (x==0 ) { g2Dcanvas1.setColor(Color.BLUE);}
  121.       g2Dcanvas1.drawString(Integer.toString((int)t),(int)(x*25+251-4),(int)(251+4-t*25));
  122.      
  123.      
  124.       x_p = gamma*(x - vrel * t);  // c^2 = 1 when vrel is multiple of c so no need for /c^2 in the formula
  125.       t_p = gamma*(t - (vrel*x));
  126.      
  127.       g2Dcanvas2.setColor(Color.RED); g2Dimg.setColor(Color.RED);
  128.       if (x==8 && t==4) { g2Dcanvas2.setColor(Color.GREEN); g2Dimg.setColor(Color.GREEN);}
  129.       if (x==4 || x==-4) { g2Dcanvas2.setColor(Color.GREEN); g2Dimg.setColor(Color.GREEN);}
  130.       if (x==0 ) { g2Dcanvas2.setColor(Color.BLUE); g2Dimg.setColor(Color.BLUE);}
  131.       g2Dcanvas2.drawString(Integer.toString((int)t),(int)(x_p*25+251-4),(int)(251+4-t_p*25));
  132.      
  133.       g2Dimg.drawLine((int)(x_p*25+251),(int)(251-t_p*25),(int)(x_p*25+251),(int)(251-t_p*25));
  134.     }
  135.   }
  136.  
  137.  
  138.   public void createFrameAndCanvases(){
  139.    
  140.     jFrame = new JFrame();
  141.     jFrame.setLayout(null);
  142.     jFrame.setSize(1280,720);
  143.     jFrame.setResizable(false);
  144.     jFrame.setIgnoreRepaint(true);
  145.    
  146.     button1 = new JButton("start/stop");
  147.     button1.setBounds(100,530,130,130);
  148.     button1.addActionListener(actionListener);
  149.     button1.setActionCommand("start/stop");
  150.     jFrame.add(button1);
  151.    
  152.    
  153.     button2 = new JButton("reset");
  154.     button2.setBounds(300,530,130,130);
  155.     button2.addActionListener(actionListener);
  156.     button2.setActionCommand("reset");
  157.     button2.setVisible(true);
  158.     jFrame.add(button2);
  159.    
  160.     canvas1 = new Canvas();
  161.     canvas1.setSize(501,501);
  162.     canvas1.setLocation(75,0);
  163.     canvas1.setIgnoreRepaint(true);
  164.     canvas1.setBackground(new Color(222,222,222));
  165.     jFrame.add(canvas1);
  166.    
  167.     canvas2 = new Canvas();
  168.     canvas2.setSize(501,501);
  169.     canvas2.setLocation(1280-501-75,0);
  170.     canvas2.setIgnoreRepaint(true);
  171.     canvas2.setBackground(new Color(222,222,222));
  172.     jFrame.add(canvas2);
  173.    
  174.     checkBox = new JCheckBox("show/hide paths",false);
  175.     checkBox.addActionListener(actionListener);
  176.     checkBox.setActionCommand("show/hide paths");
  177.     checkBox.setBounds(500,600,200,20);
  178.     jFrame.add(checkBox);
  179.    
  180.    
  181.     jFrame.setVisible(true);
  182.    
  183.     jFrame.createBufferStrategy(2);
  184.    
  185.     canvas1.createBufferStrategy(2);
  186.     canvas2.createBufferStrategy(2);
  187.    
  188.     strategyCanvas1 = canvas1.getBufferStrategy();
  189.     strategyCanvas2 = canvas2.getBufferStrategy();
  190.    
  191.     strategyJFrame = jFrame.getBufferStrategy();
  192.    
  193.     g2DjFrame = (Graphics2D) strategyJFrame.getDrawGraphics();
  194.    
  195.     g2Dcanvas1 = (Graphics2D) strategyCanvas1.getDrawGraphics();
  196.     g2Dcanvas2 = (Graphics2D) strategyCanvas2.getDrawGraphics();
  197.    
  198.    
  199.   }
  200.  
  201.  
  202.   public void plotDiagrams() {
  203.    
  204.     g2Dcanvas1.setFont(stringFont2);
  205.     g2Dcanvas2.setFont(stringFont2);
  206.    
  207.     g2Dcanvas1.setColor(Color.BLACK);
  208.     g2Dcanvas2.setColor(Color.BLACK);
  209.    
  210.     g2Dcanvas1.drawLine(0,250,500,250);
  211.     g2Dcanvas1.drawLine(250,0,250,500);
  212.    
  213.     g2Dcanvas2.drawLine(0,250,500,250);
  214.     g2Dcanvas2.drawLine(250,0,250,500);
  215.    
  216.     int x,t;
  217.     for (x=0; x<21;x++){
  218.       if (x!=10) {
  219.         g2Dcanvas1.drawLine(x*(500/20),245, x*(500/20),255);
  220.         g2Dcanvas1.drawString(Integer.toString(x-10),x*(500/20)-5,268);
  221.         g2Dcanvas2.drawLine(x*(500/20),245, x*(500/20),255);
  222.         g2Dcanvas2.drawString(Integer.toString(x-10),x*(500/20)-5,268);
  223.       }
  224.     }
  225.    
  226.     for (t=0; t<21;t++){
  227.       if (t!=10) {
  228.         g2Dcanvas1.drawLine(245,t*(500/20), 255,t*(500/20));
  229.         g2Dcanvas1.drawString(Integer.toString(10-t),258,t*(500/20)+5);
  230.         g2Dcanvas2.drawLine(245,t*(500/20), 255,t*(500/20));
  231.         g2Dcanvas2.drawString(Integer.toString(10-t),258,t*(500/20)+5);
  232.       }
  233.     }
  234.   }
  235.  
  236.  
  237.   public void generateEvents(){
  238.    
  239.     int x,t;
  240.     for (x=-10; x<11;x++){
  241.       for (t=-10; t<11;t++){
  242.        
  243.         eventsArraylist.add( new Event(x,t));
  244.        
  245.       }
  246.     }
  247.   }
  248.  
  249.  
  250.   class Event {
  251.    
  252.     double xPosInLightSeconds;
  253.     double timeInSeconds;
  254.    
  255.     public Event(int x, int t) {
  256.      
  257.       xPosInLightSeconds = x;
  258.       timeInSeconds = t;
  259.      
  260.     }
  261.   }
  262.  
  263.  
  264.   JFrame jFrame;
  265.   Canvas canvas1,canvas2;
  266.  
  267.   boolean lock=true, showPaths=false;
  268.  
  269.   BufferedImage bufferedImage = new BufferedImage(501, 501,BufferedImage.TYPE_INT_ARGB);
  270.  
  271.   Color backgroundColor = new Color(211,222,222);
  272.   BufferStrategy strategyCanvas1,strategyCanvas2,strategyJFrame;
  273.   Graphics2D g2Dcanvas1, g2Dcanvas2, g2DjFrame, g2Dimg = bufferedImage.createGraphics();;
  274.  
  275.   JButton button1,button2;
  276.   JCheckBox checkBox;
  277.  
  278.   ArrayList<Event> eventsArraylist = new ArrayList<Event>();
  279.  
  280.   double gamma,vrel,v_rel;
  281.  
  282.   Font stringFont = new Font( "SansSerif", Font.BOLD, 14 );
  283.   Font stringFont2 = new Font( "SansSerif", Font.PLAIN, 10 );
  284.   Font stringFont3 = new Font( "SansSerif", Font.PLAIN, 20 );
  285.  
  286.   ActionListener actionListener = new ActionListener() {
  287.    
  288.     public void actionPerformed(ActionEvent e) {
  289.       String str=e.getActionCommand();
  290.      
  291.       if (str.equals("show/hide paths")) {
  292.        
  293.         if (showPaths) {showPaths=false;} else {showPaths=true;};
  294.        
  295.       }
  296.      
  297.       if (str.equals("reset")) {
  298.        
  299.         vrel=0;
  300.         v_rel=0;
  301.         g2Dimg.setBackground(new Color(0,0,0,0));
  302.         g2Dimg.clearRect(0,0,501,501);
  303.         lock = true;
  304.       }
  305.      
  306.       if (str.equals("start/stop")){
  307.         if (lock){lock=false;} else {lock=true;};
  308.       }
  309.     }
  310.   };
  311. }
Advertisement
RAW Paste Data Copied
Advertisement