Java 绘图问题-从其他形状绘制复合形状

Java 绘图问题-从其他形状绘制复合形状,java,drawing,Java,Drawing,所以我有一个大学作业。概念是我们要完成一些类层次结构的东西。基本上,它是让我们画不同形状的东西 我能成功地画出每个形状;我需要它在哪里,我需要它有多大,就像要求的那样。。。我遇到的问题是这个复合层次结构 基本上,我们应该有一个名为component.java的新类,并从中扩展出另外三个类,House,tree,和earth;每一个都应该采用我们创建的形状对象(矩形、正方形、直线、椭圆形和圆形),并按照类名绘制所需的图片 我遇到的问题是家庭课;例如:我可以让它画一个矩形,但当我试图让它画第二个矩形

所以我有一个大学作业。概念是我们要完成一些类层次结构的东西。基本上,它是让我们画不同形状的东西

我能成功地画出每个形状;我需要它在哪里,我需要它有多大,就像要求的那样。。。我遇到的问题是这个复合层次结构

基本上,我们应该有一个名为
component.java
的新类,并从中扩展出另外三个类,
House
tree
,和
earth
;每一个都应该采用我们创建的形状对象(矩形、正方形、直线、椭圆形和圆形),并按照类名绘制所需的图片

我遇到的问题是家庭课;例如:我可以让它画一个矩形,但当我试图让它画第二个矩形后,它基本上忘记了第一个,只画第二个

我们没有任何关于图形的练习,所以我不知道任何方法或任何东西,我可以调用它们来绘制,然后在
House
构造函数中继续

我理解为什么它会覆盖第一个矩形,当调用
House
构造函数时,它会遍历构造函数中的所有内容,然后返回到
component.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
    }

}
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();








}
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;
    }

}
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);
        }

    }

}
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);

    }


}
ClosedShape.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
    }

}
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();








}
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;
    }

}
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);
        }

    }

}
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);

    }


}
component.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
    }

}
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();








}
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;
    }

}
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);
        }

    }

}
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);

    }


}
House.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
    }

}
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();








}
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;
    }

}
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);
        }

    }

}
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);

    }


}

从示例代码中,无法将任何形状“添加”到
复合类中。根据您的描述,
composite
应该是形状的“容器”。更接近于

public class Compound
{
    private List<Shape> shapes;

    public Compound() {
        shapes = new ArrayList<Shape>(25);
    }

    public void addShape(Shape shape) {
        shapes.add(shape);
    }

    public Iterable<Shape> getShapes() {
        return shape;
    }

    public void draw(Graphics g) {
        for (Shape shape : shapes) {
            shape.draw(g);
        }
    }
}
公共类化合物
{
私有列表形状;
公共大院(){
形状=新阵列列表(25);
}
公共空心形状(形状){
形状。添加(形状);
}
公共Iterable getShapes(){
返回形状;
}
公共空间绘制(图g){
用于(形状:形状){
形状绘制(g);
}
}
}

现在您需要决定,在哪里最好将
颜色
形状
关联,是否应该为形状本身定义它?这意味着您不能重复使用该形状。或者用
复合物
形状?

@Danny Beckett lol是的,对不起。。。。我只是在这方面工作了这么长时间,我真的很恼火,因为我无法找出我做错了什么,似乎无法从任何地方得到帮助…我编辑了你的问题,所以它更容易阅读。。。也许你会有更好的运气!
房屋
矩形
如何被
房屋
记住。首先,它们是在本地定义的,其次,
复合类似乎根本没有办法真正识别它们。我可以想象,
复合
类的目的不是构造一个
多边形
,而是维护一个
形状
的列表,它将遍历并
绘制
。现在我的问题是,你为什么要重新发明轮子<代码>形状
矩形
已经是JavaSE@MadProgrammer嗯,
房子
应该扩建
大院
。。。而
House
应该画两个
矩形
s,两个
正方形
s和一个
三角形
…我不太确定如何让它画出所需的形状,而不是我设置它的方式…如果我尝试调用
矩形(a,b,c,d)
房子里
我在左上角只看到一个点。。。正确的颜色,但无论我把a、b、c或d改成什么,它都只是一个点。。。我知道,这正是我疯狂的老师让我们做的。。。她曾经让我们做一个类来计算cos(x)。。。