Java2D绘图实例代码

public class MyJava2D {
    public static void main(String[] args) throws FileNotFoundException, IOException {
        int width = 400;
        int height = 400;
       BufferedImage bi = new 
               BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
       Graphics2D graphics2d = (Graphics2D) bi.getGraphics();
       //设置颜色为白色的填充形矩形
       graphics2d.setColor(Color.WHITE);
       graphics2d.fillRect(0, 0, 400, 400);
     //设置颜色为黑色的描边形矩形
       graphics2d.setColor(Color.BLACK);
       graphics2d.drawRect(100, 100, 200, 200); 
       //设置颜色为粉红色,字体为40的宋黑体文字learn
       graphics2d.setFont(new Font("宋体",Font.BOLD,40));
       graphics2d.setColor(Color.PINK);
       graphics2d.drawString("learn", 50, 50);
       //设置颜色为红色的线条
       graphics2d.setColor(Color.RED);
       graphics2d.drawLine(150, 150, 20, 20); 
       
       FileOutputStream fos = new FileOutputStream("aaa.jpg");
       ImageIO.write(bi, "JPG", fos);
       
       fos.flush();  
       fos.close();  
       
       graphics2d.dispose();
       
    }

}
Java2D绘图实例代码_第1张图片
Java2D绘图实例代码.jpg

你可能感兴趣的:(Java2D绘图实例代码)