[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 514
  • Last Modified:

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
0
littlegiraffe
Asked:
littlegiraffe
  • 4
  • 3
  • 2
  • +2
4 Solutions
 
ksivananthCommented:
Graphics --> Graphics2D --> SunGraphics2D

the implementation is passed on runtime by the system ToolKit!
0
 
CEHJCommented:
>> 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
 
rnevetCommented:
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
CEHJCommented:
>>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
 
littlegiraffeAuthor Commented:


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
 
objectsCommented:
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
 
rnevetCommented:
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
 
CEHJCommented:
>> 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
 
littlegiraffeAuthor Commented:
I think there is more to be learn here, but thank you all  for your assistance.
0
 
CEHJCommented:
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
 
rnevetCommented:
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

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 4
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now