java – 当我说红色时,BufferedImage画出白色
发布时间:2020-05-25 01:26:58 所属栏目:Java 来源:互联网
导读:它一定是一个非常愚蠢的解决方案,但我是盲目的. 我有这个代码: BufferedImage bi = new BufferedImage(800, 600, BufferedImage.TYPE_INT_RGB);bi.getGraphics().setColor(Color.red);bi.getGraphics().fillRect(300, 350, 100, 50);ImageIO.
|
它一定是一个非常愚蠢的解决方案,但我是盲目的. 我有这个代码: BufferedImage bi = new BufferedImage(800,600,BufferedImage.TYPE_INT_RGB);
bi.getGraphics().setColor(Color.red);
bi.getGraphics().fillRect(300,350,100,50);
ImageIO.write(bi,"jpeg",new File("image.jpg"));
我得到了这个黑色800×600矩形和一个白色矩形.为什么是这样? 谢谢 :) 解决方法每次在BufferedImage上调用getGraphics()时,都会得到一个新的Graphics对象,因此将颜色设置为一个,不会在下一个上设置它.所以缓存图形对象.BufferedImage bi = new BufferedImage(800,BufferedImage.TYPE_INT_RGB);
Graphics g = bi.getGraphics();
g.setColor(Color.red);
g.fillRect(300,new File("/home/dave/image.jpg")); (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
