当前位置: 技术问答>java相关
paint和repaint函数的区别,一定给分
来源: 互联网 发布时间:2014-12-27
本文导语: 请教paint()与repaint()方法的区别,是否可以自己写一个方法代替paint()或repaint()?我在panel上画了一个图,但移动panel后,图就不见了,why?how to resolve? | Applet子类完全正常 JApplet的子类要在paint...
请教paint()与repaint()方法的区别,是否可以自己写一个方法代替paint()或repaint()?我在panel上画了一个图,但移动panel后,图就不见了,why?how to resolve?
|
Applet子类完全正常
JApplet的子类要在paint方法中先调用父类的paint
以下Applet运行正常
import java.awt.*;
import java.applet.*;
public class Applet1 extends Applet
{
public void init()
{
setLayout(null);
setSize(426,266);
button1.setLabel("draw");
add(button1);
button1.setBackground(java.awt.Color.lightGray);
button1.setBounds(252,48,112,47);
SymAction lSymAction = new SymAction();
button1.addActionListener(lSymAction);
}
java.awt.Button button1 = new java.awt.Button();
class SymAction implements java.awt.event.ActionListener
{
public void actionPerformed(java.awt.event.ActionEvent event)
{
Object object = event.getSource();
if (object == button1)
button1_ActionPerformed(event);
}
}
void button1_ActionPerformed(java.awt.event.ActionEvent event)
{
repaint();
}
public void paint(Graphics g){
g.drawRect(20, 20, 100, 100);
}
}
//
以下JApplet运行正常
import java.awt.*;
import javax.swing.*;
/**
* A basic extension of the javax.swing.JApplet class
*/
public class JApplet1 extends JApplet
{
public void init()
{
getContentPane().setLayout(null);
setSize(426,266);
JButton1.setText("draw");
getContentPane().add(JButton1);
JButton1.setBounds(300,108,71,46);
//}}
//{{REGISTER_LISTENERS
SymAction lSymAction = new SymAction();
JButton1.addActionListener(lSymAction);
//}}
}
//{{DECLARE_CONTROLS
javax.swing.JButton JButton1 = new javax.swing.JButton();
//}}
class SymAction implements java.awt.event.ActionListener
{
public void actionPerformed(java.awt.event.ActionEvent event)
{
Object object = event.getSource();
if (object == JButton1)
JButton1_actionPerformed(event);
}
}
void JButton1_actionPerformed(java.awt.event.ActionEvent event)
{
repaint();
}
public void paint(Graphics g){
super.paint(g);
g.drawRect(20, 20, 100, 100);
}
}
JApplet的子类要在paint方法中先调用父类的paint
以下Applet运行正常
import java.awt.*;
import java.applet.*;
public class Applet1 extends Applet
{
public void init()
{
setLayout(null);
setSize(426,266);
button1.setLabel("draw");
add(button1);
button1.setBackground(java.awt.Color.lightGray);
button1.setBounds(252,48,112,47);
SymAction lSymAction = new SymAction();
button1.addActionListener(lSymAction);
}
java.awt.Button button1 = new java.awt.Button();
class SymAction implements java.awt.event.ActionListener
{
public void actionPerformed(java.awt.event.ActionEvent event)
{
Object object = event.getSource();
if (object == button1)
button1_ActionPerformed(event);
}
}
void button1_ActionPerformed(java.awt.event.ActionEvent event)
{
repaint();
}
public void paint(Graphics g){
g.drawRect(20, 20, 100, 100);
}
}
//
以下JApplet运行正常
import java.awt.*;
import javax.swing.*;
/**
* A basic extension of the javax.swing.JApplet class
*/
public class JApplet1 extends JApplet
{
public void init()
{
getContentPane().setLayout(null);
setSize(426,266);
JButton1.setText("draw");
getContentPane().add(JButton1);
JButton1.setBounds(300,108,71,46);
//}}
//{{REGISTER_LISTENERS
SymAction lSymAction = new SymAction();
JButton1.addActionListener(lSymAction);
//}}
}
//{{DECLARE_CONTROLS
javax.swing.JButton JButton1 = new javax.swing.JButton();
//}}
class SymAction implements java.awt.event.ActionListener
{
public void actionPerformed(java.awt.event.ActionEvent event)
{
Object object = event.getSource();
if (object == JButton1)
JButton1_actionPerformed(event);
}
}
void JButton1_actionPerformed(java.awt.event.ActionEvent event)
{
repaint();
}
public void paint(Graphics g){
super.paint(g);
g.drawRect(20, 20, 100, 100);
}
}
|
repaint()方法会执行:update()方法,先刷新屏幕(以背景填充),在调用
paint()方法重画。
一般可以重载update()和paint()方法。
paint()方法重画。
一般可以重载update()和paint()方法。