Skip to content

App Icon Generator — Java source

Turn one image into the full web app icon set — favicon, apple-touch-icon and manifest sizes as crisp PNGs. Letterboxed contain-fit, optional white background — runs entirely in your browser.

This is the Java implementation — the same logic the interactive tool runs, in a shareable, citable form.

// App Icon Generator — contain-fit, centered export geometry for the
// 16/32/180/192/512 icon set.
//
// Language: Java (Java 17, standard library only)
// Source:   CosmoDev polyglot showcase port, from the authored TypeScript
//           snippet src/tool-sources/app-icon-generator/typescript.ts.

import java.util.ArrayList;
import java.util.List;

public final class AppIconGeometry {

    /// One export target: the square canvas edge and the centered contain-fit
    /// rect the source image is drawn into.
    public record IconRect(int size, int x, int y, int w, int h) {}

    public static final int[] DEFAULT_SIZES = {16, 32, 180, 192, 512};

    private AppIconGeometry() {}

    /// The deterministic half of the tool: per export size, the contain-fit
    /// rect (scale = min(px/w, px/h), clamped to >= 1) plus the centered draw
    /// offset — the geometry the TS pipeline feeds ctx.drawImage before
    /// canvas.toBlob PNG-encodes each square. Bitmap decode and PNG encode
    /// stay with the host. Math.round matches JS: half away from zero.
    public static List<IconRect> iconSetPlan(int srcW, int srcH, int... sizes) {
        if (sizes.length == 0) sizes = DEFAULT_SIZES;
        List<IconRect> plan = new ArrayList<>(sizes.length);
        for (int px : sizes) {
            double scale = Math.min((double) px / srcW, (double) px / srcH);
            int w = Math.max(1, Math.round((float) (srcW * scale)));
            int h = Math.max(1, Math.round((float) (srcH * scale)));
            plan.add(new IconRect(px,
                    Math.round((px - w) / 2f), // letterbox on transparency
                    Math.round((px - h) / 2f),
                    w, h));
        }
        return plan;
    }
}

Also available in 8 other languages

Every CosmoDev tool ships its pure logic in TypeScript (web) and Go (CLI), with authored implementations in a dozen-plus languages — the same contract, ported. Compare all languages side by side →