任何java包绘制简单的几何形状?

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

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

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

作为参考,以下是产生的32×32图像。

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

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