witam, chce zaprogramować grę Sokoban, na tą chwile gra składa się z 3 klas, mam problem z implementacją klasy odpowiadającej za poruszanie się, chciałem się dowiedzieć czemu metoda addKeyListener zawierająca się w metodzie ruch() odpowiadająca za obsługę klawiatury nie jest wywoływana?? tworze referencje do tej klasy i w konstruktorze ją wywołuje ale ta metoda nie jest wywoływana. bardzo proszę o wskazanie błędu ;) i przepraszam za chaotyczne opisanie problemu. o to kod:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javaswingsokoban;
import javax.swing.SwingUtilities;
/**
*
* @author admin
*/
public class JavaSwingSokoban {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
ClassBoard classBoard = new ClassBoard();
}
});
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javaswingsokoban;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
*
* @author marcin kaszuba
*/
public class ClassBoard extends JFrame{
private int widthWindow = 660;
private int heightWindow = 500;
private int sizeSquare = 40;
private int positionX = 50;
private int positionY = 50;
JPanel jPanel = new JPanel();
ClassMove classMove = new ClassMove();
int defaultPositionX = 2;
int defaultPositionY = 6;
int tmp = 0;
//0 - mur czarny
//1 - puste pole bialy
//2 - pudelko zielony
//3 - ludzik czerwony
static int[][] plansza = {
{0,0,0,0,0,0,0,0,0,0,0,0,1,1},
{0,1,1,1,1,0,1,1,1,1,1,0,0,0},
{0,1,1,1,1,0,1,2,1,1,2,1,1,0},
{0,1,1,1,1,0,2,0,0,0,0,1,1,0},
{0,1,1,1,1,1,1,1,1,0,0,1,1,0},
{0,3,1,1,1,0,1,0,1,1,2,1,0,0},
{0,0,0,0,0,0,1,0,0,2,1,2,1,0},
{1,1,0,1,2,1,1,2,1,2,1,2,1,0},
{1,1,0,1,1,1,1,0,1,1,1,1,1,0},
{1,1,0,0,0,0,0,0,0,0,0,0,0,0}
};
public ClassBoard() {
setTitle("sokoban !!!!");
setLocation(new Point(100, 100));
setSize(new Dimension(widthWindow, heightWindow));
setDefaultCloseOperation(EXIT_ON_CLOSE);
setResizable(false);
setVisible(true);
add(jPanel);
revalidate();
classMove.ruch();
}
@Override
public void paint(Graphics graphics){
drawBoard(graphics);
}
void drawBoard(Graphics graphics){
super.paint(graphics);
for(int i = 0; i < plansza.length; ++i){
for(int j = 0; j < plansza[0].length; ++j){
if(plansza[i][j] == 0){
graphics.setColor(Color.BLACK);
graphics.fillRect((sizeSquare * j) + positionX, (sizeSquare * i) + positionY, sizeSquare, sizeSquare);
} else if(plansza[i][j] == 1){
graphics.setColor(Color.WHITE);
graphics.fillRect((sizeSquare * j) + positionX, (sizeSquare * i) + positionY, sizeSquare, sizeSquare);
} else if(plansza[i][j] == 2){
graphics.setColor(Color.GREEN);
graphics.fillRect((sizeSquare * j) + positionX, (sizeSquare * i) + positionY, sizeSquare, sizeSquare);
} else if(plansza[i][j] == 3){
graphics.setColor(Color.RED);
graphics.fillRect((sizeSquare * j) + positionX, (sizeSquare * i) + positionY, sizeSquare, sizeSquare);
}
}
}
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javaswingsokoban;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JPanel;
/**
*
* @author marcin
*/
public class ClassMove extends JPanel{
public void ruch(){
addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent keyEvent){
switch(keyEvent.getKeyCode()){
case KeyEvent.VK_UP:
System.out.println("wcisles up");
break;
case KeyEvent.VK_DOWN:
System.out.println("wcisles down");
break;
case KeyEvent.VK_LEFT:
System.out.println("wcisles left");
break;
case KeyEvent.VK_RIGHT:
System.out.println("wcisles right");
break;
}
}
});
}
}
pomyliłem się, sprawdziłem dokładniej i nie jest wywoływana metoda keyPressed w new KeyAdapter(), proszę o pomoc