Paketstruktur und import Klauseln

Ich habe die Aufgabe bekommen mein Projekt nach folgender Vorgabe auf Pakete zu verteilen:
- edu.hm.cs.swe1 für das Hauptprogramm
- edu.hm.cs.swe1.sheep für die Klasse Sheep.java und alle Schafsbestandteile
- edu.hm.cs.swe1.graphics für alle Klassen und Typen, die die Grafik umsetzen

Ich habe es also wie folgt aufgeteilt 12683
und graphics in den Klassen von Sheep und Main importiert

Nachdem ich das nun gemacht habe kriege ich bei einigen Methoden die Fehlermeldung
- The type [...] is not visible
- [...] cannot be resolved


Java:
package edu.hm.cs.swe1;
import edu.hm.cs.swe1.sheep.*;
import edu.hm.cs.swe1.graphics.*;



/**
*
*
*
*/

public class Main {

    public static void main(String[] args) {
      
        Sheep shawna = new Sheep(170, 100, Colour.RED);
        shawna.draw();
      
      
    }
}
Java:
package edu.hm.cs.swe1.sheep;

import edu.hm.cs.swe1.graphics.*;


public class Head {
  

  
   private int xPos;
   private int yPos;
   private Colour colour;
   private Ellipse face;
   private Nostril leftNostril;
   private Nostril rightNostril;
   private Eye leftEye;
   private Eye rightEye;
   private Ear leftEar;
   private Ear rightEar;



   public void draw() {
       HelperClass.wait(250);
       face.draw();
       HelperClass.wait(250);
       leftNostril.draw();
       HelperClass.wait(250);
       rightNostril.draw();
       HelperClass.wait(250);
       leftEar.draw();
       HelperClass.wait(250);
       rightEar.draw();
       HelperClass.wait(250);
       leftEye.draw();
       HelperClass.wait(250);
       rightEye.draw();

}


   public Head(int x, int y, Colour initialColour) {
       xPos = x;
       yPos = y;
       colour = initialColour;
    
    

       face = new Ellipse(80, 120, xPos, yPos, colour);
    
       leftNostril = new Nostril (20 + xPos, 84 + yPos);
       rightNostril = new Nostril (50 + xPos, 84 + yPos);
    
       leftEye = new Eye(10+xPos, 31+yPos, Colour.BLACK);
       rightEye = new Eye(50+xPos, 31+yPos, Colour.BLACK);
    
       leftEar = new Ear(-35+xPos, 10+yPos, Colour.RED);
       rightEar = new Ear(66+xPos, 10+yPos, Colour.RED);
      

   }


}

In der Head.java Klasse kann er zB den Typ Ellipse nicht auflösen weil er nicht sichtbar ist, dabei ist Ellipse doch importiert durch das Paket graphics
Fehlen da jetzt noch imports oder liegt der Fehler in der Aufteilung an sich?
 
Das liegt vermutlich daran, dass irgendwelche Klassen oder Methoden package private oder protected sind.

Damit man auf Methoden bzw. Klassen aus anderen Packages zugreifen können, müssen die public sein.
 
Java:
package edu.hm.cs.swe1.graphics;
/**
 * Contains helper methods such as wait.
 *
 *
 */
class HelperClass {

    /**
     * Wait for a specified number of milliseconds before finishing. This
     * provides an easy way to specify a small delay which can be used when
     * producing animations.
     *
     * @param milliseconds
     *            the number
     */
    static void wait(int milliseconds) {
        try {
            Thread.sleep(milliseconds);
        } catch (Exception e) {
            // ignoring exception at the moment
        }
    }

}

Java:
package edu.hm.cs.swe1.graphics;
import java.awt.*;
import java.awt.geom.*;
import java.util.*;

import javax.swing.*;

/**
 * Class represents and draws ellipses in a window. The half axes of the
 * ellipses run parallel to the cartesian coordinate system underlying the
 * window, whose origin is positioned in its upper left corner.
 *
 * The ellipse's position denotes the upper left corner of the (virtual)
 * rectangle which circumscribes the ellipse.
 *

 */
class Ellipse {
    /** Length of horizontal axis */
    int horizontalLength;

    /** Length of vertical axis */
    int verticalLength;

    /**
     * The x- and y-coordinates describing the ellipse's position, denoting the
     * top left corner of its surrounding rectangle.
     */
    int xPos, yPos;

    /**
     * The ellipse's colour. Available colours are enumerated in Type Colour.
     */
    Colour colour;

