Solved

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

Posted on 2001-06-17
4
269 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

630 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