Results 1 to 10 of 10
Like Tree1Likes
  • 1 Post By pbrockway2

Thread: Does Graphics2D's drawImage abstract method have an implementation?

  1. #1
    shall is offline Senior Member
    Join Date
    Apr 2012
    Posts
    199
    Rep Power
    0

    Default Does Graphics2D's drawImage abstract method have an implementation?

    I found a program on the internet that uses the drawImage method from the Graphics2D class. I'm not sure how the program's use of drawImage works since All Graphics2D drawImage methods are abstract methods. I thought abstract methods don't have an implementation, so why does it work?

    Here's the web page that has the code: A Beginning Programmer's Guide to Java: Rotating an Image with Java

    Stephen

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Does Graphics2D's drawImage abstract method have an implementation?

    The Graphics2D object that you've received is an instance of a concrete class that extends from Graphics2D, and it still *is* a Graphics2D object. This is how inheritance works.

  3. #3
    shall is offline Senior Member
    Join Date
    Apr 2012
    Posts
    199
    Rep Power
    0

    Default Re: Does Graphics2D's drawImage abstract method have an implementation?

    I agree the program's paintComponent method created a Graphics2D object, but the Graphics2D's drawImage is still an abstract method. So, where does it get it implementation? Even the java.awt.Graphics's drawImage methods are abstract. Why would the Graphics (Java 2 Platform SE v1.4.2) specify something as abstract when it concrete?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Does Graphics2D's drawImage abstract method have an implementation?

    Quote Originally Posted by shall View Post
    I agree the program's paintComponent method created a Graphics2D object...
    Nope. paintComponent creates nothing, but instead it gets passed a Graphics2D object from the JVM.


    ... but the Graphics2D's drawImage is still an abstract method. So, where does it get it implementation? Even the java.awt.Graphics's drawImage methods are abstract. Why would the Graphics (Java 2 Platform SE v1.4.2) specify something as abstract when it concrete?
    The JVM creates the concrete class and the instance, and we don't really see or care how it does so (in daily use that is). All one can say is that the object passed into paintComponent is an instance of a class that extends Graphics2D. The details are not visible to you or me.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Does Graphics2D's drawImage abstract method have an implementation?

    For instance, say you have a class, AbstractFoo:

    Java Code:
    abstract class AbstractFoo {
       abstract void someMethod();
    }

    And say you have another class, UsesFoo, that uses an instance of AbstractFoo:

    Java Code:
    class UsesFoo {
       public void anotherMethod(AbstractFoo foo) {
          foo.someMethod();
       }
    }
    UsesFoo has no knowledge of who made the concrete instance of AbstractFoo, nor does it care. All it can do is use the instance. So behind the scenes there would have to be a concrete instance made, something like so:

    Java Code:
       public static void main(String[] args) {
          UsesFoo usesFoo = new UsesFoo();
          usesFoo.anotherMethod(new AbstractFoo() {
             
             @Override
             void someMethod() {
                System.out.println("in someMethod of a concrete AbstractFoo");
             }
          });
       }
    But UsesFoo doesn't see this, and again, nor does it care.

  6. #6
    shall is offline Senior Member
    Join Date
    Apr 2012
    Posts
    199
    Rep Power
    0

    Default Re: Does Graphics2D's drawImage abstract method have an implementation?

    I am just trying to end my confusing on why the class specification would specify a method as abstract when, in fact, it does have an implementation. Why does the class specification need to specify the method as abstract. It has a default implementation, so why not remove the abstract specifier. If anyone wants to extend the class, they can just override the drawImage method with their own implementation.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Does Graphics2D's drawImage abstract method have an implementation?

    Quote Originally Posted by shall View Post
    I am just trying to end my confusing on why the class specification would specify a method as abstract when, in fact, it does have an implementation. Why does the class specification need to specify the method as abstract. It has a default implementation, so why not remove the abstract specifier. If anyone wants to extend the class, they can just override the drawImage method with their own implementation.
    I think the confusion is that you think an instance has been created from the abstract class, and it hasn't. Deep inside of the JVM there is some code that creates a concrete class that extends Graphics2D, and in this code, it *must* implement all abstract methods of Graphics2D. So the object you see is from a concrete subclass of Graphics2D.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Does Graphics2D's drawImage abstract method have an implementation?

    I'm obviously not explaining it clearly enough. doWhile, Norm, please help me out here! :)

  9. #9
    shall is offline Senior Member
    Join Date
    Apr 2012
    Posts
    199
    Rep Power
    0

    Default Re: Does Graphics2D's drawImage abstract method have an implementation?

    I understood that the class was abstract since some of the methods are abstract, but I think it was the first time that I noticed that one of the java platform's implemented method was abstract. I had thought that abstract methods were only implemented by a subclass. But It doesn't looks like that's the case for the java platform classes. I guess the makers of the platform wanted to give me the ability to override the method without that super class having a default implementation.

  10. #10
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Does Graphics2D's drawImage abstract method have an implementation?

    I had thought that abstract methods were only implemented by a subclass.
    That's absolutely true: abstract methods have their implementation in all and only nonabstract subclasses.

    From 8.4.3.1. abstract Methods: "An abstract method declaration introduces the method as a member, providing its signature (8.4.2), return type, and throws clause (if any), but does not provide an implementation. The declaration of an abstract method m must appear directly within an abstract class (call it A) unless it occurs within an enum (8.9); otherwise a compile-time error occurs. Every subclass of A that is not abstract (8.1.1.1) must provide an implementation for m, or a compile-time error occurs". (my emphasis)

    If we take the JLS and the API documentation that says drawImage() is abstract at their word (and we must) then in code like the following g must have a runtime type that *does* provide an implementation of drawImage() and, therefore, cannot merely be Graphics (or Graphics2D) but rather some nonabstract subclass of these.

    Java Code:
    import java.awt.Graphics;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    
    import javax.imageio.ImageIO;
    import javax.swing.JComponent;
    import javax.swing.JFrame;
    import javax.swing.SwingUtilities;
    
        /** A component that paints an image in its upper left corner. */
    public class WhatsMyGraphics extends JComponent {
        
        private BufferedImage im;
        
        public WhatsMyGraphics() {
            try {
                im = ImageIO.read(new File("test.jpg"));
            } catch(IOException ioe) {
                ioe.printStackTrace();
            }
        }
        
        @Override
        public void paintComponent(Graphics g) {
            System.out.println(g);
            g.drawImage(im, 0, 0, null);
        }
        
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    JFrame frame = new JFrame("What's my graphics?");
                    frame.add(new WhatsMyGraphics());
                    frame.setSize(200, 200);
                    frame.setLocationRelativeTo(null);
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setVisible(true);
                }
            });
        }
    }
    The System.out.println() is included to get an idea to what manner of thing g refers.
    Last edited by pbrockway2; 04-08-2012 at 02:41 AM.
    Fubarable likes this.

Similar Threads

  1. Subclass and Abstract method implementation
    By konman795 in forum New To Java
    Replies: 2
    Last Post: 02-24-2012, 10:44 AM
  2. Replies: 1
    Last Post: 02-22-2012, 09:48 AM
  3. Abstract class, implementation, getName()?
    By manalinik in forum New To Java
    Replies: 2
    Last Post: 12-27-2011, 03:08 PM
  4. Does not recognize drawImage method...
    By Koba in forum New To Java
    Replies: 13
    Last Post: 11-21-2011, 05:06 AM
  5. Replies: 4
    Last Post: 02-19-2011, 05:32 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •