Kane Williamson
Kane Williamson

Reputation: 127

Java GUI: paintComponent method in the JComponent class

So, first I created a JFrame object, then a class that extends JComponent with an appropriate overridden version of the paintComponent method.

Finally, I add an object of this class to the JFrame object.

Here's the code for reference:

import java.awt.*;
import javax.swing.*;

public class MyFrame {

    public static void main(String[] args) {
        JFrame frame = new JFrame("My Frame");
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        frame.setSize(screenSize);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        MyComponent comp = new MyComponent();
        frame.add(comp);
    }

}

class MyComponent extends JComponent {

    public void paintComponent(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;
        Rectangle r = new Rectangle(0,0,100,100);
        g2.draw(r);
    }
}

My question is...the paintComponent method takes a Graphics2D object as it's argument, but who is passing this coz I'm not.

Upvotes: 2

Views: 185

Answers (2)

Radiodef
Radiodef

Reputation: 37875

My question is...the paintComponent method takes a Graphics2D object as it's argument, but who is passing this coz I'm not.

The paint system passes it in when a paint event is processed on the EDT.

In many ways, paintComponent is like other event handlers (actionPerformed, itemStateChanged, etc.) in that you don't call it explicitly. The system prepares the object passed to you (Graphics in this case) and calls it when it needs to.

Excerpts from Painting in AWT and Swing:

[If] the paint request originates on the first heavyweight ancestor (usually JFrame, JDialog, JWindow, or JApplet):

  • the event dispatching thread invokes paint() on that ancestor

  • The default implementation of Container.paint() recursively calls paint() on any lightweight descendents

[If] the paint request originates from a call to repaint() on an extension of javax.swing.JComponent:

  • JComponent.repaint() registers an asynchronous repaint request to the component's RepaintManager, which uses invokeLater() to queue a Runnable to later process the request on the event dispatching thread.

  • The runnable executes on the event dispatching thread and causes the component's RepaintManager to invoke paintImmediately() on the component, which [...] invokes paint() on the root component.

(And paintComponent is called by paint.)

Upvotes: 2

Sildoreth
Sildoreth

Reputation: 1935

The graphics object originates in Java's GUI framework. Any* GUI application you program in Java is built on this GUI framework. So, for instance, you don't have to program all the code that controls what a basic window looks like, that controls how a GUI program starts up, or that controls how user interactions via peripheral hardware (i.e., keyboard and mouse) are processed.

The Graphics object is just one of many classes that come with Java and that get instantiated and passed around for you in the GUI applications you create in order to make the program a full-blown GUI program.

[*] Technically, you could write a GUI program that doesn't use Java's pre-built GUI framework. But for the most part, no one does that.

Upvotes: 0

Related Questions