Results 1 to 2 of 2
  1. #1
    Sokoman is offline Member
    Join Date
    Nov 2012
    Posts
    1
    Rep Power
    0

    Lightbulb Sokoban java - Help

    Hi guys,
    I'm creating a sokoban game for my java class and i need some help in few aspects.
    That is my board class where i create the objects in a gridlayout. The layout must be created using labels, when i execute the program the interface is created properly, the problem is the keylistener method, how can i make for exemple when i press the arrow up i get the position of the object man and move him for the cell above in the grid layout?
    i hope you can help me.
    tks Sokoman.


    package Sokoban.com;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;

    import javax.swing.*;

    class sokobanBoard extends JFrame implements KeyListener
    {
    private JLabel lblPos;
    int posX;
    int posY;
    int[][] Level1 = {
    {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
    {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
    {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
    {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
    {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
    {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
    {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
    {0,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0},
    {0,0,0,0,1,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0},
    {0,0,0,0,1,1,2,0,0,1,0,0,0,0,0,0,0,0,0,0},
    {0,0,1,1,1,1,0,0,2,1,1,0,0,0,0,0,0,0,0,0},
    {0,0,1,1,0,0,2,0,2,0,1,0,0,0,0,0,0,0,0,0},
    {1,1,1,1,0,1,0,1,1,0,1,1,1,1,1,1,1,1,1,1},
    {1,1,0,0,0,1,0,1,1,0,1,1,1,1,1,0,1,1,1,1},
    {1,1,0,2,0,0,2,0,0,0,0,0,0,0,0,0,1,1,1,1},
    {1,1,1,1,1,1,0,1,1,1,0,1,3,1,1,1,1,1,1,1},
    {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
    {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
    {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
    {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
    };

    public sokobanBoard()
    {
    this.setSize(400,400);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
    this.setLocation(100,350);
    this.setTitle("Sokoban V1.0");
    this.setLayout(null);
    CreateBoard();
    this.setVisible(true);
    }

    public void CreateBoard()
    {

    Wall wall;
    Area area;
    Cretes cretes;
    Man man;
    ImageIcon img;

    setLayout (new GridLayout(20, 20, 0, 0));

    for (int row = 0; row < 20; row ++)
    {
    for (int Column =0; Column < 20 ; Column++) {
    switch (Level1[row][Column] ) {
    case 0:
    area = new Area(row, Column);
    img = area.getImage();
    lblPos = new JLabel();
    lblPos.setSize(20, 20);
    lblPos.setIcon(img);
    add(lblPos);
    //lblPos.setVisible(true);
    break;
    case 1:
    wall = new Wall(row, Column);
    img = wall.getImage();
    lblPos = new JLabel();
    lblPos.setSize(20, 20);
    lblPos.setIcon(img);
    add(lblPos);

    break;
    case 2:
    cretes = new Cretes(row, Column);
    img = cretes.getImage();
    lblPos = new JLabel();
    lblPos.setSize(20, 20);
    lblPos.setIcon(img);
    this.addKeyListener(this);
    add(lblPos);

    //lblPos.setVisible(true);
    break;
    case 3:
    man = new Man(row, Column);
    img = man.getImage();
    lblPos = new JLabel();
    lblPos.setSize(20, 20);
    lblPos.setIcon(img);
    lblPos.addKeyListener(this);
    add(lblPos);
    posX = man.x();
    posY = man.y();


    //lblPos.setVisible(true);
    break;
    default:
    break;
    }
    }
    }
    }

    public void actionPerformed(ActionEvent e) {
    // TODO Auto-generated method stub

    }
    public void keyPressed(KeyEvent e) {
    if (e.getKeyCode() == KeyEvent.VK_UP ) {



    //Right arrow key code
    } else if (e.getKeyCode() == KeyEvent.VK_LEFT ) {

    //Left arrow key code
    } else if (e.getKeyCode() == KeyEvent.VK_UP ) {

    //Up arrow key code
    } else if (e.getKeyCode() == KeyEvent.VK_DOWN ) {
    //Down arrow key code
    }


    }



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

    }

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

    }


    }
    Last edited by Sokoman; 11-02-2012 at 04:15 PM.

  2. #2
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: Sokoban java - Help

    Hello and welcome! Please use [code][/code] tags when posting code so we can easily read it!

    Forum Rules
    Guide For New Members
    BB Code List - Java Programming Forum

    I see you have listener methods in place, but you don't do anything with them. I would keep track of where the player is at all times using array indexes, and from there, you can inspect what is in his surroundings by doing simple array access.

Similar Threads

  1. Sokoban java tutorial error- How can I fix it?
    By OurOhnlyHope in forum New To Java
    Replies: 3
    Last Post: 05-01-2012, 06:43 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •