Solved

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

Posted on 2001-06-17
4
267 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
collection output issue 9 95
learn programming 8 95
listing all the respondents to a twitter feed - Java 5 49
Java import explained 4 49
In this post we will learn different types of Android Layout and some basics of an Android App.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

751 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