BufferedImage知识点

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);
    }
}

  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值