最近有个作业是有关多边形继承的问题, 按照提供的代码MovingCircle class, MovingRectangle class 都是 MovingShape 的子类. 另外MovingSquare, MovingParallelogram(平行四边形)是要求我们自己添加的.我是把square和parallelogram都做为rectangle的子类,这里且不说这样做对不对,但是我在画平行四边形时却遇到一个问题. 重写了Draw方法之后运行得到的只是一个点, 我又试着把MovingShape直接作为平行四边形的父类(本来应该是super of the super class), 在不改变任何code的情况下得到的确是square,总之画不出平行四边形. 程序中Circle, rectangle, square都运行正常.
 
代码有点多, 希望高手有耐心看看问题出在哪 (部分class和代码省略)import java.awt.*;
public abstract class MovingShape {  public int marginWidth, marginHeight; // the margin of the animation panel area
  protected Point p;  // the top left coner of shapes
  protected int width, height; // the width and height of shapes
  protected Color fillColor;  // the fill colour of shapes
  protected MovingPath path; // the moving path of shapes
  protected Color borderColor;  // the border colour of shapes
  protected boolean selected = false;  // draw handles if selected
  protected int side;            //the side of the shape
  protected float penWidth;     // the pen width of a shape
  /** constuctor to create a shape with default values
   */
  public MovingShape() {
    this(0, 0, 20, 20, 20, 500, 500, Color.blue, Color.black, 1.0f, 0); // the default properties
  }  /** constuctor to create a shape
   * @param x  the x-coordinate of the new shape
   * @param y the y-coordinate of the new shape
   * @param w  the width of the new shape
   * @param h  the height of the new shape
   * @param mw  the margin width of the animation panel
   * @param mh the margin height of the animation panel
   * @param fc the fill colour of the new shape
   * @param bc the border colour of the new shape
   * @param typeOfPath  the path of the new shape
   */
  public MovingShape(int x, int y, int s, int w, int h, int mw, int mh, Color fc, Color bc, float pw, int pathType) {
    p = new Point(x,y);
    marginWidth = mw;
    marginHeight = mh;
    fillColor = fc;
    borderColor = bc;
    width = w;
    height = h;
    side = s;
    penWidth = pw; 
    setPath (pathType);
  }  /** Return the x-coordinate of the shape.
   * @return the x coordinate
   */
  public int getX() { return p.x; }  /** Return the y-coordinate of the shape.
   * @return the y coordinate
   */
  public int getY() { return p.y;}  /** Return the selected property of the shape.
   * @return the selected property
   */
  public boolean isSelected() { return selected; }  /** Set the selected property of the shape.
   *  When the shape is selected, its handles are shown.
   * @param s  the selected value
   */
  public void setSelected(boolean s) { selected = s; }  /** Set the width of the shape.
   * @param w  the width value
   */
  public void setWidth(int w) { width = w; }  /** Set the height of the shape.
   * @param h  the height value
   */
  public void setHeight(int h) { height = h; }  /**
   * Return a string representation of the shape, containing
   * the String representation of each element.
   */
  public String toString() {
    return "[" + this.getClass().getName() + "," + p.x + "," + p.y + "," + width + "," + height + "]";
  }  /** Draw the handles of the shape
   * @param g  the Graphics control
   */
  public void drawHandles(Graphics g) {
    // if the shape is selected, then draw the handles
    if (isSelected()) {
      g.setColor(Color.black);
      g.fillRect(p.x -2, p.y-2, 4, 4);
      g.fillRect(p.x + width -2, p.y + height -2, 4, 4);
      g.fillRect(p.x -2, p.y + height -2, 4, 4);
      g.fillRect(p.x + width -2, p.y-2, 4, 4);
    }
  }
   
  public void drawOutLine(Graphics g) {
    Graphics2D g2d = (Graphics2D)g;
g2d.setStroke(new BasicStroke(penWidth));
  }

  /** Reset the margin for the shape
   * @param w  the margin width
   * @param h  the margin height
   */
  public void setMarginSize(int w, int h) {
    marginWidth = w;
    marginHeight = h;
  }  /** Set the fill colour of the shape.
  * @param c  the fill colour
  */
  public void setFillColor(Color c) { fillColor = c; }  /** Set the border colour of the shape.
  * @param c  the border colour
  */
  public void setBorderColor(Color c) { borderColor =c; }
  
