I am bit lost with the anonymous classes in Java, I've read about them, but how can I use this class:
private abstract class CustomRectangle {
protected final int width;
protected final int height;
protected final int xOffset;
protected final int yOffset;
protected final int borderSize;
public CustomRectangle(final int width, final int height, final int xOffset, final int yOffset, final int borderSize) {
this.width = width;
this.height = height;
this.xOffset = xOffset;
this.yOffset = yOffset;
this.borderSize = borderSize;
}
abstract void inBorder(final int dx, final int dy);
abstract void outBorder(final int dx, final int dy);
public void draw(Graphics2D g2d) {
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
int dx = Math.min(x, width - 1 - x);
int dy = Math.min(y, height - 1 - y);
if (dx < borderSize || dy < borderSize) {
inBorder(dx, dy);
}
else {
outBorder(dx, dy);
}
g2d.drawLine(x + xOffset, y + yOffset, x + xOffset, y + yOffset);
}
}
}
}
In another method to do the following things at the same time:
- Extend the CustomRectangle to override InBorder() and outBorder()
- Draw the new CustomRectangle by invoking the draw() method.
There must be an easy way for doing this and I do not feel like making a ton of classes for every time I want to draw a CustomRectangle.
Help is appreciated :)
EDIT including solution:
new CustomRectangle(CARD_DIMENSION.width, CARD_DIMENSION.height, 0, 0, 5) {
@Override
public void inBorder(final int dx, final int dy) {
g2d.setColor(new Color(red, green, blue, 255 - Math.min(dx, dy)));
}
@Override
public void outBorder(final int dx, final int dy) {
g2d.setColor(new Color(red, green, blue, 192 - Math.min(dx, dy)));
}
}.draw(g2d);
Maybe it does not look that pretty, but it comes in pretty handy in the design of my application.