Hello everyone,

I am designing a simple game with graphics in which I am trying to add a keyListener to my Jframe but the keyListener nor the mouse Listener seem to respond. Here's the code:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import javax.imageio.ImageIO;

public class PaintDemo extends JFrame implements KeyListener{

    String birdpath="C:\\Users\\Public\\Pictures\\Sample Pictures\\flappy.png";
    String path1="C:\\Users\\Public\\Pictures\\Sample Pictures\\brick.png";
    String path2="C:\\Users\\Public\\Pictures\\Sample Pictures\\brick2.png";
    String path3="C:\\Users\\Public\\Pictures\\Sample Pictures\\brick3.png";
    ImageIcon back= new ImageIcon("C:\\Users\\Public\\Pictures\\Sample Pictures\\city.jpg");
    JLabel background= new JLabel(back);
    Image bird;
    Image wall1;
    Image wall2;
    Image wall3;
    int birdy= 350;
    JFrame f= this;

    public PaintDemo(){

        setLayout(null);
        setBounds(0,0,1005,730);
        f.setDefaultCloseOperation(EXIT_ON_CLOSE);
        setTitle("FLAPPY BIRD");
        background.setBounds(0,0,1000,700);
        setResizable(false);
        add(background);

        try{

            bird= ImageIO.read(new File(birdpath));
            wall1= ImageIO.read(new File(path1));
            wall2= ImageIO.read(new File(path2));
                wall3= ImageIO.read(new File(path3));
        }catch(Exception e){}

        f.addMouseListener(new MouseListener() {

                @Override
                public void mouseClicked(MouseEvent e) {
                        System.out.println("Mouse was clicked");
                }

                @Override
                public void mouseEntered(MouseEvent arg0) {
                }

                @Override
                public void mouseExited(MouseEvent arg0) {
                }

                @Override
                public void mousePressed(MouseEvent arg0) {
                System.out.println("Mouse was clicked");
                }

                @Override
                public void mouseReleased(MouseEvent arg0) {
                }
            });

        f.addKeyListener(this);
        f.setFocusable(true);
        Graphics g = getGraphics();

        setVisible(true);
    }

    @Override
    public void paintComponent(Graphics g){

        loop:
        for(int x=100; x>=0; x-=10){

            Graphics2D g2= (Graphics2D)g;
            super.paintComponent(g);

            g2.drawImage(bird,-10,birdy,null);
            g2.drawImage(wall1,x,30,null);
            g2.drawImage(wall2,x+120,30,null);
            g2.drawImage(wall3,x+240,30,null);
            g2.drawImage(wall2,x+360,30,null);
            g2.drawImage(wall1,x+480,30,null);
            g2.drawImage(wall3,x+600,30,null);
            g2.drawImage(wall1,x+720,30,null);
            g2.drawImage(wall2,x+840,30,null);


            g2.drawImage(wall2,x,450,null);
            g2.drawImage(wall3,x+120,570,null);
            g2.drawImage(wall2,x+240,450,null);
            g2.drawImage(wall3,x+360,570,null);
            g2.drawImage(wall2,x+480,450,null);
            g2.drawImage(wall1,x+600,520,null);
            g2.drawImage(wall3,x+720,570,null);
            g2.drawImage(wall1,x+840,520,null);

            birdy+=10;
            try{
                Thread.sleep(300);
            }catch(Exception ex){ex.printStackTrace();} 

            if(x==0){
                x=100;
                continue loop;
            }


        } //ends loop


    }

    public void keyPressed(KeyEvent ev){

        System.out.println("hello");

        int code= ev.getKeyCode();



    }

    public void keyTyped(KeyEvent ev){

    }

    public void keyReleased(KeyEvent ev){

    }


    public static void main(String arg[]){

        new PaintDemo(); 


    }

}

Everything to do with Swing (eg painting the screen, running actionPerformed methods) happens on a single thread - the "Event Dispatch Thread", or "EDT", or "Swing thread".
That means that once your paintComponent method starts NOTHING else will happen in Swing, including no mouse or keyboard events, until your method finishes. You can update stuff and loop and sleep as much as you like, but none of that will affect what's on the screen until your method has terminated.

If you want to do stuff in steps over a period of time, here is the right way:
Start a javax.swing.Timer, and return. Every time the timer fires you can update whatever needs updating and return. Inbetween the timer firings Swing will be free to update the screen. Swing will call paintComponent when needed, but you can call repaint() if you want Swing to schedule a call soon.

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.