Skip to content

Instantly share code, notes, and snippets.

@shannah
Created January 14, 2016 18:26
Show Gist options
  • Save shannah/5494b9a58d07a0aa7b4e to your computer and use it in GitHub Desktop.
Save shannah/5494b9a58d07a0aa7b4e to your computer and use it in GitHub Desktop.
A background painter that puts a circle in the background of a component.
BackgroundPainter p = new BackgroundPainter(cmp) {
@Override
public void paint(Graphics g, Rectangle rect) {
boolean antiAliased = g.isAntiAliased();
g.setAntiAliased(true);
int r = Math.min(rect.getWidth(), rect.getHeight())/2;
int x = rect.getX() + rect.getWidth()/2 - r;
int y = rect.getY() + rect.getHeight()/2 - r;
switch (style) {
case CircleButtonStrokedDark:
case CircleButtonStrokedLight: {
if (cmp.getStyle().getBgTransparency() != 0) {
int alpha = cmp.getStyle().getBgTransparency();
if (alpha <0) {
alpha = 0xff;
}
g.setColor(cmp.getStyle().getBgColor());
g.setAlpha(alpha);
g.fillArc(x, y, 2*r-1, 2*r-1, 0, 360);
g.setAlpha(0xff);
}
g.setColor(cmp.getStyle().getFgColor());
g.drawArc(x, y, 2*r-1, 2*r-1, 0, 360);
break;
}
case CircleButtonFilledDark:
case CircleButtonFilledLight:
case CircleButtonTransparentDark:
case CircleButtonTransparentLight: {
int alpha = cmp.getStyle().getBgTransparency();
if (alpha < 0) {
alpha = 0xff;
}
g.setAlpha(alpha);
g.setColor(cmp.getStyle().getBgColor());
g.fillArc(x, y, 2*r, 2*r, 0, 360);
g.setAlpha(0xff);
break;
}
}
g.setAntiAliased(antiAliased);
}
};
cmp.getAllStyles().setBgPainter(p);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment