Results 1 to 3 of 3
  1. #1
    dunafrothint is offline Member
    Join Date
    Oct 2009
    Posts
    26
    Rep Power
    0

    Default Problem with keylisteners

    The keylistener won't do anything. Can some people either resubmit edited versions of this code, or their own examples that will work with this code? We have looked at a lot of examples, so we would prefer if you could just edit this code:

    Java Code:
    package FinalxProject;
    import java.awt.*;
    import javax.swing.*;
    import javax.imageio.*;
    import java.awt.Graphics.*;
    import java.io.*;
    import java.awt.image.*;
    import java.awt.Component.*;
    import java.*;
    import javax.*;
    import java.awt.event.KeyEvent.*;
    import java.awt.event.KeyListener.*;
    import java.awt.event.*;
    public class Panel extends JFrame{
        public static class Background extends JPanel implements KeyListener, Runnable{
            Thread tGame = null;
            ImageObserver imgo = new ImageObserver(){
                public boolean imageUpdate(Image image, int flags, int x, int y, int width, int height) {
                return true;
              }
            };
            int nX=0, nY=0;
            boolean bLeft, bRight;
            JLabel label = new JLabel();
            Graphics gBuffer;
            BufferedImage Background = ImageIO.read(new File("123.png"));
            BufferedImage Buffer = ImageIO.read(new File("123.png"));
            BufferedImage Char = ImageIO.read(new File("thing.png"));
            ImageIcon I = new ImageIcon(Buffer);
            Dimension size = new Dimension();
            public Background() throws IOException{
                this.addKeyListener((KeyListener)this);
                if(tGame == null){
                    tGame = new Thread(this);
                    tGame.start();
                    label.setIcon(I);
                    add(label);
                    setSize(size);  
                }
                size.height = Background.getHeight();
                size.width = Background.getWidth();
            }
            public void run(){
                try{
                    while(true){
                        if(bRight = true){
                            nX+=20;
                        }
                        if(bLeft = true){
                            nX-=20;
                        }
                        Draw();
                        tGame.sleep(50);
                    }
                }
                catch(InterruptedException e){}
            }
            public void keyPressed(KeyEvent e) {
                int nKey=e.getKeyCode();
                if(nKey==KeyEvent.VK_RIGHT){
                    bRight = true;
                }
                if(nKey==KeyEvent.VK_LEFT){
                    bLeft = true;
                }
                if(nKey==KeyEvent.VK_UP){
                }
                if(nKey==KeyEvent.VK_DOWN){
    
                }
            }
    
        public void keyReleased(KeyEvent e) {
            int nKey = e.getKeyCode();
            if(nKey==KeyEvent.VK_RIGHT){
                bRight = false;
            }
            if(nKey==KeyEvent.VK_LEFT){
                bLeft = false;
            }
        }
        public void keyTyped(KeyEvent e){}
            public void Draw(){
                gBuffer = Buffer.getGraphics();
                gBuffer.drawImage(Background, 0, 0,this);
                gBuffer.drawImage(Char, nX, nY, this);
                I = new ImageIcon(Buffer);
                label.setIcon(I);
            }
        }
        public static void main (String args[]) throws IOException{
            Panel Panel = new Panel();
            Background BG = new Background();
            Panel.add(BG);
            Panel.pack();
            Panel.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            Panel.setVisible(true);
        }
    }
    Thanks

    ~Jamie

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

  3. #3
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    7

    Default

    Also, look at using a Swing Timer, not a while(true) loop.

    And don't use AWT components in a Swing application. "Panel" is NOT a Swing component.

Similar Threads

  1. simple line problem / for loop problem
    By helpisontheway in forum New To Java
    Replies: 1
    Last Post: 11-17-2009, 06:12 AM
  2. [SOLVED] KeyListeners in JavaApplets
    By Unome in forum Java Applets
    Replies: 18
    Last Post: 10-30-2008, 10:36 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •