Skip to content
This repository has been archived by the owner on Dec 13, 2022. It is now read-only.

Commit

Permalink
Redesign - Final release of v3.0 (#11)
Browse files Browse the repository at this point in the history
* Fixed example 1

* Added verbose option to the CLI and fixed issue #9

* Fixed Example 2

* Make the runtime error pop up a window

* Added the help button

* Fixed issue #8 and improved Error reporting
  • Loading branch information
margual56 committed Nov 13, 2021
1 parent 23c8a4e commit dfa61a4
Show file tree
Hide file tree
Showing 6 changed files with 246 additions and 13 deletions.
Binary file added libs/core.jar
Binary file not shown.
5 changes: 0 additions & 5 deletions src/Exceptions/SyntaxError.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
package Exceptions;

import javax.swing.JOptionPane;

@SuppressWarnings("serial")
public class SyntaxError extends Exception {
String str = "";

public SyntaxError() {
JOptionPane.showMessageDialog(null, "Unknown syntax error", "Syntax error", JOptionPane.ERROR_MESSAGE);
}

public SyntaxError(String text) {
str = text;

JOptionPane.showMessageDialog(null, text, "Syntax error", JOptionPane.ERROR_MESSAGE);
}

@Override
Expand Down
1 change: 1 addition & 0 deletions src/Machines/TMd.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ protected void move(String m) {
this.tape = newtape;

this.head += 2;
this.prevHead += 2;
}
}
}
Expand Down
79 changes: 71 additions & 8 deletions src/app/MainWindow.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
package app;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.io.IOException;
import java.nio.file.Paths;

import javax.swing.BoxLayout;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;
import javax.swing.filechooser.FileNameExtensionFilter;

import Exceptions.RuntimeError;
Expand All @@ -17,6 +25,7 @@
import processing.core.PApplet;

