witam, chce napisać grę Sokoban, zrobiłem plansze, zaimplementowałem obsługę klawiatury, mam problem z odświeżeniem planszy po przesunięciu kwadratu. Napisałem trzy klasy: JavaSwingSokoban w której jest robione okno, ClassBoard w która odpowiada za rysowanie planszy, i ClassMove która odpowiada za obsługę klawiatury. Jest opcja żeby użyć Timera i odświeżać co określony czas ale to bez sensu trochę, chcę żeby się odświeżało co kliknięcie odpowiedniego klawisza odpowiadającego za poruszanie. bardzo proszę o podpowiedzi i o uwagi dotyczące kodu, nie jest to projekt na jakieś zaliczenie, o to kod:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javaswingsokoban;
import java.awt.Dimension;
import java.awt.Point;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
/**
*
* @author marcin
*/
public class JavaSwingSokoban{
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JFrame jFrame = new JFrame();
int widthWindow = 660;
int heightWindow = 500;
jFrame.setTitle("sokoban !!");
jFrame.setLocation(new Point(100, 100));
jFrame.setSize(new Dimension(widthWindow, heightWindow));
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jFrame.setResizable(false);
jFrame.setVisible(true);
ClassBoard classBoard = new ClassBoard();
jFrame.add(classBoard);
}
});
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javaswingsokoban;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;
/**
*
* @author marcin
*/
public class ClassBoard extends JPanel{
ClassMove classMove = new ClassMove();
private int sizeSquare = 40;
private int positionX = 50;
private int positionY = 50;
//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() {
this.setFocusable(true);
addKeyListener(classMove);
}
@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.KeyEvent;
import java.awt.event.KeyListener;
/**
*
* @author marcin
*/
public class ClassMove implements KeyListener{
private int x = 5;
private int y = 1;
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent keyEvent) {
switch(keyEvent.getKeyCode()){
case KeyEvent.VK_UP:
if(ClassBoard.plansza[x - 1][y] == 1){
ClassBoard.plansza[x - 1][y] = 3;
ClassBoard.plansza[x][y] = 1;
x--;
}
System.out.println("wcisles up");
break;
case KeyEvent.VK_DOWN:
System.out.println("wcisles down");
if(ClassBoard.plansza[x + 1][y] == 1){
ClassBoard.plansza[x + 1][y] = 3;
ClassBoard.plansza[x][y] = 1;
x++;
}
break;
case KeyEvent.VK_LEFT:
System.out.println("wcisles left");
if(ClassBoard.plansza[x][y - 1] == 1){
ClassBoard.plansza[x][y - 1] = 3;
ClassBoard.plansza[x][y] = 1;
y--;
}
break;
case KeyEvent.VK_RIGHT:
System.out.println("wcisles right");
if(ClassBoard.plansza[x][y + 1] == 1){
ClassBoard.plansza[x][y + 1] = 3;
ClassBoard.plansza[x][y] = 1;
y++;
}
break;
}
}
@Override
public void keyReleased(KeyEvent e) {
}
}
[/code]