Java-Gaming.org    
Featured games (78)
games approved by the League of Dukes
Games in Showcase (426)
Games in Android Showcase (89)
games submitted by our members
Games in WIP (466)
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 737 times)
0 Members and 1 Guest are viewing this topic.
Offline alaslipknot
« Posted 2013-04-15 16: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


Am trying ...
hoping that it works
Offline cubus

Junior Member


Medals: 2



« Reply #1 - Posted 2013-04-15 16: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 16: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 ?

Am trying ...
hoping that it works
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline SHC
« Reply #3 - Posted 2013-04-15 17: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 17: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();
   }
   
}

Am trying ...
hoping that it works
Offline SHC
« Reply #5 - Posted 2013-04-15 17: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 17:20:10 »

You are not initializing the
control
variable!


Thank you  Grin

Am trying ...
hoping that it works
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.

xsi3rr4x (75 views)
2014-04-15 18:08:23

BurntPizza (68 views)
2014-04-15 03:46:01

UprightPath (80 views)
2014-04-14 17:39:50

UprightPath (65 views)
2014-04-14 17:35:47

Porlus (81 views)
2014-04-14 15:48:38

tom_mai78101 (105 views)
2014-04-10 04:04:31

BurntPizza (165 views)
2014-04-08 23:06:04

tom_mai78101 (261 views)
2014-04-05 13:34:39

trollwarrior1 (210 views)
2014-04-04 12:06:45

CJLetsGame (220 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
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!