Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

JAVA 2D Question

  Asked By: Maddison    Date: Jan 17    Category: Java    Views: 1427

I have the code below. There is one thing I can't understand.If I execute this
code, the methode public void paint(...) is executed eventhough it is not called
in the main method. Can anyone tell me why?

import java.awt.*;

import java.awt.geom.*;

import javax.swing.*;

class First2DDemo extends JFrame {

public void paint( Graphics g ) {

Graphics2D g2 = (Graphics2D) g;

g2.setRenderingHint( RenderingHints.KEY_ANTIALIASING,


g2.draw( new Line2D.Double( 20, 30, 90, 70 ) );


public static void main( String args[] ) {

JFrame f = new First2DDemo();

f.setSize( 100, 100 );






3 Answers Found

Answer #1    Answered By: Jennie Harris     Answered On: Jan 17

Actually paint is method  which call whenever class
is called. So, when you call it through f.show();
It is definitely paint that will be displayed.
for overcome it, you have to do it in some other
manual function. /paint is the program function

Answer #2    Answered By: Melissa King     Answered On: Jan 17

Whenever the Frame gets created, resized, or moved, paint is
automatically called. The same is true for Applets.

Answer #3    Answered By: Clayton Richardson     Answered On: Jan 17

From Java-Api:(paint(...) method  description)
"This method is called when the contents of the component should be
painted in response to the component first being shown"

from the sentence we know that paint method will called first time when
the program add the component (JFrame also a component)

Didn't find what you were looking for? Find more on JAVA 2D Question Or get search suggestion and latest updates.