Compare commits

...

4 commits

7 changed files with 363 additions and 3 deletions

16
Labyrinth.txt Normal file
View file

@ -0,0 +1,16 @@
0-6 4-6
4-6 7-6
7-6 9-6
7-6 7-4
7-4 6-4
7-4 9-4
9-4 9-1
7-4 7-1
7-1 5-1
4-6 4-4
4-4 4-3
4-4 1-4
1-4 1-1
1-1 3-1
3-1 3-2
3-1 3-0

View file

@ -154,9 +154,8 @@ public class ExBoxFrame extends JFrame implements ActionListener, ItemListener {
ex.printStackTrace();
}
double scaleFaktor = (double)Toolkit.getDefaultToolkit().getScreenResolution() / 96.0;
setFontSize((int) (11 * scaleFaktor));
setSize(new Dimension((int) (400 * scaleFaktor), (int) (400 * scaleFaktor)));
setFontSize(11);
setSize(new Dimension(400, 400));
setTitle("ExBox");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
initComponents();

View file

@ -0,0 +1,73 @@
package ch.nuth.zhaw.exbox;
import java.awt.Color;
import java.io.BufferedReader;
import java.io.StringReader;
public class LabyrinthServer implements CommandExecutor {
ServerGraphics g = new ServerGraphics();
/**
* Gets a component for creating graphic content.
*
* @return A component for creating graphic content.
*/
public ServerGraphics getGraphics() {
return g;
}
public Graph<DijkstraNode, Edge> createGraph(String s) {
// TODO implement 8.2
Graph<DijkstraNode, Edge> graph = new AdjListGraph<>(DijkstraNode.class, Edge.class);
try {
try (BufferedReader reader = new BufferedReader(new StringReader(s))) {
String currentLine;
while ((currentLine = reader.readLine()) != null) {
String[] elements = currentLine.split(" ");
graph.addEdge(elements[0], elements[1], 0);
}
}
} catch (Throwable e) {
}
return graph;
}
public void drawLabyrinth(Graph<DijkstraNode, Edge> graph) {
// TODO implement 8.3
ServerGraphics graphics = getGraphics();
graphics.setColor(Color.GRAY);
graphics.fillRect(0, 0, 1, 1);
graphics.setColor(Color.WHITE);
for (DijkstraNode node : graph.getNodes()) {
for (Edge edge : node.getEdges()) {
if (edge.getDest() instanceof DijkstraNode destination) {
graphics.drawPath(node.getName(), destination.getName(), false);
}
}
}
}
private boolean search(DijkstraNode current, DijkstraNode ziel) {
// TODO implement 8.4
return false;
}
// search and draw result
public void drawRoute(Graph<DijkstraNode, Edge> graph, String startNode, String zielNode) {
// TODO implement 8.4
}
public String execute(String s) {
Graph<DijkstraNode, Edge> graph;
ServerGraphics graphics = getGraphics();
graphics.clear();
graph = createGraph(s);
drawLabyrinth(graph);
drawRoute(graph, "0-6", "3-0");
return graphics.getTrace();
}
}

View file

