创建一个Shape接口,该接口具有area()方法,该方法将形状区域返回为double。创建实现Shape接口的类Circle和Rectangle。这些类具有反映其构造函数设置的尺寸(圆的半径,矩形的高度和宽度)的属性。
使用setColour(Color c),setPosition(int x,int y)和draw(Graphics g)方法创建Drawable接口。使用适当的构造函数创建DrawableCircle和DrawableRectangle类,它们分别扩展Circle和Rectangle并实现Drawable接口。
创建一个ShapesDriver类,它扩展了Frame并在其中包含main和paint(Graphics g)方法。在ShapesDriver中创建一个Drawable类型的ArrayList。在此数组列表中放置一个DrawableCircle和一个DrawableRectangle,您可以在其中设置尺寸,位置和颜色。在paint方法中迭代ArrayList并为每个形状调用draw方法。
这是我必须去的地方,但我似乎无法让可绘制的课程工作?
包装形状;
public interface Shape
{
public double area();
}
包装形状;
public abstract class Circle implements Shape
{
private double radius;
Circle(double radius)
{
this.radius = radius;
}
public double area()
{
return radius;
}
}
包装形状;
public abstract class Rectangle implements Shape
{
public double height, width;
Rectangle(double Height, double Width)
{
this.height = Height;
this.width = Width;
}
public double area()
{
return width * height;
}
}
包装形状;
import java.awt.*;
public interface Drawable
{
public void setColour(Color c);
public void setPosition(double x, double y);
public void draw(Graphics g);
}
包装形状;
import java.awt.Color;
import java.awt.Graphics;
public class DrawableCircle extends Circle implements Drawable
{
public DrawableCircle(double radius)
{
super(radius);
}
@Override
public void setColour(Color c)
{
c = Color.ORANGE;
}
@Override
public void setPosition(int x, int y)
{
x = 100;
y = 150;
}
@Override
public void draw(Graphics g)
{
//g.drawOval(x, y, width, height);
}
}
包装形状;
import java.awt.Color;
import java.awt.Graphics;
public class DrawableRectangle extends Rectangle implements Drawable
{
private double x, y;
public DrawableRectangle(double height, double width)
{
super(height, width);
}
@Override
public void setColour(Color c)
{
this.setColour(c);
}
@Override
public void setPosition(double x, double y)
{
this.x = x;
this.y = y;
}
@Override
public void draw(Graphics g)
{
g.drawRect(x, y, width, height);
}
}
答案 0 :(得分:0)
这应该让你入门
public interface Shape {
public double area();
}
public class Circle implements Shape{
// constructors
// radius
// implement area();
}
public class Rectangle implements Shape {
// constructors
// height, width
// implement area();
}
public interface Drawable {
public void setColor(Color c);
public void setPoistion(int x, int y);
public void draw(Graphics g);
}
public class DrawableCirlce extends Circle implements Drawable {
// constructors
// implement setColor();
// implement setPosition();
// implement draw();
}
public class DrawableRectangle extends Rectangle implements Drawable {
// constructors
// implement setColor();
// implement setPosition();
// implement draw();
}