所以我一直试图绘制多个形状,但似乎我的超类不可见。另外,我也遇到宽度和高度问题,因为这对于所有形状都不常见,我决定只将它们放在theRectangle和theOval中。我的班级theLine没有放宽度(因为所有的线都是在点上绘制的(x1,y1,x2,y2))。我的所有课程都列在下面。 包Project2;
import java.awt.Graphics; import 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
-my绘制面板类
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
-my test driver 包Project2;
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 class
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
-theOval类 包Project2;
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());
}
}
-theRectangle Class 包Project2;
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不可见,我也很困惑是否要制作一套并进入椭圆形和矩形,虽然我很确定我需要,因为这些特性不是'与超类相同。
答案 0 :(得分:0)
对x和y使用get()作为公共
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