javacastingawtmultiple-inheritancesuperclass

acquiring float properties from supclass to super


im creating a paint project like app for school and in my current code i have a several subclasses and a super. The super should hold the an array of the shapes to draw and every shape object should be its own subclass that i later have to put into a Array and call from the app. I have to use JDesktopPane and JInternalFrame, I cant use Arraylists and Im currently stuck on trying to cast the Float of my RectDraw subclass to my super. All this before finally nesting the tools in a super named MyShapes. any help is welcomed. I dont used jdesktopPane much and im bad at casting.

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

These are my Sub Classes

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

the others are completely the same save for the names

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

I keep getting cannot be resolved to a variable


Solution

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

    Based on the 1st line drawRectangle is a instance of RectDraw;

    In the second line you're confusing your reference name "drawRectangle" with the method of same name in RectDraw class

    You have to call the method using the instance of your RectDraw class like below..changing the instance name for clarity

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

    Same issue for all shapes..

    Also you're creating a lot of instances in a loop which is not a good practice.