Code
class BlockedColorLayerUI extends LayerUI<JProgressBar> {
public boolean isPreventing;
private transient BufferedImage bi;
private int prevw = -1;
private int prevh = -1;
@Override public void paint(Graphics g, JComponent c) {
if (isPreventing && c instanceof JLayer) {
JLayer jlayer = (JLayer) c;
JProgressBar progress = (JProgressBar) jlayer.getView();
int w = progress.getSize().width;
int h = progress.getSize().height;
if (bi == null || w != prevw || h != prevh) {
bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
}
prevw = w;
prevh = h;
Graphics2D g2 = bi.createGraphics();
super.paint(g2, c);
g2.dispose();
Image image = c.createImage(new FilteredImageSource(
bi.getSource(), new RedGreenChannelSwapFilter()));
g.drawImage(image, 0, 0, null);
} else {
super.paint(g, c);
}
}
}
class RedGreenChannelSwapFilter extends RGBImageFilter {
@Override public int filterRGB(int x, int y, int argb) {
int r = (int) ((argb >> 16) & 0xFF);
int g = (int) ((argb >> 8) & 0xFF);
int b = (int) (argb & 0xFF);
return (argb & 0xFF_00_00_00) | (g << 16) | (r << 8) | (b);
}
}
References
Uhm, did you know you can just use JProgressBar.setForground(java.awt.Color) ?
ReplyDelete* setForeground. Sorry, typo
ReplyDeleteHi Jop, thanks for your comment.
DeleteThe following is a supplement to this example(be clearly unexplained...):
The second top `JProgressBar+setStringPainted(true)` is using `setForeground(...)` method.
However, `setForeground(...)` have no effect on the second from the bottom `JProgressBar+setStringPainted(false)+WindowsLookAndFeel`.
Plus, sets the foreground color of JProgressBar have different meanings to different `LookAndFeel`.
For example, `NimbusLookAndFeel` foreground color mean to a progress string color not filled area color of progress.