Swing - 来自 Color 的新 ImageIcon

Jac*_*ck' 1 java icons swing image colors

我需要以编程方式创建ImageIcon一个简单的颜色(比方说蓝色):

蓝色方块

所以我开始这样做:

ImageIcon imageIcon = new ImageIcon();
Run Code Online (Sandbox Code Playgroud)

现在我试图ImageIcon用蓝色填充我的。

Neu*_*ron 5

这应该可以解决问题

BufferedImage image = new BufferedImage(60, 60, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = image.createGraphics();

graphics.setPaint(new Color(0, 0, 128));
graphics.fillRect(0, 0, image.getWidth(), image.getHeight());

ImageIcon imageIcon = new ImageIcon(image);
Run Code Online (Sandbox Code Playgroud)