10/01/31 21:36:24
>>821
import java.awt.Graphics;
import javax.swing.*;
public class Parabola extends JComponent {
@Override public void paintComponent(Graphics g) {
int w = getWidth(), h = getHeight();
for (int x = 0; x < w; x++) g.drawLine(x, y(x, w, h), x+1, y(x+1, w, h));
}
private int y(int x, int w, int h) { // y = - (4h/w^2)(x - w/2)^2 + h
return (int)(h - 4.0 * h / w / w * (x - w / 2.0) * (x - w / 2.0));
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new Parabola());
f.setSize(500, 500);
f.setVisible(true);
}
});
}
}