Solved

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

Posted on 2010-11-30
11
485 Views
Last Modified: 2012-05-10
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
0
Comment
Question by:littlegiraffe
  • 4
  • 3
  • 2
  • +2
11 Comments
 
LVL 26

Accepted Solution

by:
ksivananth earned 125 total points
ID: 34237690
Graphics --> Graphics2D --> SunGraphics2D

the implementation is passed on runtime by the system ToolKit!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 34237692
>> 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
0
 
LVL 2

Assisted Solution

by:rnevet
rnevet earned 125 total points
ID: 34238144
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.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 34238213
>>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
0
 

Author Comment

by:littlegiraffe
ID: 34242983


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.



0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 92

Assisted Solution

by:objects
objects earned 125 total points
ID: 34243142
The implementation is in the subclass SunGraphics2D (as pointed out by ksivananth)
Heres some some source to show you:

 public class SunGraphics2D extends Graphics2D

...

       public void drawArc(int x, int y, int w, int h,
                           int startAngl, int arcAngl) {
           try {
               drawpipe.drawArc(this, x, y, w, h, startAngl, arcAngl);
           } catch (InvalidPipeException e) {
              revalidateAll();
               try {
                   drawpipe.drawArc(this, x, y, w, h, startAngl, arcAngl);
                } catch (InvalidPipeException e2) {
                    // Still catching the exception; we are not yet ready to
                    // validate the surfaceData correctly.  Fail for now and
                    // try again next time around.
               }
           } finally {
               surfaceData.markDirty();
           }
       }


When the paint system calls paint() (and subsequently paintComponent()) its passes an instance of SunGraphics2D.

> This suggests that method paintComponent is called 3 times - I don't understand why this is either.

It will get called many times. Basically whenever any part of the component needs (re)painting the paint system will call paint()

http://java.sun.com/products/jfc/tsc/articles/painting/
0
 
LVL 2

Expert Comment

by:rnevet
ID: 34243237
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

0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 125 total points
ID: 34244021
>> drawpipe.drawArc(this, x, y, w, h, startAngl, arcAngl);

The actual implementation is there. These 'dark areas' are implemented in native code and that's where it really happens. If you can't find the source of what's actually taking place, then that brings up all sorts of other questions about how 'open' Java is
0
 

Author Closing Comment

by:littlegiraffe
ID: 34256163
I think there is more to be learn here, but thank you all  for your assistance.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 34256208
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
0
 
LVL 2

Expert Comment

by:rnevet
ID: 34256871
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.)
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

744 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now