paint 方法接收到从 Graphics 类创建的对象作为参数后会做什么?
例如这段代码:
public class unaClase extends Applet{
public void paint(Graphics g){
g.drawRect(0, 0, 400, 200);
}
}
尝试追踪源代码以查看其代码,从而能够理解它的作用,但它没有向我显示任何内容
我想知道是什么用它收到的参数进行绘制?
PD:我知道苹果已经死了,我只是想了解清楚正在发生什么,它做了什么
最佳答案
when I use the paint method (as in the example code), I'm technically overwriting it (unless I had used super.paint ...), and then its code would be the one I just defined; so my question is, how does paint know what to do with the parameter (Graphics) that I'm going through?
实际术语是覆盖
。通常您要做的第一件事就是调用父版本的 super.paint() 。 Graphics(或具有附加方法但需要强制转换的 Graphics2D)允许使用这些方法来绘制、旋转以及一般操作像素。它是使用操作系统支持的 native 方法调用在内部处理的。
如果您要绘画,您应该在 JPanel
中进行,并覆盖 paintComponent(Graphics g)
。查看绘画教程 https://docs.oracle.com/javase/tutorial/index.html
关于java - 油漆方法是什么?它是如何工作的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56712846/