BufferedImage+Graphics 生成網格彩色圖

在矩形圖上拼畫小矩形,填色

// 創建image
BufferedImage image = new BufferedImage(1000, 1000, BufferedImage.TYPE_INT_ARGB);
// 創建畫筆
Graphics graphics = image.getGraphics();
// 填充色 r,g,b,a(alpha透明度)
graphics.setColor(new Color(255, 255, 255,0));//透明
// 地圖大矩形
graphics.fillRect(0, 0, 1000, 1000);
try {
graphics.setColor(new Color(0,0,205));
graphics.fillRect(200, 0, 100,100);//參數(x,y,width,heigh )左上角座標x,y,寬,高
graphics.setColor(new Color(255,255,255,0));//透明
graphics.fillRect(300, 0, 100,100);
graphics.setColor(new Color(0,0,205));
graphics.fillRect(400, 0, 100,100);
graphics.setColor(new Color(0,0,0,0));
ImageIO.write(image, "png", new File("F:/test/GridImage/2.png"));
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();

}

ps:

image的width,heigth,小矩形的width,heigth,決定在圖上可以畫多少個的矩形;

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章