我开始学习c ++但是现在已经切换到Java了,因为它似乎更容易进入。
有人可以向我解释一下这个paintcomponent函数是如何运行的,而不是在主函数中严格调用???
package javaapplication3;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class JavaApplication3 extends JPanel{
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.blue);
double y, x, z, q;
int amp = 30;
int amp2 = 50;
for(x = 0; x < 1000; x += .01){
q = amp * Math.sin(.05 * x);
z = amp2 * Math.sin(q + x);
g2d.draw(new Line2D.Double(x ,(z + 100) ,x ,(z + 100)));
}
}
public static void main(String[] args) {
JavaApplication3 points = new JavaApplication3();
JFrame frame = new JFrame("Sine");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(points);
frame.setSize(1000, 250);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}