package page.example; import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.util.Arrays; import org.wikiwebserver.core.WareHouse; import org.wikiwebserver.handler.http.FormData; import org.wikiwebserver.handler.http.HTTPException; import org.wikiwebserver.handler.http.interfaces.HTTPResponder; import page.config.SiteTemplatedPage; import page.image.Fractal; import org.wikiwebserver.util.comparator.FileModifiedComparator; import static org.wikiwebserver.html.HTMLHelper.*; import com.sun.image.codec.jpeg.JPEGCodec; import com.sun.image.codec.jpeg.JPEGEncodeParam; import com.sun.image.codec.jpeg.JPEGImageEncoder; public class FractalGallery extends SiteTemplatedPage implements HTTPResponder { private String storeLocation = "image/.fractals/"; private static final int THUMBNAIL_WIDTH = 150; private static final double START_X = -1.85f; private static final double START_Y = -1.25f; private static final double START_S = 0.004f; public void generate() throws HTTPException { setTitle("Fractal Gallery - WikiWebServer.org"); addResourceRoot("/templates/default/fractal/"); addCSSLink("fractal.css"); int w = 700; int h = 600; boolean home = true; double x = START_X; double y = START_Y; double s = START_S; // Obtain fractal properties FormData formData = getFormData(); if (formData != null) { String action = formData.getFirst("action"); if (action == null || !action.equals("Home")) { home = false; w = (int) getDouble("w", w, formData); h = (int) getDouble("h", h, formData); x = getDouble("x", START_X, formData); y = getDouble("y", START_Y, formData); s = getDouble("s", START_S, formData); int idx = getRequest().getQuery().indexOf('?'); if (idx > -1) { // Zoom into clicked location String coords = getRequest().getQuery().substring(idx+1); String[] parts = coords.split(","); int clickX = Integer.parseInt(parts[0]); int clickY = Integer.parseInt(parts[1]); x += (clickX - (w/4)) * s; y += (clickY - (h/4)) * s; s /= 2; } } if (action != null && action.equals("del")) { home = true; createFile(s, x, y).delete(); } if (action != null && action.equals("Save snapshot")) { try { Fractal fracGen = new Fractal(); BufferedImage frac = (BufferedImage) fracGen.respond(getFormData()); saveThumb(frac, createFile(s, x, y)); } catch (Exception ex) { throw new HTTPException(500, "Failed to save snapshot"); } } } if (home) { x = START_X; y = START_Y; s = START_S; } String params = "?w=" + w + "&h=" + h + "&s=" + s + "&x=" + x + "&y=" + y + "&"; append(a(WareHouse.getUrlPathForClass(page.example.FractalGallery.class) + params, image(WareHouse.getUrlPathForClass(page.image.Fractal.class) + params, "FractalGallery Image, click to zoom", "width='" + w + "' height='" + h + "' ismap='true'"))); if (!home) { append("
Click the fractal to zoom in.
"); } append("