Link to home
Start Free TrialLog in
Avatar of littlegiraffe
littlegiraffe

asked on

java.awt.Graphics - Where are abstract methods implemented?

In Class java.awt.Graphics, methods such as drawRect are abstract and have no implementation. So where are the implementations?. The code listed below works, but I cannot see how.   It is from the book Java How to Program (Deitel).

public class DrawArcs
{
   // execute application
   public static void main( String args[] )
   {
      // create frame for ArcsJPanel
      JFrame frame = new JFrame( "Drawing Arcs" );
      frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

      ArcsJPanel arcsJPanel = new ArcsJPanel(); // create ArcsJPanel
      frame.add( arcsJPanel ); // add arcsJPanel to frame
      frame.setSize( 300, 210 ); // set frame size
      frame.setVisible( true ); // display frame
   } // end main
} // end class DrawArcs

// Fig. 12.24: ArcsJPanel.java
// Drawing arcs.
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;

public class ArcsJPanel extends JPanel
{
   // draw rectangles and arcs
   public void paintComponent( Graphics g )
   {
      super.paintComponent( g ); // call superclass's paintComponent

      // start at 0 and sweep 360 degrees
      g.setColor( Color.RED );
      g.drawRect( 15, 35, 80, 80 );
      g.setColor( Color.BLACK );
      g.drawArc( 15, 35, 80, 80, 0, 360 );

      // start at 0 and sweep 110 degrees
      g.setColor( Color.RED );
      g.drawRect( 100, 35, 80, 80 );
      g.setColor( Color.BLACK );
      g.drawArc( 100, 35, 80, 80, 0, 110 );

      // start at 0 and sweep -270 degrees
      g.setColor( Color.RED );
      g.drawRect( 185, 35, 80, 80 );
      g.setColor( Color.BLACK );
      g.drawArc( 185, 35, 80, 80, 0, -270 );

      // start at 0 and sweep 360 degrees
      g.fillArc( 15, 120, 80, 40, 0, 360 );

      // start at 270 and sweep -90 degrees
      g.fillArc( 100, 120, 80, 40, 270, -90 );
                 
      // start at 0 and sweep -270 degrees
      g.fillArc( 185, 120, 80, 40, 0, -270 );
   } // end method paintComponent
} // end class ArcsJPanel
ASKER CERTIFIED SOLUTION
Avatar of ksivananth
ksivananth
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of CEHJ
>> methods such as drawRect are abstract and have no implementation.

That's not the case. If it were, they'd be declared abstract. What *is* abstract is the class itself and that's because it's largely platform-specific and uses native code
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>>You can find out the specific class by calling g.getClass().getName() you'll get the name of the implementing class on your system.

That's true, but it won't necessarily tell you which class is *actually* doing the drawing
Avatar of littlegiraffe
littlegiraffe

ASKER



Hmmm I am not sure that I am getting the full answer here, but going in the right direction.  Thank you all for replying but I hope you will be able to clarify a few points for me.

ksivananth replied:

Graphics --> Graphics2D --> SunGraphics2D

the implementation is passed on runtime by the system ToolKit!

Could you please expand on this ksivananth, what exactly is passed to what?


CEHJ Replied

>> methods such as drawRect are abstract and have no implementation.

That's not the case. If it were, they'd be declared abstract. What *is* abstract is the class itself and that's because it's largely platform-specific and uses native code.

Yes I was wrong to say that method  drawRect   of class java.awt.Graphics is abstract, I actually meant to say method drawArc. I can see the code for it with Netbeans and it is declared abstract and you can also see this here:
http://download.oracle.com/javase/6/docs/api/java/awt/Graphics.html

So I would really like to know where drawArc is implemented and why it is abstract. I would also like to know why other methods such as drawRect are implemented in java.awt.Graphics

.



rnevet replied:


Every platform implements its own class that extends the Graphics class, and that's where the platform specific implementation of these abstract functions is. You are getting a reference to a Graphics class but actually the object is an extended class like ksivananth mentioned.

You can find out the specific class by calling g.getClass().getName() you'll get the name of the implementing class on your system
.  

I added the statement System.out.println(g.getClass().getName()); to class ArcsJPanel and it printed sun.java2d.SunGraphics2D, three times. This suggests that method paintComponent is called 3 times - I don't understand why this is either.  I would appreciate if someone could clarify this too.



SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I think you have all the info to answer you specific questions

some reading about abstract classes:
http://download.oracle.com/javase/tutorial/java/IandI/abstract.html

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I think there is more to be learn here, but thank you all  for your assistance.
So I would really like to know where drawArc is implemented and why it is abstract.

Where: http:#34244021
Why:
>>...  because it's largely platform-specific and uses native code

If you find the source for your platform, please let me know
take a look here:
http://www.docjar.com/docs/api/sun/java2d/SunGraphics2D.html

you can follow trough the classes that are involved in the process but in the end it will lead to native classes that are implemented in platform native code.

To get to the underline code you need to specify a specific OS and specific Java build that you want the code for. (I actually don't know what is the use for this.)