这是一个家庭作业问题。还涉及一些其他java文件。请注意,shapes.Rectangle和shapes.Oval包含一个getArea方法。我有一个令人抓狂的时间试图弄清楚任何帮助是值得赞赏的。
package model;
import java.awt.Color;
import java.awt.Container;
import shapes.Line;
import shapes.Oval;
import shapes.Rectangle;
import shapes.Shape;
import shapes.Triangle;
import interfaces.ComparableShape;
import interfaces.Resettable;
public class Model implements Resettable,ComparableShape {
private Container container;
private String message;
private String action = DRAW;
private boolean fill = false;
private String currentShapeType;
private Shape currentShape;
private Color fillColor = Color.gray;
public Color lineColor;
public final static String DRAW = "Draw";
public final static String MOVE = "Move";
public final static String REMOVE = "Remove";
public final static String RESIZE = "Resize";
public final static String FILL = "Fill";
public final static String CHANGE = "Change";
public final static String RECTANGLE = "Rectangle";
public final static String OVAL = "Oval";
public final static String LINE = "Line";
public final static String TRIANGLE = "Triangle";
public static String[] selections = {"Rectangle", "Oval", "Line", "Triangle"};
//project 9 begin
public Shape[] myShapes = new Shape[2];
//project 9 stop
public Shape createShape() {
if(currentShapeType == RECTANGLE){
currentShape = new Rectangle(0, 0, 0, 0, lineColor, fillColor, fill);
}
if(currentShapeType == OVAL) {
currentShape = new Oval(0,0,0,0, lineColor, fillColor, fill);
}
if(currentShapeType == LINE) {
currentShape = new Line(0,0,0,0, lineColor, fillColor, fill);
}
if(currentShapeType == TRIANGLE) {
currentShape = new Triangle(0,0,0,0, lineColor, fillColor, fill);
}
//project 9 start
if(myShapes[0] == null) {
myShapes[0]=currentShape;
}
else {
myShapes[1]=currentShape;
}
//project 9 stop
return currentShape;
}
public Shape getCurrentShape() {
return currentShape;
}
//project 9 begin
public Shape[] getMyShapearray() {
return myShapes;
}
//project 9 end
public String getCurrentShapeType(){
return currentShapeType;
}
public void setCurrentShapeType(String shapeType){
currentShapeType = shapeType;
}
public Model(Container container) {
this.container = container;
}
public void repaint() {
container.repaint();
}
public String getAction() {
return action;
}
public void setAction(String action) {
this.action = action;
}
public boolean isFill() {
return fill;
}
public void setFill(boolean fill) {
this.fill = fill;
}
public void setMessage(String msg) {
this.message = msg;
}
public String getMessage() {
return this.message;
}
public Color getLineColor() {
return this.lineColor;
}
public void setLineColor(Color c) {
this.lineColor = c;
}
public String toString() {
return "Model:\n\tAction: " + action + "\n\tFill: " + fill + "\n\tArea: " ;
}
public void resetComponents() {
action = DRAW;
currentShape = null;
myShapes = null;
if (container instanceof Resettable) {
((Resettable) container).resetComponents();
}
}
//Add a method to your model called compareShapes(),
//which will return either 0, 1, or 2--1 if the area of the first Shape is bigger than the second,
//2 if it is smaller, and 0 if the two Shapes are the same size.
//Create an interface named ComparableShape that will be used by the shape objects.
//The interface should require implementing classes to have a
//method getArea() capable of returning the area of the object. Obviously, only closed shapes can do this.
//The instanceof operator will be handy here.
public int getArea() {
return getWidth()*getHeight();
}
private int getHeight() {
///what goes here?!
return 0;
}
private int getWidth() {
//what goes here?!
return 0;
}
public int compareShapes(ComparableShape b) {
ComparableShape oneToCompare = null;
if (b instanceof ComparableShape) {
oneToCompare = (ComparableShape)b;
if (getArea() < oneToCompare.getArea()) return 2; // this one is smaller
if (getArea() > oneToCompare.getArea()) return 1; // this one is larger
return 0;
}
return 0;
}
}
答案 0 :(得分:0)
矩形区域的长度为*宽度。然而,它与其他形状不同。因此,对于矩形,您需要访问其长度和宽度。对于一个圆,你需要它的半径。圆的面积是piR ^ 2.
如下所述,对于椭圆:
椭圆的面积是pi * a * b,其中a和b是长轴和短轴的一半长度。圆的面积可以写成pi * r * r或pi * r ^ 2,因为a和b相等。
答案 1 :(得分:0)
您的getArea()
应该是这样的: -
如果currentShapeType
不是LINE
(因为某条线未关闭形状),则根据currentShape
计算并返回currentShapeType
的区域。