  /** Set the pen width of the shape.
  * @param pw  the pen width value
  */
  public void setPenWidth(float pw) { penWidth = pw;}  /** abstract contains method
   * Returns whether the point p is inside the shape or not.
   * @param p the mouse point
   */
  public abstract boolean contains(Point p);  /** abstract draw method
   * draw the shape
   * @param g  the Graphics control
   */
  public abstract void draw(Graphics g);
  
  /** Set the path of the shape.
   * @param pathID  the integer value of the path
   *  MovingPath.BOUNDARY is the boundary path
   *  MovingPath.FALLING is the falling path
   */
  public void setPath(int pathID) {
    switch (pathID) {
      case MovingPath.BOUNDARY : {
        path = new BoundaryPath(10, 10);
        break;
      }
      case MovingPath.FALLING : {
        path = new FallingPath();
        break;
      }
    }
  }  /** move the shape by the path
   */
  public void move() {
    path.move();
  }---------------------------------------------------------------------
import java.awt.*;
public class MovingRectangle extends MovingShape {  /** constuctor to create a rectangle with default values
   */
  public MovingRectangle() {
    super();
  }  /** constuctor to create a rectangle shape
   */
  public MovingRectangle(int x, int y, int s, int w, int h, int mw, int mh, Color fc, Color bc, float pw, int pathType) {
    super(x ,y, 0, w, h, mw, mh, fc, bc, pw, pathType);
  }  /** draw the rectangle with the fill colour
   *  If it is selected, draw the handles
   *  @param g the Graphics control
   */
  public void draw(Graphics g) {
    g.setColor(fillColor);
    g.fillRect(p.x, p.y, width, height);
    g.setColor(borderColor);
    drawOutLine(g);  //Change thickness of the border
g.drawRect(p.x, p.y, width, height);
    drawHandles(g);
  }  /** Returns whether the point is in the rectangle or not
   * @return true if and only if the point is in the rectangle, false otherwise.
   */
  public boolean contains(Point mousePt) {
    return (p.x <= mousePt.x && mousePt.x <= (p.x + width + 1)  &&  p.y <= mousePt.y && mousePt.y <= (p.y + height + 1));
  }
}
-------------------------------------------------------------------------------
import java.awt.*;
public class MovingParallelogram extends MovingShape {
  private int offset;
  //private double dSide = double(side);
  /** constuctor to create an rhombus with default values
   */
  public MovingParallelogram() {
   super();
  }
  
  /** constuctor to create a rhombus shape
   */
  public MovingParallelogram(int x, int y, int s, int mw, int mh, Color fc, Color bc, float pw, int pathType) {
    super(x ,y, s, s, s, mw, mh, fc, bc, pw, pathType);
    offset = (int)0.2 * s;
  }
  
  /** draw the parallelogram with the fill colour
   *  If it is selected, draw the handles
   *  @param g the Graphics control
   */
  public void draw(Graphics g) {
    int[] xP = new int[]{p.x + offset, p.x + side + offset, p.x + side, p.x};
int[] yP = new int[]{p.y, p.y, p.y + side, p.y + side};
int nP = 4;
Polygon poly = new Polygon(xP, yP, nP);
Graphics2D g2d = (Graphics2D)g;
g.setColor(fillColor);
    g2d.fill(poly);
    g.setColor(borderColor);
    drawOutLine(g);  //Change thickness of the border
g2d.draw(poly);
    drawHandles(g);
  }
  
  
  public boolean contains(Point mousePt) {
    return (p.x <= mousePt.x && mousePt.x <= (p.x + side + 1)  &&  p.y <= mousePt.y && mousePt.y <= (p.y + side + 1));
  }
  
  }

解决方案 »

  1.   

