以下这个小程序的目的是:点击一下鼠标,换一幅图片.
但是没有达到预期的效果呀.请高手帮助给改一下呀
import   java.applet.*;
import   java.awt.*;
import   java.awt.event.*;
public class gun   extends   Applet   implements   MouseListener{
Image   image,shot1,shot2;
Graphics     draw;
boolean  b;
int  appletwidth,appletheight;
public   void  init(){
setBackground(Color.darkGray);
appletwidth=getSize().width;
appletheight=getSize().height;
addMouseListener(this);
shot1=getImage(getDocumentBase(),"image/java1.gif");
shot2=getImage(getDocumentBase(),"image/java2.gif");
image=createImage(appletwidth,appletheight);
draw=image.getGraphics();
setSize(700,700);
}
public  void  update(Graphics   g){paint(g);}
public  void   paint(Graphics  g){
g.drawImage(image,0,0,this);
  draw.drawImage(shot2,20,20,this);b=false;



}

public  void  mouseClicked(MouseEvent  e){
b=true;
if(b)  {  draw.clearRect(0,0,appletwidth,appletheight);
        draw.drawImage(shot2,20,20,this);b=false;
}if(!b){ draw.clearRect(0,0,appletwidth,appletheight);
   draw.drawImage(shot1,20,20,this);
   b=true;

repaint();
} public  void   mousePressed(MouseEvent  e){}
public  void   mouseReleased(MouseEvent  e){}
public  void  mouseEntered(MouseEvent  e){}
public  void  mouseExited(MouseEvent  e){}

}

解决方案 »

  1.   

    package com.test;import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.File;import javax.imageio.ImageIO;public class gun extends Applet implements MouseListener {
    Image image, shot1, shot2; Graphics draw; boolean b; int appletwidth, appletheight; public void init() {
    setBackground(Color.darkGray);
    appletwidth = getSize().width;
    appletheight = getSize().height;
    addMouseListener(this);shot1=getImage(getDocumentBase(), "image/java1.gif "); 
    shot2=getImage(getDocumentBase(), "image/java2.gif ");  image = createImage(appletwidth, appletheight);
    draw = image.getGraphics();
    setSize(700, 700);
    } public void update(Graphics g) {
    paint(g);
    } public void paint(Graphics g) {
    g.drawImage(image, 0, 0, this);
    // draw.drawImage(shot2, 20, 20, this);
    // b = false;
    } public void mouseClicked(MouseEvent e) {
    System.out.println(b);
    // b = true;
    if (b) {
    image = shot2;
    // draw.clearRect(0, 0, appletwidth, appletheight);
    // draw.drawImage(shot2, 20, 20, this);
    b = false;
    } else if (!b) {
    image = shot1;
    // draw.clearRect(0, 0, appletwidth, appletheight);
    // draw.drawImage(shot1, 20, 20, this);
    b = true;
    }
    repaint();
    } public void mousePressed(MouseEvent e) {
    } public void mouseReleased(MouseEvent e) {
    } public void mouseEntered(MouseEvent e) {
    } public void mouseExited(MouseEvent e) {
    }}