class BufferedImageTest {
@Test
void testImage1() throws IOException {
BufferedImage bufferedImage = new BufferedImage(200, 200, BufferedImage.TYPE_INT_RGB);
Graphics2D g = bufferedImage.createGraphics();
g.drawLine(3,3,50,50);//在(3,3)与(50,50)之间画一条线段
g.drawString("hello world",60,60);
File outputfile = new File("save.png");
ImageIO.write(bufferedImage, "png", outputfile);
}
@Test
void testImage2() throws IOException {
int w=800;
int h=800;
BufferedImage bufferedImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Graphics2D g = bufferedImage.createGraphics();
String s1 = "22Hello, Java World!";
// 消除边缘抗锯齿
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
// 文本抗锯齿
g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
// 设置前景色(画笔颜色)
g.setColor(Color.red);
//设置背景色
g.setBackground(new Color(0,255,255));
//g.setBackground(Color.WHITE);
//通过使用当前绘图表面的背景色进行填充来清除指定的矩形
g.clearRect(0, 0, w, h);
// Font font1 = g.getFont();
// System.out.println("====== font1:"+font1);
Font font = new Font("Arial", Font.BOLD, 38);
g.setFont(font);
FontMetrics fm = g.getFontMetrics(font);
int height = fm.getHeight();
int width = fm.stringWidth(s1);
int posx =50; int posy = 50;
g.drawString(s1 ,posx, posy);
g.drawString("I will come in." ,posx + width, posy + height);
File outputfile = new File("save.png");
ImageIO.write(bufferedImage, "png", outputfile);
g.dispose();
}
@Test
void testImage3() throws IOException {
int w=800;
int h=800;
BufferedImage bufferedImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Graphics2D g = bufferedImage.createGraphics();
g.setColor(Color.red);
//设置背景色
g.setBackground(new Color(0,255,255));
//g.setBackground(Color.WHITE);
//通过使用当前绘图表面的背景色进行填充来清除指定的矩形
g.clearRect(0, 0, w, h);
int px1[]={50,90,10,50};//首末点相重,才能画多边形
int py1[]={10,50,50,10};
int px2[]={140,180,170,180,140,100,110,140};
int py2[]={5,25,35,45,65,35,25,5};
g.setColor(Color.blue);
g.fillPolygon(px1,py1,4);
g.setColor(Color.red);
g.drawPolygon(px2,py2,8);
g.fillOval(50,50,100,100);
g.clearRect(70,70,40,55);
File outputfile = new File("save.png");
ImageIO.write(bufferedImage, "png", outputfile);
}
@Test
void testImage4() throws IOException {
int w=800;
int h=800;
BufferedImage bufferedImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Graphics2D g = bufferedImage.createGraphics();
g.setColor(Color.red);
//设置背景色
//g.setBackground(new Color(0,255,255));
g.setBackground(Color.WHITE);
//通过使用当前绘图表面的背景色进行填充来清除指定的矩形
g.clearRect(0, 0, w, h);
String imgPath = "hello.jpg";
BufferedImage image = ImageIO.read( new FileInputStream(imgPath) );
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 1f));
g.drawImage(image, null, 0,0);
File outputfile = new File("save.png");
ImageIO.write(bufferedImage, "png", outputfile);
}
@Test
void testImage5() throws IOException {
int w=800;
int h=800;
BufferedImage bufferedImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Graphics2D g = bufferedImage.createGraphics();
g.setColor(Color.red);
//设置背景色
//g.setBackground(new Color(0,255,255));
g.setBackground(Color.WHITE);
//通过使用当前绘图表面的背景色进行填充来清除指定的矩形
g.clearRect(0, 0, w, h);
g.drawRect(80,100,40,25);//画线框
g.setColor(Color.yellow);
g.fillRect(20,70,20,30);//画着色块
File outputfile = new File("save.png");
ImageIO.write(bufferedImage, "png", outputfile);
}
@Test
void testImage6() throws IOException {
int w=800;
int h=800;
BufferedImage bufferedImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Graphics2D g = bufferedImage.createGraphics();
g.setColor(Color.red);
//设置背景色
//g.setBackground(new Color(0,255,255));
g.setBackground(Color.WHITE);
//通过使用当前绘图表面的背景色进行填充来清除指定的矩形
g.clearRect(0, 0, w, h);
g.drawRoundRect(10,10,150,70,40,25);//画一个圆角矩形
g.setColor(Color.blue);
g.fillRoundRect(80,100,100,100,60,40);//涂一个圆角矩形块
g.drawRoundRect(10,150,40,40,40,40);//画圆
g.setColor(Color.red);
g.fillRoundRect(300,300,100,100,100,100);//画圆块
File outputfile = new File("save.png");
ImageIO.write(bufferedImage, "png", outputfile);
}
@Test
void testImage7() throws IOException {
int w=800;
int h=800;
BufferedImage bufferedImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Graphics2D g = bufferedImage.createGraphics();
g.setColor(Color.red);
//设置背景色
//g.setBackground(new Color(0,255,255));
g.setBackground(Color.WHITE);
//通过使用当前绘图表面的背景色进行填充来清除指定的矩形
g.clearRect(0, 0, w, h);
g.draw3DRect(80,100,40,25,true);//画一个线框
g.setColor(Color.yellow);
g.fill3DRect(20,70,20,30,true);//画一个着色块
File outputfile = new File("save.png");
ImageIO.write(bufferedImage, "png", outputfile);
}
@Test
void testImage8() throws IOException {
int w=800;
int h=800;
BufferedImage bufferedImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Graphics2D g = bufferedImage.createGraphics();
g.setColor(Color.red);
//设置背景色
//g.setBackground(new Color(0,255,255));
g.setBackground(Color.WHITE);
//通过使用当前绘图表面的背景色进行填充来清除指定的矩形
g.clearRect(0, 0, w, h);
g.drawArc(10,40,90,50,0,180);//画圆弧线
g.drawArc(100,40,90,50,180,180);//画圆弧线
File outputfile = new File("save.png");
ImageIO.write(bufferedImage, "png", outputfile);
}
}
BufferedImage知识点
于 2024-05-19 11:03:53 首次发布