Inheritance 在drawpanel上绘制多个形状,但我的超类不可见?(附代码)

Inheritance 在drawpanel上绘制多个形状,但我的超类不可见?(附代码),inheritance,panel,draw,shapes,super,Inheritance,Panel,Draw,Shapes,Super,所以我一直在尝试绘制多个形状,但我的超类似乎不可见。此外,我也有问题的宽度和高度,因为这是不常见的所有形状,我决定把他们只在直角和theOval。我并没有把宽度和高度放在同一条线上,因为这条线所画的都是点x1,y1,x2,y2。下面列出了我所有的课程。 一揽子计划2 import javax.swing.JFrame; public class testDraw { public static void main (String [] args) { final int WINDOW

所以我一直在尝试绘制多个形状,但我的超类似乎不可见。此外,我也有问题的宽度和高度,因为这是不常见的所有形状,我决定把他们只在直角和theOval。我并没有把宽度和高度放在同一条线上,因为这条线所画的都是点x1,y1,x2,y2。下面列出了我所有的课程。 一揽子计划2

import javax.swing.JFrame;

public class testDraw 
{
public static void main (String [] args)
{
    final int WINDOW_WIDTH = 300, WINDOW_HEIGHT = 300;
    panelDraw panel = new panelDraw ();
    JFrame application = new JFrame ();

    application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    application.add (panel);
    application.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
    application.setVisible(true);

}//end main

}//end class
import java.awt.Graphics;
import java.awt.Color;

public class theOval extends MyShape
{
private boolean flag;

public theOval()
{
    super();
    setFlag(true);
}

public theOval(int x1, int y1, int x2, int y2, Color color, boolean flag)
{
    setX1(x1);
    setY1(y1);
    setX2(x2);
    setY2(y2);
    setColor(color);
    setFlag(flag);
}

//sets&gets none needed
public void setFlag (boolean flag) 
{
    this.flag = flag;
}

public boolean getFlag () 
{
    return flag;
}

//draw the shape
public void draw (Graphics g)
{
    g.drawOval(getX1(), getY1(), getX2(), getY2());
    if (flag)
        g.fillOval(getX1(), getY1(), getX2(), getY2());
    else
        g.drawOval (getX1(), getY1(), getX2(), getY2());
}


}
import java.awt.Color;
import java.awt.Graphics;

public class theRectangle extends MyShape
{
// instance variables
private boolean flag;
private int width;

//methods, constructors first
public theRectangle () 
{
    super();
    setFlag (true);
}
public theRectangle (int x1, int y1, int x2, int y2, Color color, boolean flag) {
    setX1 (x1);
    setY1 (y1);
    setX2 (x2);
    setY2 (y2);
    setColor (color);
    setFlag (flag);
}

// sets
public void setFlag (boolean flag) 
{
    this.flag = flag;
}

//gets
public boolean getFlag () 
{
    return flag;
}

// draw the shape
public void draw (Graphics g) {

    if (flag)
        g.fillRect(getX1(), getX2(), x2, y2);
    else
        g.drawRect (x1, y1, x2, y2);
}// end method draw

}// end class MyRectangle
导入java.awt.Graphics; 导入java.awt.Color

public class MyShape 
{
//instance variables
private int x1, y1, x2, y2;
private Color color;

//no argument constructor
public MyShape()
{
    setX1 (x1);
    setY1 (y1);
    setX2 (x2);
    setY2 (y2);
    setColor(color);
}

//sets&gets
public void setX1(int x1)
{
    this.x1 = x1;
}

public void setX2(int x2)
{
    this.x2 = x2;
}

public void setY1(int y1)
{
    this.y1 = y1;
}

public void setY2(int y2)
{
    this.y2 = y2;
}

public void setColor(Color color)
{
    this.color = color;
}


//gets
public int getX1()
{
    return x1;
}

public int getX2()
{
    return x2;
}

public int getY1()
{
    return y1;
}

public int getY2()
{
    return y2;
}

public Color getColor(Color color)
{
    return color;
}

public void draw (Graphics g) 
{
    g.setColor (color);
}
}//end class
-我的画板课

package Project2;
import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;
import javax.swing.JPanel;

public class panelDraw extends JPanel 
{

//instance variables
private static final long serialVersionUID = 1L;
private Random randomNumbers = new Random ();
private theLine lines [];
private theRectangle rects [];
private theOval ovals [];
private int count;

//sets
public void setCount(int count) 
{
    this.count = count;
}
public void setOvalCount(int count)
{
    this.count = count;
}
public void setRectangleCount(int count)
{
    this.count = count;
}
public void setLineCount(int count)
{
    this.count = count;
}
//gets
public int getOvalCount()
{
    return count;
}
public int getLineCount()
{
    return count;
}
public int getRectangleCount()
{
    return count;
}
// the constructor
public panelDraw () 
{
    //declare local variables, using constant values where sensible
    final int NUMBER_COLORS = 256;  // (0-255 color values)
    final int WINDOW_WIDTH = 300, WINDOW_HEIGHT = 300;
    int x1, y1, x2, y2;
    Color color;
    boolean flag;

    //set background color  and other sets  
    setBackground (Color.WHITE);
    setCount(count);

    //allocate the array for MyLine objects  (5 to 9 references)
    lines = new theLine [5 + randomNumbers.nextInt (5)];
    rects = new theRectangle [5 + randomNumbers.nextInt (5)];
    ovals = new theOval [5 + randomNumbers.nextInt (5)];

    //create the theLine objects
    for (int count = 0; count < lines.length; count++) {

        //generate random coordinates (0 to 299)
        x1 = randomNumbers.nextInt (WINDOW_WIDTH);
        y1 = randomNumbers.nextInt (WINDOW_HEIGHT);
        x2 = randomNumbers.nextInt (WINDOW_WIDTH);
        y2 = randomNumbers.nextInt (WINDOW_HEIGHT);

        //display some output
        System.out.printf ("x1=%d, y1 = %d\n", x1, y1);

        //generate a random color
        color = new Color (randomNumbers.nextInt (NUMBER_COLORS),
                                    randomNumbers.nextInt (NUMBER_COLORS),
                                        randomNumbers.nextInt (NUMBER_COLORS));

        //construct a theLine object
        lines [count] = new theLine (x1, y1, x2, y2, color);

        setLineCount(count);
    }//end for loop to create theLine objects

    // for loop to create theRectangles
    for (int count = 0; count < rects.length; count++) {
        // generate random coordinates (0 to 299)
        x1 = randomNumbers.nextInt (WINDOW_WIDTH);
        y1 = randomNumbers.nextInt (WINDOW_HEIGHT);
        x2 = randomNumbers.nextInt (WINDOW_WIDTH);
        y2 = randomNumbers.nextInt (WINDOW_HEIGHT);

        //display some output
        System.out.printf ("x1=%d, y1 = %d\n", x1, y1);

        //generate a random color
        color = new Color (randomNumbers.nextInt (NUMBER_COLORS),
                                    randomNumbers.nextInt (NUMBER_COLORS),
                                        randomNumbers.nextInt (NUMBER_COLORS));

        //generate a random flag
        flag = randomNumbers.nextBoolean ();

        //construct a theRectangle object
        rects [count] = new theRectangle (x1, y1, x2, y2, color, flag);

        setRectangleCount(count);
    }//end for loop to create theRectangle objects

            for (int count = 0; count < ovals.length; count++) {
                //generate random coordinates (0 to 299)
                x1 = randomNumbers.nextInt (WINDOW_WIDTH);
                y1 = randomNumbers.nextInt (WINDOW_HEIGHT);
                x2 = randomNumbers.nextInt (WINDOW_WIDTH);
                y2 = randomNumbers.nextInt (WINDOW_HEIGHT);

                //display some output
                System.out.printf ("x1=%d, y1 = %d\n", x1, y1);

                //generate a random color
                color = new Color (randomNumbers.nextInt (NUMBER_COLORS),
                                            randomNumbers.nextInt (NUMBER_COLORS),
                                                randomNumbers.nextInt (NUMBER_COLORS));

                //generate a random flag
                flag = randomNumbers.nextBoolean ();

                //construct a theOvals object
                ovals [count] = new theOval (x1, y1, x2, y2, color, flag);
            } // end for loop to create theOvals

}// end panelDraw constructor



//when time to paint the panel, draw the lines, rectangles, and ovals
public void paintComponent (Graphics g) {
    //paint the parent first
    super.paintComponent (g);

    //draw the lines, rectangles, and ovals using the enumerated for statement
    for (theLine line : lines) 
        line.draw (g);
    for (theRectangle rect : rects)
        rect.draw (g);
    for (theOval oval : ovals)
        oval.draw (g);

}//end method paintComponent

}//end class DrawPanel
-线路类

package Project2;

import java.awt.Graphics;
import java.awt.Color;

public class theLine extends MyShape
{
// declare instance variables


//methods, constructors
public theLine () 
{
    super();
}

public theLine (int x1, int x2, int y1, int y2, Color color)
{
    setX1 (x1);
    setY1 (y1);
    setX2 (x2);
    setY2 (y2);
    setColor(color);
}
// sets/gets - none needed, because they are inherited


// draw the shape
public void draw (Graphics g) 
{   
    g.drawLine (getX1(), getY1(), getX2(), getY2());
}  // end method draw

}  // end class MyLine
-Theove类 一揽子计划2

import javax.swing.JFrame;

public class testDraw 
{
public static void main (String [] args)
{
    final int WINDOW_WIDTH = 300, WINDOW_HEIGHT = 300;
    panelDraw panel = new panelDraw ();
    JFrame application = new JFrame ();

    application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    application.add (panel);
    application.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
    application.setVisible(true);

}//end main

}//end class
import java.awt.Graphics;
import java.awt.Color;

public class theOval extends MyShape
{
private boolean flag;

public theOval()
{
    super();
    setFlag(true);
}

public theOval(int x1, int y1, int x2, int y2, Color color, boolean flag)
{
    setX1(x1);
    setY1(y1);
    setX2(x2);
    setY2(y2);
    setColor(color);
    setFlag(flag);
}

//sets&gets none needed
public void setFlag (boolean flag) 
{
    this.flag = flag;
}

public boolean getFlag () 
{
    return flag;
}

//draw the shape
public void draw (Graphics g)
{
    g.drawOval(getX1(), getY1(), getX2(), getY2());
    if (flag)
        g.fillOval(getX1(), getY1(), getX2(), getY2());
    else
        g.drawOval (getX1(), getY1(), getX2(), getY2());
}


}
import java.awt.Color;
import java.awt.Graphics;

public class theRectangle extends MyShape
{
// instance variables
private boolean flag;
private int width;

//methods, constructors first
public theRectangle () 
{
    super();
    setFlag (true);
}
public theRectangle (int x1, int y1, int x2, int y2, Color color, boolean flag) {
    setX1 (x1);
    setY1 (y1);
    setX2 (x2);
    setY2 (y2);
    setColor (color);
    setFlag (flag);
}

// sets
public void setFlag (boolean flag) 
{
    this.flag = flag;
}

//gets
public boolean getFlag () 
{
    return flag;
}

// draw the shape
public void draw (Graphics g) {

    if (flag)
        g.fillRect(getX1(), getX2(), x2, y2);
    else
        g.drawRect (x1, y1, x2, y2);
}// end method draw

}// end class MyRectangle
-三角形类 一揽子计划2

import javax.swing.JFrame;

public class testDraw 
{
public static void main (String [] args)
{
    final int WINDOW_WIDTH = 300, WINDOW_HEIGHT = 300;
    panelDraw panel = new panelDraw ();
    JFrame application = new JFrame ();

    application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    application.add (panel);
    application.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
    application.setVisible(true);

}//end main

}//end class
import java.awt.Graphics;
import java.awt.Color;

public class theOval extends MyShape
{
private boolean flag;

public theOval()
{
    super();
    setFlag(true);
}

public theOval(int x1, int y1, int x2, int y2, Color color, boolean flag)
{
    setX1(x1);
    setY1(y1);
    setX2(x2);
    setY2(y2);
    setColor(color);
    setFlag(flag);
}

//sets&gets none needed
public void setFlag (boolean flag) 
{
    this.flag = flag;
}

public boolean getFlag () 
{
    return flag;
}

//draw the shape
public void draw (Graphics g)
{
    g.drawOval(getX1(), getY1(), getX2(), getY2());
    if (flag)
        g.fillOval(getX1(), getY1(), getX2(), getY2());
    else
        g.drawOval (getX1(), getY1(), getX2(), getY2());
}


}
import java.awt.Color;
import java.awt.Graphics;

public class theRectangle extends MyShape
{
// instance variables
private boolean flag;
private int width;

//methods, constructors first
public theRectangle () 
{
    super();
    setFlag (true);
}
public theRectangle (int x1, int y1, int x2, int y2, Color color, boolean flag) {
    setX1 (x1);
    setY1 (y1);
    setX2 (x2);
    setY2 (y2);
    setColor (color);
    setFlag (flag);
}

// sets
public void setFlag (boolean flag) 
{
    this.flag = flag;
}

//gets
public boolean getFlag () 
{
    return flag;
}

// draw the shape
public void draw (Graphics g) {

    if (flag)
        g.fillRect(getX1(), getX2(), x2, y2);
    else
        g.drawRect (x1, y1, x2, y2);
}// end method draw

}// end class MyRectangle

正如你所看到的,在g.fillRectgetX1,getX2,x2,y2,我相信我需要宽度为x2,高度为y2,但当我试图想象它时,我感到慌乱,我不太确定该怎么做,因为我的形状不可见,我也不知道是否要做一个集合,然后进入椭圆形和矩形,尽管我很确定我需要这样做,因为这些特性在超类中并不常见。

使用get作为公共的x和y

void draw (Graphics g) {

    if (flag)
        g.fillRect(getX1(), getX2(), getX2(), getY2());
    else
        g.drawRect (getX1(), getY1(), getX2(), getY2());
}// end method draw

}// end class MyRectangle