Java2D基本用法

Java2D基本用法简单罗列

对外接口:
public void CreateImage(OutputStream out,int width,int height) throws Exception
{
    BufferedImage readImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);
		
    try
    {
        Graphics2D g = readImage.createGraphics();
        //...
        ImageIO.write(readImage, "png", out);
    }
    catch(Exception ex)
    {
        throw ex;
    }
}

设置字体:
g.setFont(new Font("MS Gothic", Font.BOLD, 13));

设置颜色:
g.setColor(Color.BLACK);

设置线型:
Stroke s = new BasicStroke(0.2f,BasicStroke.CAP_SQUARE, BasicStroke.JOIN_MITER, 1.0f, new float[]{3.0f, 3.0f}, 0f );
		g.setStroke(s);

设置旋转:
AffineTransform ct = AffineTransform.getRotateInstance(-Math.PI/50);
g.transform(ct);

简单纹理(砌板砖):
BufferedImage bi2 = new BufferedImage(10, 10,
BufferedImage.TYPE_INT_RGB);
Graphics2D big2 = bi2.createGraphics();
big2.setColor(Color.WHITE);
big2.fillRect(0, 0, 10, 10);
		
big2.setColor(Color.RED);
big2.drawLine(10, 0, 0, 10);
big2.drawLine(0, 0, 4, 4);
		
Rectangle2D r2 = new Rectangle(0,0,10,10);
TexturePaint tp2 = new TexturePaint(bi2,r2);

g.setPaint(tp2);

你可能感兴趣的:(Java2D)