    /**
     * Creates the ellipse, initializes all its attributes and draws it.
     *
     * @param h
     *            Horizontal Length
     * @param v
     *            Vertical Length
     * @param x
     *            x-coordinate
     * @param y
     *            y-coordinate
     * @param initialColour
     *            Initial color
     */
    Ellipse(int h, int v, int x, int y, Colour initialColour) {
        horizontalLength = h;
        verticalLength = v;
        xPos = x;
        yPos = y;
        colour = initialColour;
    }

    public Ellipse() {
        // TODO Auto-generated constructor stub
    }

    /**
     * Sets the ellipse's color to a new value and redraws the ellipse.
     *
     * @param newColour
     */
    void setColour(Colour newColour) {
        colour = newColour;
    }

    /**
     * Reset horizontalLength to a new value.
     *
     * @param h
     *            New horizontal length.
     */
    void setHorizontalLength(int h) {
        horizontalLength = h;
    }

    /**
     * Reset verticalLength to a new value.
     *
     * @param v
     *            New vertical length.
     */
    void setVerticalLength(int v) {
        verticalLength = v;
    }

    /**
     * Erases existing ellipse. Moves the ellipse by resetting its coordinates.
     * Redraws moved ellipse.
     *
     * @param xDelta
     *            Denotes horizontal movement
     * @param yDelta
     *            Denotes vertical movement
     */
    void move(int xDelta, int yDelta) {
        xPos = xPos + xDelta;
        yPos = yPos + yDelta;
    }

    // /////////////////////////////////////////////////////////////////
    // Alles was ab hier kommt wurde in der Vorlesung nicht besprochen
    // /////////////////////////////////////////////////////////////////

    void draw() {
        Canvas canvas = Canvas.getCanvas();
        canvas.draw(this, colour, new Ellipse2D.Double(xPos, yPos,
                horizontalLength, verticalLength));
        canvas.wait(250);
    }

    void erase() {
        Canvas canvas = Canvas.getCanvas();
        canvas.erase(this);
    }

    private static class Canvas {
        // Note: The implementation of this class (specifically the handling of
        // shape identity and colors) is slightly more complex than necessary.
        // This
        // is done on purpose to keep the interface and instance fields of the
        // shape objects in this project clean and simple for educational
        // purposes.

        private static Canvas canvasSingleton;

        /**
         * Factory method to get the canvas singleton object.
         */
        public static Canvas getCanvas() {
            if (canvasSingleton == null) {
                canvasSingleton = new Canvas("Shawna das Schaf", 500, 300,
                        Color.white);
            }
            canvasSingleton.setVisible(true);
            return canvasSingleton;
        }

        // ----- instance part -----

        private JFrame frame;
        private CanvasPane canvas;
        private Graphics2D graphic;
        private Color backgroundColor;
        private Image canvasImage;
        private java.util.List<Object> objects;
        private HashMap<Object, ShapeDescription> shapes;

        /**
         * Create a Canvas.
         *
         * @param title
         *            title to appear in Canvas Frame
         * @param width
         *            the desired width for the canvas
         * @param height
         *            the desired height for the canvas
         * @param bgColor
         *            the desired background color of the canvas
         */
        private Canvas(String title, int width, int height, Color bgColor) {
            frame = new JFrame();
            canvas = new CanvasPane();
            frame.setContentPane(canvas);
            frame.setTitle(title);
            frame.setLocation(30, 30);
            canvas.setPreferredSize(new Dimension(width, height));
            backgroundColor = bgColor;
            frame.pack();
            objects = new ArrayList<Object>();
            shapes = new HashMap<Object, ShapeDescription>();
        }

        /**
         * Set the canvas visibility and brings canvas to the front of screen
         * when made visible. This method can also be used to bring an already
         * visible canvas to the front of other windows.
         *
         * @param visible
         *            boolean value representing the desired visibility of the
         *            canvas (true or false)
         */
        public void setVisible(boolean visible) {
            if (graphic == null) {
                // first time: instantiate the offscreen image and fill it with
                // the background color
                Dimension size = canvas.getSize();
                canvasImage = canvas.createImage(size.width, size.height);
                graphic = (Graphics2D) canvasImage.getGraphics();
                graphic.setColor(backgroundColor);
                graphic.fillRect(0, 0, size.width, size.height);
                graphic.setColor(Color.black);
            }
            frame.setVisible(visible);
        }

        /**
         * Draw a given shape onto the canvas.
         *
         * @param referenceObject
         *            an object to define identity for this shape
         * @param color
         *            the color of the shape
         * @param shape
         *            the shape object to be drawn on the canvas
         */
        // Note: this is a slightly backwards way of maintaining the shape
        // objects. It is carefully designed to keep the visible shape
        // interfaces
        // in this project clean and simple for educational purposes.
        public void draw(Object referenceObject, Colour colour, Shape shape) {
            objects.remove(referenceObject); // just in case it was already
                                                // there
            objects.add(referenceObject); // add at the end
            shapes.put(referenceObject, new ShapeDescription(shape, colour));
            redraw();
        }

