所以我有一個大學的作業。這個概念是我們要完成一些類層次結構的東西。基本上這是讓我們畫出不同形狀的東西。繪圖問題 - 從其他形狀繪製複合形狀
我可以成功繪製每個形狀;我需要它的地方,以及我需要多大的地方,就像需要的一樣......我遇到麻煩的部分是這個複合層次結構。
基本上我們應該有一個叫做Compound.java
和新類的,我們應該有其他三類,House
,tree
和earth
延伸;其中每一個都應該採用我們創建的形狀對象(矩形,方形,線條,橢圓形和圓形)並繪製所需的圖片,如類名所示。
我遇到問題的地方在於家庭課堂;例如:我可以繪製一個矩形,但是當我嘗試繪製第二個矩形之後,它基本上忘記了第一個矩形,並且只繪製了第二個矩形!
我們還沒有任何與圖形的東西練習,所以我不知道任何方法或任何我可以調用繪製然後繼續在House
構造函數。
我明白爲什麼它會覆蓋第一個矩形,當House
構造函數被調用,它通過在構造函數中所有的東西運行,然後返回到Compound.java
和使用draw(Graphics g)
方法繪製它....
但我不知道如何解決它!任何幫助,將不勝感激......明天將到期。
這裏的所有代碼:
Shape.java:
import java.awt.*;
public abstract class Shape {
int initX, initY;
Color fillColour;
public Shape() {
initX = 0;
initY = 0;
}
public Shape(int x, int y) {
initX = x;
initY = y;
}
public void setInitX (int x) {
initX = x;
}
public void setInitY (int y) {
initY = y;
}
public abstract void draw(Graphics g);
public abstract double Area();
public abstract double Perimeter();
public void Move(int deltaX, int deltaY){
//future work
}
}
ClosedShape.java:
import java.awt.Graphics;
public abstract class ClosedShape extends Shape {
boolean polygon;
int numPoints;
int[] xVertices;
int[] yVertices;
int x,y,width, height;
public ClosedShape(boolean isPolygon, int numPoints) {
super(0,0);
this.polygon = isPolygon;
this.numPoints = numPoints;
}
public ClosedShape(boolean isPolygon, int numPoints, int[] x, int[] y) {
super(x[0],y[0]);
this.polygon = isPolygon;
if (isPolygon) {
this.numPoints = numPoints;
xVertices = new int[numPoints]; // error check? if x.length == numPoints
for (int i = 0; i < x.length; i++) { // make copy of array: why?
xVertices[i] = x[i];
}
yVertices = new int[numPoints]; // error check? if y.length == numPoints
for (int i = 0; i < y.length; i++) { // make copy of array
yVertices[i] = y[i];
}
}
else { // its an oval - define bounding box
this.numPoints = 4;
this.x = x[0];
this.y = y[0];
width = x[1];
height = y[1];
}
}
public void setXYCoords(int[] x, int[] y){
this.xVertices = x;
this.yVertices = y;
}
// Gives access to the width attribute
public void setWidth(int width){
this.width = width;
}
// Gives access to the height attribute
public void setHeight(int height) {
this.height = height;
}
public void draw(Graphics g) {
if (polygon) {
g.drawPolygon(xVertices, yVertices, numPoints);
}
else {
g.drawOval(x, y, width, height);
}
}
public abstract double Area();
public abstract double Perimeter();
}
Rectangle.java:
public class Rectangle extends ClosedShape
{
public Rectangle(int x, int y, int width, int height)
{
super(true, 4);
setWidth(width);
setHeight(height);
int [] arrayX = new int[4];
arrayX[0] = x;
arrayX[1] = (x+width);
arrayX[2] = (x+width);
arrayX[3] = x;
int [] arrayY = new int[4];
arrayY[0] = y;
arrayY[1] = y;
arrayY[2] = y+height;
arrayY[3] = y+height;
setXYCoords(arrayX, arrayY);
}
public double Area()
{
return 0;
}
public double Perimeter()
{
return 0;
}
}
Compound.java:
import java.awt.*;
import java.awt.Graphics;
public class Compound
{
boolean polygon;
int[] xVertices;
int[] yVertices;
int initX, initY;
Color fillColour;
public void setXYCoords(int[] x, int[] y)
{
this.xVertices = x;
this.yVertices = y;
}
public void draw(Graphics g)
{
if (polygon) {
g.drawPolygon(xVertices, yVertices, 4);
}
else {
g.drawOval(1, 1, 1, 1);
}
}
}
House.java:
import java.awt.*;
import java.awt.Graphics;
public class House extends Compound
{
public House(int x, int y, int width, int height)
{
int [] arrayX = new int[4];
arrayX[0] = x;
arrayX[1] = (x+width);
arrayX[2] = (x+width);
arrayX[3] = x;
int [] arrayY = new int[4];
arrayY[0] = y;
arrayY[1] = y;
arrayY[2] = y+height;
arrayY[3] = y+height;
setXYCoords(arrayX, arrayY);
this.polygon = true;
Rectangle house = new Rectangle(x, y, width, height);
int [] arrayXTwo = new int[4];
arrayXTwo[0] = x+(width/4);
arrayXTwo[1] = x+(2*(width/4));
arrayXTwo[2] = x+(2*(width/4));
arrayXTwo[3] = x+(width/4);
int [] arrayYTwo = new int[4];
arrayYTwo[0] = y+(height/4);
arrayYTwo[1] = y+(height/4);
arrayYTwo[2] = y+height;
arrayYTwo[3] = y+height;
setXYCoords(arrayXTwo, arrayYTwo);
this.polygon = true;
Rectangle door = new Rectangle(x, y, width, height);
}
}
哇,文本牆! – 2013-03-04 03:44:25
@Danny Beckett哈哈耶對不起....我剛剛在這方面工作了很長時間,而且我變得非常煩躁,因爲我無法找出我做錯了什麼,並且似乎無法從任何地方獲得幫助...... – MicroMumbler 2013-03-04 03:48:17
我編輯了您的問題閱讀起來有點容易...也許你會得到更好的運氣! – 2013-03-04 03:53:55