Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2001-06-17
4
Medium Priority
?
271 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 210 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

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…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

670 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