将BufferedImage设置为Java中的颜色 [英] Set BufferedImage to be a color in Java

查看:124
本文介绍了将BufferedImage设置为Java中的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要用指定的背景颜色创建一个矩形的 BufferedImage ,在背景上绘制一些图案并保存到文件中。我不知道如何创建背景。

I need to create a rectangular BufferedImage with a specified background color, draw some pattern on the background and save it to file. I don't know how to create the background.

我使用嵌套循环:

I am using a nested loop:

BufferedImage b_img = ...
for every row
for every column
setRGB(r,g,b);

但是当图片很大时它非常慢。

But it's very slow when the image is large.

如何以更有效的方式设置颜色?

How to set the color in a more efficient way?

推荐答案

获取图像的图形对象,将当前的颜色设置为所需的颜色,然后调用 fillRect(0,0,宽度,高度)

Get the graphics object for the image, set the current paint to the desired colour, then call fillRect(0,0,width,height).

BufferedImage b_img = ...
Graphics2D    graphics = b_img.createGraphics();

graphics.setPaint ( new Color ( r, g, b ) );
graphics.fillRect ( 0, 0, b_img.getWidth(), b_img.getHeight() );

这篇关于将BufferedImage设置为Java中的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