这是一项更大的家庭作业。 如何使用getX()和getY()绘制一条线? 这是我的代码,请帮忙。
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";
}
}
答案 0 :(得分:1)
您误解了drawline(...)
方法中的论据。
drawLine(int x1, int y1, int x2, int y2)
从点(x1,y1)到点(x2,y2)绘制一条线。没有从一个高度和宽度的点画一条线。