Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2010-11-30
11
Medium Priority
?
506 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
[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
  • 4
  • 3
  • 2
  • +2
11 Comments
 
LVL 26

Accepted Solution

by:
ksivananth earned 375 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 375 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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
 
LVL 92

Assisted Solution

by:objects
objects earned 375 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 375 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

609 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