Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Problem With Key Listener In Swing Applet

$
0
0
I've been trying to fix this for over half a day: my key listener won't register input (I know this because I added a showStatus line inside the listener). I figured that before I mess up my code because I'm only a beginner when it comes to applets, I'd better ask you guys.

What I've tried:
  • having the JApplet request focus in window.
  • switching the component that gets focus to JPanel a1a1
  • making sure all containers containing a1a1 were focusable
  • removing the line in the stop method setting focusable to false
  • adding a showStatus to the key listener


Here's my init(), start(), stop(), and run() methods:

    public void init()
    {
        t = null;
        showStatus("Use a, w, s, and d keys like the arrow keys to move.");
        try
        {
            Thread.sleep(5000);
        }
        catch (InterruptedException exc) {}
        showStatus("Use x key to check the squares around you.");
        try
        {
            Thread.sleep(5000);
        }
        catch (InterruptedException exc) {}
        showStatus("Use j key to attack and defend.");
        try
        {
            Thread.sleep(5000);
        }
        catch (InterruptedException exc) {}
        showStatus("Use k key to collect something.");
        try
        {
            Thread.sleep(5000);
        }
        catch (InterruptedException exc) {}
        showStatus("Enemies will appear red on your map.");
        try
        {
            Thread.sleep(5000);
        }
        catch (InterruptedException exc) {}
        showStatus("Resources will appear green on your map.");
        try
        {
            Thread.sleep(5000);
        }
        catch (InterruptedException exc) {}
        showStatus("Permanent resources will eliminate your need for a certain item.");
        try
        {
            Thread.sleep(5000);
        }
        catch (InterruptedException exc) {}
        showStatus("Your ultimate goal will be to take the ice castle from the walruses.");
        try
        {
            Thread.sleep(5000);
        }
        catch (InterruptedException exc) {}
        life = rnd.nextInt(9);
        life += 6;
        showStatus("Your life: " + life + " out of 16 life.");
        try
        {
            Thread.sleep(5000);
        }
        catch (InterruptedException exc) {}
        hunger = rnd.nextInt(9);
        hunger += 6;
        showStatus("Your hunger: " + hunger + " out of 16 hunger.");
        try
        {
            Thread.sleep(5000);
        }
        catch (InterruptedException exc) {}
        thirst = rnd.nextInt(9);
        thirst += 6;
        showStatus("Your thirst: " + thirst + " out of 16 thirst.");
        try
        {
            Thread.sleep(5000);
        }
        catch (InterruptedException exc) {}
        showStatus("If any stats reach zero, you will die.");
        try
        {
            Thread.sleep(5000);
        }
        catch (InterruptedException exc) {}
        showStatus("By the way, the castle is disguised as a normal piece of land.");
        try
        {
            Thread.sleep(5000);
        }
        catch (InterruptedException exc) {}
        try
        {
            SwingUtilities.invokeAndWait(new Runnable()
            {
                public void run()
                {
                    makeGUI();
                }
            });
        }
        catch (Exception exc)
        {
        }
        
        monsterALocationSet();
        monsterBLocationSet();
        monsterCLocationSet();
        healALocationSet();
        healBLocationSet();
        healCLocationSet();
        foodLocationSet();
        drinkLocationSet();
        itemsLocationStringSet();
        castleX = rnd.nextInt(49);
        castleY = rnd.nextInt(49);
        castleX++;
        castleY++;
        setCastleCoordinates();
        castleX = rnd.nextInt(49);
        castleY = rnd.nextInt(49);
        castleX++;
        castleY++;
        setPermFoodCoordinates();
        castleX = rnd.nextInt(49);
        castleY = rnd.nextInt(49);
        castleX++;
        castleY++;
        setPermDrinkCoordinates();
        
        addKeyListener(this);
    }
    
    public void start()
    {
        t = new Thread(this);
        pause = false;
        this.setFocusable(true);
        a1.setFocusable(true);
        a1a1.setFocusable(true);
        t.start();
    }
    
    public void stop()
    {
        pause = true;
        t = null;
    }
    
    public void run()
    {
        for( ; ; )
        {
            a1a1.requestFocusInWindow();
            try
            {
                if ((castleSmallLocation.equals(currentLocation) && (castleLargeLocation.equals(largeLocation))))
                {
                    repaint();
                    break;
                }
                else if ((castleSmallLocation.equals(currentLocation) && (castleLargeLocation.equals(largeLocation))))
                {
                    showStatus("Permanent food found!");
                    food = false;
                }
                else if ((castleSmallLocation.equals(currentLocation) && (castleLargeLocation.equals(largeLocation))))
                {
                    showStatus("Permanent drink found!");
                    drink = false;
                }
                if ((monsterALocation.equals(currentLocation) || monsterBLocation.equals(currentLocation) || monsterCLocation.equals(currentLocation)) && ch == 'j')
                {
                    monsterAttack();
                }
                else if (ch == 'x')
                {
                    look();
                }
                else if (ch == 'a')
                {
                    moveLeft();
                }
                else if (ch == 'd')
                {
                    moveRight();
                }
                else if (ch == 'w')
                {
                    moveUp();
                }
                else if (ch == 's')
                {
                    moveDown();
                }
                else if ((healALocation.equals(currentLocation) || healBLocation.equals(currentLocation) || healCLocation.equals(currentLocation) || foodALocation.equals(currentLocation) || foodBLocation.equals(currentLocation) || drinkALocation.equals(currentLocation) || drinkBLocation.equals(currentLocation)) && ch == 'k')
                {
                    collect();
                }
                else if (ch == 'B' && bobness == 0)
                {
                    bobness = 1;

                }
                else if (bobness == 1 && ch != 20000)
                {
                    if (ch == 'o')
                    {
                        bobness = 2;
                    }
                    else
                    {
                        bobness = 0;
                    }
                }
                else if (bobness == 2 && ch != 20000)
                {
                    if (ch == 'b')
                    {
                        bobness = 3;
                    }
                    else
                    {
                        bobness = 0;
                    }
                }
                if (bobness == 3)
                {
                    repaint();
                }
                
                Thread.sleep(20);
                if(pause)
                    break;
            }
            catch(InterruptedException exc) {}
            ch = 20000;
        }
    }



Here's my keyTyped(KeyEvent e), keyPressed(KeyEvent e), keyReleased(KeyEvent e), and paint(Graphics g) methods:

    public void keyTyped(KeyEvent e)
    {
        try
        {
            ch = e.getKeyChar();
            Thread.sleep(20);
            //run();
            showStatus("Key typed: " + ch);
        }
        catch(InterruptedException exc) {}
    }
    
    @Override
    public void keyPressed(KeyEvent e)
    {
    }
    
    @Override
    public void keyReleased(KeyEvent e)
    {
    }
    
    public void paint(Graphics g)
    {
        if(bobness == 3)
        {
            g.drawString(bob, 50, 50);
            bobness = 4;
        }
        
        String end = "You win!";
        if ((castleSmallLocation.equals(currentLocation) && (castleLargeLocation.equals(largeLocation))))
        {
            g.drawString(end, 50, 50);
        }
    }
}



Those are all the methods that make any mention of a key listener. Does anyone know what's going wrong?

I would post the whole applet, but it's 250,000 lines of code, most of which are graphics.

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>