Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Help with Graphics class and keyPressed method

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help with Graphics class and keyPressed method

    Hi, here is the lab assignment that I'm currently working on : Using nested loops and objects in an applet

    Right now I'm stuck at these two steps :

    Now add some more code to your paint method so that it displays the terrain type where the player is currently located. This can be displayed near the bottom of the applet. To do this you use the drawString method of the Graphics class.

    Next, fill in keyPressed so that the user can move by using the arrow keys. Don't forget to repaint so they get an update on what kind of terrain they are on. The user should be able to move around now (after clicking the applet to gain focus).
    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
     
    public class TreasureApplet extends Applet implements KeyListener {
        private Island island;
     
        public void init() {
            island = new Island(10);
            addKeyListener(this);
        }
     
        public void keyPressed(KeyEvent e) {
            if (island.currentLocation() == Island.WATER) {
                return;
            }
            if (island.currentLocation() == Island.TREASURE) {
                return;
            }
            if (island.currentLocation() == Island.PIRATE) {
                return;
            }
            if (e.getKeyCode() == KeyEvent.VK_LEFT) {
                island.moveWest();
            }
            repaint();
            if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
                island.moveEast();
            }
            repaint();
     
            if (e.getKeyCode() == KeyEvent.VK_UP) {
                island.moveNorth();
            }
            repaint();
     
            if (e.getKeyCode() == KeyEvent.VK_DOWN) {
                island.moveSouth();
            }
            repaint();
        }
     
        public void keyReleased(KeyEvent e) {
        }
     
        public void keyTyped(KeyEvent e) {
        }
     
        public void paint(Graphics g) {
            int x = 0;
            while(x < 10) {
                int y = 0;
                while(y < 10) {
                    if (island.terrainAt(x,y) == Island.WATER) {
                        //draw the water
                        g.setColor(new Color(0,0,210));
                        g.fillRect(x*40, y*40, 40, 40);
                        g.drawString("Splash! You got to the water.", 50, 430);
                    }
                    else if (island.terrainAt(x,y) == Island.SAND) {
                        //draw the sand
                        g.setColor(new Color(160,82,45));
                        g.fillRect(x*40, y*40, 40, 40);
                        g.drawString("Ouch. Sand got into your sandals.", 50, 430);
                    }
                    else if (island.terrainAt(x,y) == Island.TREE) {
                        //draw the treasure
                        g.setColor(new Color(50,205,50));
                        g.fillRect(x*40, y*40, 40, 40);
                        g.drawString("You are standing by a tree.", 50, 430);
                    }
                    else if (island.terrainAt(x,y) == Island.TREASURE) {
                        //draw the treasure
                        g.setColor(new Color(225,215,0));
                        g.fillRect(x*40, y*40, 40, 40);
                        g.drawString("Congratulations. You found the treasure", 50, 430);
                    }
                    else {
                        //draw the pirate
                        g.setColor(new Color(0,0,0));
                        g.fillRect(x*40, y*40, 40, 40);
                        g.drawString("It's the pirate. Run!", 50, 430);
                    }
     
                    //draw a square so the user can distinguish locations clearly
                    g.setColor(Color.BLACK);
                    g.drawRect(x*40, y*40, 40, 40);
     
                    y++;
                }
                x++;
            }
        }
    }

    When I try to compile and display the applet, things got really weird when all 5 drawString strings appear at the same time. I mean, they sit on top of each other. It's weird because I have already used an if-else sentence to classify them, how in the world they still appear all at once?

    Another thing I would like to ask is the keyPressed method. I think I have wrote the code wrong somewhere because I don't see the player moving at all. Please take a look and give me some suggestions on how to change it. Thank you all so much.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,297
    Thanks
    180
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: Help with Graphics class and keyPressed method

    First, when overrideing methods such as paint, be sure to make a call to the parent method using super.paintComponent (this clears the previous calls painting, amongst other things). Next, be sure the appet has focus for a KeyListener (call requestFocus() ), or use KeyBindings instead

  3. #3
    Junior Member
    Join Date
    Mar 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with Graphics class and keyPressed method

    Thank you. I know how to solve my problem now. First, to classify and display message, I have to use "island.currentLocation" and not "island.terrainAt". Second, I did NOT click on the applet to gain focus, so that's why I don't see any movement lol. Anw, thanks a lot for a very nice suggestion. I'll keep that in mind.

Similar Threads

  1. Method & Class confusion
    By jog98 in forum Object Oriented Programming
    Replies: 8
    Last Post: December 8th, 2010, 10:28 AM
  2. Override class method
    By mekie in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 31st, 2010, 07:06 PM
  3. Getting a string value from within a method in another class
    By sp11k3t3ht3rd in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 11th, 2010, 04:17 PM
  4. Accessing a method of one class in another class
    By Sai in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 23rd, 2010, 04:06 PM
  5. Help Calling Method From Another Class
    By CheekySpoon in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 15th, 2010, 09:24 AM