(原)super.paintComponent解析
来源:互联网 发布:东村明子 知乎 编辑:程序博客网 时间:2024/04/30 16:42
在学习java的画图功能时候,经常会看到super.paintComponent,书上说一定要有而且必须是在第一句,不然就会出问题!可恨的是书上没有解析这是为什么。下面简单的解析一下这个语句的作用。
首先我们要知道GUI组件(如JPanel,JButton等)本身并没有paintComponent的方法,它的paintComponent是JComponent继承下来的。
然后我们来看看JComponent的paintComponent
protected void paintComponent(Graphics g) {
if (ui != null) {
Graphics scratchGraphics = SwingGraphics.createSwingGraphics(g);
try {
ui.update(scratchGraphics, this);
}
finally {
scratchGraphics.dispose();
}
}
}
createSwingGraphics的有什么用呢? 只是用于创建一个新的画图对象而与
public static Graphics createSwingGraphics(Graphics g) {
if (g == null) {
Thread.dumpStack();
return null;
}
return g.create();
}
那update呢? 明显是用于画出组件的背景色
public void update(Graphics g, JComponent c) {
if (c.isOpaque()) {
g.setColor(c.getBackground());
g.fillRect(0, 0, c.getWidth(),c.getHeight());
}
paint(g, c);
}
最后就用dispose把画图的对象占用的资源释放掉!
综合以上,我们发现paintComponenet的作用是给组件画上背景色。如果不调用此方法,我们之前对组件设置的背景色等属性将不会被展现。如果此方法在子类的实现中最先被调用,背景就处于最底下的一层,子类其他利用g进行的绘图将在有一个背景的基础下进行。
然后我们来看看JComponent的paintComponent
protected void paintComponent(Graphics g) {
if (ui != null) {
Graphics scratchGraphics = SwingGraphics.createSwingGraphics(g);
try {
ui.update(scratchGraphics, this);
}
finally {
scratchGraphics.dispose();
}
}
}
createSwingGraphics的有什么用呢? 只是用于创建一个新的画图对象而与
public static Graphics createSwingGraphics(Graphics g) {
if (g == null) {
Thread.dumpStack();
return null;
}
return g.create();
}
那update呢? 明显是用于画出组件的背景色
public void update(Graphics g, JComponent c) {
if (c.isOpaque()) {
g.setColor(c.getBackground());
g.fillRect(0, 0, c.getWidth(),c.getHeight());
}
paint(g, c);
}
最后就用dispose把画图的对象占用的资源释放掉!
综合以上,我们发现paintComponenet的作用是给组件画上背景色。如果不调用此方法,我们之前对组件设置的背景色等属性将不会被展现。如果此方法在子类的实现中最先被调用,背景就处于最底下的一层,子类其他利用g进行的绘图将在有一个背景的基础下进行。
0 0
- (原)super.paintComponent解析
- void和void指针解析(原)
- swing 中paint与paintComponent的区别(jcomponent)
- 关于paintComponent的小细节(Graphics 类)
- 【原】javascript prototype 解析
- (原)TouchXML解析
- ConcurrentSkipListMap原码解析
- ConcurrentLinkedQueue原码解析
- RecyclerViewHelper原码解析
- Gilde原码解析
- 【原】discuz! 7.2 超详细代码解析(1)
- 【原】discuz! 7.2 超详细代码解析(2)
- 【原】discuz! 7.2 超详细代码解析(3)
- [原]极域电子教室V6 2007通信协议解析(一)
- Android解析JSON(原生态 Gson FastJson)
- python的super函数解析
- super关键字及内存解析
- 关于paint(),repaint(),paintcomponent()
- 将应用部署到jboss,java.lang.OutOfMemoryError: PermGen space解决方法
- (转)livecd 修复Grub
- (转)乌班图12.04超级用户登录
- Eddy's picture(并查集+最小生成树)
- (原)双核四线程
- (原)super.paintComponent解析
- sed行编辑器使用介绍
- (原)GridBagLayout
- (转)去掉QTreeWidget中节点之间的虚线
- hibernate使用@where实现条件过滤功能
- (转)C++延时效果
- (原)关于指针的一些个人想法
- 寻找字符串A中完全包含字符串B的最短子字符串
- (转)关于C++引用