Skip to content
Snippets Groups Projects
Commit 9f040197 authored by niklasBr's avatar niklasBr
Browse files

Gui (schlecht)

parent 2811a5bb
No related branches found
No related tags found
No related merge requests found
import javax.swing.SwingUtilities;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.BorderFactory;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseAdapter;
import java.awt.geom.Line2D;
import java.util.Arrays;
public class Plotter {
public static void plot(ValuesToDraw valuesToDraw) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI(valuesToDraw);
}
});
}
public static void createAndShowGUI(ValuesToDraw valuesToDraw) {
JFrame f = new JFrame("Function Plotter");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new PlotterPanel(valuesToDraw));
f.pack();
f.setVisible(true);
}
}
class PlotterPanel extends JPanel {
final int ZERO_POINT_X = 400;
final int ZERO_POINT_Y = 300;
ValuesToDraw valuesToDraw;
PlotterPanel(ValuesToDraw valuesToDraw) {
this.valuesToDraw = valuesToDraw;
}
public Dimension getPreferredSize() {
return new Dimension(800,600);
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawLine(400, 0, 400, 600);
g.drawLine(0, 300, 800, 300);
int[] xCords = valuesToDraw.getXCords();
int[] yCords = valuesToDraw.getYCords();
System.out.println(Arrays.toString(xCords));
int[] xCordsFromZeroPoint = new int[41];
int[] yCordsFromZeroPoint = new int[41];
for(int i = 0; i < 41; i++) {
xCordsFromZeroPoint[i] = xCords[i] + ZERO_POINT_X;
}
for(int i = 0; i < 41; i++) {
yCordsFromZeroPoint[i] = yCords[i] + ZERO_POINT_Y;
}
g.drawPolyline(xCordsFromZeroPoint, yCordsFromZeroPoint, 21);
}
}
...@@ -109,4 +109,6 @@ class MyPanel extends JPanel { ...@@ -109,4 +109,6 @@ class MyPanel extends JPanel {
g.setColor(Color.BLACK); g.setColor(Color.BLACK);
g.drawRect(squareX,squareY,squareW,squareH); g.drawRect(squareX,squareY,squareW,squareH);
} }
} }
import java.util.ArrayList;
public class ValuesToDraw {
private ArrayList<int[]> xCordArrays;
int i = 0;
ValuesToDraw(ArrayList<int[]> xCordArrays) {
this.xCordArrays = xCordArrays;
}
public int[] getXCords() {
return xCordArrays.get(i++);
}
public int[] getYCords () {
return new int[]{-20, -19, -18, -17, -16, -15, -14, -13, -12, -11, -10,
-9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20};
}
}
...@@ -8,6 +8,11 @@ public class main { ...@@ -8,6 +8,11 @@ public class main {
public static void main(String[] args) throws IOException { public static void main(String[] args) throws IOException {
// values above integer space will produce errors // values above integer space will produce errors
ArrayList<String> stringsFromFile = new ArrayList<>(); ArrayList<String> stringsFromFile = new ArrayList<>();
...@@ -57,17 +62,32 @@ public class main { ...@@ -57,17 +62,32 @@ public class main {
Evaluator e = new Evaluator(); Evaluator e = new Evaluator();
ArrayList<int[]> arraysOfXValues = new ArrayList<>();
for(AstExpression astExpression : rootAstExpressions) { for(AstExpression astExpression : rootAstExpressions) {
int[] xCords = new int[41];
int j = 0;
for(int i = -20; i < 21; i++) { for(int i = -20; i < 21; i++) {
e.setCounter(i); e.setCounter(i);
xCords[j] = (int) e.visit(astExpression);
System.out.println(e.visit(astExpression)); System.out.println(e.visit(astExpression));
j++;
} }
arraysOfXValues.add(xCords);
System.out.println("------------"); System.out.println("------------");
} }
ValuesToDraw valuesToDraw = new ValuesToDraw(arraysOfXValues);
Plotter.plot(valuesToDraw);
//printAst(result); //printAst(result);
......
No preview for this file type
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment