Link to home
Start Free TrialLog in
Avatar of ken021600
ken021600Flag for Australia

asked on

a simple question about "Graphics" and "setFont()"

hi experts,
I have a question to ask:
many times we can see the following method in a class which has some stuff to do with panel:

public void paintComponent(Graphics g)
{
   super.paintComponent(g);
   Font f = new Font("SansSerif", Font.BOLD, 10);
   g.setFont(f);
   ...
   ...
}

so,my questions are:

1)it looks like i don't have to explicitly pass a parameter,
which is of type "Graphics", why? eg, i can use "repaint()" somewhere in this class, and the panel will be repainted. and i even don't have to put it like this:

Graphics xyz = new Graphics();
repaint(xyz);

why is that?

2)I had a look at API and the method "setFont()" is an abstract
method. usually we re-define an abstract method in a class inheriting that abstract class, right? but as in the above example, it was just:

g.setFont(f);
rather than:
g.setFont(f)
{
   do some stuff;
}

and everything's going fine. anyone can explain it to me?

Thanks a lot for your help!
ASKER CERTIFIED SOLUTION
Avatar of Ovi
Ovi

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ken021600

ASKER

thanks a lot,but can you say something about my second question to me please?

ken
Avatar of Ovi
Ovi

I've allready said. When the component become visible, the paint(...) or paintComponent(...) methods are called, having as a parameter an Graphics object created and initialized by the system. This means that you don't have to implement all those methods, they are allready implemented by the system. The implementation of abstract methods is required only when you are subclassing an abstract class. In your example supose you want to create your own graphics class called MyGraphics which extends Graphics you must implement all the abstract methods from the Graphics.
thank you for your help.
ken