    又把代码简化了点, 高手帮看!!import java.awt.*;
    public abstract class MovingShape {  public int marginWidth, marginHeight; // the margin of the animation panel area
      protected Point p;  // the top left coner of shapes
      protected int width, height; // the width and height of shapes
      protected Color fillColor;  // the fill colour of shapes
      protected MovingPath path; // the moving path of shapes
      protected Color borderColor;  // the border colour of shapes
      protected boolean selected = false;  // draw handles if selected
      protected int side;            //the side of the shape
      protected float penWidth;     // the pen width of a shape
      /** constuctor to create a shape with default values
       */
      public MovingShape() {
        this(0, 0, 20, 20, 20, 500, 500, Color.blue, Color.black, 1.0f, 0); // the default properties
      }
    public MovingShape(int x, int y, int s, int w, int h, int mw, int mh, Color fc, Color bc, float pw, int pathType) {
        p = new Point(x,y);
        marginWidth = mw;
        marginHeight = mh;
        fillColor = fc;
        borderColor = bc;
        width = w;
        height = h;
    side = s;
    penWidth = pw; 
        setPath (pathType);
      }
    public abstract boolean contains(Point p);public abstract void draw(Graphics g);---------------------------------------------------------------------------import java.awt.*;
    public class MovingRectangle extends MovingShape {  /** constuctor to create a rectangle with default values
       */
      public MovingRectangle() {
        super();
      }  /** constuctor to create a rectangle shape
       */
      public MovingRectangle(int x, int y, int s, int w, int h, int mw, int mh, Color fc, Color bc, float pw, int pathType) {
        super(x ,y, 0, w, h, mw, mh, fc, bc, pw, pathType);
      }  /** draw the rectangle with the fill colour
       *  If it is selected, draw the handles
       *  @param g the Graphics control
       */
      public void draw(Graphics g) {
        g.setColor(fillColor);
        g.fillRect(p.x, p.y, width, height);
        g.setColor(borderColor);
        drawOutLine(g);  //Change thickness of the border
    g.drawRect(p.x, p.y, width, height);
        drawHandles(g);
      }  /** Returns whether the point is in the rectangle or not
       * @return true if and only if the point is in the rectangle, false otherwise.
       */
      public boolean contains(Point mousePt) {
        return (p.x <= mousePt.x && mousePt.x <= (p.x + width + 1)  &&  p.y <= mousePt.y && mousePt.y <= (p.y + height + 1));
      }
    }-----------------------------------------------------------------------------
    import java.awt.*;
    public class MovingSquare extends MovingRectangle {
      
      /** constuctor to create an square with default values
       */
      public MovingSquare() {
       super();
      }  /** constuctor to create a square shape
       */
      public MovingSquare(int x, int y, int s, int mw, int mh, Color fc, Color bc, float pw, int pathType) {
        super(x ,y, s, s, s, mw, mh, fc, bc, pw, pathType);
      }
      
      }
    ------------------------------------------------------------------import java.awt.*;
    public class MovingParallelogram extends MovingRectangle {
      private int offset;
      //private double dSide = double(side);
      /** constuctor to create an rhombus with default values
       */
      public MovingParallelogram() {
       super();
      }
      
      /** constuctor to create a rhombus shape
       */
      public MovingParallelogram(int x, int y, int s, int mw, int mh, Color fc, Color bc, float pw, int pathType) {
        super(x ,y, s, s, s, mw, mh, fc, bc, pw, pathType);
        offset = (int)0.2 * s;
      }
      
       public void draw(Graphics g) {
        int[] xP = new int[]{p.x + offset, p.x + side + offset, p.x + side, p.x};
    int[] yP = new int[]{p.y, p.y, p.y + side, p.y + side};
    int nP = 4;
    Polygon poly = new Polygon(xP, yP, nP);
    Graphics2D g2d = (Graphics2D)g;
    g.setColor(fillColor);
        g2d.fill(poly);
        g.setColor(borderColor);
        drawOutLine(g);  //Change thickness of the border
    g2d.draw(poly);
        drawHandles(g);
      }
      
      
      public boolean contains(Point mousePt) {
        return (p.x <= mousePt.x && mousePt.x <= (p.x + side + 1)  &&  p.y <= mousePt.y && mousePt.y <= (p.y + side + 1));
      }
      
      }