Graphics2D简单示例

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class TestGraphics2D {
 public static void main(String[] args) {

  BufferedImage bi = new BufferedImage(60, 60, BufferedImage.TYPE_INT_RGB);

  Graphics2D g2 = bi.createGraphics();
  g2.setBackground(Color.black);
  g2.setColor(Color.green);
  g2.drawLine(15, 15, 25, 15);// 点的坐标到坐标 以左上点为 (0.0) 第一点为(15,15)
         // 第二点(25,15)
  g2.setColor(Color.blue);// 重新设置
  g2.setFont(new Font("隶书", Font.PLAIN, 15));// 字体设置
  g2.drawString("你好", 15, 35);// //要写的文字和开始坐标
  g2.setColor(Color.green);
  // g2.setStroke((Stroke) new
  // BasicStroke(2,BasicStroke.CAP_SQUARE,BasicStroke.JOIN_ROUND));
  g2.setStroke(new BasicStroke(3.5f));// 设置划线的宽度

  g2.drawRect(15, 17, 35, 23);//花巨型  (15,17)坐标 ,35,23 宽高
  g2.setColor(Color.WHITE);
  g2.fillRect(15, 17, 5, 5);//填充矩形区域
  g2.setColor(Color.orange);
  g2.drawLine(15, 55, 45, 55);

  try {
   // 写入文件
   ImageIO.write(bi, "png", new File("D:/testGraphics2D/test8.png"));
  } catch (IOException e) {
   e.printStackTrace();
  } finally {
   System.out.println("over");
  }

 }

}

你可能感兴趣的:(Graphics2D简单示例)