【问题标题】:Drawing Multiple Shapes on a drawpanel, but my Superclass is not visible? (Code attached)在绘图板上绘制多个形状,但我的超类不可见? (附代码)
【发布时间】:2015-11-04 20:01:59
【问题描述】:

所以我一直在尝试绘制多个形状,但似乎我的超类不可见。此外,我也遇到了宽度和高度的问题,因为这在所有形状中并不常见,我决定只将它们放在矩形和椭圆形中。我没有在我的类 theLine 中将宽度放在高度上(因为所有线条都是在点 (x1, y1, x2, y2) 上绘制)。下面列出了我所有的课程。 包项目2;

导入 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

-我的测试驱动程序 包项目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

-theLine 类

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

-Oval 类 包项目2;

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


}

-矩形类 包项目2;

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.fillRect(getX1(),getX2(), x2, y2) 处,我相信我需要 x2 处的 WIDTH 和 y2 处的 HEIGHT,但是当我尝试将其可视化时,我感到很慌张和我不太确定该怎么做,因为它 MyShape 不可见,而且我也很困惑是否要制作一组并进入椭圆形和矩形,尽管我很确定我需要这样做,因为这些特征不是t 与超类通用。

【问题讨论】:

    标签: inheritance panel draw shapes super


    【解决方案1】:

    对 x 和 y 使用 get() 作为 public

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-15
      • 2014-03-10
      相关资源
      最近更新 更多