ficabj5 ficabj5 - 1 year ago 79
Java Question

Extending the Graphics class in Java

I need to extend the Graphics class in order to

some methods including
However, I have no idea how to do that. This is what I've got so far:

public class myGraphics extends Graphics {
public void drawRect(int x, int y, int width, int height) {
super.fillRect(x, y, width, height);
super.fillRect(x, y, width-6, height-6);

public void drawRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight) {
super.fillRoundRect(x, y, width, height, arcWidth, arcHeight);
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
is mentioned saying:
abstract method fill..(..) in java.awt.Graphics cannot be accessed directly
Does anyone have an idea on what can I do?

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download