任何java包来绘制简单的几何形状?
Posted
技术标签:
【中文标题】任何java包来绘制简单的几何形状?【英文标题】:Any java package to draw simple geometric shape? 【发布时间】:2013-04-09 16:09:44 【问题描述】:有没有人对任何库/包有任何建议来绘制简单的几何图形,例如三角形、正方形?可以保存成png格式。
【问题讨论】:
我要在这里冒个险,说我认为你所要求的可以使用BufferedImage
Graphics
和ImageIO
来实现,但我不确定
谢谢。是的。我用谷歌搜索了一下。
【参考方案1】:
此源使用 Java 2D API 的Graphics2D
来绘制一些简单的彩色形状。
作为参考,这里是生成的 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://***.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
;
【讨论】:
【参考方案2】:您会想看看 Java 2D API: http://docs.oracle.com/javase/tutorial/2d/overview/index.html
【讨论】:
以上是关于任何java包来绘制简单的几何形状?的主要内容,如果未能解决你的问题,请参考以下文章