开发者

How to make a triangle in java with getX() and getY()?

This is for a homework problem. I'm having a heck of a time trying to create xPoints and yPoints using getX() and getY() please help.

package shapes;

import java.awt.Color;
import java.awt.Graphics;

public class Triangle extends Rectangle {

    //private int[] xPoints = {50, 100, 150};
    //private int[] yPoints = {200, 100, 200};
    private int[] xPoints = {(getX()/2), getX(), (getX()+(getX()/2))};
    private int[] yPoints = {(getY()+getY()), getY(),(getY()+getY())};

    public Triangle(int x, int y, int w, int h, Color lineColor, Color fillColor, boolean fill) {
      super(x, y, w, h, lineColor, fillColor, fill);
    }  

    public void draw(Graphics g) {
        // Be nice. Save the state of the object before changing it.
        Color oldColor = g.getColor();
        if (isFill()) {
            g.setColor(getFillColor());
            g.fillPolygon(xPoints, yPoints, 3);
        }
        g.setColor(getLineColor());
        g.drawPolygon(xPoints, yPoints, 3);
        //g.drawOval(getX(), getY(), getWidth(), getHeight());
        // Set the state back when done.
        g.setColor(oldColor);
    }
    public int getArea() {
      //return area;
      return getWidth()*getHeight();
    }
    /**
    * Returns a String representing this object.
    */
    public String toString() {
        //return "Triangle: \n\tx = " + getX() + "\n\ty = " + getY() + 
               //"\n\tw = " + getWidth() + "\n\th = " + getHeight();
        return "Triangle";
    }
}

// HERE IS THE SHAPE.JAVA FILE...

package shapes;

import java.awt.Color;
import java.awt.Graphics;

public abstract class Shape {
    private int x, y;//,w,h;
    private Color lineC开发者_JAVA技巧olor;

    public Shape(int x, int y, Color lineColor) {
        this.x = x;
        this.y = y;
        this.lineColor = lineColor;
    }

    public abstract void draw(Graphics g);
    public abstract boolean containsLocation(int x, int y);


    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }


    public Color getLineColor() {
        return lineColor;
    }

    public void setLineColor(Color lineColor) {
        this.lineColor = lineColor;
    }
}


First off, let's think about what you're trying to do in the opening line.

public class Triangle extends Rectangle {

Last I checked, a Triangle wasn't a type of Rectangle. Perhaps you should modify this to something like

public class Triangle extends Shape {

Or something more appropriate. Look in the shapes class to find the exact one.

Now, let's consider what you're doing in the class.

private int[] xPoints = {(getX()/2), getX(), (getX()+(getX()/2))};
private int[] yPoints = {(getY()+getY()), getY(),(getY()+getY())};

Are you sure you need to define these here? I'll bet you the Shape class already has arrays for xPoints and yPoints. Look in there for inspiration.

Also, if Shape DOES have xPoints and yPoints, and they're trying to use them, they won't be able to! Because you're defining Triangle.xPoints, Shape.xPoints will get "shadowed"... meaning xPoints in Shape will point to it's own xPoints, not the one you defined, resulting in a very quick NullPointerException.

Now, with a Rectangle, it's easy to define it as X,Y + W,H. It looks like this:

X,Y       X+W,Y
+---------+
|         |
|         |
+---------+
X,Y+H    X+W,Y+H

Now how are you going to represent your Triangle like that? Looks like you need to put some thought into how to represent your Triangle. Maybe there's some information in the Shape class that can help you here too.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