xxxbt 2012-07-27 20:15
浏览 216
已采纳

java 图片处理

怎么利用java生成图片,希望给出代码,颜色可以不一致

[img]http://dl.iteye.com/upload/attachment/0071/4507/45ba89e2-0465-32bd-b123-b0bbaaba3393.jpg[/img]

  • 写回答

2条回答 默认 最新

  • weixin_42608382 2012-07-27 21:06
    关注

    [code="java"]
    import java.awt.Color;
    import java.awt.Font;
    import java.awt.Graphics;
    import java.awt.image.BufferedImage;
    import java.io.BufferedOutputStream;
    import java.io.FileOutputStream;

    import com.sun.image.codec.jpeg.JPEGCodec;
    import com.sun.image.codec.jpeg.JPEGImageEncoder;

    public class Drawing {

    public void graphicsImage() throws Exception {
        int imageWidth = 400;
        int imageHeight = 15;
        int imageBackHeight = 45;
        int width = 80;
    
        BufferedImage image = new BufferedImage(imageWidth, imageBackHeight, BufferedImage.TYPE_INT_RGB);
        Graphics graphics = image.getGraphics();
        graphics.setColor(Color.WHITE);
        graphics.fillRect(0, 0, imageWidth, imageBackHeight);
        graphics.setColor(Color.YELLOW);
        graphics.fillRect(0, imageHeight, width, imageHeight);
    
        graphics.setColor(new Color(244, 184, 0));
        graphics.fillRect(1*width, imageHeight, width, imageHeight);
    
        graphics.setColor(new Color(225, 127, 30));
        graphics.fillRect(2*width, imageHeight, width, imageHeight);
    
        graphics.setColor(new Color(211, 72, 29));
        graphics.fillRect(3*width, imageHeight, width, imageHeight);
    
        graphics.setColor(Color.RED);
        graphics.fillRect(4*width, imageHeight, width, imageHeight);
        graphics.setColor(Color.BLACK);
        graphics.setFont(new Font("黑体",Font.BOLD, 16));
        graphics.drawString("PM", 150, 15);
    
        graphics.setFont(new Font("宋体",Font.ITALIC, 8));
        graphics.drawString("2.5", 168, 15);
    
        graphics.dispose();
    
        FileOutputStream fos = new FileOutputStream("D:\\.jpg");
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(bos);
        encoder.encode(image);
    
        bos.close();
    
    }
    
    public static void main(String[] args) throws Exception {
        new Drawing().graphicsImage();
    }
    

    }
    [/code]

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100