#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    1
    Rep Power
    0

    Java logic and the paint method


    I'm a total beginner and although I can handle the logic of pseudo code and real language algorithms and want to progress to proper programming, I'm really struggling with the logic of classes and methods.

    I keep feeling I have an intuitive grasp of the way they work, but it keeps being foiled by specific examples.

    One such is the invocation of the paint method.

    As I understand it, this is a method which has already been defined inside an existing class (java.awt) and yet, whenever I see it, it appears as though it is being redefined.

    For instance, this is from an online tutorial:
    Code:
    import java.awt.Frame;
    import java.awt.Canvas;
    import java.awt.Graphics;
    import java.awt.Color;
    import java.awt.AWTEvent;
    import java.awt.event.WindowEvent;
    
    public class DrawFillOvals extends Frame {
        public static void main(String arg[]) {
            new DrawFillOvals();
        }
        DrawFillOvals() {
            enableEvents(AWTEvent.WINDOW_EVENT_MASK);
            DFOvals ovals = new DFOvals();
            add(ovals);
            pack();
            show();
        }
        public void processWindowEvent(WindowEvent event) {
            if(event.getID() == WindowEvent.WINDOW_CLOSING)
                System.exit(0);
        }
    }
    
    class DFOvals extends Canvas {
        DFOvals() {
            setSize(200,300);
            setBackground(Color.magenta);
        }
        public void paint(Graphics g) {
            g.setColor(Color.yellow);
            g.fillOval(20,0,160,300);
            g.setColor(Color.black);
            g.drawOval(20,0,160,300);
            g.setColor(Color.red);
            g.fillOval(20,40,160,220);
            g.setColor(Color.black);
            g.drawOval(20,40,160,220);
            g.setColor(Color.black);
            g.fillOval(20,100,160,100);
            g.setColor(Color.white);
            g.drawOval(20,100,160,100);
        }
    }
    It looks like public void paint is being defined as a method inside the DFOvals class. Why can't paint be invoked by making an awt object and writing something like myawtobject.paint...?

    Also (and I'm sure this is part of my same confusion) what exactly is g pointing to? As I understand it, a parameter is something which is passed to a method/class. I get that g is a Graphics object that is used by the paint method, but what/where is the object that is passed to that parameter?

    Apologies for these naive questions - I'm just trying to get the logic.

    Thanks for reading!
  2. #2
  3. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,021
    Rep Power
    1285
    paint is a method that is inherited by Components. awt is a package and not a class.

    The paint method lets you as the programmer decided what is meant by "painting" the component. The code you place in the paint method is what happens when the component is painted.

    The Graphics object is created for you and the paint method is called automatically when the component first becomes visible or you call repaint() on the component.

    (Note that paint is a method used by older classes, the heavyweight classes. In the classes in javax.swing, you typically override the paintComponent method.)
  4. #3
  5. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    Already said.

IMN logo majestic logo threadwatch logo seochat tools logo