简体   繁体   中英

AWT: I am trying to learn Java and was unable to understand the following program

  1. What is "new MyCanvas()" in f.add(new MyCanvas());
  2. How did we get the oval even though the paint() method is not called either in CanvasExample class constructor or in the main() method
  3. What is new CanvasExample() in the main() method

import java.awt.*;

public class CanvasExample {  
    public CanvasExample() {  
        Frame f = new Frame("Canvas Example");  
        f.add(new MyCanvas());  
        f.setLayout(null);  
        f.setSize(400, 400);  
        f.setVisible(true);  
    }  

    public static void main(String args[]) {  
        new CanvasExample();
    }  
}  

class MyCanvas extends Canvas {  
    public MyCanvas() {  
        setBackground (Color.GRAY);  
        setSize(300, 200);  
    }

    public void paint(Graphics g){  
        g.setColor(Color.red);  
        g.fillOval(75, 75, 150, 75);  
    } 
}

Please explain

  1. It's an instance of Canvas class. In Java new is a keyword used to create an object (an instance of given class). That being said - new Canvas() creates new instance of Canvas class. Brackets after class name indicate constructor - block of code that is being called when you wan to create an object of given type.

  2. You got oval because the paint method was called . It just wasn't called explicitly from your code. As you can read in this tutorial article published by Oracle , the paint method will be always triggerred as so called "callback mechanism". This method belongs to Container class. I suggest reading docs about it . The paint method in MyCanvas class overrides the paint method from Canvas . When extending Canvas class and overriding paint method you should always call the super method at the beginning of your method. You can read why in the links I have already included in this point.

  3. As in point #1 - new CanvasExample() creates new instance of MyCanvas class. More specifically, it calls constructor ( public CanvasExample() { ... } ). In code you presented, constructor of CanvasExample creates new object of type Frame and calls some of it's methods. One of these methods is add and it was inherited by class Frame from its the superclass - Container .
    As Java awt API explains, the add method:

    appends the specified component to the end of this container.

I hope you will understand :D

at first it call public static void main(String args[])
then main make new instance of class CanvasExample and call its constructor CanvasExample()

that constructor make new instance of class Frame named "f" and call constructor of class Frame it set title of that frame to "Canvas Example"
in next row of constructor CanvasExample() it make and add new instance of class MyCanvas to instance of Frame named "f"
third row of constructor CanvasExample() it just set Layout of "f" to null
fourth row of constructor CanvasExample() it set size of "f" to 400x400
fifth row of constructor CanvasExample() it just show "f" to screen (so you can see it)
and end of constructor CanvasExample() it get back to main

when creating new instance of class MyCanvas is called constructor of it and that constructor set background of it to "Color.GRAY" and size of it to 300x200 and all of MyCanvas() constructor

everytime when "f" needs to be rendered again it call method paint(Graphics g) of instance of class MyCanvas bit that instance is smaller than "f" so you can see white behind
that white is background of "f"

I think thats all what you ask for.
have a nice day

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM