`

paint和paintComponent方法的关系

    博客分类:
  • Java
 
阅读更多
关键词:swing,paint,paintComponent,paintBorder

paint :绘制容器。
paintComponents : 绘制此容器中的每个组件。

由此不难看出,二者就是房子与家具的关系。

但是该类中并不包含paintBorder方法,由此我想,该方法应该是位于扩展包中,很幸运,在javax.Swing包中的JComponent类中,找到了paint,paintComponent和paintBorder三个方法,我想这应该就是小朱宇要问的,查看API,有如下解释:

paint :由 Swing 调用,以绘制组件。此方法实际上将绘制工作委托给三个受保护的方法:paintComponent、paintBorder 和 paintChildren。按列出的顺序调用这些方法,以确保子组件出现在组件本身的顶部。子类可以始终重写此方法。只想特殊化 UI(外观)委托的 paint 方法的子类只需重写 paintComponent。

paintComponent :如果 UI 委托为非 null,则调用该 UI 委托的 paint 方法。向该委托传递 Graphics 对象的副本,以保护其余的 paint 代码免遭不可取消的更改

paintBorder :绘制组件的边框。
paintChildren :绘制此组件的子组件。

由此可以看出,在Swing 中,组件绘制 paint() 方法会依次调用 paintComponent(),paintBorder(),paintChildren() 三个方法。根据方法名就可以看出,paintComponent() 绘制组件本身,paintBorder() 绘制组件的边框,paintChildren() 绘制组件的子组件,所以Swing 编程时,如果继承 JComponent 或者其子类需要重绘的话,只要覆写 paintComponent() 而不是 paint(),方法 paintBorder(),paintChildren() 一般默认即可。

paint是全部东西都要重画的
paintComponent只是重画上面的容器
所以很明显的
后者速度比前者要快
一般情况下都是用后者的
除非对于JFrame,才会用到前者




如下面的程序我们写了一个类ZPanle继承自JPanel,我们只要重写protected void paintComponent(Graphics g) 就可以得到不同的显示效果。


    package com.zakisoft.frame02;  
      
    import java.awt.Graphics;  
    import java.awt.Image;  
      
    import javax.swing.Icon;  
    import javax.swing.ImageIcon;  
    import javax.swing.JPanel;  
      
    public class ZPanel extends JPanel {  
      
        private static final long serialVersionUID = 6702278957072713279L;  
        private Icon wallpaper;  
      
        public ZPanel() {  
            System.out.println("f:ZPanel()");  
        }  
      
        protected void paintComponent(Graphics g) {  
            if (null != wallpaper) {  
                processBackground(g);  
            }  
            System.out.println("f:paintComponent(Graphics g)");  
        }  
      
        public void setBackground(Icon wallpaper) {  
            this.wallpaper = wallpaper;  
            this.repaint();  
        }  
      
        private void processBackground(Graphics g) {  
            ImageIcon icon = (ImageIcon) wallpaper;  
            Image image = icon.getImage();  
            int cw = getWidth();  
            int ch = getHeight();  
            int iw = image.getWidth(this);  
            int ih = image.getHeight(this);  
            int x = 0;  
            int y = 0;  
            while (y <= ch) {  
                g.drawImage(image, x, y, this);  
                x += iw;  
                if (x >= cw) {  
                    x = 0;  
                    y += ih;  
                }  
            }  
        }  
    }  
分享到:
评论

相关推荐

    java paintComponent 拖拽操作

    java 实现的纯paint流界面 实现了拖拽操作

    Java开发的五子棋系统

    3、 DrawPanel中覆盖paintComponent方法来进行绘制。 绘制15*15网格的棋盘, 绘制前先定义边距、行数、列数、网格宽度等常量 4、 定义Chess类,包括x,y索引, 颜色。 定义构造函数和相应的get方法。 5、 在DrawPanel...

    自定义Button

    自定义Button,利用paintComponent方法。

    PintandoAndo:实用的paintComponent

    PintandoAndo:实用的paintComponent

    基于Java Swing的推箱子游戏

    这是一个基于Java Swing的推箱子游戏。游戏界面由一个名为PushBoxGame的类实现,其中包含了游戏的迷宫布局信息以及方块的移动...游戏的绘制通过重写paintComponent方法实现。运行程序后,即可显示出推箱子游戏的界面。

    JAVA实现贪吃蛇游戏毕业设计(源代码+论文)

    三种不同的食物在被毒液吃...在该类中通过方法paintComponent(Graphics g)来调用该类中的所有paint方法,进行绘图。 在该类中通过方法keyPressed(KeyEvent e)来调用该类中的所有Response方法,进行对键盘的监听和响应。

    通讯录管理系统 Java设计 源程序

    * 重写paintComponent方法 */ public class DAO { private static DAO dao=new DAO(); // 创建DAO对象 public DAO(){ try { Class.forName("com.mysql.jdbc.Driver"); // 加载数据库驱动类 } catch ...

    Java 语言基础 —— 非常符合中国人习惯的Java基础教程手册

    对象的使用包括引用对象的成员变量和方法,通过运算符·可以实现对变量的访问和方法的调 用,变量和方法可以通过设定一定的访问权限(见下面的例子)来允许或禁止其它对象对它的 访问。 我们先定义一个类 Point。 例子...

    魔板游戏Java课程设计报告.doc

    PuzzleFrame类的主要成员的作用将在后面的详细设计中阐述,Puzzle Frame类主要成员变量(属性)和主要方法如表3-1、3-2所示。 "成员变量描述 "变量类型 "名称 " "魔板 "PuzzlePad "PuzzlePad " "开始游戏的按钮 ...

    java中透明窗口 再也不用局限于矩形窗口了

    //继承的绘制组件方法 Graphics2D gg=(Graphics2D)g; if(imageIcon !=null){//绘制背景 gg.drawImage(imageIcon.getImage(),0,0,f.getWidth(),f.getHeight(),f); } } }; p.setLayout(null);...

    java项目-俄罗斯方块

    * paint方法实际上把绘图的主要工作委派给paintComponent方法等方法 */ public void paintComponent(Graphics g) { super.paintComponent(g); g.setColor(frontColor); for (int i = 0; i ; i++) { ...

    带图片背景的面板

    import javax.swing.*;... } public void paintComponent(Graphics g) { g.drawImage(myimage,0,0,myimage.getWidth(this),myimage.getHeight(this),Color.red,this); super.paintComponent(g); } }

    java开发爱心代码示例,开发代码的步骤解析

    public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D) g; g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2d....

    Java在窗口上加载显示GIF动画图像.rar

     public void paintComponent(Graphics g) { //重载组件绘制方法   super.paintComponent(g); //调用父类函数   images[currentImage].paintIcon(this,g,70,0); //绘制图标   currentImage=(currentImage 1)%...

    五子棋代码

    public void paintComponent Graphics g { super paintComponent g ; Font f new Font &quot;宋体&quot; Font BOLD 22 ; g setFont f ; g drawString &quot;五子棋)&quot; 10 30 ; f new Font &quot;楷体 ...

    draws 10 random filled shapes in random positions.

    Method paintComponent should contain a loop that iterates 10 times. In each iteration, the loop should determine whether to draw a rectangle or an oval, and choose coordinates and dimensions at ...

    Java图形填充实例.rar

    JAva对绘制出的图形进行填充的一个例子,包括了图形绘制和图形填充两个模块功能的演示,  super.paintComponent(g); //调用父类的绘制组件方法  Graphics2D g2D = (Graphics2D)g;  setBackground(Color.white)...

    俄罗斯方块

    // 生成新方块的方法 public void newblock() { blockType = (int) (Math.random() * 1000) % 7; turnState = (int) (Math.random() * 1000) % 4; x = 4; y = 0; if (gameover(x, y) == 1) { newmap(); ...

    Java图片旋转程序实例

    旋转图像文件,期间用到了滑动条jSlider事件处理技术,得到旋转角度(弧度制),图像缓冲区创建、创建bufImage的图形环境、让用于显示的缓冲区图像指向过滤后的图像、重载容器的paintComponent()方法等。

    仿QQ登录窗体,拉伸展开!

    public void paintComponent(Graphics g) { g.drawImage(new ImageIcon("image\\logintop1.jpg").getImage(), 0, 0,500,48, null); super.paintComponent(g); } };//标题栏panel topPanel....

Global site tag (gtag.js) - Google Analytics