这是一项更大的家庭作业。 如何使用 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";
}
}
最佳答案
您误解了 drawline(...)
方法中的参数。
drawLine(int x1, int y1, int x2, int y2)
从点(x1, y1) 到点(x2, y2) 绘制一条线。不存在从一个点画一条具有高度和宽度的线这样的事情。
关于java - 如何使用 getX() 和 getY() 画一条线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5307531/