when the space key is pressed then the menu apears, but when you press the resume button (cont) then the menu should dissapear, but it doesn't. please help. Thanks

// The "ArrowKeys" class.
import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class ArrowKeys2 extends Applet implements KeyListener, Runnable, MouseListener, MouseMotionListener
{
    int x = 50, dx, y = 50, dy, a = 0, score, total, locx, locy, p = 0, mx, my, enx, eny;
    double rand, location;
    char m;
    boolean get = false;
    private Image dbImage;
    private Graphics dbg;
    Thread thread;
    Image img, tile, background, target, menubg, cont, menu, en;
    Graphics backg;

    // Place instance variables here

    public void init ()
    {
        rand = Math.random ();
        location = Math.random () * 1000;
        locx = (int) Math.round (location);
        location = Math.random () * 800;
        locy = (int) Math.round (location);
        img = getImage (getDocumentBase (), "bug1right.gif");
        if (rand > .63)
        {
            target = getImage (getDocumentBase (), "flblue.gif");
            score = 5;
        }
        else if (rand > .33)
        {
            target = getImage (getDocumentBase (), "flred.gif");
            score = 2;
        }
        else
        {
            target = getImage (getDocumentBase (), "flyellow.gif");
            score = 1;
        }

        //background = getImage (getDocumentBase (), "hedgelarge.gif");
        addKeyListener (this);
        menubg = getImage (getDocumentBase (), "menubg.gif");
        cont = getImage (getDocumentBase (), "contb.gif");
        //target = getImage (getDocumentBase (), "cont2.gif");
        menu = getImage (getDocumentBase (), "menub.gif");
        //target = getImage (getDocumentBase (), "menub2.gif");
        en = getImage (getDocumentBase (), "en.jpg");
        /*
                tile = createImage (1280, 690);
                backg = tile.getGraphics ();
                backg.setColor (Color.white);
        */
        // Place the body of the initialization method here
    } // init method


    public void start ()
    {
        thread = new Thread (this);
        thread.start ();
    }


    public void stop ()
    {
        thread.stop ();
    }


    public void run ()
    {
        Thread.currentThread ().setPriority (Thread.MIN_PRIORITY);
        while (true)
        {

            // Delay for 20 milliseconds
            try
            {
                Thread.sleep (20);
            }
            catch (InterruptedException e)
            {
            }

            if (dx == -2)
            {
                // If you hit an edge, reverse the direction
                img = getImage (getDocumentBase (), "bug1left.gif");
                for (int i = 0 ; i < 500000 ; i++)
                    ;


                repaint ();
                if (y + 25 > locy && y + 25 < locy + 50 && x < locx + 50)
                {
                    get = true;
                    get = false;
                    total += score;
                    rand = Math.random ();
                    location = Math.random () * 1000;
                    locx = (int) Math.round (location);
                    location = Math.random () * 800;
                    locy = (int) Math.round (location);

                    if (rand > .63)
                    {
                        target = getImage (getDocumentBase (), "flblue.gif");
                        score = 5;
                    }
                    else if (rand > .33)
                    {
                        target = getImage (getDocumentBase (), "flred.gif");
                        score = 2;
                    }
                    else
                    {
                        target = getImage (getDocumentBase (), "flyellow.gif");
                        score = 1;
                    }
                    for (int i = 0 ; i < 1000000 ; i++)
                    {
                    }
                }
            }
            else if (dx == 2)
            {
                img = getImage (getDocumentBase (), "bug1right.gif");
                for (int i = 0 ; i < 500000 ; i++)
                    ;

                repaint ();
                if (y + 25 > locy && y + 25 < locy + 50 && x + 50 > locx)
                {
                    get = true;
                    get = false;
                    total += score;
                    rand = Math.random ();
                    location = Math.random () * 1000;
                    locx = (int) Math.round (location);
                    location = Math.random () * 800;
                    locy = (int) Math.round (location);

                    if (rand > .63)
                    {
                        target = getImage (getDocumentBase (), "flblue.gif");
                        score = 5;
                    }
                    else if (rand > .33)
                    {
                        target = getImage (getDocumentBase (), "flred.gif");
                        score = 2;
                    }
                    else
                    {
                        target = getImage (getDocumentBase (), "flyellow.gif");
                        score = 1;
                    }
                    for (int i = 0 ; i < 1000000 ; i++)
                    {
                    }
                }
            }
            else if (dy == -2)
            {
                img = getImage (getDocumentBase (), "bug1up.gif");
                for (int i = 0 ; i < 500000 ; i++)
                    ;

                repaint ();
                if (x + 25 > locx && x + 25 < locx + 50 && y < locy + 50)
                {
                    get = true;
                    get = false;
                    total += score;
                    rand = Math.random ();
                    location = Math.random () * 1000;
                    locx = (int) Math.round (location);
                    location = Math.random () * 800;
                    locy = (int) Math.round (location);

                    if (rand > .63)
                    {
                        target = getImage (getDocumentBase (), "flblue.gif");
                        score = 5;
                    }
                    else if (rand > .33)
                    {
                        target = getImage (getDocumentBase (), "flred.gif");
                        score = 2;
                    }
                    else
                    {
                        target = getImage (getDocumentBase (), "flyellow.gif");
                        score = 1;
                    }
                    for (int i = 0 ; i < 1000000 ; i++)
                    {
                    }
                }
            }
            else if (dy == 2)
            {
                img = getImage (getDocumentBase (), "bug1down.gif");
                for (int i = 0 ; i < 500000 ; i++)
                    ;

                repaint ();
                if (x + 25 > locx && x + 25 < locx + 50 && y + 50 > locy)
                {
                    get = true;
                    get = false;
                    total += score;
                    rand = Math.random ();
                    location = Math.random () * 1000;
                    locx = (int) Math.round (location);
                    location = Math.random () * 1000;
                    locy = (int) Math.round (location);

                    if (rand > .63)
                    {
                        target = getImage (getDocumentBase (), "flblue.gif");
                        score = 5;
                    }
                    else if (rand > .33)
                    {
                        target = getImage (getDocumentBase (), "flred.gif");
                        score = 2;
                    }
                    else
                    {
                        target = getImage (getDocumentBase (), "flyellow.gif");
                        score = 1;
                    }
                    for (int i = 0 ; i < 1000000 ; i++)
                    {
                    }
                }

            }
            if ((x + dx < 0) || (x + dx > getSize ().width))
            {
                dx = 0;
            }
            if ((y + dy < 0) || (y + dy > getSize ().height))
            {
                dy = 0;
            }
            //calculate the new position
            x += dx;
            y += dy;

            /*if (get = true)
            {
                get = false;
                total += score;
                rand = Math.random ();
                location = Math.random () * 1000;
                locx = (int) Math.round (location);
                location = Math.random () * 1000;
                locy = (int) Math.round (location);

                if (rand > .63)
                {
                    target = getImage (getDocumentBase (), "flblue.gif");
                    score = 5;
                }
                else if (rand > .33)
                {
                    target = getImage (getDocumentBase (), "flred.gif");
                    score = 2;
                }
                else
                {
                    target = getImage (getDocumentBase (), "flyellow.gif");
                    score = 1;
                }

            }*/
            // reset thread priority
            Thread.currentThread ().setPriority (Thread.MAX_PRIORITY);

        }

    } // while (true)


    public void paint (Graphics g)
    {
        //backg.drawImage(background, 0, 0, this);
        g.drawImage (target, locx, locy, 50, 50, this);
        g.drawImage (img, x, y, 50, 50, this);
        g.drawString ("Score: " + total, 10, 10);

        if (p == 1)
        {
            g.drawImage (menubg, 100, 100, 500, 700, this);
            g.drawImage (menu, 240, 550, this);
            g.drawImage (cont, 360, 550, this);
            if (mx < 360 && mx < 460 && my > 550 && my < 620)
            {
                cont = getImage (getDocumentBase (), "contb2.gif");
                g.drawImage (cont, 360, 550, this);
            }
            else
            {
                cont = getImage (getDocumentBase (), "contb.gif");
                g.drawImage (cont, 360, 550, this);
            }
        }


        // backg.drawImage( background, 0, 0, this );
        // Place the body of the drawing method here
    } // paint method


    public void keyPressed (KeyEvent e)
    {
        int key = e.getKeyCode ();
        if (key == KeyEvent.VK_RIGHT)
        {
            //backg.drawImage (background, 0, 0, 1280, 960, this);
            if (p != 1)
            {
                dx = 2;
                dy = 0;
                x = x + dx;
            }
        }
        else if (key == KeyEvent.VK_LEFT)
        {
            //backg.drawImage (img, 0, 0, 1280, 960, this);
            if (p != 1)
            {
                dx = -2;
                dy = 0;
                x = x + dx;
            }

        }
        else if (key == KeyEvent.VK_UP)
        {

            //backg.drawImage (img, 0, 0, 1280, 960, this);
            if (p != 1)
            {
                dy = -2;
                dx = 0;
                y = y + dy;
            }
        }
        else if (key == KeyEvent.VK_DOWN)
        {
            //backg.drawImage (img, 0, 0, 1280, 960, this);
            if (p != 1)
            {
                dy = 2;
                dx = 0;
                y = y + dy;
            }
        }
        else if (key == KeyEvent.VK_SPACE)
        {
            dx = 0;
            dy = 0;
            p = 1;
            repaint ();
        }

    }


    public void keyReleased (KeyEvent e)
    {

    }


    public void keyTyped (KeyEvent e)
    {

    }


    public void update (Graphics g)
    {
        // initialize doublebuffers
        if (dbImage == null)
        {
            dbImage = createImage (this.getSize ().width, this.getSize ().height);
            dbg = dbImage.getGraphics ();
        }

        // save background
        dbg.setColor (getBackground ());
        dbg.fillRect (0, 0, this.getSize ().width, this.getSize ().height);

        // draw foreground on background
        dbg.setColor (getForeground ());
        paint (dbg);

        // Now indicate ready drawn picture Offscreen on the right screen
        g.drawImage (dbImage, 0, 0, this);
    }


    public void mouseEntered (MouseEvent e)
    {
    }


    public void mouseExited (MouseEvent e)
    {
    }


    public void mousePressed (MouseEvent e)
    {
    if (mx < 360 && mx < 460 && my > 550 && my < 620)
        {
            p = 0;
            repaint ();
        }
    }


    public void mouseReleased (MouseEvent e)
    {
    }


    public void mouseDragged (MouseEvent e)
    {
    }


    public void mouseMoved (MouseEvent e)
    {
        mx = e.getX ();
        my = e.getY ();
        repaint ();

    }


    public void mouseClicked (MouseEvent e)
    {
        
    }
} // ArrowKeys class