@ -0,0 +1,103 @@
package ch.nuth.zhaw.exbox;
import java.awt.Color;
public class ServerGraphics {
private StringBuffer b;
private static ServerGraphics theGraphics;
public static ServerGraphics instance() {
if (theGraphics == null) {
theGraphics = new ServerGraphics();
}
return theGraphics;
}
public ServerGraphics() {
clear();
theGraphics = this;
}
public void clear() {
b = new StringBuffer();
}
public String getTrace() {
return new String(b);
}
private double round(double d) {
return Math.round(d * 10000) / 10000.0;
}
public void drawLine(double x1, double y1, double x2, double y2) {
b.append("<line x1=\"");
b.append(round(x1));
b.append("\" y1=\"");
b.append(round(y1));
b.append("\" x2=\"");
b.append(round(x2));
b.append("\" y2=\"");
b.append(round(y2));
b.append("\"/>\n");
}
public void drawRect(double x, double y, double w, double h) {
b.append("<rect x=\"");
b.append(round(x));
b.append("\" y=\"");
b.append(round(y));
b.append("\" ");
b.append("width=\"");
b.append(round(w));
b.append("\" height=\"");
b.append(round(h));
b.append("\" style=\"draw\" />\n");
}
public void fillRect(double x, double y, double w, double h) {
b.append("<rect x=\"");
b.append(round(x));
b.append("\" y=\"");
b.append(round(y));
b.append("\" ");
b.append("width=\"");
b.append(round(w));
b.append("\" height=\"");
b.append(round(h));
b.append("\" style=\"fill\" />\n");
}
public void setColor(Color c) {
b.append("<color red=\"");
b.append(c.getRed());
b.append("\" green=\"");
b.append(c.getGreen());
b.append("\" blue=\"");
b.append(c.getBlue());
b.append("\"/>\n");
}
public void drawPath(String from, String to, boolean line) {
double scale = 11;
double xh0 = from.charAt(0) - '0';
double yh0 = from.charAt(2) - '0';
double xh1 = to.charAt(0) - '0';
double yh1 = to.charAt(2) - '0';
double x0 = Math.min(xh0, xh1) / scale;
double y0 = Math.min(yh0, yh1) / scale;
double x1 = Math.max(xh0, xh1) / scale;
double y1 = Math.max(yh0, yh1) / scale;
double w = 1 / scale;
if (line) {
drawLine(x0 + w / 2, y0 + w / 2, x1 + w / 2, y1 + w / 2);
} else {
if (Math.abs(y0 - y1) < 1E-10) {
fillRect(x0, y0, x1 - x0 + w, w);
} else {
fillRect(x0, y0, w, y1 - y0 + w);
}
}
}
}

View file

@ -0,0 +1,43 @@
package ch.nuth.zhaw.exbox;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.fail;
/**
* @author K Rege
* @version 1.00 2018/3/17
* @version 1.01 2021/8/1
*/
public class ADS8_2_test {
LabyrinthServer labyrinthServer;
Graph<DijkstraNode, Edge> graph;
@BeforeEach
public void init() {
String labyrinth = "0-6 4-6\n4-6 7-6\n7-6 9-6\n7-6 7-4\n7-4 6-4\n7-4 9-4\n9-4 9-1\n" +
"7-4 7-1\n7-1 5-1\n4-6 4-4\n4-4 4-3\n4-4 1-4\n1-4 1-1\n1-1 3-1\n3-1 3-2\n3-1 3-0\n";
labyrinthServer = new LabyrinthServer();
graph = labyrinthServer.createGraph(labyrinth);
}
private void testEdge(String startName, String destName) {
DijkstraNode node = graph.findNode(startName);
for (Edge edge : node.getEdges()) {
if (edge.getDest().getName().equals(destName)) {
return;
}
}
fail(startName + " not connected to " + destName);
}
@Test
public void testCreateGraph() {
testEdge("0-6", "4-6");
testEdge("4-6", "0-6");
testEdge("1-4", "1-1");
testEdge("3-1", "3-0");
testEdge("3-0", "3-1");
}
}

View file

