从超级继承到超级收购

时间:2019-04-26 14:38:37

标签: java casting awt multiple-inheritance superclass

我正在为学校创建一个类似于app的绘画项目,在我当前的代码中,我有几个子类和一个超级类。超级控件应包含要绘制的形状数组,每个形状对象都应是其自己的子类,以后我必须将其放入Array并从应用程序调用。我必须使用JDesktopPane和JInternalFrame,我不能使用Arraylists,而Im目前仍在尝试将RectDraw子类的Float强制转换为我的super。所有这些最终都将工具嵌套在一个名为MyShapes的超类中。欢迎任何帮助。我并没有太多使用jdesktopPane,而且在投放时效果很差。

        public class myShapes {

            public void paint(Graphics g) {

                graphSettings = (Graphics2D)g;
                graphSettings.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                graphSettings.setStroke(new BasicStroke(4));

                Iterator<Color> strokeCounter = shapeStroke.iterator();
                Iterator<Color> fillCounter = shapeFill.iterator();
                Iterator<Float> transCounter = transPercent.iterator();

                for (Shape s : shapes){
                    graphSettings.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, transCounter.next()));
                    graphSettings.setPaint(strokeCounter.next());
                    graphSettings.draw(s);
                    graphSettings.setPaint(fillCounter.next());
                    graphSettings.fill(s);
                    }


                if (drawStart != null && drawEnd != null){

                    graphSettings.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.40f));
                    graphSettings.setPaint(Color.LIGHT_GRAY);

                    Shape aShape = null;                            

                    if (currentAction == 2){                            
                        RectDraw drawRectangle = new RectDraw();
                        aShape = drawRectangle(x1, y1, x2, y2);                     
                    } 
                    else if (currentAction == 3){                           
                        CircleDraw drawEllipse = new CircleDraw();
                        aShape = drawEllipse(x1, y1, x2, y2);
                    } 
                    else if (currentAction == 4) {
                        LineDraw drawLine = new LineDraw();
                        aShape = drawLine(x1, y1, x2, y2);
                    }

                    graphSettings.draw(aShape);
                    }
                }
            }

这些是我的子类

package mainPackage;

import java.awt.geom.Rectangle2D;

public class RectDraw extends myShapes {

public Rectangle2D.Float drawRectangle(int x1, int y1, int x2, int y2) {

    int RDx, RDy, RDwidth, RDheight;

    RDx = Math.min(x1, x2);
    RDy = Math.min(y1, y2);
    RDwidth = Math.abs(x1 - x2);
    RDheight = Math.abs(y1 - y2);

    return new Rectangle2D.Float(RDx, RDy, RDwidth, RDheight);
}
}

其他人的名字完全相同

public class CircleDraw extends myShapes {

public Ellipse2D.Float drawEllipse(int x1, int y1, int x2, int y2){

     int x = Math.min(x1, x2);
     int y = Math.min(y1, y2);
     int width = Math.abs(x1 - x2);
     int height = Math.abs(y1 - y2);

     return new Ellipse2D.Float(x, y, width, height);
 }
}


public class LineDraw extends myShapes {

    public Line2D.Float drawLine(int x1, int y1, int x2, int y2) {

        return new Line2D.Float(x1, y1, x2, y2);
    }
}

我一直无法将其解析为变量

2 个答案:

答案 0 :(得分:0)

  RectDraw drawRectangle = new RectDraw();
  aShape = drawRectangle(x1, y1, x2, y2);

在第一行drawRectangle的基础上,是RectDraw的实例;

在第二行中,您将引用名称“ drawRectangle”与RectDraw类中的同名方法混淆

您必须使用RectDraw类的实例来调用该方法,如下所示。更改实例名称以使其更加清晰

  RectDraw rectDraw = new RectDraw();
  aShape = rectDraw.drawRectangle(x1, y1, x2, y2); 

所有形状都有相同的问题。

另外,您正在循环中创建很多实例,这不是一个好习惯。

您的问题太令人困惑,请在询问问题时尽量保持清晰:-https://stackoverflow.com/help/how-to-ask:)

答案 1 :(得分:0)

我想补充一点,这不是一个很好的类层次结构。您的形状应该从基本的Shape Abstract类扩展,然后在您的主要方法上可以创建形状列表。

您不能访问“超类”对象,因为形状不是实例变量。我不会使用myShapes类,但是如果必须的话,您可能应该将Shape方法与Shape.java类分开,并让myShapes.java类负责形状列表的创建。

abstract class Shape(){
    abstract void draw();
}

然后您的其他子类形状应扩展并实现paint方法:

public class Rectangle extends Shape(){
    //instance variables
    public void draw();
}

在主要方法上:

Shape[] myShapesArray = new Shape()[10];

我建议查找工厂模式。 Factory使用它并清理代码非常有意义。另外,限制为Array而不使用Lists会使绘图应用程序的管理变得有些困难。因为您将始终需要知道在数组初始化时需要多少项。

相关问题