当前位置: 技术问答>java相关
为什么执行repaint()后,不能清除原来所画的内容?
来源: 互联网 发布时间:2015-08-12
本文导语: source code: import java.awt.*; import java.awt.event.*; import java.applet.*; import javax.swing.*; /** * Title: hello * Description: hello world! * Copyright: Copyright (c) 2002 * Company: sound * @author lwq * @versio...
source code:
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
/**
* Title: hello
* Description: hello world!
* Copyright: Copyright (c) 2002
* Company: sound
* @author lwq
* @version 1.0
*/
public class Applet5 extends JApplet
{
boolean isStandalone = false;
int x,y;
/**Get a parameter value*/
public String getParameter(String key, String def)
{
return isStandalone ? System.getProperty(key, def) :
(getParameter(key) != null ? getParameter(key) : def);
}
/**Construct the applet*/
public Applet5()
{
}
/**Initialize the applet*/
public void init()
{
try
{
jbInit();
}
catch(Exception e)
{
e.printStackTrace();
}
}
/**Component initialization*/
private void jbInit() throws Exception
{
this.setSize(new Dimension(400,300));
this.addMouseMotionListener(new java.awt.event.MouseMotionAdapter()
{
public void mouseDragged(MouseEvent e)
{
this_mouseDragged(e);
}
});
}
/**Start the applet*/
public void start()
{
}
/**Stop the applet*/
public void stop()
{
}
/**Destroy the applet*/
public void destroy()
{
}
/**Get Applet information*/
public String getAppletInfo()
{
return "Applet Information";
}
/**Get parameter info*/
public String[][] getParameterInfo()
{
return null;
}
//static initializer for setting look & feel
static
{
try
{
//UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
//UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
}
catch(Exception e)
{
}
}
public void paint(Graphics g)
{
g.setColor(Color.blue);
g.setFont(new Font("宋体",Font.BOLD,20));
g.drawLine(x,y,x,y);
}
void this_mouseDragged(MouseEvent e)
{
x = (int)e.getX();
y = (int)e.getY();
repaint();
}
}
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
/**
* Title: hello
* Description: hello world!
* Copyright: Copyright (c) 2002
* Company: sound
* @author lwq
* @version 1.0
*/
public class Applet5 extends JApplet
{
boolean isStandalone = false;
int x,y;
/**Get a parameter value*/
public String getParameter(String key, String def)
{
return isStandalone ? System.getProperty(key, def) :
(getParameter(key) != null ? getParameter(key) : def);
}
/**Construct the applet*/
public Applet5()
{
}
/**Initialize the applet*/
public void init()
{
try
{
jbInit();
}
catch(Exception e)
{
e.printStackTrace();
}
}
/**Component initialization*/
private void jbInit() throws Exception
{
this.setSize(new Dimension(400,300));
this.addMouseMotionListener(new java.awt.event.MouseMotionAdapter()
{
public void mouseDragged(MouseEvent e)
{
this_mouseDragged(e);
}
});
}
/**Start the applet*/
public void start()
{
}
/**Stop the applet*/
public void stop()
{
}
/**Destroy the applet*/
public void destroy()
{
}
/**Get Applet information*/
public String getAppletInfo()
{
return "Applet Information";
}
/**Get parameter info*/
public String[][] getParameterInfo()
{
return null;
}
//static initializer for setting look & feel
static
{
try
{
//UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
//UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
}
catch(Exception e)
{
}
}
public void paint(Graphics g)
{
g.setColor(Color.blue);
g.setFont(new Font("宋体",Font.BOLD,20));
g.drawLine(x,y,x,y);
}
void this_mouseDragged(MouseEvent e)
{
x = (int)e.getX();
y = (int)e.getY();
repaint();
}
}
|
public void paint(Graphics g)
{
super.paint(g);
g.setColor(Color.blue);
g.setFont(new Font("宋体",Font.BOLD,20));
g.drawLine(x,y,x,y);
}
试一试
{
super.paint(g);
g.setColor(Color.blue);
g.setFont(new Font("宋体",Font.BOLD,20));
g.drawLine(x,y,x,y);
}
试一试