2

有没有人对任何库/包有任何建议来绘制简单的几何图形,例如三角形、正方形?它可以保存为 png 格式。

4

2 回答 2

8

此源使用Graphics2DJava 2D API 来绘制一些简单的彩色形状。

作为参考,这里是生成的 32x32 图像。

在此处输入图像描述在此处输入图像描述在此处输入图像描述在此处输入图像描述
在此处输入图像描述在此处输入图像描述在此处输入图像描述在此处输入图像描述
在此处输入图像描述在此处输入图像描述在此处输入图像描述在此处输入图像描述
在此处输入图像描述在此处输入图像描述在此处输入图像描述在此处输入图像描述
在此处输入图像描述在此处输入图像描述在此处输入图像描述在此处输入图像描述

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);
    }
}

界面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
    };
}
于 2013-04-17T05:18:25.847 回答
3

您需要查看 Java 2D API: http ://docs.oracle.com/javase/tutorial/2d/overview/index.html

于 2013-04-17T03:24:11.973 回答