user3598181
user3598181

Reputation: 43

Java displaying images in applet

I created an Pokemon guessing app that displays an image of a Pokemon silhouette on the left and a black rectangle on the right. If the user is unable to guess the Pokemon correctly, he/she can press a button that displays the picture and name of the Pokemon. When the applet is first launched, it looks like this: https://i.sstatic.net/kk4Z1.png

When the user clicks 'reveal' for the first time it looks like this: https://i.sstatic.net/Gprom.png

And when "random Pokemon" is pressed again, it looks like the second picture, with the Pokemon silhouette on the left and the revealed picture on the right.

I need it so that when the user presses the "random Pokemon" button again, it displays the black rectangle again.

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();              
        }
    }
}

Upvotes: 0

Views: 418

Answers (1)

explodingcreeperssss
explodingcreeperssss

Reputation: 319

Just add a drawrectangle to

if(evt.getSource()== randompoke) 

inside actionperfomred.

Upvotes: 1

Related Questions