        /**
         * Erase a given shape's from the screen.
         *
         * @param referenceObject
         *            the shape object to be erased
         */
        public void erase(Object referenceObject) {
            objects.remove(referenceObject); // just in case it was already
                                                // there
            shapes.remove(referenceObject);
            redraw();
        }

        /**
         * Set the foreground color of the Canvas.
         *
         * @param newColor
         *            the new color for the foreground of the Canvas
         */
        public void setForegroundColor(Colour colorString) {
            switch (colorString) {
            case WHITE:
                graphic.setColor(Color.white);
                break;
            case YELLOW:
                graphic.setColor(new Color(255, 230, 0));
                break;
            case GREEN:
                graphic.setColor(new Color(80, 160, 60));
                break;
            case BLUE:
                graphic.setColor(new Color(30, 75, 220));
                break;
            case MAGENTA:
                graphic.setColor(Color.magenta);
                break;
            case RED:
                graphic.setColor(new Color(235, 25, 25));
                break;
            case BLACK:
                graphic.setColor(Color.black);
                break;
            default:
                graphic.setColor(Color.black);
            }
        }

        /**
         * Wait for a specified number of milliseconds before finishing. This
         * provides an easy way to specify a small delay which can be used when
         * producing animations.
         *
         * @param milliseconds
         *            the number
         */
        public void wait(int milliseconds) {
            try {
                Thread.sleep(milliseconds);
            } catch (Exception e) {
                // ignoring exception at the moment
            }
        }

        /**
         * Redraw ell shapes currently on the Canvas.
         */
        private void redraw() {
            erase();
            for (Object shape : objects) {
                shapes.get(shape).draw(graphic);
            }
            canvas.repaint();
        }

        /**
         * Erase the whole canvas. (Does not repaint.)
         */
        private void erase() {
            Color original = graphic.getColor();
            graphic.setColor(backgroundColor);
            Dimension size = canvas.getSize();
            graphic.fill(new Rectangle(0, 0, size.width, size.height));
            graphic.setColor(original);
        }

        /************************************************************************
         * Inner class CanvasPane - the actual canvas component contained in the
         * Canvas frame. This is essentially a JPanel with added capability to
         * refresh the image drawn on it.
         */
        private class CanvasPane extends JPanel {
            private static final long serialVersionUID = 1L;

            public void paint(Graphics g) {
                g.drawImage(canvasImage, 0, 0, null);
            }
        }

        /************************************************************************
         * Inner class CanvasPane - the actual canvas component contained in the
         * Canvas frame. This is essentially a JPanel with added capability to
         * refresh the image drawn on it.
         */
        private class ShapeDescription {
            private Shape shape;
            private Colour colourString;

            public ShapeDescription(Shape shape, Colour colour) {
                this.shape = shape;
                colourString = colour;
            }

            public void draw(Graphics2D graphic) {
                setForegroundColor(colourString);
                graphic.fill(shape);
            }
        }

    }

}
 
Sind jetzt public aber folgende Fehlermeldung bleibt:
Cannot make a static reference to the non-static method wait(long) from the type Object

dabei ist wait doch static?
 
ok, ich sehs. es muss wirklich alles separat als public gesetzt werden und alles wo nichts steht ist automatisch nicht public und muss auch public gesetzt werden, danke
 
Zuletzt bearbeitet:
Es soll auch der Grundsatz "so restriktiv wie möglich, so locker wie nötig" berücksichtigt werden. Ist dies noch der Fall wenn die Methoden alle auf public gesetzt sind?
Und wie sorge ich dafür, dass die Main Methode nicht auf die Einzelteile(Leg,Ear,Eye etc) sondern nur auf das ganze Schaaf zugreifen kann? Ich habe versucht die Klassen auf protected zu setzen, Eclipse erlaubt dies aber nicht...
 
Es soll auch der Grundsatz "so restriktiv wie möglich, so locker wie nötig" berücksichtigt werden. Ist dies noch der Fall wenn die Methoden alle auf public gesetzt sind?
Wenn einfach alles öffentlich ist, ist dass dann noch "so restriktiv wie möglich"? :)

Und wie sorge ich dafür, dass die Main Methode nicht auf die Einzelteile(Leg,Ear,Eye etc) sondern nur auf das ganze Schaaf zugreifen kann? Ich habe versucht die Klassen auf protected zu setzen, Eclipse erlaubt dies aber nicht...
Es gibt noch "package-private", wenn man eine Angabe weg lässt :)
 
Passende Stellenanzeigen aus deiner Region:

Neue Themen

Oben