关于protected void paintComponent(Graphics g),我想知道参数中的g是从那里来的。

来源:百度知道 编辑:UC知道 时间:2024/05/29 07:28:11
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
public class LoginPanel extends JPanel {
protected ImageIcon icon = new ImageIcon("res/login.jpg");
public int width = icon.getIconWidth(), height = icon.getIconHeight();
public LoginPanel() {
super();
setSize(width, height);
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Image img = icon.getImage();
g.drawImage(img, 0, 0,getParent());
}
}

我想知道(Graphics g)里面的g是从那里来的?会的请解答一下。

从容器中获得的啊 在哪个容器中画画 就从哪个容器中获得
跟在画图版里画画一样 你肯定是从画图板里获得画笔吧

Graphics g的g是系统new出来的,通过系统来传给相应的方法,并调用!!

this.getGraphics();