Java在applet中显示图像

时间:2014-05-14 10:49:43

标签: java image applet awt

我创建了一个口袋妖怪猜测应用程序,它在左侧显示一个神奇宝贝轮廓的图像,在右侧显示一个黑色矩形。如果用户无法正确猜出口袋妖怪,他/她可以按下显示口袋妖怪图片和名称的按钮。 首次启动applet时,它看起来像这样:http://i.imgur.com/2IO7kb3.png

当用户第一次点击“显示”时,它看起来像这样:http://i.imgur.com/xbTJzrF.png

当再次按下“随机口袋妖怪”时,它看起来像第二张图片,左边是口袋妖怪剪影,右边是透露图片。

我需要它,以便当用户再次按下“随机口袋妖怪”按钮时,它会再次显示黑色矩形。

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.applet.*;
import java.util.Random;

public class giffs extends Applet implements ActionListener
{
    boolean b = true;       
    AudioClip music0;
    Random r = new Random(); // random number generator
    int min = 1;
    int max = 70;
    int randomNumber = r.nextInt(max - min + 1) + min;      
    Button randompoke; // button to display a random pokemon
    Button reveal;  // reveals pokemon name
    Image poke; // shaded out pokemon picture
    Image poker; // revealed pokemon picture
    Image pokeball;

    public void init()
    {
        setSize(700,700);
        music0 = getAudioClip(getDocumentBase(), "music2.au");
        randompoke = new Button("Random Pokemon");
        reveal = new Button("Reveal");
        add(randompoke);
        add(reveal);
        randompoke.addActionListener(this);
        reveal.addActionListener(this);         
        poke = getImage(getDocumentBase(), "poke" + randomNumber + ".PNG");
        poker = getImage(getDocumentBase(), "poke" + randomNumber +"r"+ ".PNG");
        pokeball = getImage(getDocumentBase(), "pokeball.gif");
        music0.play();
    }

    public void update(Graphics g)
    {       
        g.drawImage(poke,20,20,this);
        g.drawImage(pokeball,450,20,this);
        g.fillRect(650,20,450,640);         
        if (b == false)
        {
            g.drawImage(poker,650,20,this);
        }
    }    

    public void actionPerformed(ActionEvent evt)
    {
        if(evt.getSource()== randompoke)
            {
                Random r = new Random();
                int randomNumber = r.nextInt(max - min + 1) + min;                  
                poke = getImage(getDocumentBase(), "poke" + randomNumber + ".PNG");
                poker = getImage(getDocumentBase(), "poke" + randomNumber +"r"+ ".PNG");                    
                repaint();

            }
        else if(evt.getSource() == reveal)
        {
            b = false;
            repaint();              
        }
    }
}

1 个答案:

答案 0 :(得分:1)

只需添加一个drawrectangle到

if(evt.getSource()== randompoke) 

在actionperfomred内部。