user2495627
user2495627

Reputation: 5

KeyListener and runnable not works

I make the stage class as a JFrame before and all is working well.. until I tried to make it as JPanel then its just stopped and nothing happen. KeyListener not works, Any idea what wrong with my source

import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.awt.event.*;
import javax.imageio.ImageIO;
class stage extends JPanel implements Runnable,KeyListener
{   
    private int timer;
    int y_back = -4500;
    int x_back = 0;
    int x_player =295;
    int y_player = 590;
    int x_nenek = 600;  
    private Image img;
    private Image img_player;
    private Image img_nenek;    
    stage()
    {
        super();
        //setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocation(0,0);
        setSize(700,700);
        //setResizable(false);
        setVisible(true);       
        try{
            img = ImageIO.read(getClass().getResourceAsStream("jalan.png"));
            img_player= ImageIO.read(getClass().getResourceAsStream("player.png"));
            img_nenek= ImageIO.read(getClass().getResourceAsStream("Nenek.png"));
            }
        catch(Exception e){}        
    new Thread(this).start();
    addKeyListener(this);    
    }
    public void update(Graphics g)
    {
        paint(g);
    }
    public void paint(Graphics g)
    {
        g.drawImage(img,x_back,y_back+200,null);//background
        g.drawImage(img_player,x_player,y_player,null);//player
        g.drawImage(img_nenek,x_nenek,y_back+1750,null);// nenek    
    }
    public void keyPressed(KeyEvent e)
    {
        int c = e.getKeyCode();
        if (y_back<-200) 
        {
            if (c == KeyEvent.VK_UP)
            {
                if(x_nenek>=200 &&x_nenek<=500)
                {
                        JOptionPane.showMessageDialog(null,"awas ada yang menyeberang","Ehem~~~~",JOptionPane.WARNING_MESSAGE);
                        y_back+=0       ;
                }
                if (x_player >=310 && y_back>=-3550 && y_back<= -3400)
                {
                    JOptionPane.showMessageDialog(null,"Kamu telah masuk kubangan","peringatan",JOptionPane.WARNING_MESSAGE);
                    y_back =-4500;
                }

                if (x_player <=265 && y_back >=-3300 && y_back <=-3250)
                {
                    JOptionPane.showMessageDialog(null,"Kamu telah masuk kubangan","Peringatan",JOptionPane.WARNING_MESSAGE);
                    y_back =-4500;
                }
                if ((x_player>=182 && x_player<=365) && (y_back>=-2100 && y_back<=-1800))//clear
                {
                    JOptionPane.showMessageDialog(null," itu lubang mas =_+ , balik sana","Peringatan",JOptionPane.WARNING_MESSAGE);
                    y_back =-4500;
                }   
                if ((x_player>=130 && x_player <=200)&& y_back>=-900 && y_back<=-800)
                {
                    JOptionPane.showMessageDialog(null,"baksoooooooooooooo","Peringatan",JOptionPane.WARNING_MESSAGE);
                    y_back -=10;    
                }               
                if ((x_player>=280 && x_player<=600) && y_back>=-750)//clear
                {
                    JOptionPane.showMessageDialog(null,"lewat kiri mas =_+","Peringatan",JOptionPane.WARNING_MESSAGE);
                    y_back -=10;
                }   

                if((x_player>=130 && x_player <=180) && y_back>=-4200 && y_back<=-4000) // clear
                {
                    JOptionPane.showMessageDialog(null,"Minggir... ntar ketatap mas =_=","Peringatan",JOptionPane.WARNING_MESSAGE); 
                    y_back -=10;

                }

                if((x_player >= 0 && x_player<= 275 && y_back>=-2800 && y_back<=-2600) || x_player >=400&& y_back>=-2800 && y_back<=-2600)//clear       
                    y_back +=0;
                else
                y_back+=10;
            }
            if (c == KeyEvent.VK_LEFT && x_player>135)
            {
                if(x_nenek>=200 &&x_nenek<=500)
                    {   
                        JOptionPane.showMessageDialog(null,"awas ada yang menyeberang","Ehem~~~~",JOptionPane.WARNING_MESSAGE);

                    }
                if (x_player <=265 && y_back >=-3300 && y_back <=-3250)
                {
                    JOptionPane.showMessageDialog(null,"Kamu telah masuk kubangan","Peringatan",JOptionPane.WARNING_MESSAGE);
                    y_back =-4500;
                }
                if ((x_player>=130 && x_player <=200)&& y_back>=-900 && y_back<=-800)
                {
                    JOptionPane.showMessageDialog(null,"baksoooooooooooooo","Peringatan",JOptionPane.WARNING_MESSAGE);
                    y_back -=3;
                    x_player+=3;        
                }               
                if ((x_player>=182 && x_player<=365) && (y_back>=-2100 && y_back<=-1700))//clear
                {
                    JOptionPane.showMessageDialog(null," itu lubang mas =_+ , balik sana","Peringatan",JOptionPane.WARNING_MESSAGE);
                    y_back =-4500;
                }   
                if((x_player>=130 && x_player <=180) && y_back>=-4200 && y_back<=-4000)// clear
                {
                    JOptionPane.showMessageDialog(null,"Minggir... ntar ketatap mas =_=","Peringatan",JOptionPane.WARNING_MESSAGE); 
                    y_back -=3;
                    x_player-=3;
                }   
                else
                x_player-=3;
                y_back +=3;

            }
            if (c == KeyEvent.VK_RIGHT && x_player<465)
            {
                if(x_nenek>=200 &&x_nenek<=500)//clear
                    {
                    JOptionPane.showMessageDialog(null,"awas ada yang menyeberang","Ehem~~~~",JOptionPane.WARNING_MESSAGE);
                    y_back+=5;
                    x_player+=5;
                    }
                if (x_player >=340 && y_back>=-3550 && y_back<= -3400)
                {
                    JOptionPane.showMessageDialog(null,"Kamu telah masuk pada kubangan","peringatan",JOptionPane.WARNING_MESSAGE);
                    y_back =-4500;
                }
                if ((x_player>=280 && x_player<=600) && y_back>=-750)
                    JOptionPane.showMessageDialog(null,"lewat kiri mas =_+","Peringatan",JOptionPane.WARNING_MESSAGE);
                x_player+=3;
                y_back +=3;
            }
        }
        else
        JOptionPane.showMessageDialog(null,"Selamat, kamu sudah sampai di tempat tujuan","",JOptionPane.WARNING_MESSAGE);              
    }
    public void keyTyped(KeyEvent e){}
    public void keyReleased(KeyEvent e){}
    public void run()
    {
        while (true)
        try
        {
            repaint();
            if(y_back>=-2000)
            x_nenek-=10;
            Thread.sleep(200);
        }    
            catch (Exception e)
            {
            }       
    }
}
public class Tes2 extends JFrame
{
    Tes2()
    {
        stage s = new stage();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLocation(50,50);
    setSize(700,700);
    setResizable(false);
    setVisible(true);
        add(s);       
    }
    public static void main(String[] args)
    {
        new Tes2();
    }
}

Upvotes: 0

Views: 609

Answers (1)

camickr
camickr

Reputation: 324118

Don't override paint() and update(). That is only done for AWT components, not Swing components.

For custom painting on a JPanel you override the paintComponent() method and make sure to invoke super.paintComponent() at the beginning. Read the section from the Swing tutorial on Custom Painting for more information and working examples.

Key events are only dispatch to the component with focus. You need to make the panel focusable.

However, in general you should not be using a KeyListener. A better approach is to use Key Bindings. Check out Motion With the Keyboard for more information on this topic and some working examples and a link to the Swing tutorial.

Upvotes: 2

Related Questions