[英]AWT: I am trying to learn Java and was unable to understand the following program
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请解释
It's an instance of Canvas
class.它是
Canvas
类的一个实例。 In Java new
is a keyword used to create an object (an instance of given class).在 Java 中
new
是用于创建对象(给定类的实例)的关键字。 That being said - new Canvas()
creates new instance of Canvas
class.话虽如此 -
new Canvas()
创建Canvas
类的新实例。 Brackets after class name indicate constructor - block of code that is being called when you wan to create an object of given type.类名后面的括号表示构造函数——当你想创建一个给定类型的对象时被调用的代码块。
You got oval because the paint
method was called .因为调用了
paint
方法,所以得到了椭圆形。 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".正如您在Oracle 发布的这篇教程文章中所读到的,
paint
方法将始终作为所谓的“回调机制”被触发。 This method belongs to Container
class.该方法属于
Container
类。 I suggest reading docs about it .我建议阅读有关它的文档。 The
paint
method in MyCanvas
class overrides the paint
method from Canvas
.该
paint
的方法MyCanvas
类覆盖了paint
的方法Canvas
。 When extending Canvas
class and overriding paint
method you should always call the super method at the beginning of your method.当扩展
Canvas
类并覆盖paint
方法时,您应该始终在方法的开头调用 super 方法。 You can read why in the links I have already included in this point.您可以在我已经包含在这一点的链接中了解原因。
As in point #1 - new CanvasExample()
creates new instance of MyCanvas
class.与第 1 点一样 -
new CanvasExample()
创建MyCanvas
类的新实例。 More specifically, it calls constructor ( public CanvasExample() { ... }
).更具体地说,它调用构造函数(
public CanvasExample() { ... }
)。 In code you presented, constructor of CanvasExample creates new object of type Frame
and calls some of it's methods.在您提供的代码中, CanvasExample 的构造函数创建了
Frame
类型的新对象并调用了其中的一些方法。 One of these methods is add
and it was inherited by class Frame
from its the superclass - Container
.其中一种方法是
add
,它是由Frame
类从其超类Container
继承的。
As Java awt API explains, the add
method:正如Java awt API 所解释的,
add
方法:
appends the specified component to the end of this container.
将指定的组件附加到此容器的末尾。
I hope you will understand :D我希望你会明白:D
at first it call public static void main(String args[])
首先它调用
public static void main(String args[])
then main
make new instance of class CanvasExample
and call its constructor CanvasExample()
然后
main
创建类CanvasExample
新实例并调用其构造函数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"该构造函数创建名为“f”的类
Frame
新实例并调用类Frame
构造函数,它将该框架的标题设置为“Canvas Example”
in next row of constructor CanvasExample()
it make and add new instance of class MyCanvas
to instance of Frame
named "f"在构造函数
CanvasExample()
下一行中,它创建并将类MyCanvas
新实例添加到名为“f”的Frame
实例中
third row of constructor CanvasExample()
it just set Layout of "f" to null
构造函数
CanvasExample()
第三行它只是将“f”的布局设置为null
fourth row of constructor CanvasExample()
it set size of "f" to 400x400构造函数
CanvasExample()
第四行将“f”的大小设置为 400x400
fifth row of constructor CanvasExample()
it just show "f" to screen (so you can see it)构造函数
CanvasExample()
第五行它只是在屏幕上显示“f”(所以你可以看到它)
and end of constructor CanvasExample()
it get back to main
并在构造函数
CanvasExample()
结束时返回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创建类
MyCanvas
新实例时称为它的构造函数,该构造函数将其背景设置为“Color.GRAY”,并将其大小设置为MyCanvas()
和所有MyCanvas()
构造函数
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每次需要再次渲染“f”时,它都会调用
MyCanvas
类实例的方法paint(Graphics g)
,该实例小于“f”,因此您可以看到后面的白色
that white is background of "f"那个白色是“f”的背景
I think thats all what you ask for.我想这就是你所要求的。
have a nice day祝你今天过得愉快
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.