public int distance(AsciiImage i1, AsciiImage i2) { int diff = 0; if ((i1.getWidth() * i1.getHeight()) == (i2.getWidth() * i2.getHeight())) { return 0; } else { diff = ((i1.getWidth() * i1.getHeight()) - (i2.getWidth() * i2.getHeight())); return diff < 0 ? (diff * -1) : diff; } }
/** * Verarbeitet ein Kommando indem es die entsprechende Funktion aufruft. * * <p>Gibt das gelesene Bild nach erfolgreicher Bearbeitung über die Standardausgabe aus und * kümmert sich um die Fehlerbehandlung. * * @throws InputMismatchException Wenn ein ungültiges Kommando übergeben wurde. * @throws OperationFailedException Wenn fill nicht aufgerufen werden kann. */ public static void main(String[] args) throws InputMismatchException, OperationFailedException { try { AsciiImage image = new AsciiImage(); Scanner sc = new Scanner(System.in); Scanner commandScanner = new Scanner(sc.nextLine()); String command = commandScanner.next(); if (command.equals("read")) { int n = commandScanner.nextInt(); for (int i = 0; i < n; i++) { if (image.addLine(sc.nextLine()) == false) { throw new InputMismatchException(); } } } else { throw new InputMismatchException(); } while (sc.hasNextLine()) { commandScanner = new Scanner(sc.nextLine()); command = commandScanner.next(); if (command.equals("uniqueChars")) { System.out.println(image.getUniqueChars()); } else if (command.equals("flip-v")) { image.flipV(); } else if (command.equals("transpose")) { image.transpose(); } else if (command.equals("fill")) { int x = commandScanner.nextInt(); int y = commandScanner.nextInt(); char c = commandScanner.next().charAt(0); if (x >= 0 && y >= 0 && x < image.getWidth() && y < image.getHeight()) { image.fill(x, y, c); } else { throw new OperationFailedException(); } } else if (command.equals("symmetric-h")) { System.out.println(image.isSymmetricH()); } else { throw new InputMismatchException(); } } System.out.println(image.toString()); System.out.println(image.getWidth() + " " + image.getHeight()); } catch (OperationFailedException e) { System.out.println("OPERATION FAILED"); } catch (RuntimeException e) { System.out.println("INPUT MISMATCH"); } }
public AsciiImage execute(AsciiImage img) throws OperationException { image = new AsciiImage(img); for (int i = 0; i < img.getHeight(); i++) { for (int j = 0; j < img.getWidth(); j++) { image.setPixel(j, i, img.getCharset().charAt(img.getCharset().length() - 1)); } } return image; }
public static void main(String[] args) { Scanner in = new Scanner(System.in); // Init der lokalen Variablen String a, b; AsciiImage image, imagecp; AsciiPoint p; AsciiStack stack = new AsciiStack(3); int width, height, x0, x1, y0, y1; char c, c1; a = in.next(); // einlesen des create-Befehls if (a.equals("create")) { width = in.nextInt(); // einlesen 1. Parameter - breite if (width <= 0) { // unpassende breite - Fehlerbehandlung panic(); return; } height = in.nextInt(); // einlesen 2. Parameter - höhe if (height <= 0) { // unpassende höhe - Fehlerbehandlung panic(); return; } image = new AsciiImage(width, height); // Aufruf Konstruktor } else { // Wenn 1. Befehl nicht create panic(); return; } while (in.hasNext()) { // solange Eingabe a = in.next(); // Speichern um zu vergleichen if (a.equals("clear")) { imagecp = new AsciiImage(image); stack.push(imagecp); image.clear(); } else if (a.equals("line")) { imagecp = new AsciiImage(image); stack.push(imagecp); if (!in.hasNextInt()) { // 1. Parameter kein Integer - Fehlerbehandlung panic(); return; } x0 = in.nextInt(); if (!in.hasNextInt()) { // 2. Parameter kein Integer - Fehlerbehandlung panic(); return; } y0 = in.nextInt(); if (!in.hasNextInt()) { // 3. Parameter kein Integer - Fehlerbehandlung panic(); return; } x1 = in.nextInt(); if (!in.hasNextInt()) { // 4. Parameter kein Integer - Fehlerbehandlung panic(); return; } y1 = in.nextInt(); if (!in.hasNext()) { // 5. Parameter nicht vorhanden - Fehlerbehandlung panic(); return; } a = in.next(); c = a.charAt(0); // char aus der Eingabe rausfiltern image.drawLine(x0, y0, x1, y1, c); // Methodenaufruf } else if (a.equals("load")) { imagecp = new AsciiImage(image); stack.push(imagecp); a = in.next(); // EoF String einlesen int j = 0; while (in.hasNext() && j < image.getHeight()) { // solange Eingabe und Höhe nicht überschritten b = in.next(); // einlesen Zeile if (b == a || b.length() != image .getWidth()) { // Höhe zu klein oder breite zu klein/groß - Fehlerbehandlung panic(); return; } for (int i = 0; i < image.getWidth(); i++) { // Zeilen in Image speichern image.setPixel(i, j, b.charAt(i)); } j++; // Höhe inkrementieren } b = in.next(); // Wenn das Bild die richtige Höhe hat if (!b.equals(a)) { // aber das EoF String nicht kommt - Fehlerbehandlung panic(); return; } } else if (a.equals("print")) { System.out.println(image.toString()); } else if (a.equals("replace")) { imagecp = new AsciiImage(image); stack.push(imagecp); if (!in.hasNext()) { // 1. Parameter nicht vorhanden - Fehlerbehandlung panic(); return; } a = in.next(); // char aus der Eingabe rausfiltern c = a.charAt(0); if (!in.hasNext()) { // 2. Parameter nicht vorhanden - Fehlerbehandlung panic(); return; } a = in.next(); // char aus der Eingabe rausfiltern c1 = a.charAt(0); image.replace(c, c1); // Methodenaufruf } else if (a.equals("transpose")) { imagecp = new AsciiImage(image); stack.push(imagecp); image.transpose(); } else if (a.equals("fill")) { if (!in.hasNextInt()) { // 1. Parameter kein Int - Fehlerbehandlung panic(); return; } x0 = in.nextInt(); if (x0 > image.getWidth() || x0 < 0) { // Bereichsüberprüfung System.out.println("OPERATION FAILED"); return; } if (!in.hasNextInt()) { // 2. Parameter kein Int - Fehlerbehandlung panic(); return; } y0 = in.nextInt(); if (y0 > image.getHeight() || y0 < 0) { // Bereichsüberprüfung System.out.println("OPERATION FAILED"); return; } if (!in.hasNext()) { // 3. Parameter nicht vorhanden - Fehlerbehandlung panic(); return; } a = in.next(); c = a.charAt(0); // char aus der Eingabe rausfiltern image.fill(x0, y0, c); } else if (a.equals("centroid")) { if (!in.hasNext()) { // kein Parameter - Fehlerbehandlung panic(); return; } a = in.next(); c = a.charAt(0); // Char aus der EIngabe rausfiltern p = image.getCentroid(c); System.out.println(p.toString()); // Ausgabe des Schwerpunktes } else if (a.equals("grow")) { imagecp = new AsciiImage(image); // Kopie erstellen für "undo" stack.push(imagecp); // Kopie auf Stack if (!in.hasNext()) { // kein Parameter - Fehlerbehandlung panic(); return; } a = in.next(); c = a.charAt(0); // Char aus der EIngabe rausfiltern image.growRegion(c); } else if (a.equals("undo")) { if (stack.size() == 0) { // WEnn Stack leer System.out.println("STACK EMPTY"); } else { // von Stack die alte version des Bildes holen image = stack.pop(); System.out.println( "STACK USAGE " + stack.size() + "/" + stack.capacity()); // Ausgabe des StacksSpeicherZustdands } } else { // Wenn kein gültiger Befehl System.out.println("UNKNOWN COMMAND"); return; } } }