Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import java.util.ArrayList;
- import java.awt.*;
- import java.awt.image.BufferStrategy;
- import static java.lang.Math.*;
- import java.awt.event.*;
- import java.awt.image.BufferedImage;
- import java.awt.geom.Rectangle2D;
- import javax.swing.event.*;
- public class Spacetime {
- public static void main(String[] args) {
- new Spacetime().start();
- }
- double v_rel_MAX = 0.5;
- double totalTravelTime = 20;
- Font stringFont = new Font("Monospace", Font.BOLD, 15);
- Font stringFont2 = new Font("Monospace", Font.PLAIN, 10);
- Font stringFont3 = new Font("Monospace", Font.PLAIN, 18);
- public Spacetime() {
- }
- public void start() {
- createFrameAndCanvases();
- generateEvents();
- generateEvents_p();
- mainLoop();
- }
- public void mainLoop() {
- do {
- updateState();
- render();
- try { Thread.sleep(1);}catch (Exception e){};
- } while (!exitApp);
- jFrame.dispose();
- System.exit(0);
- }
- public void updateState() {
- here:
- if (!lock) {
- if (nextStage == true) {
- g2Dimg.setBackground(new Color(0, 0, 0, 0));
- g2Dimg.clearRect(0, 0, 501, 501);
- nextStage = false;
- if (stage < 6) {
- stage += 1;
- }
- }
- if (stage == 0) {
- nextStage = true;
- }
- if (stage == 1) {
- if ((round(v_rel * 10000) / 10000.0) < v_rel_MAX) {
- accelerate();
- } else {
- lock = true;
- nextStage = true;
- break here;
- }
- }
- if (stage == 2) {
- if ((round(timePassed * 10000) / 10000.0) < totalTravelTime / 2d) {
- letSomeTimePass();
- } else {
- turnAroundTime = timePassed;
- turnAroundPosition = v_rel * timePassed;
- lock = true;
- nextStage = true;
- break here;
- }
- }
- if (stage == 3) {
- if ((round(v_rel * 10000) / 10000.0) > 0) {
- deccelerate();
- } else {
- lock = true;
- nextStage = true;
- break here;
- }
- }
- if (stage == 4) {
- if ((round(v_rel * 10000) / 10000.0) > -v_rel_MAX) {
- deccelerate();
- } else {
- lock = true;
- nextStage = true;
- break here;
- }
- }
- if (stage == 5) {
- if ((round(timePassed * 10000) / 10000.0) < totalTravelTime) {
- letSomeTimePass();
- } else {
- lock = true;
- nextStage = true;
- break here;
- }
- }
- if (stage == 6) {
- if ((round(v_rel * 10000) / 10000.0) < 0) {
- accelerate();
- } else {
- lock = true;
- nextStage = true;
- break here;
- }
- }
- }
- max_gamma = 1 / (sqrt(1 - pow(v_rel_MAX, 2)));
- temporary_gamma = 1 / (sqrt(1 - pow(v_rel, 2)));
- timePassed_p = timePassed / max_gamma;
- if (showSymmetry) {
- timeMoved = timePassed - timePassed_p;
- } else {
- timeMoved = 0;
- };
- if (stage == 1 || stage == 2) {
- traveller_x = v_rel * timePassed;
- blue_ClockCounter = timePassed_p / max_gamma;
- }
- if (stage == 3 || stage == 4 || stage == 5) {
- traveller_x = v_rel * (timePassed - turnAroundTime) + turnAroundPosition;
- }
- if (stage == 2) {
- not_traveller_x_p = -v_rel * timePassed_p;
- rest_length = not_traveller_x_p * max_gamma;
- }
- if (stage == 3 || stage == 4) {
- not_traveller_x_p = rest_length / temporary_gamma;
- turnAroundPosition_p = not_traveller_x_p;
- turnAroundTime_p = timePassed_p;
- }
- if (stage == 5) {
- not_traveller_x_p = -v_rel * (timePassed_p - turnAroundTime_p) + turnAroundPosition_p;
- }
- xPos = 0;
- tPos = temporary_gamma * v_rel * not_traveller_x_p;
- blue_ClockCounter = tPos + timePassed;
- not_traveller_t_p = temporary_gamma * (tPos - v_rel * -traveller_x) + timeMoved;
- }
- public void accelerate() {
- if (stage == 1) {
- if (v_rel + 0.0001 * animationSpeed < v_rel_MAX) {
- v_rel += 0.0001 * animationSpeed;
- } else {
- v_rel = v_rel_MAX;
- }
- }
- if (stage == 6) {
- if (v_rel + 0.0001 * animationSpeed < 0) {
- v_rel += 0.0001 * animationSpeed;
- } else {
- v_rel = 0;
- }
- }
- }
- public void deccelerate() {
- if (stage == 3) {
- if (v_rel - 0.0001 * animationSpeed > 0) {
- v_rel -= 0.0001 * animationSpeed;
- } else {
- v_rel = 0;
- }
- }
- if (stage == 4) {
- if (v_rel - 0.0001 * animationSpeed > -v_rel_MAX) {
- v_rel -= 0.0001 * animationSpeed;
- } else {
- v_rel = -v_rel_MAX;
- }
- }
- }
- public void letSomeTimePass() {
- if (stage == 2) {
- if (timePassed + 0.001 * animationSpeed < totalTravelTime / 2d) {
- timePassed += 0.001 * animationSpeed;
- } else {
- timePassed = totalTravelTime / 2d;
- }
- }
- if (stage == 5) {
- if (timePassed + 0.001 * animationSpeed < totalTravelTime) {
- timePassed += 0.001 * animationSpeed;
- } else {
- timePassed = totalTravelTime;
- }
- }
- }
- public void plotLightGrid() {
- for (int i = 0; i < eventsArraylist.size(); i++) {
- x = eventsArraylist.get(i).xPosInLightSeconds;
- t = eventsArraylist.get(i).timeInSeconds - timePassed;
- if ((x < 11 && x > -11) && (t < 11 && t > -11)) {
- g2Dcanvas1.setColor(Color.YELLOW);
- g2Dcanvas1.setStroke(new BasicStroke(1));
- 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));
- 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));
- }
- if ((x < 11 + traveller_x && x > -11 + traveller_x) && (t < 11 && t > -11)) {
- clockCounter = eventsArraylist.get(i).clockCounter;
- x_p = temporary_gamma * (x - traveller_x - v_rel * t);
- t_p = temporary_gamma * (t - (v_rel * (x - traveller_x)));
- g2Dcanvas2.setColor(Color.YELLOW);
- g2Dcanvas2.setStroke(new BasicStroke(1));
- if (v_rel > 0) {
- factor = pow(temporary_gamma, 4);
- } else {
- factor = 1 / pow(temporary_gamma, 4);
- }
- 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));
- 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));
- }
- }
- }
- public void plotEvents() {
- g2Dcanvas1.setFont(stringFont);
- g2Dcanvas2.setFont(stringFont);
- for (int i = 0; i < eventsArraylist.size(); i++) {
- x = eventsArraylist.get(i).xPosInLightSeconds;
- t = eventsArraylist.get(i).timeInSeconds - timePassed;
- clockCounter = eventsArraylist.get(i).clockCounter;
- x_p = temporary_gamma * (x - traveller_x - v_rel * t);
- t_p = temporary_gamma * (t - (v_rel * (x - traveller_x)));
- message = Integer.toString((int) clockCounter);
- midWidth = mW(message, stringFont);
- midHeight = mH(message, stringFont);
- if ((x < 11 && x > -11) && (t < 11 && t > -11)) {
- g2Dcanvas1.setColor(Color.BLACK);
- if (x == 5 || x == -5) {
- g2Dcanvas1.setColor(Color.GREEN);
- g2Dcanvas1.drawString(message, (int) (x * 25 + 251 - midWidth), (int) (251 + midHeight - t * 25));
- }
- if (x == 0) {
- g2Dcanvas1.setColor(Color.BLUE);
- g2Dcanvas1.drawString(message, (int) (x * 25 + 251 - midWidth), (int) (251 + midHeight - t * 25));
- }
- if (showBlackCounters) {
- g2Dcanvas1.drawString(message, (int) (x * 25 + 251 - midWidth), (int) (251 + midHeight - t * 25));
- }
- }
- if ((x < 11 + traveller_x && x > -11 + traveller_x) && (t < 11 && t > -11)) {
- g2Dimg.setColor(Color.BLACK);
- g2Dcanvas2.setColor(Color.BLACK);
- if (showBlackCounters) {
- g2Dcanvas2.drawString(message, (int) (x_p * 25 + 251 - midWidth), (int) (251 + midHeight - t_p * 25));
- }
- if (x == 5 || x == -5) {
- g2Dcanvas2.setColor(Color.GREEN);
- g2Dimg.setColor(Color.GREEN);
- g2Dcanvas2.drawString(message, (int) (x_p * 25 + 251 - midWidth), (int) (251 + midHeight - (t_p - timeMoved) * 25));
- }
- if (x == 0) {
- g2Dcanvas2.setColor(Color.BLUE);
- g2Dimg.setColor(Color.BLUE);
- g2Dcanvas2.drawString(message, (int) (x_p * 25 + 251 - midWidth), (int) (251 + midHeight - (t_p - timeMoved) * 25));
- }
- x_p0 = -temporary_gamma * traveller_x;
- t_p0 = temporary_gamma * (v_rel * traveller_x);
- g2Dimg.setStroke(new BasicStroke(2));
- g2Dimg.drawLine((int) (x_p * 25 + 251), (int) (251 - t_p * 25), (int) (x_p * 25 + 251), (int) (251 - t_p * 25));
- }
- }
- for (int i = 0; i < events_pArraylist.size(); i++) {
- x = events_pArraylist.get(i).xPosInLightSeconds;
- t = events_pArraylist.get(i).timeInSeconds - timePassed_p;
- if ((x < 11 && x > -11) && (t < 11 && t > -11) && (showWorldline == true)) {
- clockCounter = events_pArraylist.get(i).clockCounter;
- message = Integer.toString((int) clockCounter);
- midWidth = mW(message, stringFont);
- midHeight = mH(message, stringFont);
- x_p = temporary_gamma * (x + v_rel * t) + traveller_x;
- t_p = temporary_gamma * (t + v_rel * x);
- g2Dcanvas1.setColor(Color.WHITE);
- /* if ((x==5 || x ==-5) && showSymmetry==true ) {//g2Dcanvas1.setColor(new Color(55,155,55));}
- if (x==0 && showSymmetry==true ) {//g2Dcanvas1.setColor(new Color(100,115,211));}*/
- g2Dcanvas1.drawString(message, (int) (x_p * 25 + 251 - midWidth), (int) (251 + midHeight - t_p * 25));
- g2Dcanvas2.setColor(Color.WHITE);
- /* if ((x==5 || x ==-5) && showSymmetry==true ) {g2Dcanvas2.setColor(new Color(55,155,55));}
- if (x==0 && showSymmetry==true ) {g2Dcanvas2.setColor(new Color(100,115,211));}*/
- g2Dcanvas2.drawString(message, (int) (x * 25 + 251 - midWidth), (int) (251 + midHeight - (t - timeMoved) * 25));
- }
- }
- }
- public int mH(String message, Font whichFont) {
- int fontHeight;
- bounds = whichFont.getStringBounds(message, g2Dcanvas1.getFontRenderContext());
- fontHeight = (int) bounds.getHeight();
- return fontHeight / 4;
- }
- public int mW(String message, Font whichFont) {
- int fontWidth;
- bounds = whichFont.getStringBounds(message, g2Dcanvas1.getFontRenderContext());
- fontWidth = (int) bounds.getWidth();
- return fontWidth / 2;
- }
- public void render() {
- g2Dcanvas1 = (Graphics2D) strategyCanvas1.getDrawGraphics();
- g2Dcanvas2 = (Graphics2D) strategyCanvas2.getDrawGraphics();
- g2Dcanvas3 = (Graphics2D) strategyCanvas3.getDrawGraphics();
- g2Dcanvas1.scale(scaleFactor, scaleFactor);
- g2Dcanvas2.scale(scaleFactor, scaleFactor);
- g2Dcanvas3.scale(scaleFactor, scaleFactor);
- /*g2Dimg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
- RenderingHints.VALUE_ANTIALIAS_ON);
- g2Dcanvas1.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
- RenderingHints.VALUE_ANTIALIAS_ON);
- g2Dcanvas2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
- RenderingHints.VALUE_ANTIALIAS_ON);
- g2Dcanvas3.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
- RenderingHints.VALUE_ANTIALIAS_ON);
- */
- g2Dcanvas1.setColor(backgroundColor);
- g2Dcanvas2.setColor(backgroundColor);
- g2Dcanvas3.setColor(darkGrey);
- g2Dcanvas1.fillRect(0, 0, 501, 501);
- g2Dcanvas2.fillRect(0, 0, 501, 501);
- g2Dcanvas3.fillRect(0, 0, 1280, 720);
- if (setClearPaths) {
- g2Dimg.setBackground(new Color(0, 0, 0, 0));
- g2Dimg.clearRect(0, 0, 501, 501);
- setClearPaths = false;
- }
- if (showGrid) {
- plotLightGrid();
- }
- plotMovingClocks();
- if (showPaths) {
- g2Dcanvas2.drawImage(bufferedImage, 0, 0, 501, 501, null);
- }
- g2Dcanvas1.setStroke(new BasicStroke(2));
- g2Dcanvas2.setStroke(new BasicStroke(2));
- plotLightbeams();
- g2Dcanvas1.setStroke(new BasicStroke(1));
- g2Dcanvas2.setStroke(new BasicStroke(1));
- plotDiagrams();
- g2Dcanvas1.setStroke(new BasicStroke(3));
- g2Dcanvas2.setStroke(new BasicStroke(3));
- plotEvents();
- plotValues();
- strategyCanvas1.show();
- strategyCanvas2.show();
- strategyCanvas3.show();
- g2Dcanvas1.dispose();
- g2Dcanvas2.dispose();
- g2Dcanvas3.dispose();
- jFrame.repaint();
- }
- public void plotValues() {
- int offsetX = (int) (canvas2.getBounds().x / scaleFactor);
- int offsetX2 = (int) (canvas1.getBounds().x / scaleFactor);
- g2Dcanvas3.setColor(Color.BLACK);
- g2Dcanvas3.setFont(stringFont3);
- g2Dcanvas3.drawString("Vrel " + (Double.toString(round(v_rel * 10000) / 10000.0) + "c"), 590, 100);
- g2Dcanvas3.setColor(Color.MAGENTA);
- g2Dcanvas3.drawString("stage " + stage, 600, 150);
- g2Dcanvas3.setColor(Color.CYAN);
- g2Dcanvas3.drawString("Observer clock count " + (Double.toString(round(timePassed * 10000) / 10000.0)) + "s", offsetX2, 520);
- g2Dcanvas3.drawString("x-Pos / t-Pos: 0ls / 0s", offsetX2, 550);
- g2Dcanvas3.setColor(whiteish);
- g2Dcanvas3.drawString("White clock count " + (Double.toString(round(timePassed_p * 10000) / 10000.0)) + "s", offsetX2 + 300, 520);
- g2Dcanvas3.drawString("x Position " + (Double.toString(round(traveller_x * 10000) / 10000.0)) + "ls", offsetX2 + 300, 550);
- g2Dcanvas3.drawString("t Position 0s", offsetX2 + 300, 570);
- g2Dcanvas3.setColor(whiteish);;
- g2Dcanvas3.drawString("Observer clock count " + (Double.toString(round(timePassed_p * 10000) / 10000.0)) + "s", offsetX, 520);
- g2Dcanvas3.drawString("x-Pos / t-Pos: 0ls / 0s ", offsetX, 550);
- g2Dcanvas3.setColor(Color.BLUE);
- g2Dcanvas3.drawString("Blue clock count " + (Double.toString(round(blue_ClockCounter * 10000) / 10000.0)) + "s", offsetX + 300, 520);
- g2Dcanvas3.drawString("x Position " + (Double.toString(round(not_traveller_x_p * 10000) / 10000.0)) + "ls", offsetX + 300, 550);
- g2Dcanvas3.drawString("t Position 0s", offsetX + 300, 570);
- g2Dcanvas3.drawString("Blue clock count " + (Double.toString(round(blue_ClockCounter * 10000) / 10000.0)) + "s", offsetX2, 590);
- g2Dcanvas3.drawString("x Position 0ls", offsetX2, 620);
- g2Dcanvas3.drawString("t Position " + (Double.toString(round(tPos * 10000) / 10000.0)) + "s", offsetX2, 640);
- }
- public void plotMovingClocks() {
- if (addEvents == true) {
- if (showSymmetry == true) {
- offsetTime = timeMoved;
- } else {
- offsetTime = 0;
- }
- if (showSymmetry == false) {
- offsetTime2 = timePassed - timePassed_p;
- offsetTime = offsetTime2;
- } else {
- offsetTime2 = 0;
- }
- g2Dcanvas2.setColor(orangeish);
- g2Dcanvas2.fillOval((int) (-xPos * 25 - 12 + 251), (int) ((-tPos - offsetTime2) * 25 - 12 + 251), 24, 24);
- x = temporary_gamma * (xPos + v_rel * (tPos + offsetTime)) + traveller_x;
- t = temporary_gamma * (tPos + offsetTime - (-v_rel * xPos));
- g2Dcanvas1.setColor(orangeish);
- g2Dcanvas1.fillOval((int) (x * 25 - 12 + 251), (int) (-t * 25 - 12 + 251), 24, 24);
- g2Dcanvas2.setColor(redish);
- g2Dcanvas2.fillOval((int) (-traveller_x * 25 - 11 + 251), (int) ((traveller_t - offsetTime2) * 25 - 11 + 251), 22, 22);
- x = temporary_gamma * (-traveller_x + v_rel * (-traveller_t + offsetTime)) + traveller_x;
- t = temporary_gamma * (-traveller_t + offsetTime - (-v_rel * -traveller_x));
- g2Dcanvas1.setColor(redish);
- g2Dcanvas1.fillOval((int) (x * 25 - 11 + 251), (int) ((-t) * 25 - 11 + 251), 22, 22);
- g2Dcanvas2.setColor(violetish);
- g2Dcanvas2.fillOval(251 - 10, (int) (251 - 10 - offsetTime2 * 25), 20, 20);
- x = temporary_gamma * (0 + v_rel * (0 + offsetTime)) + traveller_x;
- t = temporary_gamma * (0 + offsetTime - (-v_rel * 0));
- g2Dcanvas1.setColor(violetish);
- g2Dcanvas1.fillOval((int) (x * 25 - 10 + 251), (int) (-t * 25 - 10 + 251), 20, 20);
- }
- g2Dcanvas1.setColor(blueish);
- g2Dcanvas1.fillOval((int) (xPos * 25 - 12 + 251), (int) (-tPos * 25 - 12 + 251), 24, 24);
- g2Dcanvas1.setColor(whiteish);
- g2Dcanvas1.fillOval((int) (traveller_x * 25 - 11 + 251), (int) (traveller_t * 25 - 12 + 251), 22, 22);
- g2Dcanvas1.setColor(Color.CYAN);
- g2Dcanvas1.fillOval(251 - 10, 251 - 10, 20, 20);
- g2Dcanvas2.setColor(blueish);
- g2Dcanvas2.fillOval((int) (not_traveller_x_p * 25 - 12 + 251), (int) ((not_traveller_t_p) * 25 - 12 + 251), 24, 24);
- g2Dcanvas2.setColor(Color.CYAN);
- g2Dcanvas2.fillOval((int) (x_p0 * 25 + 251 - 11), (int) ((-t_p0 + timeMoved) * 25 + 251 - 11), 22, 22);
- g2Dcanvas2.setColor(whiteish);
- g2Dcanvas2.fillOval(251 - 10, (int) (251 - 10 + timeMoved * 25), 20, 20);
- }
- public void plotLightbeams() {
- g2Dcanvas1.setColor(Color.YELLOW);
- g2Dcanvas2.setColor(Color.YELLOW);
- g2Dcanvas1.drawLine(0, 0, 501, 501);
- g2Dcanvas1.drawLine(0, 501, 501, 0);
- g2Dcanvas2.drawLine(0, 0, 501, 501);
- g2Dcanvas2.drawLine(0, 501, 501, 0);
- }
- public void createFrameAndCanvases() {
- jFrame = new JFrame();
- jFrame.setLayout(null);
- jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- jFrame.getContentPane().setBackground(darkGrey);
- jFrame.setResizable(true);
- jFrame.setIgnoreRepaint(false);
- myPanel = new JPanel();
- myPanel.setSize(1280, 720);
- myPanel.setLayout(null);
- myPanel.setIgnoreRepaint(false);
- myPanel.setBackground(darkGrey);
- guiPanel = new JPanel();
- guiPanel.setSize(640, 100);
- guiPanel.setLocation(1280 - 640 - 320, 502 + 80);
- guiPanel.setLayout(null);
- guiPanel.setIgnoreRepaint(true);
- guiPanel.setBackground(new Color(110, 110, 111));
- myPanel.add(guiPanel);
- button1 = new JButton("start/stop");
- button1.setBounds(210, 0, 100, 100);
- button1.addActionListener(actionListener);
- button1.setActionCommand("start/stop");
- guiPanel.add(button1);
- button2 = new JButton("reset");
- button2.setBounds(330, 0, 100, 100);
- button2.addActionListener(actionListener);
- button2.setActionCommand("reset");
- button2.setVisible(true);
- guiPanel.add(button2);
- button3 = new JButton("exit app");
- button3.setBounds(100, 80, 100, 20);
- button3.addActionListener(actionListener);
- button3.setActionCommand("exit");
- button3.setVisible(true);
- guiPanel.add(button3);
- canvas1 = new Canvas();
- canvas1.setSize(501, 501);
- canvas1.setLocation(75, 0);
- canvas1.setIgnoreRepaint(true);
- canvas1.setBackground(new Color(222, 222, 222));
- myPanel.add(canvas1);
- canvas2 = new Canvas();
- canvas2.setSize(501, 501);
- canvas2.setLocation(1280 - 501 - 75, 0);
- canvas2.setIgnoreRepaint(true);
- canvas2.setBackground(new Color(222, 222, 222));
- myPanel.add(canvas2);
- canvas3 = new Canvas();
- canvas3.setSize(1280, 720);
- canvas3.setLocation(0, 0);
- canvas3.setIgnoreRepaint(true);
- canvas3.setBackground(new Color(222, 222, 222));
- myPanel.add(canvas3);
- checkBox = new JCheckBox("show/hide paths", showPaths);
- checkBox.addActionListener(actionListener);
- checkBox.setActionCommand("show/hide paths");
- checkBox.setBounds(440, 0, 200, 20);
- guiPanel.add(checkBox);
- checkBox1 = new JCheckBox("show symmetry (<=stage2)", showSymmetry);
- checkBox1.addActionListener(actionListener);
- checkBox1.setActionCommand("show the symmetry");
- checkBox1.setBounds(440, 20, 200, 20);
- guiPanel.add(checkBox1);
- checkBox2 = new JCheckBox("white clock (est.)worldline", showWorldline);
- checkBox2.addActionListener(actionListener);
- checkBox2.setActionCommand("white clock (est.)worldline");
- checkBox2.setBounds(440, 40, 200, 20);
- guiPanel.add(checkBox2);
- checkBox3 = new JCheckBox("show light grid", showGrid);
- checkBox3.addActionListener(actionListener);
- checkBox3.setActionCommand("show light grid");
- checkBox3.setBounds(0, 20, 200, 20);
- guiPanel.add(checkBox3);
- checkBox4 = new JCheckBox("add more events", addEvents);
- checkBox4.addActionListener(actionListener);
- checkBox4.setActionCommand("add more events");
- checkBox4.setBounds(0, 40, 200, 20);
- guiPanel.add(checkBox4);
- checkBox5 = new JCheckBox("show black clock counters", showBlackCounters);
- checkBox5.addActionListener(actionListener);
- checkBox5.setActionCommand("show black clock counters");
- checkBox5.setBounds(0, 60, 200, 20);
- guiPanel.add(checkBox5);
- checkBox6 = new JCheckBox("fullscreen(borderless window)", isFullscreen);
- checkBox6.addActionListener(actionListener);
- checkBox6.setActionCommand("fake Fullscreen");
- checkBox6.setBounds(0, 0, 200, 20);
- guiPanel.add(checkBox6);
- checkBox7 = new JCheckBox("bold font", boldFont);
- checkBox7.addActionListener(actionListener);
- checkBox7.setActionCommand("bold font");
- checkBox7.setBounds(0, 80, 90, 20);
- guiPanel.add(checkBox7);
- slider = new JSlider(0, 100, 40);
- slider.addChangeListener(changeListener);
- slider.setBounds(440, 60, 200, 40);
- slider.setMinorTickSpacing(2);
- slider.setMajorTickSpacing(20);
- slider.setPaintTicks(true);
- slider.setPaintLabels(true);
- guiPanel.add(slider);
- jFrame.add(myPanel);
- if (isFullscreen) {
- jFrame.setUndecorated(true);
- jFrame.setSize((int)Toolkit.getDefaultToolkit().getScreenSize().getWidth(), (int) Toolkit.getDefaultToolkit().getScreenSize().getHeight());
- } else {
- jFrame.setSize(1280, 720);
- jFrame.setUndecorated(false);
- }
- jFrame.setVisible(true);
- canvas1.createBufferStrategy(2);
- canvas2.createBufferStrategy(2);
- canvas3.createBufferStrategy(2);
- strategyCanvas1 = canvas1.getBufferStrategy();
- strategyCanvas2 = canvas2.getBufferStrategy();
- strategyCanvas3 = canvas3.getBufferStrategy();
- g2Dcanvas1 = (Graphics2D) strategyCanvas1.getDrawGraphics();
- g2Dcanvas2 = (Graphics2D) strategyCanvas2.getDrawGraphics();
- g2Dcanvas3 = (Graphics2D) strategyCanvas2.getDrawGraphics();
- jFrame.addComponentListener(new ComponentAdapter() {
- public void componentResized(ComponentEvent e) {
- if (jFrame.getWidth() < 1300 || jFrame.getHeight() < 740) {
- scaleFactor = 1;
- if (canvas1.getWidth() > 501) {
- canvas1.setSize(501, 501);
- canvas2.setSize(501, 501);
- }
- myPanel.setSize(1280, 720);
- canvas2.setLocation(1280 - 501 - 75, 0);
- guiPanel.setLocation(1280 - 640 - 320, 502 + 80);
- jFrame.repaint();
- } else {
- myPanel.setSize(jFrame.getWidth(), jFrame.getHeight());
- canvas1.setSize((int) (jFrame.getWidth() * 501 / 1280.0), (int) (jFrame.getWidth() * 501 / 1280.0));
- canvas2.setSize((int) (jFrame.getWidth() * 501 / 1280.0), (int) (jFrame.getWidth() * 501 / 1280.0));
- canvas2.setLocation(jFrame.getWidth() - canvas2.getWidth() - 75, 0);
- canvas3.setSize((int) (jFrame.getWidth()), (int) (jFrame.getHeight()));
- scaleFactor = jFrame.getWidth() / 1280.0;
- guiPanel.setLocation((int) (myPanel.getWidth() / 2.0 - 320), (int) (myPanel.getHeight() - 100 - 40));
- jFrame.repaint();
- }
- }
- });
- }
- public void plotDiagrams() {
- g2Dcanvas1.setFont(stringFont2);
- g2Dcanvas2.setFont(stringFont2);
- g2Dcanvas1.setColor(Color.BLACK);
- g2Dcanvas2.setColor(Color.BLACK);
- g2Dcanvas1.drawLine(0, 250, 500, 250);
- g2Dcanvas1.drawLine(250, 0, 250, 500);
- g2Dcanvas2.drawLine(0, 250, 500, 250);
- g2Dcanvas2.drawLine(250, 0, 250, 500);
- int x, t;
- for (x = 0; x < 21; x++) {
- if (x != 10) {
- message = Integer.toString(x - 10);
- midWidth = mW(message, stringFont2);
- midHeight = mH(message, stringFont2);
- g2Dcanvas1.drawLine(x * (500 / 20), 245, x * (500 / 20), 255);
- g2Dcanvas1.drawString(message, x * (500 / 20) - midWidth, 251 + midHeight + 10);
- g2Dcanvas2.drawLine(x * (500 / 20), 245, x * (500 / 20), 255);
- g2Dcanvas2.drawString(message, x * (500 / 20) - midWidth, 251 + midHeight + 10);
- }
- }
- for (t = 0; t < 21; t++) {
- if (t != 10) {
- message = Integer.toString(10 - t);
- midWidth = mW(message, stringFont2);
- midHeight = mH(message, stringFont2);
- g2Dcanvas1.drawLine(245, t * (500 / 20), 255, t * (500 / 20));
- g2Dcanvas1.drawString(message, 251 + midWidth + 5, t * (500 / 20) + midHeight);
- g2Dcanvas2.drawLine(245, t * (500 / 20), 255, t * (500 / 20));
- g2Dcanvas2.drawString(message, 251 + midWidth + 5, t * (500 / 20) + midHeight);
- }
- }
- }
- public void generateEvents() {
- int i = 0;
- int x, t, clockCounter;
- for (x = -(int) totalTravelTime; x < (int) totalTravelTime + 21; x++) {
- for (t = -(int) totalTravelTime; t < (int) totalTravelTime + 21; t++) {
- clockCounter = t;
- eventsArraylist.add(new Event(x, t, clockCounter));
- if (x == 0 && t == 0) {
- zeroEvent = i;
- }
- i++;
- }
- }
- }
- public void generateEvents_p() {
- int x, t, clockCounter;
- x = 0;
- for (t = -(int) totalTravelTime; t < (int) totalTravelTime + 21; t++) {
- clockCounter = t;
- events_pArraylist.add(new Event_p(x, t, clockCounter));
- }
- x = -5;
- for (t = -(int) totalTravelTime; t < (int) totalTravelTime + 21; t++) {
- clockCounter = t;
- events_pArraylist.add(new Event_p(x, t, clockCounter));
- }
- x = 5;
- for (t = -(int) totalTravelTime + 10; t < (int) totalTravelTime + 11; t++) {
- clockCounter = t;
- events_pArraylist.add(new Event_p(x, t, clockCounter));
- }
- }
- class Event_p {
- double xPosInLightSeconds;
- double timeInSeconds;
- double clockCounter;
- public Event_p(int x, int t, int cc) {
- xPosInLightSeconds = x;
- timeInSeconds = t;
- clockCounter = cc;
- }
- }
- class Event {
- double xPosInLightSeconds;
- double timeInSeconds;
- double clockCounter;
- public Event(int x, int t, int cc) {
- xPosInLightSeconds = x;
- timeInSeconds = t;
- clockCounter = cc;
- }
- }
- public void resetAll() {
- v_rel = 0;
- timePassed = 0;
- not_traveller_x_p = 0;
- traveller_x = 0;
- nextStage = false;
- stage = 0;
- setClearPaths = true;
- lock = true;
- }
- JFrame jFrame;
- JPanel guiPanel, myPanel;
- JSlider slider;
- Canvas canvas1, canvas2, canvas3;
- BufferedImage bufferedImage = new BufferedImage(501, 501, BufferedImage.TYPE_INT_ARGB);
- BufferedImage bufferedImageClock;
- BufferStrategy strategyCanvas1, strategyCanvas2, strategyCanvas3;
- Graphics2D g2Dcanvas1, g2Dcanvas2, g2Dcanvas3, g2Dimg = bufferedImage.createGraphics(), g2DimgClock;
- JButton button1, button2, button3;
- JCheckBox checkBox, checkBox1, checkBox2, checkBox3, checkBox4, checkBox5, checkBox6, checkBox7;
- double turnAroundTime, turnAroundPosition;
- double traveller_x, traveller_t, not_traveller_x_p, not_traveller_t_p;
- double x, t, x_p, t_p, clockCounter;
- double rest_length;
- double timePassed_p, turnAroundPosition_p, turnAroundTime_p;
- double max_gamma, blue_ClockCounter;
- double temporary_gamma, v_rel, timePassed, timeMoved;
- double xPos, tPos;
- double x_p0, t_p0;
- double factor = 1;
- double offsetTime, offsetTime2, scaleFactor = 1;
- int zeroEvent;
- int animationSpeed = 40;
- int midWidth, midHeight;
- String message;
- Rectangle2D bounds;
- int stage = 0;
- int tickUPS;
- int tickFPS;
- long delta = System.nanoTime();
- long deltaNanoTime1, deltaNanoTime2, timeStampA = 0, timeStampB = 0;
- double UPS, FPS;
- long delta2 = System.nanoTime();
- boolean lock = true, showPaths = false, showWorldline = true, showSymmetry = false, setClearPaths = false, firstRun = true, boldFont = true;
- boolean showBlackCounters = true, nextStage = false, showGrid = false, addEvents = false, isFullscreen = false, exitApp = false;
- Color backgroundColor = new Color(170, 180, 180);
- Color whiteish = new Color(232, 222, 212);
- Color blueish = new Color(100, 115, 245);
- Color orangeish = new Color(222, 160, 100);
- Color redish = new Color(222, 106, 100);
- Color violetish = new Color(184, 116, 190);
- Color darkGrey = new Color(110, 110, 111);
- ArrayList<Event> eventsArraylist = new ArrayList<Event>();
- ArrayList<Event_p> events_pArraylist = new ArrayList<Event_p>();
- ActionListener actionListener = new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- String str = e.getActionCommand();
- if (str.equals("show/hide paths")) {
- if (showPaths) {
- showPaths = false;
- } else {
- showPaths = true;
- };
- }
- if (str.equals("show the symmetry")) {
- if (showSymmetry) {
- showSymmetry = false;
- } else {
- showSymmetry = true;
- };
- }
- if (str.equals("white clock (est.)worldline")) {
- if (showWorldline) {
- showWorldline = false;
- } else {
- showWorldline = true;
- };
- }
- if (str.equals("reset")) {
- resetAll();
- }
- if (str.equals("start/stop")) {
- if (lock) {
- lock = false;
- } else {
- lock = true;
- };
- }
- if (str.equals("show light grid")) {
- if (showGrid) {
- showGrid = false;
- } else {
- showGrid = true;
- };
- }
- if (str.equals("add more events")) {
- if (addEvents) {
- addEvents = false;
- } else {
- addEvents = true;
- };
- }
- if (str.equals("show black clock counters")) {
- if (showBlackCounters) {
- showBlackCounters = false;
- } else {
- showBlackCounters = true;
- };
- }
- if (str.equals("exit")) {
- if (exitApp) {
- exitApp = false;
- } else {
- exitApp = true;
- };
- }
- if (str.equals("bold font")) {
- if (boldFont) {
- boldFont = false;
- stringFont = new Font("Monospace", Font.PLAIN, 13);
- } else {
- boldFont = true;
- stringFont = new Font("Monospace", Font.BOLD, 15);
- };
- }
- if (str.equals("fake Fullscreen")) {
- if (isFullscreen == true) {
- isFullscreen = false;
- JFrame _oldFrame = jFrame;
- createFrameAndCanvases();
- _oldFrame.dispose();
- } else {
- isFullscreen = true;
- JFrame _oldFrame = jFrame;
- createFrameAndCanvases();
- _oldFrame.dispose();
- }
- }
- }
- };
- ChangeListener changeListener = new ChangeListener() {
- public void stateChanged(ChangeEvent e) {
- animationSpeed = ((JSlider) e.getSource()).getValue();
- }
- };
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement