0

i started learning c++ but have switched to Java for the moment cos it seems a lot easier to get into.

Can someone please explain to me how this paintcomponent function runs without ever being called strictly in the main fuction???

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);
  }

}
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
  • A little searching will yield you similar questions. Please look at [this one](http://stackoverflow.com/questions/15544549/how-does-paintcomponent-work). – Hovercraft Full Of Eels Feb 02 '15 at 22:41
  • You created a new instance `points` of your class in main(), and then you gave that instance to a `JFrame` object. Since your code never calls points.paintComponent(g), then the only possible explanation is that the JFrame either called it, or somehow caused it to be called. – Solomon Slow Feb 02 '15 at 23:34

0 Answers0