Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (476)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (532)
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  
  AddKeylistener() Kevin Glass' code problem  (Read 542 times)
0 Members and 1 Guest are viewing this topic.
Offline Beppe

Junior Member





« Posted 2013-08-26 19:30:46 »

Hi !
I believe that you hate me in this community  Grin so whats the problem ?? Pratically I don't know why  the keylistener doesn't work ..
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
public GameView(){
    //System.out.println("hey");
   JFrame container= new JFrame("Loderunner");
    JPanel panel= (JPanel) container.getContentPane();
    panel.setPreferredSize(new Dimension(460,460));
    panel.setMinimumSize(new Dimension(460,460));
    panel.setLayout(null);
    setBounds(0,0,460,460);
    panel.add(this);
    setIgnoreRepaint(true);
    requestFocus();
    addKeyListener(new KeyHandle());
    container.pack();
    container.setVisible(true);
    container.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    createBufferStrategy(2);
    strategy=getBufferStrategy();
    loadLevel("level.txt");
}

This class is in GameView class
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
private class KeyHandle extends KeyAdapter {
   
    public void KeyPressed(KeyEvent e){
    System.out.println("hey");
    if(e.getKeyCode()==KeyEvent.VK_LEFT){
        System.out.println("hey");
        runner.setRunning(true);
        runner.setDX(1);
    }
       
    }
   
    public void KeyReleased(KeyEvent e){
   
    if(e.getKeyCode()==KeyEvent.VK_LEFT){
        runner.setRunning(false);
        runner.setDX(0);
    }
    }
       
    }
   
}

I put some System.out.println but if i press left they print nothing , so i believe that the problem is in the costructor ...
Offline kevglass

JGO Kernel


Medals: 120
Projects: 23
Exp: 18 years


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #1 - Posted 2013-08-26 19:43:32 »

Looks like you're adding the key listener to the wrong object - is GameView a JFrame? If so, why are you creating a new one called "container". If you're using the "container" JFrame you might try:

1  
container.addKeyListener(new KeyHandle());


The other option I'd try is:

1  
panel.addKeyListener(new KeyHandle());


Not sure which component owns focus by default, it's been a while since I touched swing.

Cheers,

Kev

Offline Beppe

Junior Member





« Reply #2 - Posted 2013-08-26 20:04:20 »

GameView extends Canvas like in the tutorial ...  so i try with container and panel but nothing.... No one print of System.out.println() .....  Shocked
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline kevglass

JGO Kernel


Medals: 120
Projects: 23
Exp: 18 years


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #3 - Posted 2013-08-26 20:11:16 »

Could you post the whole code, cause it looks like you're displaying a canvas and then creating panels and frames outside of that?

Cheers,

Kev

Offline Beppe

Junior Member





« Reply #4 - Posted 2013-08-26 20:15:05 »

Thanks a lot !
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  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.image.BufferStrategy;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import javax.swing.JFrame;
import javax.swing.JPanel;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


/**
 *
 * @author FEDERICA
 */

public class GameView extends Canvas {
    private BufferStrategy strategy;
    private boolean gameRunning=true;
    private Room room = new Room();
    private RunnerEntity runner;
   
public GameView(){
    //System.out.println("hey");
   JFrame container= new JFrame("Loderunner");
    JPanel panel= (JPanel) container.getContentPane();
    panel.setPreferredSize(new Dimension(460,460));
    panel.setMinimumSize(new Dimension(460,460));
    panel.setLayout(null);
    setBounds(0,0,460,460);
    panel.add(this);
    setIgnoreRepaint(true);
    requestFocus();
    addKeyListener(new KeyHandle());
    container.pack();
    container.setVisible(true);
    container.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    createBufferStrategy(2);
    strategy=getBufferStrategy();
    loadLevel("level.txt");
}

public Room getRoomGame(){
    return room;
}

public void loadLevel (String ref){
    try{
       
       File file = new File(ref);
            Scanner load = new Scanner(file);
       for(int i=0;i<20;i++){
       for(int j=0;j<20;j++){
            int n= load.nextInt();
                 if(n==0) room.getList().add(new Block("tileset_green.png",j*23,i*23));
                 if(n==1) room.getList2().add(new Block("tileset_ladder.png",j*23,i*23));
            if(n==2) runner= new RunnerEntity("runner.png",j*23,i*23-13);//for now
                }
       }
    }catch(FileNotFoundException e){ System.out.println("Error");}
}

public void draw(Graphics2D g){
   room.draw(g);
   runner.draw(g);
}

public void move(){
    runner.move();
}

public void gameLoop(){
    while(true){
        Graphics2D g = (Graphics2D) strategy.getDrawGraphics();
        g.setColor(Color.WHITE);
        g.fillRect(0,0,460,460);
        move();
        draw(g);
        g.dispose();
        strategy.show();
        try{Thread.sleep(10); } catch (Exception e) {}
    }
}

public static void main (String args[]){
    GameView g = new GameView();
    g.gameLoop();
}

private class KeyHandle extends KeyAdapter {
   
    public void KeyPressed(KeyEvent e){
    System.out.println("hey");
    if(e.getKeyCode()==KeyEvent.VK_G){
        System.out.println("hey");
        runner.setRunning(true);
        runner.setDX(1);
    }
       
    }
   
    public void KeyReleased(KeyEvent e){
   
    if(e.getKeyCode()==KeyEvent.VK_LEFT){
        runner.setRunning(false);
        runner.setDX(0);
    }
    }
       
    }
   
}
Offline kevglass

JGO Kernel


Medals: 120
Projects: 23
Exp: 18 years


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #5 - Posted 2013-08-26 20:21:07 »

You have typos in your key listener:

1  
      public void KeyPressed(KeyEvent e) {


Should be:

1  
      public void keyPressed(KeyEvent e) {


Note the lower case "k"

Cheers,

Kev

Offline Beppe

Junior Member





« Reply #6 - Posted 2013-08-26 21:16:13 »

No it's ok ... I have find the problem the code work only if i click  into a windows... I don't know why...
Online SHC
« Reply #7 - Posted 2013-08-27 03:57:23 »

Try adding the same keylistener to both the container and the canvas.

1  
2  
3  
4  
KeyHandle handler = new KeyHandle();

this.addKeyListener(handler);
container.addKeyListener(handler);

Offline Beppe

Junior Member





« Reply #8 - Posted 2013-08-27 23:16:15 »

Yes now work .... Thanks at all ...
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.

pw (14 views)
2014-07-24 01:59:36

Riven (14 views)
2014-07-23 21:16:32

Riven (13 views)
2014-07-23 21:07:15

Riven (15 views)
2014-07-23 20:56:16

ctomni231 (43 views)
2014-07-18 06:55:21

Zero Volt (40 views)
2014-07-17 23:47:54

danieldean (32 views)
2014-07-17 23:41:23

MustardPeter (36 views)
2014-07-16 23:30:00

Cero (51 views)
2014-07-16 00:42:17

Riven (50 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!