Advertisement
Guest User

Jeronimus

a guest
May 26th, 2016
78
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.  
  8. class Spacetime {
  9.  
  10. public static void main(String[] args) {
  11.  
  12. new main();
  13.  
  14. }
  15. }
  16.  
  17.  
  18. class main {
  19.  
  20. JFrame jFrame;
  21. Canvas canvas1,canvas2;
  22.  
  23. BufferStrategy strategyCanvas1,strategyCanvas2;
  24. Graphics2D g2Dcanvas1, g2Dcanvas2;
  25.  
  26. ArrayList<Event> eventsArraylist = new ArrayList<Event>();
  27.  
  28. double gamma,vrel,v_rel;
  29.  
  30. Font stringFont = new Font( "SansSerif", Font.BOLD, 14 );
  31. Font stringFont2 = new Font( "SansSerif", Font.PLAIN, 10 );
  32.  
  33.  
  34. ActionListener actionListener = new ActionListener() {
  35.  
  36. public void actionPerformed(ActionEvent e) {
  37.  
  38. String str=e.getActionCommand();
  39.  
  40. if (str.equals("start")){
  41.  
  42. mainloop();
  43.  
  44. }
  45. }
  46. };
  47.  
  48. public main() {
  49.  
  50. createFrameAndCanvases();
  51. generateEvents();
  52.  
  53.  
  54. g2Dcanvas1.setStroke(new BasicStroke(3));
  55. g2Dcanvas2.setStroke(new BasicStroke(3));
  56.  
  57. g2Dcanvas1.setColor(Color.WHITE);
  58. g2Dcanvas2.setColor(Color.WHITE);
  59.  
  60. g2Dcanvas1.fillRect(0,0,501,501);
  61. g2Dcanvas2.fillRect(0,0,501,501);
  62.  
  63. plotDiagrams();
  64. plotLightbeams();
  65. plotEvents(v_rel);
  66.  
  67. strategyCanvas2.show();
  68. strategyCanvas1.show();
  69.  
  70. }
  71.  
  72.  
  73. public void mainloop(){
  74.  
  75.  
  76. for (v_rel=0;v_rel<=0.5;v_rel+=0.001){
  77. plotDiagrams();
  78. plotLightbeams();
  79. plotEvents(v_rel);
  80. try{Thread.sleep(30);} catch (Exception e){};
  81.  
  82. strategyCanvas2.show();
  83. strategyCanvas1.show();
  84.  
  85. g2Dcanvas1.setColor(Color.WHITE);
  86. g2Dcanvas2.setColor(Color.WHITE);
  87.  
  88. g2Dcanvas1.fillRect(0,0,501,501);
  89. g2Dcanvas2.fillRect(0,0,501,501);
  90. }
  91. }
  92.  
  93. public void plotLightbeams() {
  94.  
  95. g2Dcanvas1.setColor(Color.YELLOW);
  96. g2Dcanvas2.setColor(Color.YELLOW);
  97.  
  98. g2Dcanvas1.drawLine(0,0,501,501);
  99. g2Dcanvas1.drawLine(0,501,501,0);
  100.  
  101. g2Dcanvas2.drawLine(0,0,501,501);
  102. g2Dcanvas2.drawLine(0,501,501,0);
  103. }
  104.  
  105.  
  106. public void plotEvents(double v_rel){
  107.  
  108. g2Dcanvas1.setFont(stringFont);
  109. g2Dcanvas2.setFont(stringFont);
  110.  
  111. double x,t,x_p,t_p;
  112. vrel = v_rel;
  113.  
  114. gamma = 1/(sqrt(1-pow(vrel,2))); // c^2 = 1 when vrel is multiple of c
  115.  
  116.  
  117. for (int i=0; i<eventsArraylist.size();i++){
  118. x=eventsArraylist.get(i).xPosInLightSeconds;
  119. t=eventsArraylist.get(i).timeInSeconds;
  120. g2Dcanvas1.setColor(Color.RED);
  121. if (x==8 && t==4) { g2Dcanvas1.setColor(Color.GREEN);}
  122. if (x==4 && t==8) { g2Dcanvas1.setColor(Color.GREEN);}
  123. if (x==0 ) { g2Dcanvas1.setColor(Color.BLUE);}
  124. g2Dcanvas1.drawString(Integer.toString((int)t),(int)(x*25+251-4),(int)(251+4-t*25));
  125.  
  126.  
  127. x_p = gamma*(x - vrel * t); // c^2 = 1 when vrel is multiple of c so no need for /c^2 in the formula
  128. t_p = gamma*(t - (vrel*x));
  129.  
  130.  
  131. g2Dcanvas2.setColor(Color.RED);
  132. if (x==8 && t==4) { g2Dcanvas2.setColor(Color.GREEN);}
  133. if (x==4 && t==8) { g2Dcanvas2.setColor(Color.GREEN);}
  134. if (x==0 ) { g2Dcanvas2.setColor(Color.BLUE);}
  135. g2Dcanvas2.drawString(Integer.toString((int)t),(int)(x_p*25+251-4),(int)(251+4-t_p*25));
  136.  
  137. }
  138. }
  139.  
  140.  
  141. public void createFrameAndCanvases(){
  142.  
  143. jFrame = new JFrame();
  144. jFrame.setLayout(null);
  145. jFrame.setSize(1280,720);
  146. jFrame.setResizable(false);
  147. // jFrame.setIgnoreRepaint(true);
  148.  
  149. JButton button1 = new JButton("start");
  150. button1.setBounds(100,530,130,130);
  151. button1.addActionListener(actionListener);
  152. button1.setActionCommand("start");
  153. jFrame.add(button1);
  154.  
  155.  
  156. canvas1 = new Canvas();
  157. canvas1.setSize(501,501);
  158. canvas1.setLocation(75,0);
  159. canvas1.setIgnoreRepaint(true);
  160. canvas1.setBackground(new Color(222,222,222));
  161. jFrame.add(canvas1);
  162.  
  163. canvas2 = new Canvas();
  164. canvas2.setSize(501,501);
  165. canvas2.setLocation(1280-501-75,0);
  166. canvas2.setIgnoreRepaint(true);
  167. canvas2.setBackground(new Color(222,222,222));
  168. jFrame.add(canvas2);
  169.  
  170. jFrame.setVisible(true);
  171.  
  172. canvas1.createBufferStrategy(2);
  173. canvas2.createBufferStrategy(2);
  174. strategyCanvas1 = canvas1.getBufferStrategy();
  175. strategyCanvas2 = canvas2.getBufferStrategy();
  176.  
  177. g2Dcanvas1 = (Graphics2D) strategyCanvas1.getDrawGraphics();
  178. g2Dcanvas2 = (Graphics2D) strategyCanvas2.getDrawGraphics();
  179. }
  180.  
  181.  
  182. public void plotDiagrams() {
  183.  
  184. g2Dcanvas1.setFont(stringFont2);
  185. g2Dcanvas2.setFont(stringFont2);
  186.  
  187. g2Dcanvas1.setColor(Color.BLACK);
  188. g2Dcanvas2.setColor(Color.BLACK);
  189.  
  190. g2Dcanvas1.drawLine(0,250,500,250);
  191. g2Dcanvas1.drawLine(250,0,250,500);
  192.  
  193. g2Dcanvas2.drawLine(0,250,500,250);
  194. g2Dcanvas2.drawLine(250,0,250,500);
  195.  
  196. int x,t;
  197. for (x=0; x<21;x++){
  198. if (x!=10) {
  199. g2Dcanvas1.drawLine(x*(500/20),245, x*(500/20),255);
  200. g2Dcanvas1.drawString(Integer.toString(x-10),x*(500/20)-5,268);
  201. g2Dcanvas2.drawLine(x*(500/20),245, x*(500/20),255);
  202. g2Dcanvas2.drawString(Integer.toString(x-10),x*(500/20)-5,268);
  203. }
  204. }
  205.  
  206. for (t=0; t<21;t++){
  207. if (t!=10) {
  208. g2Dcanvas1.drawLine(245,t*(500/20), 255,t*(500/20));
  209. g2Dcanvas1.drawString(Integer.toString(10-t),258,t*(500/20)+5);
  210. g2Dcanvas2.drawLine(245,t*(500/20), 255,t*(500/20));
  211. g2Dcanvas2.drawString(Integer.toString(10-t),258,t*(500/20)+5);
  212. }
  213. }
  214. }
  215.  
  216.  
  217. public void generateEvents(){
  218.  
  219. int x,t;
  220. for (x=-10; x<11;x++){
  221. for (t=-10; t<11;t++){
  222.  
  223. eventsArraylist.add( new Event(x,t));
  224.  
  225. }
  226. }
  227. }
  228.  
  229.  
  230.  
  231. class Event {
  232.  
  233. double xPosInLightSeconds;
  234. double timeInSeconds;
  235.  
  236. public Event(int x, int t) {
  237.  
  238. xPosInLightSeconds = x;
  239. timeInSeconds = t;
  240.  
  241. }
  242. }
  243. }
Advertisement
RAW Paste Data Copied
Advertisement