Example #1
0
  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;
      }
    }
  }
Example #2
0
  /**
   * liest die Daten und Befehle ein und gibt das Ergebnis aus. Allein diese Methode liest direkt
   * von System.in ein und gibt direkt auf System.out aus.
   *
   * @param args
   */
  public static void main(String[] args) {

    AsciiImage image;
    Scanner sc = new Scanner(System.in);
    int height = 0;
    int width = 0;
    String command;
    boolean inputMismatchError = false;
    boolean unkownCommandError = false;
    boolean oparationFailedError = false;
    AsciiStack as = new AsciiStack();
    String charset = "";
    HashMap<String, Factory> hm = new HashMap<String, Factory>();
    hm.put("clear", new ClearFactory());
    hm.put("binary", new BinaryFactory());
    hm.put("filter", new FilterFactory());
    hm.put("load", new LoadFactory());
    hm.put("replace", new ReplaceFactory());

    // create
    if (sc.hasNext()) {

      if (sc.next().contentEquals("create") && !inputMismatchError) {

        if (sc.hasNextInt() && !inputMismatchError) {

          width = sc.nextInt();

          if ((width <= 0) && !inputMismatchError) inputMismatchError = true;

        } else inputMismatchError = true;

        if (sc.hasNextInt() && !inputMismatchError) {

          height = sc.nextInt();

          if ((height <= 0) && !inputMismatchError) inputMismatchError = true;

        } else inputMismatchError = true;

        if (sc.hasNext() && !inputMismatchError) {
          charset = sc.next();
        } else inputMismatchError = true;
      } else inputMismatchError = true;
    } else inputMismatchError = true;

    if (inputMismatchError) {
      System.out.println("INPUT MISMATCH");
      return;
    }
    sc.nextLine();

    image = new AsciiImage(width, height, charset);

    // read commands
    try {
      while (sc.hasNext() && !inputMismatchError && !unkownCommandError && !oparationFailedError) {

        command = sc.next();

        if (command.contentEquals("print")) {
          System.out.println(image.toString());
        } else if (command.contentEquals("undo")) {
          if (as.empty()) System.out.println("STACK EMPTY");
          else {
            image = as.pop();
          }

        } else if (hm.get(command) != null) {
          as.push(new AsciiImage(image));
          image = hm.get(command).create(sc).execute(image);
        } else unkownCommandError = true;
      }
    } catch (OperationException oe) {
      oparationFailedError = true;
    } catch (FactoryException fe) {
      inputMismatchError = true;
    }

    if (oparationFailedError) {
      System.out.println("OPERATION FAILED");
    }
    if (unkownCommandError) {
      System.out.println("UNKNOWN COMMAND");
    }
    if (inputMismatchError) {
      System.out.println("INPUT MISMATCH");
    }
  }