diff --git a/Semester/.idea/.gitignore b/Semester/.idea/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..ae117de46c8ff6a662b3b4492c9abddb77a7ea6e --- /dev/null +++ b/Semester/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Datasource local storage ignored files +/../../../../../../../../../:\Users\enesh\Desktop\BachelorProjekt\compiler\praxisprojekt-lernsoftware-ws20-bis-ss21\ParallelPathfinderSemester6\.idea/dataSources/ +/dataSources.local.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/Semester/.idea/misc.xml b/Semester/.idea/misc.xml new file mode 100644 index 0000000000000000000000000000000000000000..91063aa21119ce3a01ff083d564fc9ebcd1fa1e9 --- /dev/null +++ b/Semester/.idea/misc.xml @@ -0,0 +1,6 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project version="4"> + <component name="ProjectRootManager" version="2" languageLevel="JDK_15" default="true" project-jdk-name="15" project-jdk-type="JavaSDK"> + <output url="file://$PROJECT_DIR$/out" /> + </component> +</project> \ No newline at end of file diff --git a/Semester/.idea/modules.xml b/Semester/.idea/modules.xml new file mode 100644 index 0000000000000000000000000000000000000000..28c395fa0e30fb94ae9d4a98ddf72bdd530eccb8 --- /dev/null +++ b/Semester/.idea/modules.xml @@ -0,0 +1,8 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project version="4"> + <component name="ProjectModuleManager"> + <modules> + <module fileurl="file://$PROJECT_DIR$/ParallelPathfinderSemester6.iml" filepath="$PROJECT_DIR$/ParallelPathfinderSemester6.iml" /> + </modules> + </component> +</project> \ No newline at end of file diff --git a/Semester/.idea/vcs.xml b/Semester/.idea/vcs.xml new file mode 100644 index 0000000000000000000000000000000000000000..94a25f7f4cb416c083d265558da75d457237d671 --- /dev/null +++ b/Semester/.idea/vcs.xml @@ -0,0 +1,6 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project version="4"> + <component name="VcsDirectoryMappings"> + <mapping directory="$PROJECT_DIR$" vcs="Git" /> + </component> +</project> \ No newline at end of file diff --git a/Semester/ParallelPathfinderSemester6.iml b/Semester/ParallelPathfinderSemester6.iml new file mode 100644 index 0000000000000000000000000000000000000000..c90834f2d607afe55e6104d8aa2cdfffb713f688 --- /dev/null +++ b/Semester/ParallelPathfinderSemester6.iml @@ -0,0 +1,11 @@ +<?xml version="1.0" encoding="UTF-8"?> +<module type="JAVA_MODULE" version="4"> + <component name="NewModuleRootManager" inherit-compiler-output="true"> + <exclude-output /> + <content url="file://$MODULE_DIR$"> + <sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" /> + </content> + <orderEntry type="inheritedJdk" /> + <orderEntry type="sourceFolder" forTests="false" /> + </component> +</module> \ No newline at end of file diff --git a/Semester/out/production/ParallelPathfinderSemester6/Program.class b/Semester/out/production/ParallelPathfinderSemester6/Program.class new file mode 100644 index 0000000000000000000000000000000000000000..d455cac1a9418cea82df60292b4f6c4d24ea23df Binary files /dev/null and b/Semester/out/production/ParallelPathfinderSemester6/Program.class differ diff --git a/Semester/out/production/ParallelPathfinderSemester6/algorithm/Explorer$1.class b/Semester/out/production/ParallelPathfinderSemester6/algorithm/Explorer$1.class new file mode 100644 index 0000000000000000000000000000000000000000..9bf91cb086236eaeb7d0882b5a38341b5194618a Binary files /dev/null and b/Semester/out/production/ParallelPathfinderSemester6/algorithm/Explorer$1.class differ diff --git a/Semester/out/production/ParallelPathfinderSemester6/algorithm/Explorer$Direction.class b/Semester/out/production/ParallelPathfinderSemester6/algorithm/Explorer$Direction.class new file mode 100644 index 0000000000000000000000000000000000000000..df527f54d3c5eb7eaeba20cb63a740434ae77e92 Binary files /dev/null and b/Semester/out/production/ParallelPathfinderSemester6/algorithm/Explorer$Direction.class differ diff --git a/Semester/out/production/ParallelPathfinderSemester6/algorithm/Explorer.class b/Semester/out/production/ParallelPathfinderSemester6/algorithm/Explorer.class new file mode 100644 index 0000000000000000000000000000000000000000..934cf500979c207979f70dd282cc1dbd87565d54 Binary files /dev/null and b/Semester/out/production/ParallelPathfinderSemester6/algorithm/Explorer.class differ diff --git a/Semester/out/production/ParallelPathfinderSemester6/algorithm/ParallelPathfinder.class b/Semester/out/production/ParallelPathfinderSemester6/algorithm/ParallelPathfinder.class new file mode 100644 index 0000000000000000000000000000000000000000..8e8ee9ffbd05f1ad67e8f916a0e2b851ba813668 Binary files /dev/null and b/Semester/out/production/ParallelPathfinderSemester6/algorithm/ParallelPathfinder.class differ diff --git a/Semester/out/production/ParallelPathfinderSemester6/algorithm/WorkingExplorers.class b/Semester/out/production/ParallelPathfinderSemester6/algorithm/WorkingExplorers.class new file mode 100644 index 0000000000000000000000000000000000000000..3121fad9cdc17adab163833ffc33771559020792 Binary files /dev/null and b/Semester/out/production/ParallelPathfinderSemester6/algorithm/WorkingExplorers.class differ diff --git a/Semester/out/production/ParallelPathfinderSemester6/parallel/EagerWorkingThread.class b/Semester/out/production/ParallelPathfinderSemester6/parallel/EagerWorkingThread.class new file mode 100644 index 0000000000000000000000000000000000000000..d33501c2abbeb0d3f1d0b5a6c471fa45a3548445 Binary files /dev/null and b/Semester/out/production/ParallelPathfinderSemester6/parallel/EagerWorkingThread.class differ diff --git a/Semester/out/production/ParallelPathfinderSemester6/parallel/MapLoader.class b/Semester/out/production/ParallelPathfinderSemester6/parallel/MapLoader.class new file mode 100644 index 0000000000000000000000000000000000000000..2e16e159b8c90f417302de605ee44da56565acbf Binary files /dev/null and b/Semester/out/production/ParallelPathfinderSemester6/parallel/MapLoader.class differ diff --git a/Semester/out/production/ParallelPathfinderSemester6/parallel/SharedRessources.class b/Semester/out/production/ParallelPathfinderSemester6/parallel/SharedRessources.class new file mode 100644 index 0000000000000000000000000000000000000000..b7625dfd43355bcf474759051cc36a4d93eec670 Binary files /dev/null and b/Semester/out/production/ParallelPathfinderSemester6/parallel/SharedRessources.class differ diff --git a/Semester/out/production/ParallelPathfinderSemester6/structs/Vec3.class b/Semester/out/production/ParallelPathfinderSemester6/structs/Vec3.class new file mode 100644 index 0000000000000000000000000000000000000000..eeb1bb0875f089539a8bc41b218a1a64d7429cbb Binary files /dev/null and b/Semester/out/production/ParallelPathfinderSemester6/structs/Vec3.class differ diff --git a/Semester/out/production/ParallelPathfinderSemester6/window/Game$1.class b/Semester/out/production/ParallelPathfinderSemester6/window/Game$1.class new file mode 100644 index 0000000000000000000000000000000000000000..ccc2c7acb5d52e7bffe7a05c889ea1910fd4dcd7 Binary files /dev/null and b/Semester/out/production/ParallelPathfinderSemester6/window/Game$1.class differ diff --git a/Semester/out/production/ParallelPathfinderSemester6/window/Game$2.class b/Semester/out/production/ParallelPathfinderSemester6/window/Game$2.class new file mode 100644 index 0000000000000000000000000000000000000000..d119eae634de8002abe20865dc7687bcea45c6c9 Binary files /dev/null and b/Semester/out/production/ParallelPathfinderSemester6/window/Game$2.class differ diff --git a/Semester/out/production/ParallelPathfinderSemester6/window/Game.class b/Semester/out/production/ParallelPathfinderSemester6/window/Game.class new file mode 100644 index 0000000000000000000000000000000000000000..a77bca504030367a5d09f45f0253ced217188f22 Binary files /dev/null and b/Semester/out/production/ParallelPathfinderSemester6/window/Game.class differ diff --git a/Semester/out/production/ParallelPathfinderSemester6/window/MainWindow.class b/Semester/out/production/ParallelPathfinderSemester6/window/MainWindow.class new file mode 100644 index 0000000000000000000000000000000000000000..35b04c16cd273899f543da3ab412619bf35f0607 Binary files /dev/null and b/Semester/out/production/ParallelPathfinderSemester6/window/MainWindow.class differ diff --git a/Semester/src/Program.java b/Semester/src/Program.java new file mode 100644 index 0000000000000000000000000000000000000000..df6b90283653568d7deac25c328d950dbe46e6fe --- /dev/null +++ b/Semester/src/Program.java @@ -0,0 +1,61 @@ +import parallel.EagerWorkingThread; +import window.MainWindow; + +public class Program { + + + public static void main(String[] args) throws Exception { + + /* + EagerWorkingThread workingThread = new EagerWorkingThread() { + @Override + public void onBegin() { + System.out.println("--- Begin ---"); + } + + @Override + public void onTrigger() { + try { + Thread.sleep(2000); + System.out.println("Triggered."); + } catch (InterruptedException e) { + e.printStackTrace(); + } + + } + + @Override + public void onEnd() { + System.out.println("--- End ---"); + } + }; + + + workingThread.setAllowRunning(true); + workingThread.start(); + + + workingThread.trigger(); + System.out.println("Triggering #1"); + workingThread.trigger(); + System.out.println("Triggering #2"); + workingThread.trigger(); + System.out.println("Triggering #3"); + + + Thread.sleep(10000); + + System.out.println(System.nanoTime()); + workingThread.trigger(); + System.out.println(System.nanoTime()); + */ + + + + MainWindow window = new MainWindow("Pathfinder v1", 1080, 1080); + window.run(); + + } + + +} diff --git a/Semester/src/algorithm/Explorer.java b/Semester/src/algorithm/Explorer.java new file mode 100644 index 0000000000000000000000000000000000000000..720407b0177267a732005c67d0315673a697d54b --- /dev/null +++ b/Semester/src/algorithm/Explorer.java @@ -0,0 +1,121 @@ +package algorithm; + +import parallel.EagerWorkingThread; +import parallel.SharedRessources; +import structs.Vec3; + +public class Explorer { + + public enum Direction { + North, + East, + South, + West + } + + private int x; + private int y; + + private int steps; + + private Direction direction; + + public Explorer(int x, int y, int steps, Direction direction) { + this.x = x; + this.y = y; + this.steps = steps; + this.direction = direction; + } + + public void explore() { + + int currentStepsOnField + = + (int) SharedRessources.getInstance().getVisitMatrix()[x + y * SharedRessources.getInstance().getWidth()] + .getZ(); + + if(currentStepsOnField < steps) + return; + + + boolean wall = + SharedRessources.getInstance().getWallMatrix()[x + y * SharedRessources.getInstance().getWidth()]; + if (wall) + return; + + Explorer recursiveNext + = null; + + switch (direction) { + + case North: + if (steps < currentStepsOnField) { + SharedRessources + .getInstance().getVisitMatrix()[x + y * SharedRessources.getInstance().getWidth()] + = new Vec3(0, +1, steps); + } + recursiveNext + = new Explorer(x, y - 1, steps++, direction); + addLeftRight(); + break; + + case East: + if (steps < currentStepsOnField) { + SharedRessources + .getInstance().getVisitMatrix()[x + y * SharedRessources.getInstance().getWidth()] + = new Vec3(-1, 0, steps); + } + recursiveNext + = new Explorer(x + 1, y, steps++, direction); + + addUpDown(); + break; + + case South: + if (steps < currentStepsOnField) { + SharedRessources + .getInstance().getVisitMatrix()[x + y * SharedRessources.getInstance().getWidth()] + = new Vec3(0, -1, steps); + } + recursiveNext + = new Explorer(x, y + 1, steps++, direction); + addLeftRight(); + break; + + case West: + if (steps < currentStepsOnField) { + SharedRessources + .getInstance().getVisitMatrix()[x + y * SharedRessources.getInstance().getWidth()] + = new Vec3(+1, 0, steps); + } + recursiveNext + = new Explorer(x - 1, y, steps++, direction); + addUpDown(); + break; + + } + + + recursiveNext.explore(); + } + + + private void addLeftRight() { + SharedRessources + .getInstance().add(new Explorer(x - 1, y, steps++, Direction.West)); + + SharedRessources + .getInstance().add(new Explorer(x + 1, y, steps++, Direction.East)); + + } + + private void addUpDown() { + SharedRessources + .getInstance().add(new Explorer(x, y - 1, steps++, Direction.North)); + + SharedRessources + .getInstance().add(new Explorer(x, y + 1, steps++, Direction.South)); + } + + +} diff --git a/Semester/src/algorithm/ParallelPathfinder.java b/Semester/src/algorithm/ParallelPathfinder.java new file mode 100644 index 0000000000000000000000000000000000000000..d0b20c7a409fd8f0c5b5746926e451bcdc9631cf --- /dev/null +++ b/Semester/src/algorithm/ParallelPathfinder.java @@ -0,0 +1,85 @@ +package algorithm; + +import parallel.EagerWorkingThread; +import parallel.SharedRessources; +import structs.Vec3; + +import java.awt.*; +import java.util.ArrayList; + +public class ParallelPathfinder { + private void flood(int destX, int destY) { + SharedRessources + .getInstance().add(new Explorer(destX - 1, destY, 1, Explorer.Direction.West)); + SharedRessources + .getInstance().add(new Explorer(destX + 1, destY, 1, Explorer.Direction.East)); + SharedRessources + .getInstance().add(new Explorer(destX, destY - 1, 1, Explorer.Direction.North)); + SharedRessources + .getInstance().add(new Explorer(destX, destY + 1, 1, Explorer.Direction.South)); + + while (!SharedRessources.getInstance().done()); + } + + public ArrayList<Point> calculatePath(int startX, int startY, int destX, int destY) { + flood(destX, destY); + + ArrayList<Point> path + = new ArrayList<>(); + + int x = startX; + int y = startY; + + while (x != destX || y != destY) { + + path.add(new Point(x, y)); + + Vec3 currentDir + = + SharedRessources.getInstance().getVisitMatrix()[x + y * SharedRessources.getInstance().getWidth()]; + + int dx = (int) currentDir.getX(); + int dy = (int) currentDir.getY(); + + if(dx == 0 && dy == 0) + return null; + + x += dx; + y += dy; + } + + + return path; + } + + public void draw(Graphics g) { + for (int j = 0; j < SharedRessources.getInstance().getHeight(); j++) { + for (int i = 0; i < SharedRessources.getInstance().getWidth(); i++) { + boolean b = + SharedRessources.getInstance().getWallMatrix()[i + j * SharedRessources.getInstance().getWidth()]; + if (b) + g.setColor(Color.blue); + else { + Vec3 currentVec + = + SharedRessources.getInstance().getVisitMatrix()[i + j * SharedRessources.getInstance().getWidth()]; + if (currentVec.getX() == 0 && currentVec.getY() == 0) + g.setColor(Color.black); + else { + int green = (int)currentVec.getZ(); + + Color color + = new Color(0, green, 0); + + g.setColor(color); + + } + } + + g.fillRect(i * 10, j * 10, 10, 10); + } + } + } + + +} diff --git a/Semester/src/algorithm/WorkingExplorers.java b/Semester/src/algorithm/WorkingExplorers.java new file mode 100644 index 0000000000000000000000000000000000000000..4307730e9b3e1bf693c25d7bfc44b9951973644a --- /dev/null +++ b/Semester/src/algorithm/WorkingExplorers.java @@ -0,0 +1,43 @@ +package algorithm; + +import parallel.EagerWorkingThread; +import parallel.SharedRessources; + +import java.util.ArrayList; +import java.util.PriorityQueue; +import java.util.Queue; +import java.util.Stack; +import java.util.concurrent.LinkedBlockingQueue; + +public class WorkingExplorers extends EagerWorkingThread { + + public WorkingExplorers() { + super(); + this.setAllowRunning(true); + + System.out.println("[*] Thread initialized @" + this.getName()); + + } + + private Stack<Explorer> currentWaitingExplorers = new Stack<>(); + + @Override + public void onBegin() { + } + + @Override + public void onTrigger() { + if (!currentWaitingExplorers.isEmpty()) + currentWaitingExplorers.pop().explore(); + } + + @Override + public void onEnd() { + } + + public void add(Explorer explorer) throws InterruptedException { + this.currentWaitingExplorers.push(explorer); + this.trigger(); + } + +} diff --git a/Semester/src/parallel/EagerWorkingThread.java b/Semester/src/parallel/EagerWorkingThread.java new file mode 100644 index 0000000000000000000000000000000000000000..696512811a58d7b626bf69e9cb9171f29d85aea7 --- /dev/null +++ b/Semester/src/parallel/EagerWorkingThread.java @@ -0,0 +1,65 @@ +package parallel; + +public abstract class EagerWorkingThread extends Thread { + private static volatile boolean RUN_THREADS = true; + + public static void stopAllThreads() { + RUN_THREADS = false; + } + + private volatile int triggerCount; + private volatile boolean isTriggered; + private volatile boolean allowRunning; + + public void setAllowRunning(boolean allowRunning) { + this.allowRunning = allowRunning; + } + + public boolean isAllowedRunning() { + return allowRunning; + } + + @Override + public void run() { + while (RUN_THREADS && allowRunning) { + synchronized (this) { + while (!isTriggered) { + try { + this.wait(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + } + + onBegin(); + for (int i = 0; i < triggerCount; i++) + onTrigger(); + onEnd(); + + + triggerCount = 0; + isTriggered = false; + + + } + } + + public synchronized boolean trigger() { + triggerCount++; + isTriggered = true; + this.notifyAll(); + return true; + } + + public synchronized boolean isReady(){ + return this.triggerCount == 0; + } + + public abstract void onBegin(); + + public abstract void onTrigger(); + + public abstract void onEnd(); + +} diff --git a/Semester/src/parallel/MapLoader.java b/Semester/src/parallel/MapLoader.java new file mode 100644 index 0000000000000000000000000000000000000000..677c2e07602d7c7e7c5ef34dfcd3fe8ea71e6397 --- /dev/null +++ b/Semester/src/parallel/MapLoader.java @@ -0,0 +1,51 @@ +package parallel; + +import structs.Vec3; + +import java.awt.*; +import java.awt.image.BufferedImage; +import java.awt.image.DataBufferByte; + +public class MapLoader { + + private int width; + private int height; + + private MapLoader() { + } + + private static MapLoader mapLoader; + + public static MapLoader getDefault() { + if (mapLoader == null) + mapLoader = new MapLoader(); + return mapLoader; + } + + public void load(BufferedImage src) { + this.width = src.getWidth(); + this.height = src.getHeight(); + + SharedRessources.getInstance().setWidth(width); + SharedRessources.getInstance().setHeight(height); + + SharedRessources.getInstance().setVisitMatrix(new Vec3[width * height]); + SharedRessources.getInstance().setWallMatrix(new boolean[width * height]); + + for (int i = 0; i < SharedRessources.getInstance().getVisitMatrix().length; i++) + SharedRessources.getInstance().getVisitMatrix()[i] = new Vec3(0, 0, 10000); + + + // https://stackoverflow.com/a/9470843/14727115 + byte[] pixels = ((DataBufferByte) src.getRaster().getDataBuffer()).getData(); + + + for (int j = 0; j < height; j++) { + for (int i = 0; i < width; i++) { + SharedRessources.getInstance().getWallMatrix()[i + j * width] = + ((BufferedImage) src).getRGB(i, j) == Color.white.getRGB(); + } + } + } + +} diff --git a/Semester/src/parallel/SharedRessources.java b/Semester/src/parallel/SharedRessources.java new file mode 100644 index 0000000000000000000000000000000000000000..a2616e8888569ebbf50ba65cb33a8166cc40f9c5 --- /dev/null +++ b/Semester/src/parallel/SharedRessources.java @@ -0,0 +1,103 @@ +package parallel; + +import algorithm.Explorer; +import algorithm.WorkingExplorers; +import structs.Vec3; + +public class SharedRessources { + private static int USING_CORES; + + /*** + * Use Integer.MAX_VALUE for all cores + * @param cores + */ + public static void setMaximumCores(int cores) { + USING_CORES = cores; + } + + private static SharedRessources sharedRessources; + + public static synchronized SharedRessources getInstance() { + if (sharedRessources == null) + sharedRessources = new SharedRessources(USING_CORES); + return sharedRessources; + } + + private int maxCores; + private int currentCore; + + private WorkingExplorers[] workingThreads; + + public void start() { + for (int i = 0; i < maxCores; i++) + this.workingThreads[i].start(); + } + + public void add(Explorer explorer) { + try { + this.workingThreads[(currentCore++) % maxCores].add(explorer); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + + public boolean done() { + for (int i = 0; i < maxCores; i++) + if (!this.workingThreads[i].isReady()) + return false; + return true; + } + + private SharedRessources(int cores) { + if (cores >= Runtime.getRuntime().availableProcessors()) + maxCores = Runtime.getRuntime().availableProcessors(); + else maxCores = cores; + + System.out.println("[*] Using " + maxCores + " cores."); + + workingThreads = new WorkingExplorers[maxCores]; + for (int i = 0; i < maxCores; i++) + workingThreads[i] = new WorkingExplorers(); + + start(); + currentCore = 0; + } + + private int width; + private int height; + + private boolean[] wallMatrix; + private Vec3[] visitMatrix; + + public Vec3[] getVisitMatrix() { + return visitMatrix; + } + + public boolean[] getWallMatrix() { + return wallMatrix; + } + + protected void setVisitMatrix(Vec3[] visitMatrix) { + this.visitMatrix = visitMatrix; + } + + protected void setWallMatrix(boolean[] wallMatrix) { + this.wallMatrix = wallMatrix; + } + + public int getWidth() { + return width; + } + + public int getHeight() { + return height; + } + + protected void setWidth(int width) { + this.width = width; + } + + protected void setHeight(int height) { + this.height = height; + } +} diff --git a/Semester/src/structs/Vec3.java b/Semester/src/structs/Vec3.java new file mode 100644 index 0000000000000000000000000000000000000000..8d45f8ab7ac6cfe9663f87512247c511e6edce42 --- /dev/null +++ b/Semester/src/structs/Vec3.java @@ -0,0 +1,38 @@ +package structs; + +public class Vec3 { + private float x; + private float y; + private float z; + + public Vec3(float x, float y, float z) { + this.x = x; + this.y = y; + this.z = z; + } + + public void setX(float x) { + this.x = x; + } + + public void setY(float y) { + this.y = y; + } + + public void setZ(float z) { + this.z = z; + } + + public float getX() { + return x; + } + + public float getY() { + return y; + } + + public float getZ() { + return z; + } +} + diff --git a/Semester/src/window/Game.java b/Semester/src/window/Game.java new file mode 100644 index 0000000000000000000000000000000000000000..297efd2959eb655be42b65debed2343dff6eb8cc --- /dev/null +++ b/Semester/src/window/Game.java @@ -0,0 +1,79 @@ +package window; + +import javax.swing.*; +import java.awt.*; +import java.awt.event.KeyAdapter; +import java.awt.event.KeyEvent; + +public abstract class Game extends JFrame { + + private JPanel drawingSurface; + private Thread windowThread; + + private boolean running; + + protected boolean[] keys; + + public Game(String title, int w, int h) { + super(title); + this.setSize(w, h); + + keys = new boolean[1024]; + + this.windowThread = new Thread(() -> { + loadGame(); + while (running) { + updateGame(); + this.drawingSurface.repaint(); + } + }); + + this.drawingSurface + = new JPanel() { + + @Override + public void paint(Graphics g) { + super.paint(g); + renderGame(g); + } + }; + + this.drawingSurface.setSize(this.getSize()); + this.add(this.drawingSurface); + + this.addKeyListener(new KeyAdapter() { + @Override + public void keyPressed(KeyEvent e) { + super.keyPressed(e); + + keys[e.getKeyCode()] = true; + } + + @Override + public void keyReleased(KeyEvent e) { + super.keyReleased(e); + + keys[e.getKeyCode()] = false; + } + }); + + this.setDefaultCloseOperation(EXIT_ON_CLOSE); + + + } + + public final void run() { + this.setVisible(true); + this.running = true; + + this.windowThread + .start(); + } + + public abstract void loadGame(); + + public abstract void updateGame(); + + public abstract void renderGame(Graphics g); + +} diff --git a/Semester/src/window/MainWindow.java b/Semester/src/window/MainWindow.java new file mode 100644 index 0000000000000000000000000000000000000000..01a0ce86f0add287da69d5ca83a4814d72aae08e --- /dev/null +++ b/Semester/src/window/MainWindow.java @@ -0,0 +1,62 @@ +package window; + +import algorithm.ParallelPathfinder; +import parallel.MapLoader; +import parallel.SharedRessources; + +import javax.imageio.ImageIO; +import java.awt.*; +import java.awt.event.KeyEvent; +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; + +public class MainWindow extends Game { + public MainWindow(String title, int w, int h) { + super(title, w, h); + } + + + private ParallelPathfinder parallelPathfinder; + private ArrayList<Point> pts; + + @Override + public void loadGame() { + SharedRessources.setMaximumCores(Integer.MAX_VALUE); + try { + MapLoader.getDefault().load(ImageIO.read(new File("c:/users/enesh/desktop/t.png"))); + } catch (IOException e) { + e.printStackTrace(); + } + + parallelPathfinder + = new ParallelPathfinder(); + } + + @Override + public void updateGame() { + + if (keys[KeyEvent.VK_ENTER]) + pts = parallelPathfinder.calculatePath(70, 26, 23, 24); + + } + + @Override + public void renderGame(Graphics g) { + + if (parallelPathfinder == null) + return; + + parallelPathfinder.draw(g); + + + if (pts == null) + return; + + g.setColor(Color.red); + for (var pt : pts) { + g.fillRect(pt.x * 10, pt.y * 10, 10, 10); + } + } + +}