Recommended Answers

All 3 Replies

Forgive me for not going through all 454 lines of code. I assume you are not using any standard "button" object, such as JButton.

I also notice that you dont have anything (relevent) in the mouseClicked or mousePressed methods, unless of course "p = 0" is you turning off your pause? If this is the case, please change p to mean something resonable!!! Its not a local variable its a field of your class, it needs to have a meaningfull name, expecially if its an int. You might know what It means now, but you wont in a few weeks if you go back to the code, and I dont so its harder for me to help you.

Right, after annother look there are a few things.

1) some tips on debugging (if you dont want to use a debugger)

print stuff out! Inserting a println("hi") on the mouseClicked, mouseMoved methods results in no output, so we arn't even getting inside these methods.

2) Now we know that, we have narrowed down the problem. You have the mouseListener and mouseMotionListener methods alright, but you havnt added them to your applet!

under addKeyListener (this);

place the lines:

addMouseListener(this);
        addMouseMotionListener( this );

3) This still isnt right, allthough its saying "hi" when we press the mouse, our second println (that you should have put) inside the if statement:

public void mouseClicked (MouseEvent e)
    {
        System.out.println("hi");
    	if (mx < 360 && mx < 460 && my > 550 && my < 620)
        {
            p = 0;
            repaint ();
            System.out.println("yey");
        }
    }
}

isnt going, yey! So now we know exactly where the code is failing, its not satisfying the statement "mx < 360 && mx < 460 && my > 550 && my < 620" so have a reeeeaaallly good stare at that and spot the problem (and kick yourself) ^_^

commented: Just seeing that huge block of code made me sick, well done for helping him +1

Thanks the mouse listeners fixed everything

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.