• Post Reply Bookmark Topic Watch Topic
  • New Topic

abstract method

 
Michael Lin
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
//I am wondering after viewing the class Graphics's api, all the methods are abstract, and even the Graphics itself is abstract,
then how can we use those methods as nomorally, where are they get implemented??
an example is as follow, can anyone explain to me??
Thanks!

import java.applet.*;
import java.awt.*;
public class Arg extends Applet{
public void paint(Graphics g){
g.setColor(Color.red); //all abstract methods??
g.fillRect(0,0, 300, 200);
g.setColor(Color.yellow);
g.drawString("hi", 100, 50);
}


public static void main(String args[]) {

short c = 10;
int i = 20;
c += i;
System.out.println("not: " + c );

}

}
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
even the Graphics itself is abstract,
then how can we use those methods as nomorally, where are they get implemented??

After some digging, I found that these functions are implemented in the DebugGraphics class, which is a direct sub-class of Graphics class. Pl. read the first few lines of the DebugGraphics class.
Regds.
- satya

 
Randall Twede
Ranch Hand
Posts: 4519
6
Java Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I read that it is actually a Graphics2D object upcast to a Graphics reference. On investigating it looks like DebugGraphics class is for swing.
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Randall:
Thats partly true, DebugGraphics is for swing.
However, Graphics2D is also an abstract class
and so are all the methods.

Thats why I was thinking that its the DebugGraphics class which
implements the methods.
Any other clues....
regds.
- satya
 
Randall Twede
Ranch Hand
Posts: 4519
6
Java Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I hadnt noticed that. I have seen methods return "instances" of interfaces. They create a class that implements the interface and return a reference of the interface type. The same thing must be going on here.
 
bill bozeman
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm searching for this also. This has been something I was wondering also. I am looking at the source files and haven't gotten anywhere yet, but my first thought was that there is an inner class somewhere that extends Graphics that is providing all of the implementation of the abstract methods.
I don't think it is DebugGraphics becuase this is provided only in the swing package so what about classes before swing was around? Also Graphics2D is abstract and those methods are not implemented there either.
I thought I could get somewhere with the Component class since it has the method getGraphics which returns an instance of the graphichs class, but here is the code of that method:

So then I went to find out what parent is and here is what I got:

So, parent is a Container, therefore I was hoping I could find a getGraphics method in Container, but I can't so it would then be calling the getGraphics method of the superclass Component which leads me back here again.
I am going to try to do more research on this because while frustrating, it is also fun to see the source files, but if anyone has any comments, please provide them.
Thanks,
Bill
 
bill bozeman
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am going to move this to the AWT forum so we can get some more opinions on this. Besides, this isn't directly related to the SCJP.
Bill
 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys,
Think I found the answer in Barry Boone's Exam Guide

Subclasses of Graphics implement the specific instructions that allow the graphics context to draw onto on-screen compoenents or off-screen images. Most likely, you'll never see the Graphics subclasses, and you shouldn't care. They are platform specific and depend on the operating environment your application is running on.

He gives a little code snippet you can use to check which class is actually being implemented:

When you run it on Win98 you get:
sun.awt.windows.WGraphics
Neat, eh!
Jane
 
bill bozeman
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
THANK YOU JANE!!! I had been looking for that and couldn't figure it out. Makes sense that it is some OS specific class that is doing the drawing, but I thought I may be able to find something in the Component class.
Thanks again.
Bill
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!