2

创建一个具有 area() 方法的 Shape 接口,该方法将形状的面积作为双精度值返回。创建实现 Shape 接口的类 Circle 和 Rectangle。这些类具有反映其尺寸的属性(圆的半径,矩形的高度和宽度),这些属性由它们的构造函数设置。

使用 setColour(Color c)、setPosition(int x, int y) 和 draw(Graphics g) 方法创建一个 Drawable 接口。使用分别扩展 Circle 和 Rectangle 并实现 Drawable 接口的适当构造函数创建类 DrawableCircle 和 DrawableRectangle。

创建一个 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);
    }

}
4

1 回答 1

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();
}
于 2013-11-08T13:33:01.370 回答