当前位置: 技术问答>java相关
高手救命:关于Applet图形的问题(附源代码),请帮忙看一下
来源: 互联网 发布时间:2015-10-30
本文导语: 背景有一些图形,然后鼠标移动时有会画一条线。可是这条线会把北京图形擦掉。怎么才能不让它擦掉呢? 不能用repaint,因为背景图形会很复杂。用repaint会使图形闪烁。 import java.applet.*; import java.awt.*; import java.aw...
背景有一些图形,然后鼠标移动时有会画一条线。可是这条线会把北京图形擦掉。怎么才能不让它擦掉呢?
不能用repaint,因为背景图形会很复杂。用repaint会使图形闪烁。
import java.applet.*;
import java.awt.*;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseEvent;
public class TestApplet extends Applet{
int x=0,y=0;
public void init(){
addMouseMotionListener(new MouseMotionAdapter(){
public void mouseMoved(MouseEvent evt){
Graphics g;
g = getGraphics();
g.setColor(Color.black );
g.drawLine(0,0,x,y);
g.setColor(Color.white);
g.drawLine(0,0,evt.getX(),evt.getY());
x=evt.getX();y=evt.getY();
}
}
);
}
public void paint(Graphics g){
setBackground( Color.black );
g.setColor(Color.white );
g.drawLine(50,50,300,500);
}
}
谢谢!!!
不能用repaint,因为背景图形会很复杂。用repaint会使图形闪烁。
import java.applet.*;
import java.awt.*;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseEvent;
public class TestApplet extends Applet{
int x=0,y=0;
public void init(){
addMouseMotionListener(new MouseMotionAdapter(){
public void mouseMoved(MouseEvent evt){
Graphics g;
g = getGraphics();
g.setColor(Color.black );
g.drawLine(0,0,x,y);
g.setColor(Color.white);
g.drawLine(0,0,evt.getX(),evt.getY());
x=evt.getX();y=evt.getY();
}
}
);
}
public void paint(Graphics g){
setBackground( Color.black );
g.setColor(Color.white );
g.drawLine(50,50,300,500);
}
}
谢谢!!!
|
1. you can use Graphics.setXORMode() to erase previous line
2. you can use double buffer drawing method to avoid flicker
2. you can use double buffer drawing method to avoid flicker