这是我在java书中找到的练习。我无法解决它。使用抽象类绘制功能图
public abstract class AbstractDrawFunction extends JPanel {
/** Polygon to hold the points */
private Polygon p = new Polygon();
protected AbstractDrawFunction() {
drawFunction();
}
/** Return the y-coordinate */
abstract double f(double x);
/** Obtain points for x-coordinates 100, 101, ..., 300 */
public void drawFunction() {
for (int x = -100; x <= 100; x++) {
p.addPoint(x + 200, 200 - (int)f(x));
}
}
/** Implement paintComponent to draw axes, labels, and
* connecting points
*/
protected void paintComponent(Graphics g) {
// To be completed by you
}
}
Test the class with the following functions:
f(x) = x2;
f(x) = sin(x);
f(x) = cos(x);
f(x) = tan(x);
f(x) = cos(x) + 5sin(x);
f(x) = 5cos(x) + sin(x);
f(x) = log(x) + x2;
对于每个功能,创建扩展AbstractDrawFunction 类并实现在方法F的类。
你做了什么? – 2012-08-08 18:55:50
我无法在Jframe中添加Jpanel。 – 2012-08-08 19:00:07
S.L.Barth解释如何做到这一点.. – 2012-08-08 19:03:13