Skip to content
Advertisement

Extending the Graphics class in Java

I need to extend the Graphics class in order to @Override some methods including drawRect(int,int,int,int) and drawRoundRect(int,int,int,int,int,int). However, I have no idea how to do that. This is what I’ve got so far:

  public class myGraphics extends Graphics {
      @Override
      public void drawRect(int x, int y, int width, int height) {
          super.fillRect(x, y, width, height);
          setColor(Color.WHITE);
          super.fillRect(x, y, width-6, height-6);
      }

      @Override
      public void drawRoundRect(int x, int  y, int width, int height, int arcWidth, int arcHeight) {
          super.fillRoundRect(x, y, width, height, arcWidth, arcHeight);
          setColor(Color.WHITE);
          super.fillRoundRect(x, y, width-6, height-6, arcWidth, arcHeight);
        }
    }

I get an error on class declaration line saying: myGraphics is not abstract and does not override abstract method dispose() in java.awt.Graphics I also get an error on every line where super.fill..(..) is mentioned saying: abstract method fill..(..) in java.awt.Graphics cannot be accessed directly. Does anyone have an idea on what can I do?

Advertisement

Answer

The Graphics class is abstract, meaning you can’t create an object out of it. This doesn’t mean you can’t extends it but it does mean that one of two must happen:

If you extend it, you have to override explicitly (actually write all the methods) all it’s methods. another option is making your myGraphics class abstract, but I don’t think that’s what you wanted.

I hope this helps.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement