1
package donut;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.geom.Ellipse2D;

import javax.swing.JPanel;


public class Board extends JPanel{

    public void paint(Graphics g)
    {
      super.paint(g);

      Graphics2D g2 = (Graphics2D) g;

      RenderingHints rh =
            new RenderingHints(RenderingHints.KEY_ANTIALIASING,
                               RenderingHints.VALUE_ANTIALIAS_ON);

      rh.put(RenderingHints.KEY_RENDERING,
             RenderingHints.VALUE_RENDER_QUALITY);

      g2.setRenderingHints(rh);

      Dimension size = getSize();
      double w = size.getWidth();
      double h = size.getHeight();

      Ellipse2D e = new Ellipse2D.Double(0, 0, 80, 130);
      g2.setStroke(new BasicStroke(1));
      g2.setColor(Color.gray);


      for (double deg = 0; deg < 360; deg += 5) {
          AffineTransform at =
              AffineTransform.getTranslateInstance(w / 2, h / 2);
          at.rotate(Math.toRadians(deg));
          g2.draw(at.createTransformedShape(e));
        }
    }
}

Then The JFrame Extended Class Where Board Object is instantiated

package donut;

import javax.swing.JFrame;


public class Donut extends JFrame {


    public Donut() {

        add(new Board());

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(360, 310);
        setLocationRelativeTo(null);
        setTitle("Donut");
        setVisible(true);
    }

    public static void main(String[] args) {
        new Donut();
    }
}

I was expecting to See Line Like this : new Board().paint(graphicsObject) So Where Actually This Line is executed Or in a proper way Where actually paint(Graphics g) function is called ?!

Youans
  • 4,801
  • 1
  • 31
  • 57
  • 1
    paint is called automatically during the render of the GUI (graphic interface), why you need to know? having problem with it? – Gianmarco Oct 12 '12 at 14:32
  • I don't get It,paint() is called Automatically somewhere Ok I'm sure of that but where exactly after which line or code,I'm trying to imagine the code and that make me confused – Youans Oct 12 '12 at 14:37
  • 2
    you can't see what's really inside each method of JFrame class (that is extended by Donut) and is specifically inside "setVisible(true)" when you do this, the system calls the paint method. – Gianmarco Oct 12 '12 at 14:43
  • 1
    Use your debugger and place a breakpoint at the method you are interested in. Then investigate the stacktrace to see where the call originated ... – Robin Oct 12 '12 at 14:50
  • 2
    You should be overriding the paintComponent method in your Board class, rather than the paint method. – Gilbert Le Blanc Oct 12 '12 at 14:56

2 Answers2

5

If you want to follow the invocation stack to paint(), simply write new Throwable().printStackTrace() or Thread.dumpStack(), this will allow you to follow the calling-stack.

Otherwise, have a look at RepaintManager.addDirtyRegion(JComponent, int, int, int, int);

Anyway, when you are painting to the screen, you should never call paint/paintComponent/paintXXX methods. Only call repaint().

You cannot rely on "when" your method paintComponent(Graphics) or paint(Graphics) will be invoked.

Guillaume Polet
  • 47,259
  • 4
  • 83
  • 117
  • `RepaintManager.addDirtyRegion(JComponent, int, int, int, int);` equals [paintImmediately](http://docs.oracle.com/javase/6/docs/api/javax/swing/JComponent.html#paintImmediately%28int,%20int,%20int,%20int%29) (Java6 because I'm not compares APIs with java7) , but maybe I'm wrong – mKorbel Oct 12 '12 at 15:28
  • @mKorbel When you call addDirtyRegion, the RepaintManagers stores in a Map which component is dirty and which rectangle of the component needs to repainted. It then schedules an InvocationEvent on the EventQueue which eventually will cause the paint(Graphics) method to be called. In the end, if you use Swing, it will call paintImmediately which in turn will call paint(Graphics). – Guillaume Polet Oct 12 '12 at 15:40
  • could be right and correct, but I'm think that paintImmediately uses that internally, please leave that, leave that for whales :-), be sure I'm not good in Graphics(2D), even I asked a question about [RepaintManager](http://stackoverflow.com/questions/7787998/how-to-generate-exceptions-from-repaintmanager), thanks for your comment – mKorbel Oct 12 '12 at 15:45
  • 1
    +1 nice post. You might also want to take a look at [Paintin int AWT and Swing](http://www.oracle.com/technetwork/java/painting-140037.html) for an overview of how painting occurs (for the OP) – MadProgrammer Oct 12 '12 at 20:49
4

paint is automatically called by Swing whenever it is needed, for example when the component becomes visible (for the first time, or after a minimize of the window, etc), or when the window is resized, basically whenever the contents of a component need to be painted. You should never explicitly call paint in your code, if you wish to force paint then you should call repaint instead.

This link might be helpful

P.S. The code of paint method that is called by super.paint(g) resides in JComponent class, which is extended by JPanel (which is extended by your Board class).

Rempelos
  • 1,220
  • 10
  • 18