public class MainWindow extends PApplet {
public static final int DEFAULT_FONT_SIZE = 12;
//The default Turing program to execute
String program = "";

Expand All @@ -25,9 +34,11 @@ public class MainWindow extends PApplet {
boolean pause = true, finished;
int state = 1;
int fps = 10;

PButton helpButton;

public void settings() {
size(1200, 1000);
size(min(1200, displayWidth), min(1000, displayHeight));
}

public void setup() {
Expand Down Expand Up @@ -70,27 +81,67 @@ public void setup() {
turing = new TMd(Paths.get(program));
} catch (SyntaxError sE) {
System.err.println(sE);
JOptionPane.showMessageDialog(null, sE, "Syntax Error", JOptionPane.ERROR_MESSAGE);
System.exit(-1);
return;
} catch (Exception e) {
} catch (RuntimeError rE) {
System.err.println(rE);
JOptionPane.showMessageDialog(null, rE, "Runtime Error", JOptionPane.ERROR_MESSAGE);
System.exit(-1);
} catch (IOException e) {
JOptionPane.showMessageDialog(frame, "\"" + program + "\" does not exist");
print(e);
System.exit(-2);
return;
}

text = turing.toString();

helpButton = new PButton(50, height-50, "Show help", this,
(mX, mY, app) -> {
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}

JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.setPreferredSize(new Dimension(640, 200));
panel.setBackground(new Color(44, 44, 44));
panel.setOpaque(true);

JLabel text = new SmoothLabel(
"<html><p style='margin-top: 5'> spacebar &#8594; pause/resume (or restart if the execution has finished)</p><p style='margin-top: 5'> right arrow &#8594; Advance just one instruction forward</p><p style='margin-top: 5'> enter &#8594; Jump to the end of the execution</p><p style='margin-top: 5'> r &#8594; Stop, load a new program and run it</p><p style='margin-top: 5'> ESC &#8594; Exit cleanly</p><p style='margin-top: 5'> + &#8594; Increase speed</p><p style='margin-top: 5'> - &#8594; Decrease speed</p><p style='margin-top: 5'> = &#8594; Restore initial speed</p></html>");

text.setBounds(150, 100, 640-150, 480);
text.setBackground(new Color(44, 44, 44));
text.setForeground(Color.WHITE);
text.setFont(new Font("Consolas", Font.PLAIN, 14));
text.setBorder(new EmptyBorder(0,50,0,0));//top,left,bottom,right
panel.add(text);

frame.getContentPane().add(panel);
frame.setResizable(true);

frame.pack();
frame.setVisible(true);
}
);
helpButton.setBgColor(255);
helpButton.setFgColor(0);
helpButton.setFontSize(15, this);
helpButton.setY(height-helpButton.getHeight()-50);
}

public void draw() {
background(44);

try {
turing.showTapeSummary(50, 50, width - 100, 300, this);
// turing.show(50, 400, width-100, 300, 6);
} catch (Exception error) {
print(error);
}
turing.showTapeSummary(50, 50, width - 100, 300, this);
// turing.show(50, 400, width-100, 300, 6);

try {
textSize(60);
Expand Down Expand Up @@ -122,8 +173,15 @@ public void draw() {
textSize(30);
textAlign(CENTER, TOP);
text(turing.getInstruction(), width/2, height/2+150);

if(helpButton.hovered(mouseX, mouseY))
helpButton.drawHovered(this);
else
helpButton.draw(this);

} catch (RuntimeError e) {
System.err.println(e);
JOptionPane.showMessageDialog(null, e, "Runtime Error", JOptionPane.ERROR_MESSAGE);
}
}

Expand Down Expand Up @@ -164,6 +222,11 @@ public void keyPressed() {
}
}

public void mousePressed() {
if(helpButton.hovered(mouseX, mouseY))
helpButton.pressed(mouseX, mouseY, this);
}

private void doStep() {
if (state <= 0)
finished = true;
Expand Down
150 changes: 150 additions & 0 deletions src/app/PButton.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
package app;

import processing.core.PApplet;

public class PButton {
private static final float DEFAULT_MARGIN_X = 0.4f;
private static final float DEFAULT_MARGIN_Y = 1f;

private float x, y, width, height;
private String text;
private int bgColor, fgColor, hoverColor, hoverStrokeColor, strokeWidth, strokeColor, fontSize;
private Action onPressed;

public PButton(float x, float y, String text, PApplet app, Action act) {
this.x = x;
this.y = y;
this.text = text;

this.bgColor = 255;
this.hoverColor = 200;
this.hoverStrokeColor = 0;
this.fgColor = 0;
this.strokeWidth = 1;
this.strokeColor = -1;

this.fontSize = MainWindow.DEFAULT_FONT_SIZE;
app.textSize(this.fontSize);

this.width = app.textWidth(text) * (1 + DEFAULT_MARGIN_X);
this.height = (app.textAscent() + app.textDescent()) * (1 + DEFAULT_MARGIN_Y);

this.onPressed = act;
}

public void draw(PApplet app) {
app.fill(bgColor);

if(strokeColor != -1) {
app.strokeWeight(strokeWidth);
app.stroke(strokeColor);
}

app.rect(x, y, width, height);
app.strokeWeight(1);

app.textSize(fontSize);
app.fill(fgColor);
app.textAlign(PApplet.CENTER, PApplet.CENTER);
app.text(text, x+width/2.0f, y+height/2.0f);

}

public void drawHovered(PApplet app) {
app.fill(hoverColor);

app.strokeWeight(strokeWidth);
app.stroke(hoverStrokeColor);

app.rect(x, y, width, height);
app.strokeWeight(1);

app.textSize(fontSize);
app.fill(fgColor);
app.textAlign(PApplet.CENTER, PApplet.CENTER);
app.text(text, x+width/2.0f, y+height/2.0f);
}

public boolean hovered(float mX, float mY) {
return mX > x && mX < x+width &&
mY > y && mY < y+height;
}

public void pressed(float mX, float mY, PApplet app) {
this.onPressed.pressed(mX, mY, app);
}

public float getX() {
return x;
}

public void setX(float x) {
this.x = x;
}

public float getY() {
return y;
}

public void setY(float y) {
this.y = y;
}

public float getWidth() {
return width;
}

public void setWidth(float width) {
this.width = width;
}

public float getHeight() {
return height;
}

public void setHeight(float height) {
this.height = height;
}

public String getText() {
return text;
}

public void setText(String text, PApplet app) {
this.text = text;

app.textSize(this.fontSize);

this.width = app.textWidth(text) * (1 + DEFAULT_MARGIN_X);
this.height = (app.textAscent() + app.textDescent()) * (1 + DEFAULT_MARGIN_Y);
}

public void setBgColor(int bgColor) {
this.bgColor = bgColor;
}

public void setFgColor(int fgColor) {
this.fgColor = fgColor;
}

public void setStrokeWidth(int strokeWidth) {
this.strokeWidth = strokeWidth;
}

public void setStrokeColor(int strokeColor) {
this.strokeColor = strokeColor;
}

public void setFontSize(int fontSize, PApplet app) {
this.fontSize = fontSize;

app.textSize(this.fontSize);

this.width = app.textWidth(text) * (1 + DEFAULT_MARGIN_X);
this.height = (app.textAscent() + app.textDescent()) * (1 + DEFAULT_MARGIN_Y);
}

public interface Action {
void pressed(float mX, float mY, PApplet app);
}
}
24 changes: 24 additions & 0 deletions src/app/SmoothLabel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package app;

import javax.swing.JLabel;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;

class SmoothLabel extends JLabel {
/**
*
*/
private static final long serialVersionUID = -488310624992033567L;

public SmoothLabel(String text) {
super(text);
}

@Override
protected void paintComponent(Graphics g) {
((Graphics2D)g).setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
super.paintComponent(g);
}
}

0 comments on commit dfa61a4

Please sign in to comment.