我正在使用Eclipse Helios和JDK 1.5来运行处理JAVA图形的程序,目标是编写一个绘制函数图的抽象类。这个问题我很奇怪!
我在我的机器上运行它编译并输出所需的结果,但它仍然显示程序和整个项目中的错误(文件上的红色X),我尝试清理项目,重新编译并重新运行它再次,但它仍然在发生。我也尝试在另外两台机器上运行它,只有轴显示而不是其他曲线。我无法找到解释。关于什么是错的任何建议?
import java.awt.*;
import javax.swing.*;
public class Thirteen extends JFrame {
public Thirteen() {
setLayout(new GridLayout(2, 2));
add(new DrawParabola());
add(new DrawSineWave());
add(new DrawCosineWave());
add(new DrawTangent());
}
public static void main(String[] args) {
Thirteen frame = new Thirteen();
frame.setTitle("Exercise15_13");
frame.setSize(800, 400);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
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) {
super.paintComponent(g);
// Draw x axis
g.drawLine(30, 100, 370, 100);
g.drawLine(370, 100, 360, 105);
g.drawLine(370, 100, 360, 95);
g.drawString("X", 370, 80);
// Draw y axis
g.drawLine(200, 30, 200, 160);
g.drawLine(200, 30, 195, 40);
g.drawLine(200, 30, 205, 40);
g.drawString("Y", 220, 30);
}
}
class DrawParabola extends AbstractDrawFunction {
protected double f(double x) {
double scaleFactor = 0.1;
return scaleFactor * x * x;
}
}
class DrawSineWave extends AbstractDrawFunction {
protected double f(double x) {
return 50 * Math.sin((x / 100.0) * 2 * Math.PI);
}
}
class DrawCosineWave extends AbstractDrawFunction {
protected double f(double x) {
return 50 * Math.cos((x / 100.0) * 2 * Math.PI);
}
}
class DrawTangent extends AbstractDrawFunction {
protected double f(double x) {
return 50 * Math.tan((x / 100.0) * 2 * Math.PI);
}
}
class DrawCosineAnd5SineWave extends AbstractDrawFunction {
protected double f(double x) {
return (50 * Math.cos((x / 100.0) * 2 * Math.PI))
+ 5 * (50 * Math.sin((x / 100.0) * 2 * Math.PI));
}
}
class Draw5CosineAndSineWave extends AbstractDrawFunction {
protected double f(double x) {
return 5 * (50 * Math.cos((x / 100.0) * 2 * Math.PI))
+ (50 * Math.sin((x / 100.0) * 2 * Math.PI));
}
}
class DrawLogAndX2 extends AbstractDrawFunction {
protected double f(double x) {
double scaleFactor = 0.1;
return Math.log(x) + scaleFactor * x * x;
}
}