@ -0,0 +1,57 @@
package ch.nuth.zhaw.exbox;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.fail;
/**
* @author K Rege
* @version 1.00 2018/3/17
* @version 1.01 2021/8/1
*/
public class ADS8_3_test {
LabyrinthServer labyrinthServer;
Graph<DijkstraNode, Edge> graph;
@BeforeEach
public void init() {
String labyrinth = "0-6 4-6\n4-6 7-6\n7-6 9-6\n7-6 7-4\n7-4 6-4\n7-4 9-4\n9-4 9-1\n" +
"7-4 7-1\n7-1 5-1\n4-6 4-4\n4-4 4-3\n4-4 1-4\n1-4 1-1\n1-1 3-1\n3-1 3-2\n3-1 3-0\n";
labyrinthServer = new LabyrinthServer();
graph = labyrinthServer.createGraph(labyrinth);
}
private void testEdge(String startName, String destName) {
DijkstraNode node = graph.findNode(startName);
for (Edge edge : node.getEdges()) {
if (edge.getDest().getName().equals(destName)) {
return;
}
}
fail(startName + " not connected to " + destName);
}
@Test
public void testCreateGraph() {
testEdge("0-6", "4-6");
testEdge("4-6", "0-6");
testEdge("1-4", "1-1");
testEdge("3-1", "3-0");
testEdge("3-0", "3-1");
}
private void testPath(String trace, String path) {
if (!trace.contains(path)) {
fail(path + " not found");
}
}
@Test
public void testDrawLabyrinth() {
labyrinthServer.drawLabyrinth(graph);
String trace = ServerGraphics.instance().getTrace();
testPath(trace, "<rect x=\"0.5455\" y=\"0.3636\" width=\"0.1818\" height=\"0.0909\"");
testPath(trace, "<rect x=\"0.2727\" y=\"0.0909\" width=\"0.0909\" height=\"0.1818\"");
}
}

View file

@ -0,0 +1,69 @@
package ch.nuth.zhaw.exbox;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.fail;
/**
* @author K Rege
* @version 1.00 2018/3/17
* @version 1.01 2021/8/1
*/
public class ADS8_4_test {
LabyrinthServer labyrinthServer;
Graph<DijkstraNode, Edge> graph;
@BeforeEach
public void init() {
String labyrinth = "0-6 4-6\n4-6 7-6\n7-6 9-6\n7-6 7-4\n7-4 6-4\n7-4 9-4\n9-4 9-1\n" +
"7-4 7-1\n7-1 5-1\n4-6 4-4\n4-4 4-3\n4-4 1-4\n1-4 1-1\n1-1 3-1\n3-1 3-2\n3-1 3-0\n";
labyrinthServer = new LabyrinthServer();
graph = labyrinthServer.createGraph(labyrinth);
}
private void testEdge(String startName, String destName) {
DijkstraNode node = graph.findNode(startName);
for (Edge edge : node.getEdges()) {
if (edge.getDest().getName().equals(destName)) {
return;
}
}
fail(startName + " not connected to " + destName);
}
@Test
public void testCreateGraph() {
testEdge("0-6", "4-6");
testEdge("4-6", "0-6");
testEdge("1-4", "1-1");
testEdge("3-1", "3-0");
testEdge("3-0", "3-1");
}
private void testPath(String trace, String path) {
if (!trace.contains(path)) {
fail(path + " not found");
}
}
@Test
public void testDrawLabyrinth() {
labyrinthServer.drawLabyrinth(graph);
String trace = ServerGraphics.instance().getTrace();
testPath(trace, "<rect x=\"0.5455\" y=\"0.3636\" width=\"0.1818\" height=\"0.0909\"");
testPath(trace, "<rect x=\"0.2727\" y=\"0.0909\" width=\"0.0909\" height=\"0.1818\"");
}
@Test
public void testDrawRoute() {
labyrinthServer.drawRoute(graph, "0-6", "3-0");
String trace = ServerGraphics.instance().getTrace();
testPath(trace, "<line x1=\"0.3182\" y1=\"0.0455\" x2=\"0.3182\" y2=\"0.1364\"/>");
testPath(trace, "<line x1=\"0.1364\" y1=\"0.1364\" x2=\"0.3182\" y2=\"0.1364\"/>");
testPath(trace, "<line x1=\"0.1364\" y1=\"0.1364\" x2=\"0.1364\" y2=\"0.4091\"/>");
testPath(trace, "<line x1=\"0.1364\" y1=\"0.4091\" x2=\"0.4091\" y2=\"0.4091\"/>");
testPath(trace, "<line x1=\"0.4091\" y1=\"0.4091\" x2=\"0.4091\" y2=\"0.5909\"/>");
testPath(trace, "<line x1=\"0.0455\" y1=\"0.5909\" x2=\"0.4091\" y2=\"0.5909\"/>");
}
}