当前位置: 技术问答>java相关
关于repaint()的一个问题???
来源: 互联网 发布时间:2015-04-19
本文导语: 我在imagepanel中用drawstring()方法显示一个字符串,但是当我鼠标移动后 字符串就消失了?是否应该调用repaint()?repaint()有什么用?谢谢!!! | drawstring() 应写在 paint()中 | 应...
我在imagepanel中用drawstring()方法显示一个字符串,但是当我鼠标移动后
字符串就消失了?是否应该调用repaint()?repaint()有什么用?谢谢!!!
字符串就消失了?是否应该调用repaint()?repaint()有什么用?谢谢!!!
|
drawstring() 应写在 paint()中
|
应该调用repait():
public void init(){
repaint();
}
public void init(){
repaint();
}
|
在paint方法中使用drawString然后不论何时更新需显示的内容都要调用repaint
不过你的情况是不用调用repaint的
不过你的情况是不用调用repaint的
|
如果是继承awt中的Panel,将drawString()写在paint中,重载update方法:
public void update(Graphics g) {
paint(g);
}
可防止闪烁。如果是JPanel就不必。
因为Panel中update方法是先擦后画,有闪烁。而JPanel中用双缓冲画。
public void update(Graphics g) {
paint(g);
}
可防止闪烁。如果是JPanel就不必。
因为Panel中update方法是先擦后画,有闪烁。而JPanel中用双缓冲画。
|
// Properties
/**
* Paints this component using the given graphics context.
* This is a standard Java AWT method which typically gets called
* by the AWT to handle painting this component. It paints this component
* using the given graphics context. The graphics context clipping region
* is set to the bounding rectangle of this component and its [0,0]
* coordinate is this component's top-left corner.
*
* @param g the graphics context used for painting
* @see java.awt.Component#repaint
* @see java.awt.Component#update
*/
public void paint(Graphics g)
{
Dimension dim = size();
if (image != null)
{
int imageWidth = image.getWidth(this);
int imageHeight = image.getHeight(this);
switch(imageStyle)
{
default:
case IMAGE_TILED:
{
//Calculate number of images that should be drawn horizontally
int numHImages = dim.width / imageWidth;
//Don't forget remainders
if (dim.width % imageWidth != 0)
numHImages++;
//Calculate number of images that should be drawn vertically
int numVImages = dim.height / imageHeight;
//Don't forget remainders
if (dim.height % imageHeight != 0)
numVImages++;
int h;
int v = 0;
for (int vCount = 0;vCount
/**
* Paints this component using the given graphics context.
* This is a standard Java AWT method which typically gets called
* by the AWT to handle painting this component. It paints this component
* using the given graphics context. The graphics context clipping region
* is set to the bounding rectangle of this component and its [0,0]
* coordinate is this component's top-left corner.
*
* @param g the graphics context used for painting
* @see java.awt.Component#repaint
* @see java.awt.Component#update
*/
public void paint(Graphics g)
{
Dimension dim = size();
if (image != null)
{
int imageWidth = image.getWidth(this);
int imageHeight = image.getHeight(this);
switch(imageStyle)
{
default:
case IMAGE_TILED:
{
//Calculate number of images that should be drawn horizontally
int numHImages = dim.width / imageWidth;
//Don't forget remainders
if (dim.width % imageWidth != 0)
numHImages++;
//Calculate number of images that should be drawn vertically
int numVImages = dim.height / imageHeight;
//Don't forget remainders
if (dim.height % imageHeight != 0)
numVImages++;
int h;
int v = 0;
for (int vCount = 0;vCount