关键词: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() 一般默认即可。
如下面的程序我们写了一个类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;
}
}
}
}
文章地址:
http://javapub.iteye.com/blog/763849
分享到:
相关推荐
java 实现的纯paint流界面 实现了拖拽操作
3、 DrawPanel中覆盖paintComponent方法来进行绘制。 绘制15*15网格的棋盘, 绘制前先定义边距、行数、列数、网格宽度等常量 4、 定义Chess类,包括x,y索引, 颜色。 定义构造函数和相应的get方法。 5、 在DrawPanel...
自定义Button,利用paintComponent方法。
PintandoAndo:实用的paintComponent
这是一个基于Java Swing的推箱子游戏。游戏界面由一个名为PushBoxGame的类实现,其中包含了游戏的迷宫布局信息以及方块的移动...游戏的绘制通过重写paintComponent方法实现。运行程序后,即可显示出推箱子游戏的界面。
三种不同的食物在被毒液吃...在该类中通过方法paintComponent(Graphics g)来调用该类中的所有paint方法,进行绘图。 在该类中通过方法keyPressed(KeyEvent e)来调用该类中的所有Response方法,进行对键盘的监听和响应。
* 重写paintComponent方法 */ public class DAO { private static DAO dao=new DAO(); // 创建DAO对象 public DAO(){ try { Class.forName("com.mysql.jdbc.Driver"); // 加载数据库驱动类 } catch ...
对象的使用包括引用对象的成员变量和方法,通过运算符·可以实现对变量的访问和方法的调 用,变量和方法可以通过设定一定的访问权限(见下面的例子)来允许或禁止其它对象对它的 访问。 我们先定义一个类 Point。 例子...
PuzzleFrame类的主要成员的作用将在后面的详细设计中阐述,Puzzle Frame类主要成员变量(属性)和主要方法如表3-1、3-2所示。 "成员变量描述 "变量类型 "名称 " "魔板 "PuzzlePad "PuzzlePad " "开始游戏的按钮 ...
//继承的绘制组件方法 Graphics2D gg=(Graphics2D)g; if(imageIcon !=null){//绘制背景 gg.drawImage(imageIcon.getImage(),0,0,f.getWidth(),f.getHeight(),f); } } }; p.setLayout(null);...
* 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); } }
public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D) g; g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2d....
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 "宋体" Font BOLD 22 ; g setFont f ; g drawString "五子棋)" 10 30 ; f new Font "楷体 ...
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对绘制出的图形进行填充的一个例子,包括了图形绘制和图形填充两个模块功能的演示, 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(); ...
旋转图像文件,期间用到了滑动条jSlider事件处理技术,得到旋转角度(弧度制),图像缓冲区创建、创建bufImage的图形环境、让用于显示的缓冲区图像指向过滤后的图像、重载容器的paintComponent()方法等。
public void paintComponent(Graphics g) { g.drawImage(new ImageIcon("image\\logintop1.jpg").getImage(), 0, 0,500,48, null); super.paintComponent(g); } };//标题栏panel topPanel....