ficabj5
ficabj5

Reputation: 111

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?

Upvotes: 2

Views: 1894

Answers (3)

Joshua4532
Joshua4532

Reputation: 1

As an abstract class, Graphics has a bunch of abstract methods. Abstract methods are methods which are not defined in the abstract class but have to be defined in an eventual sub class. You can make the subclass an abstract class if you don't want to define the abstract methods right away but if you want it to be possible to instantiate the class you will eventually have to define the methods. As dispose() is an abstract method you need to say what dispose() should do in a sub class or else trick the computer into thinking you are telling dispose what to do by leaving nothing in between the {}.

I had a similar problem and I solved it by making the class SimpleGraphics. It may solve your problem if you make your myGraphics class extend SimpleGraphics.

import java.awt.*;
import javax.swing.JFrame;
import java.awt.image.ImageObserver;
import java.text.AttributedCharacterIterator;
public class SimpleGraphics extends Graphics{
    public void clearRect(int x, int y, int width, int height){};
    public void clipRect(int x, int y, int width, int height){};
    public void copyArea(int x, int y, int width, int height, int dx, int dy){};
    public void dispose(){};
    public void drawArc(int x, int y, int width, int height, int startAngle, int arcAngle){};
    public boolean drawImage(Image img, int x, int y, Color bgcolor, ImageObserver observer){return false;};
    public boolean drawImage(Image img, int x, int y, ImageObserver observer){return false;};
    public boolean drawImage(Image img, int x, int y, int width, int height, Color bgcolor, ImageObserver observer){return false;};
    public boolean drawImage(Image img, int x, int y, int width, int height, ImageObserver observer){return false;};
    public boolean drawImage(Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, Color bgcolor, ImageObserver observer){return false;};
    public boolean drawImage(Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, ImageObserver observer){return false;};
    public void drawLine(int x1, int y1, int x2, int y2){};
    public void drawOval(int x, int y, int width, int height){};
    public void drawPolygon(int[] xPoints, int[] yPoints, int nPoints){};
    public void drawPolyline(int[] xPoints, int[] yPoints, int nPoints){};
    public void drawRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight){};
    public void drawString(AttributedCharacterIterator iterator, int x, int y){};
    public void drawString(String str, int x, int y){};
    public void fillArc(int x, int y, int width, int height, int startAngle, int arcAngle){};
    public void fillOval(int x, int y, int width, int height){};
    public void fillPolygon(int[] xPoints, int[] yPoints, int nPoints){};
    public void fillRect(int x, int y, int width, int height){};
    public void fillRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight){};
    public Shape getClip(){return null;};
    public Rectangle getClipBounds(){return null;};
    public Color getColor(){return null;};
    public Font getFont(){return null;};
    public FontMetrics getFontMetrics(Font f){return null;};
    public void setClip(int x, int y, int width, int height){};
    public void setClip(Shape clip){};
    public void setColor(Color c){};
    public void setFont(Font font){};
    public void setPaintMode(){};
    public void setXORMode(Color c1){};
    public void translate(int x, int y){};
    public Graphics create(){return null;};
}

Upvotes: 0

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

This issue:

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).

...may in fact be an XY Problem where you ask how to solve a specific code problem when the best solution is to use a completely different approach. Better that you tell us the overall problem that you're trying to solve rather than how you're currently trying to solve it:

If you want to change the drawing behavior of your own graphics program, there are better ways than in trying to extend Graphics, a very difficult task to do if you really had to undertake it. Instead, consider using a class that extends JPanel, and give it its own drawRect and drawRoundRect methods, but also add a Graphics or Graphics2D parameter to it, and within these methods do whatever changes are needed.

Upvotes: 2

ItamarG3
ItamarG3

Reputation: 4122

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.

Upvotes: 1

Related Questions