开发者

How to draw a line using getX() and getY()?

This is for a much larger homework assignment. How do I draw a line using getX() and getY()? Here is my code, please help.

package shapes;

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

public class Line extends Rectangle {
    /**
    * Constructor.  开发者_开发技巧Just passes the params to the Rectangle constructor.
    */
    public Line(int x, int y, int w, int h, Color lineColor, Color fillColor, boolean fill) {
        super(x, y, w, h, lineColor, fillColor, fill);
    }

    /*
    * Override Rectangle draw(Graphics g) method.
    */
    public void draw(Graphics g) {
        // Be nice. Save the state of the object before changing it.
        Color oldColor = g.getColor();

        g.setColor(getLineColor());
        g.drawLine(getX(), getY(), getWidth(), getHeight());
        // Set the state back when done.
        g.setColor(oldColor);
    }

    /**
    * Returns a String that represents this object.
    */
    public String toString() {
        //return "Line: \n\tx = " + getX() + "\n\ty = " + getY() + "\n\tw = " + getWidth() + "\n\th = " + getHeight();
        return "Line";
    }
}


You misunderstand the arguments in your drawline(...) method.

drawLine(int x1, int y1, int x2, int y2) 

draws a line from point(x1, y1) to point(x2, y2). There is no such thing as drawing a line from one point with a height and width.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