繁体   English   中英

在 Java 中扩展 Graphics 类

[英]Extending the Graphics class in Java

我需要扩展 Graphics 类以@Override一些方法,包括drawRect(int,int,int,int)drawRoundRect(int,int,int,int,int,int) 但是,我不知道该怎么做。 这是我到目前为止所得到的:

  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);
        }
    }

我在类声明行上收到一个错误说: myGraphics is not abstract and does not override abstract method dispose() in java.awt.Graphics我也在提到super.fill..(..)每一行上都收到一个错误说: abstract method fill..(..) in java.awt.Graphics cannot be accessed directly 有没有人知道我能做什么?

这个问题:

我需要扩展 Graphics 类以@Override 一些方法,包括 drawRect(int,int,int,int) 和 drawRoundRect(int,int,int,int,int,int)。

...实际上可能是一个XY 问题,当最好的解决方案是使用完全不同的方法时,您会询问如何解决特定的代码问题。 最好告诉我们您正在尝试解决的总体问题,而不是您目前正在尝试解决的问题:

如果你想改变你自己的图形程序的绘图行为,有比尝试扩展 Graphics 更好的方法,如果你真的必须承担它,这是一项非常困难的任务。 相反,请考虑使用扩展 JPanel 的类,并为其提供自己的 drawRect 和 drawRoundRect 方法,同时向其添加 Graphics 或 Graphics2D 参数,并在这些方法中进行任何需要的更改。

Graphics 类是抽象的,这意味着您不能从中创建对象。 这并不意味着您不能扩展它,但它确实意味着必须发生以下两种情况之一:

如果扩展它,则必须显式覆盖(实际上是编写所有方法)它的所有方法。 另一种选择是使您的 myGraphics 类抽象,但我认为这不是您想要的。

我希望这会有所帮助。

作为一个抽象类,Graphics 有一堆抽象方法。 抽象方法是未在抽象类中定义但必须在最终子类中定义的方法。 如果您不想立即定义抽象方法,则可以使子类成为抽象类,但如果您希望可以实例化该类,则最终必须定义这些方法。 由于 dispose() 是一个抽象方法,您需要说明 dispose() 应该在子类中做什么,否则通过在 {} 之间不留任何内容来欺骗计算机认为您正在告诉 dispose 做什么。

我有一个类似的问题,我通过创建 SimpleGraphics 类来解决它。 如果您让 myGraphics 类扩展 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;};
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM