Solved

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

Posted on 2001-06-17
4
263 Views
Last Modified: 2010-03-31
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!
0
Comment
Question by:ken021600
  • 2
  • 2
4 Comments
 
LVL 9

Accepted Solution

by:
Ovi earned 70 total points
ID: 6201125
1. Graphics xyz = new Graphics();
repaint(xyz);
 - is not a valid code, because you will not be able to compile'it. The Graphics class is an abstract one so it can never be instantiated. You don't have to pass explicitly the parameter, because the Graphics object is automatically created for the classes which extend Component (or JComponent).
2. The Graphics object created by the system is a real one this time, so the call of all methods in it will don't give you any exception.

To have access to this Graphics object on your entire source and not only in the paint(...) or paintComponent(...) methods you must do something like this :

public class xxx extends JPanel {
  private Graphics xxx_Graphics_Object;
............
  public void paintComponent(Graphics g) {
    xxx_Graphics_Object = g;
  }
.....................
}

Note that the Graphics object is created only when your component is "painted" in other words only when become visile on the screen. If you don't have explicitly overwrited the paintComponent method, you can do'it like this :

public class xxx extends JPanel {
  private Graphics xxx_Graphics_Object;
............
  public void getGraphicsObject() {
    xxx_Graphics_Object = this.getGraphics();
  }
.....................
}

But be sure that you are calling getGraphicsObject() only after your component has been made visible, otherwise you will get a null reference and when you will try to use'it you will get an NullPointerException
0
 

Author Comment

by:ken021600
ID: 6201706
thanks a lot,but can you say something about my second question to me please?

ken
0
 
LVL 9

Expert Comment

by:Ovi
ID: 6201720
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.
0
 

Author Comment

by:ken021600
ID: 6205369
thank you for your help.
ken
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
scoresSpecial  challenge 13 42
unix in java example 9 43
advertisement module in core php 4 94
Java JRE greater than 1.6 5 24
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now