java中画板上画一条线,怎么能实现画线清除掉原来的线 30
packagesecond;importjava.awt.*;importjava.applet.*;importjava.awt.event.*;publicclass...
package second;
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class b1_TestCanvas extends Applet {
CanvasDraw cd;
public void init(){
cd=new CanvasDraw(new Dimension(400,360),this);
add(cd);
cd.requestFocus();
}
}
class CanvasDraw extends Canvas{
Applet m_Parent;
boolean md_Flag;
int startX=0,startY=0,currentX=0,currentY=0;
StringBuffer sb=new StringBuffer();
CanvasDraw(Dimension d,Applet p){
m_Parent=p;
setSize(d);
setBackground(Color.gray);
addKeyListener(new KeyEventProcess());
addMouseListener(new MouseAdpt());
addMouseMotionListener(new MouseMotionAdpt());
}
void setStart(int x,int y){
startX=x;
startY=y;
}
void setCurrent(int x,int y){
currentX=x;
currentY=y;
}
void setMouseDragged(boolean b){
md_Flag=b;
}
void showMeg(String s){
m_Parent.showStatus(s);
}
void clearAll(){
startX=0;
startY=0;
currentX=0;
currentY=0;
repaint();
}
public void paint(Graphics g){
//super.paint(g);
g.drawString("("+currentX+","+currentY+")", 10, 20);
if(md_Flag)
g.drawLine(startX, startY, currentX, currentY);
}
class KeyEventProcess implements KeyListener{
public void keyTyped(KeyEvent e){
char ch =e.getKeyChar();
if(ch=='c'){
sb.setLength(0);
clearAll();
}
else
sb.append(ch);
showMeg("击键"+sb.toString());
}
public void keyPressed(KeyEvent e){}
public void keyReleased(KeyEvent e){}
}
class MouseAdpt extends MouseAdapter{
public void mousePressed(MouseEvent e){
setStart(e.getX(),e.getY());
showMeg("您开始画线");
}
public void mouseReleased(MouseEvent e){
if(startX!=currentX|startY!=currentY)
showMeg("您画了一条直线");
}
public void mouseEntered(MouseEvent e){
showMeg("鼠标进入画布");
}
public void mouseExited(MouseEvent e){
showMeg("鼠标离开画布");
}
}
class MouseMotionAdpt extends MouseMotionAdapter{
public void mouseMoved(MouseEvent e){
setCurrent(e.getX(),e.getY());
setMouseDragged(false);
//repaint(10,0,60,30);
}
public void mouseDragged(MouseEvent e){
setCurrent(e.getX(),e.getY());
setMouseDragged(true);
repaint();
}
}
} 展开
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class b1_TestCanvas extends Applet {
CanvasDraw cd;
public void init(){
cd=new CanvasDraw(new Dimension(400,360),this);
add(cd);
cd.requestFocus();
}
}
class CanvasDraw extends Canvas{
Applet m_Parent;
boolean md_Flag;
int startX=0,startY=0,currentX=0,currentY=0;
StringBuffer sb=new StringBuffer();
CanvasDraw(Dimension d,Applet p){
m_Parent=p;
setSize(d);
setBackground(Color.gray);
addKeyListener(new KeyEventProcess());
addMouseListener(new MouseAdpt());
addMouseMotionListener(new MouseMotionAdpt());
}
void setStart(int x,int y){
startX=x;
startY=y;
}
void setCurrent(int x,int y){
currentX=x;
currentY=y;
}
void setMouseDragged(boolean b){
md_Flag=b;
}
void showMeg(String s){
m_Parent.showStatus(s);
}
void clearAll(){
startX=0;
startY=0;
currentX=0;
currentY=0;
repaint();
}
public void paint(Graphics g){
//super.paint(g);
g.drawString("("+currentX+","+currentY+")", 10, 20);
if(md_Flag)
g.drawLine(startX, startY, currentX, currentY);
}
class KeyEventProcess implements KeyListener{
public void keyTyped(KeyEvent e){
char ch =e.getKeyChar();
if(ch=='c'){
sb.setLength(0);
clearAll();
}
else
sb.append(ch);
showMeg("击键"+sb.toString());
}
public void keyPressed(KeyEvent e){}
public void keyReleased(KeyEvent e){}
}
class MouseAdpt extends MouseAdapter{
public void mousePressed(MouseEvent e){
setStart(e.getX(),e.getY());
showMeg("您开始画线");
}
public void mouseReleased(MouseEvent e){
if(startX!=currentX|startY!=currentY)
showMeg("您画了一条直线");
}
public void mouseEntered(MouseEvent e){
showMeg("鼠标进入画布");
}
public void mouseExited(MouseEvent e){
showMeg("鼠标离开画布");
}
}
class MouseMotionAdpt extends MouseMotionAdapter{
public void mouseMoved(MouseEvent e){
setCurrent(e.getX(),e.getY());
setMouseDragged(false);
//repaint(10,0,60,30);
}
public void mouseDragged(MouseEvent e){
setCurrent(e.getX(),e.getY());
setMouseDragged(true);
repaint();
}
}
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询