Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (542)
Games in Android Showcase (133)
games submitted by our members
Games in WIP (605)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  [Solved]how to add KeyListener to JPanel from another class  (Read 1326 times)
0 Members and 1 Guest are viewing this topic.
Offline alaslipknot
« Posted 2013-04-15 14:35:01 »

Hello,
i have a class that implements KeyListener (Control.java) and i want to use that KeyListener in a class that extends JPanel and implements Runnable (Screen.java)
how do i do that ?
notice that i have another class that extends JFrame (Frame.java) which i already added the panel into it

i hope what i said make sense  Tongue

thank you


"It's not at all important to get it right the first time. It's vitally important to get it right the last time."
Offline cubus

Junior Devvie


Medals: 2



« Reply #1 - Posted 2013-04-15 14:44:13 »

if i understand correctly, you want to add an instance of Control as KeyListener to an instance of Screen?
that would be
1  
theScreenInstance.addKeyListener(theControlInstance);
Offline alaslipknot
« Reply #2 - Posted 2013-04-15 14:49:25 »

if i understand correctly, you want to add an instance of Control as KeyListener to an instance of Screen?
that would be
1  
theScreenInstance.addKeyListener(theControlInstance);


yes but it didn't work :/
wont me to post the full code ?

"It's not at all important to get it right the first time. It's vitally important to get it right the last time."
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline SHC
« Reply #3 - Posted 2013-04-15 15:04:25 »

1  
theScreenInstance.addKeyListener(theControlInstance);


yes but it didn't work :/
wont me to post the full code ?

Maybe the culprit is focus. Try

1  
mypanel.setFocusible(true);

Offline alaslipknot
« Reply #4 - Posted 2013-04-15 15:10:16 »

still doesn't work  Clueless
codes :

the panel :

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JPanel;

public class Screen extends JPanel implements Runnable {
   Thread thread = new Thread(this);

   public static Ball ball;
   public static Player player;
   public static Control control;
   
   public static boolean isFirst = true;
   
   public static int screenWidth,screenHeight,screenX,screenY;

   public Screen() {
      thread.start();
     
      this.addKeyListener(control);
      this.setFocusable(true);
   }

   public void define() {
      control = new Control();
      ball = new Ball(ball.x, ball.y, ball.height, ball.width);
      player = new Player(player.x,player.y,player.width,player.height,player.speed);
     
      screenWidth = this.getWidth();
      screenHeight=this.getHeight();
     
     
   }

   public void paintComponent(Graphics g) {

      if (isFirst) {
         define();
      }
      g.setColor(new Color(0, 0, 0));
      g.fillRect(screenX, 0, screenWidth, screenHeight);

      g.setColor(new Color(250, 50, 0));
      ball.move(ball.x, ball.y);
      ball.draw(g);
      g.setColor(new Color(255,255,255));
      player.draw(g);
     

   }

   public void run() {
      // while is true
      while (true) {
         // do stuff

         if (!isFirst) {
           
         }

         repaint();
         
         try {
            // make the thread slee every 1 milliSecond
            // the try and catch are auto-generated
            Thread.sleep(5);
         } catch (InterruptedException e) {
            e.printStackTrace();
         }
      }
   }

}


the Key class

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;


public class Control implements KeyListener{

   @Override
   public void keyPressed(KeyEvent e) {
      // TODO Auto-generated method stub
      if(e.getKeyCode()==e.VK_RIGHT){
         System.out.println("Right");
      }else if(e.getKeyCode()==e.VK_LEFT){
         System.out.println("LEFT");
      }
     
   }

   @Override
   public void keyReleased(KeyEvent e) {
      // TODO Auto-generated method stub
     
   }

   @Override
   public void keyTyped(KeyEvent e) {
      if(e.getKeyCode()==e.VK_RIGHT){
         System.out.println("Right");
      }else if(e.getKeyCode()==e.VK_LEFT){
         System.out.println("LEFT");
      }
     
   }

}


the frame class

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
import java.awt.Dimension;

import javax.swing.JFrame;


public class Frame extends JFrame  {

   public static Dimension size = new Dimension(800,600);
   public static String title ="Pong2.0";

   public Frame(){
     
      this.setSize(size);
      this.setTitle(title);
      //this.setLocationRelativeTo(null);
      this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
      init();
     
   }
   
   public void init(){
     
      Screen screen = new Screen();

      this.add(screen);
     
   
     
      this.setVisible(true);
     
   }
   
   
   
   
   public static void main(String args[]){
      Frame frame = new Frame();
   }
   
}

"It's not at all important to get it right the first time. It's vitally important to get it right the last time."
Offline SHC
« Reply #5 - Posted 2013-04-15 15:15:44 »

You are not initializing the
control
variable!

Call
define()
first before creating the thread. Remove the
isFirst
part.

So the constructor becomes

1  
2  
3  
4  
5  
6  
7  
public Screen()
{
    define();
    thread.start();
    addKeyListener(control);
    setFocusable(true);
}

Offline alaslipknot
« Reply #6 - Posted 2013-04-15 15:20:10 »

You are not initializing the
control
variable!


Thank you  Grin

"It's not at all important to get it right the first time. It's vitally important to get it right the last time."
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Elsealabs (15 views)
2014-12-28 10:39:27

CopyableCougar4 (19 views)
2014-12-28 02:10:29

BurntPizza (24 views)
2014-12-27 22:38:51

Mr.CodeIt (14 views)
2014-12-27 04:03:04

TheDudeFromCI (19 views)
2014-12-27 02:14:49

Mr.CodeIt (26 views)
2014-12-23 03:34:11

rwatson462 (58 views)
2014-12-15 09:26:44

Mr.CodeIt (47 views)
2014-12-14 19:50:38

BurntPizza (96 views)
2014-12-09 22:41:13

BurntPizza (115 views)
2014-12-08 04:46:31
How do I start Java Game Development?
by gouessej
2014-12-27 19:41:21

Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!