1

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:

  1. Extend the CustomRectangle to override InBorder() and outBorder()
  2. 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.

3
  • It's not at all clear what you mean by point 2. At what point do you need to draw the rectangle? And do you really need to create that many different ways of drawing borders? Can you not make that data-driven instead, e.g. taking the border width as a parameter? Commented Apr 4, 2013 at 15:24
  • It is not about the border width, it is about the fact that I want to handle the color assigned per pixel differently for different rectangles (and also differently on in- and outside border). And I want to draw the Rectangle immediatly when I construct this anonymous class. Commented Apr 4, 2013 at 15:25
  • So can't you just construct the instance of the anonymous class and then call draw? Commented Apr 4, 2013 at 15:47

2 Answers 2

7

The syntax for an anonymous class extending CustomRectangle would be:

CustomRectangle r = new CustomRectangle() {
    @Override
    public void inBorder(final int dx, final int dy) {
        // implementation
    }
    @Override
    public void outBorder(final int dx, final int dy) {
        // implementation
    }
}

Another approach that avoids the use of these anonymous classes would be to parameterize your rectangle class with a helper object to implement inBorder and outBorder. Then the rectangle class would not have to be abstract; instead it would delegate the implementation to the helper object (which could be supplied at construction or through setters).

Sign up to request clarification or add additional context in comments.

2 Comments

Used your solution and it works fine :) Edited my original post.
@skiwi - If this worked for you, perhaps you should mark it as the solution. As described in the FAQ, doing this helps others in the community because it marks what worked. You should also do this with the other questions of your that have received good answers.
0

You can extend this abstract class with anonymous class by simply defining the functions you need when you need an instance of the class. It looks like this:

CustomRectangle rect = new CustomRectangle() {
    @Override
    public void inBorder(final int dx, final int dy) {
        // Your implementation here.
    }
    @Override
    public void outBorder(final int dx, final int dy) {
        // Your implementation here.
    }
}

Then you can simply call rect.draw() or whatever else you need--it's an object like any other.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.