From 4d31c6d071b67334aac539620212ed5d6462fc74 Mon Sep 17 00:00:00 2001 From: toksikk Date: Fri, 31 Oct 2014 13:36:29 +0100 Subject: [PATCH] =?UTF-8?q?Statische=20Logger-Klasse=20hinzugef=C3=BCgt.?= =?UTF-8?q?=20Bitte=20Kommentare=20in=20der=20Klasse=20beachten.=20Sollte?= =?UTF-8?q?=20aber=20selbsterkl=C3=A4rend=20sein.=20K=C3=B6nnte=20praktisc?= =?UTF-8?q?h=20sein,=20wenn=20man=20Debug=20Sysos=20im=20Code=20mit=20eine?= =?UTF-8?q?m=20Aufruf=20deaktivieren=20m=C3=B6chte.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Änderungen in der MapEngine wegen Anpassung der Objekt-Klasse. --- HeatUp/src/de/heatup/logging/Logger.java | 92 +++++++++++++++++++ HeatUp/src/de/heatup/mapengine/MapEngine.java | 66 +++++-------- 2 files changed, 116 insertions(+), 42 deletions(-) create mode 100644 HeatUp/src/de/heatup/logging/Logger.java diff --git a/HeatUp/src/de/heatup/logging/Logger.java b/HeatUp/src/de/heatup/logging/Logger.java new file mode 100644 index 0000000..4f0b341 --- /dev/null +++ b/HeatUp/src/de/heatup/logging/Logger.java @@ -0,0 +1,92 @@ +package de.heatup.logging; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.PrintStream; + +public class Logger { + private static boolean logging = false; + private static boolean firstWrite = true; + private static boolean toCmd = true; + private static boolean toFile = false; + private static String logFile; + + /* + * writes a new log line to cmd via System.out.println() and/or (if enabled) appends a new line to a log file + */ + @SuppressWarnings("resource") + public static void write(String s) { + if (logging) { + if (toCmd) { + System.out.println(s); + } + if (toFile) { + PrintStream out; + try { + if (firstWrite) { + out = new PrintStream(new FileOutputStream(logFile)); + firstWrite = false; + } else { + out = new PrintStream(new FileOutputStream(logFile, true)); + } + } catch (FileNotFoundException e) { + File f = new File(logFile); + try { + f.createNewFile(); + if (firstWrite) { + out = new PrintStream(new FileOutputStream(logFile)); + firstWrite = false; + } else { + out = new PrintStream(new FileOutputStream(logFile, true)); + } + } catch (IOException e1) { + out = null; + e1.printStackTrace(); + } + } + out.println(s); + out.close(); + } + } + } + /* + * sets the log file to given logFile. + * logFile can include a path. + */ + public static void setLogFile(String logFile) { + Logger.toFile = true; + Logger.logFile = logFile; + } + /* + * prints the name (and path if given) + */ + public static String getLogFile() { + return Logger.logFile; + } + /* + * enables logging + * logging is enabled by default + */ + public static void enableLogging() { + Logger.logging = true; + } + /* + * disables logging until reenabled with enableLogging() + */ + public static void disableLogging() { + Logger.logging = false; + } + /* + * disables System.out.println() logging + */ + public static void disableCmdLogging() { + Logger.toCmd = false; + } + /* + * enables System.out.println() logging + */ + public static void enableCmdLogging() { + Logger.toCmd = true; + } +} diff --git a/HeatUp/src/de/heatup/mapengine/MapEngine.java b/HeatUp/src/de/heatup/mapengine/MapEngine.java index d17c762..27087a9 100644 --- a/HeatUp/src/de/heatup/mapengine/MapEngine.java +++ b/HeatUp/src/de/heatup/mapengine/MapEngine.java @@ -1,36 +1,23 @@ +package de.heatup.mapengine; + import java.awt.Color; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.Map; import java.util.Scanner; -import java.util.Stack; - import javax.swing.JPanel; import de.heatup.objects.*; public class MapEngine { - int[][] tiles; - Stack tileID; - Map> tilesMap; + char[][] tiles; MapEngine() { - tiles = new int[24][32]; - tileID = new Stack(); - tilesMap = new HashMap>(); - ArrayList typeArrayListOne = new ArrayList(); - ArrayList typeArrayListTwo = new ArrayList(); - ArrayList typeArrayListThree = new ArrayList(); - tilesMap.put(1, typeArrayListOne); - tilesMap.put(2, typeArrayListTwo); - tilesMap.put(3, typeArrayListThree); + tiles = new char[24][32]; Scanner input = null; try { - input = new Scanner(new File("prp/spielfeld.txt")); + input = new Scanner(new File("src/de/heatup/resources/maps/map1.txt")); } catch (FileNotFoundException e) { System.out.println("Konnte Map Datei nicht finden"); System.exit(-1); @@ -43,36 +30,35 @@ public class MapEngine { while (input.hasNextLine()) { String currentLine = input.nextLine(); for (int i = 0; i<32; i++) { - tiles[lineCount][i] = defineTileType(currentLine.charAt(i)); +// tiles[lineCount][i] = defineTileType(currentLine.charAt(i)); + tiles[lineCount][i] = currentLine.charAt(i); } lineCount++; } } - private int defineTileType(char charAt) { - for (char i = '0'; i<'9';i++) { - if (charAt == i) { - tileID.push(i-'0'); - return 2; - } - } - switch (charAt) { - case '#': - return 1; - case '-': - return 3; - default: - return 0; - } - } +// private int defineTileType(char charAt) { +// for (char i = '0'; i<'9';i++) { +// if (charAt == i) { +// tileID.push(i-'0'); +// return 2; +// } +// } +// switch (charAt) { +// case '#': +// return 1; +// case '-': +// return 3; +// default: +// return 0; +// } +// } public void addAllGameTilesToPanel(JPanel gamePanel) throws IOException { int x = 0; int y = 0; gamePanel.setBackground(new Color(500)); // hintergrund gamepanel auf blau setzen, bitte entfernen, ist unnötig for (int i = 0; i<24; i++) { for (int j = 0; j < 32; j++) { - Objekt newGameTile = new Objekt(""); // ändern!!!!! nicht mit parameter "" aufrufen!!! - System.out.println(tiles[i][j]); - newGameTile.setObjectType(tiles[i][j]); + Objekt newGameTile = new Objekt(tiles[i][j]); newGameTile.setPosition(x, y); if (x+25 >= 800) { x = 0; @@ -81,14 +67,10 @@ public class MapEngine { x += 25; } gamePanel.add(newGameTile.getLabel()); - tilesMap.get(tiles[i][j]).add(newGameTile); } } gamePanel.repaint(); } - public ArrayList getAllTilesOfType(int i) { - return tilesMap.get(i); - } }