Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
439 views
in Technique[技术] by (71.8m points)

image - Any java package to draw simple geometric shape?

Does anyone have any suggestion about any lib/package to draw simple geometric, such as triangle, square? It can be saved into a png format.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This source uses Graphics2D of the Java 2D API to draw some simple colored shapes.

For reference, here are the 32x32 images that result.

enter image description hereenter image description hereenter image description hereenter image description here
enter image description hereenter image description hereenter image description hereenter image description here
enter image description hereenter image description hereenter image description hereenter image description here
enter image description hereenter image description hereenter image description hereenter image description here
enter image description hereenter image description hereenter image description hereenter image description here

import java.awt.*;
import java.awt.geom.Ellipse2D;
import java.awt.geom.GeneralPath;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.*;

public class SimpleImages {

    public static BufferedImage getColoredShapeImage(
            int size, Shape shape, Color color) {
        BufferedImage bi = new BufferedImage(
                size, size, BufferedImage.TYPE_INT_ARGB);

        Graphics2D g = bi.createGraphics();
        g.setRenderingHint(
                RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
        g.setColor(color);
        g.fill(shape);
        g.dispose();

        return bi;
    }

    public static Shape getPointedShape(int points, int radius) {
        double angle = Math.PI * 2 / points;

        GeneralPath p = new GeneralPath();
        for (int ii = 0; ii < points; ii++) {
            double a = angle * ii;

            double x = (Math.cos(a) * radius) + radius;
            double y = (Math.sin(a) * radius) + radius;
            if (ii == 0) {
                p.moveTo(x, y);
            } else {
                p.lineTo(x, y);
            }
        }
        p.closePath();

        return p;
    }

    public static void main(String[] args) throws Exception {
        Color[] colors = {
            Color.RED,
            Color.GREEN,
            Color.BLUE,
            Color.YELLOW
        };
        File f = new File(System.getProperty("user.home"));
        f = new File(f, "ShapedImages");
        f.mkdirs();
        for (Color c : colors) {
            for (int s = 15; s < 31; s += 15) {
                Shape sh = new Ellipse2D.Double(1, 1, 2 * s, 2 * s);
                BufferedImage i = getColoredShapeImage((2 * s) + 2, sh, c);
                String name = "Image"
                        + "0point-"
                        + s + "px-"
                        + c.getRed() + "r-"
                        + c.getGreen() + "g-"
                        + c.getBlue() + "b"
                        + ".png";
                File t = new File(f, name);
                ImageIO.write(i, "png", t);
                for (int ii = 3; ii < 7; ii++) {
                    sh = getPointedShape(ii, s);
                    i = getColoredShapeImage((2 * s) + 2, sh, c);
                    name = "Image"
                            + ii + "point-"
                            + s + "px-"
                            + c.getRed() + "r-"
                            + c.getGreen() + "g-"
                            + c.getBlue() + "b"
                            + ".png";
                    t = new File(f, name);
                    ImageIO.write(i, "png", t);
                }
            }
        }
        Desktop.getDesktop().open(f);
    }
}

Interface GeometricShapeImages

/**
 * Interface for accessing the images seen in
 * http://stackoverflow.com/a/16052085/418556
 */
interface GeometricShapeImages {

    public static final int BLUE = 0;
    public static final int GREEN = 1;
    public static final int RED = 2;
    public static final int YELLOW = 3;

    public static final int CIRCLE = 0;
    public static final int TRIANGLE = 1;
    public static final int DIAMOND = 2;
    public static final int PENTAGON = 3;
    public static final int HEXAGON = 4;

    /**
     * Stores a String representation of the URL for the colored shape image.
     * Can be accessed like GeometricShapeImages.URL[DIAMOND][BLUE]
     */
    public static final String[][] URL = {
        { // CIRCLE
            "http://i.stack.imgur.com/gJmeJ.png", // BLUE
            "http://i.stack.imgur.com/T5uTa.png", // GREEN
            "http://i.stack.imgur.com/wCF8S.png", // RED
            "http://i.stack.imgur.com/IHARa.png" // YELLOW
        }, // END: CIRCLE
        { // TRIANGLE
            "http://i.stack.imgur.com/L5DGx.png", // BLUE
            "http://i.stack.imgur.com/gYxHm.png", // GREEN
            "http://i.stack.imgur.com/5v2TX.png", // RED
            "http://i.stack.imgur.com/8BGfi.png" // YELLOW
        }, // END: TRIANGLE
        { // DIAMOND
            "http://i.stack.imgur.com/in9g1.png", // BLUE
            "http://i.stack.imgur.com/1lgtq.png", // GREEN
            "http://i.stack.imgur.com/F0JHK.png", // RED
            "http://i.stack.imgur.com/6ZXhi.png" // YELLOW
        }, // END: DIAMOND
        { // PENTAGON
            "http://i.stack.imgur.com/IucNt.png", // BLUE
            "http://i.stack.imgur.com/yBOv3.png", // GREEN
            "http://i.stack.imgur.com/4EVv1.png", // RED
            "http://i.stack.imgur.com/Lqkl0.png" // YELLOW
        }, // END: PENTAGON
        { // HEXAGON
            "http://i.stack.imgur.com/yoKxT.png", // BLUE
            "http://i.stack.imgur.com/zJ8am.png", // GREEN
            "http://i.stack.imgur.com/xj49g.png", // RED
            "http://i.stack.imgur.com/c67nr.png" // YELLOW
        } // END: HEXAGON
    };
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...