I am reading swing tutorial
. From what is mentioned there:
The paintComponent method of JComponent is automatically called
when a frame is drawn/re-drawn.
But, what I do not understand is what is the Graphics Object
that is passed to it. I do not see
any new object of Graphics
type being instanstiated
and passed. so how does all this happen?
public void paintComponent(Graphics g){
Graphics2D g2 = (Graphics2D)g;
g2.drawimage(image,x,y,null);
//
}
I guess it is a similar situation with actionlisteners
. But in this case, The actionPerformed
is called automatically when event
occurs such as button click
and the events gets passed to the actionPerformed
. There is no need to separately call this method and pass the Actionevent object
. But I do not understand how it happens with paintComponent
method.
button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event){
//do something
}
});
Thanks