Results 1 to 2 of 2

Thread: Request

  1. #1
    duluf is offline Member
    Join Date
    Nov 2012
    Posts
    1
    Rep Power
    0

    Default Request

    Hi this is, i'm stack some in my java game coding known as sokoban,
    so i would like to get a help anyone with an idea, and any help will be appreciated. and this is my code for the game:



    Main.java

    package sokoban;

    public class Main {

    /**
    * @param args
    */
    @SuppressWarnings("unused")
    public static void main(String[] args) {
    MenuFrame frm = new MenuFrame();

    }

    }

    MenuFrame.java

    package sokoban;

    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;

    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;

    import sokobanEditor.EditorFrame;
    import sokobanGame.GameFrame;

    @SuppressWarnings("serial")
    public class MenuFrame extends JFrame implements ActionListener {

    JButton cmdgame = new JButton("GAME!");
    JButton cmdeditor = new JButton("EDITOR!");
    JPanel pan = new JPanel();

    public MenuFrame() {
    this.setTitle("MAIN MENU");
    this.setSize(412, 436);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
    this.setResizable(false);

    pan.add(cmdeditor);
    pan.add(cmdgame);

    cmdgame.addActionListener(this);
    cmdeditor.addActionListener(this);

    this.setContentPane(pan);

    this.setVisible(true);
    }
    @SuppressWarnings("unused")
    @Override
    public void actionPerformed(ActionEvent arg0) {
    if (arg0.getSource() == cmdgame) {
    GameFrame frm = new GameFrame();
    dispose();
    }
    else if (arg0.getSource() == cmdeditor) {
    EditorFrame frm = new EditorFrame();
    dispose();

    }

    }

    }

    EditorBoard.java

    package sokobanEditor;

    import java.awt.Frame;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Image;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.awt.event.MouseMotionListener;
    import java.awt.event.MouseWheelEvent;
    import java.awt.event.MouseWheelListener;
    import java.io.FileReader;
    import java.io.FileWriter;

    import javax.swing.ImageIcon;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;

    import sokoban.MenuFrame;

    @SuppressWarnings("serial")
    public class EditorBoard extends JPanel implements MouseListener, MouseMotionListener, MouseWheelListener, KeyListener {

    String Editor[][] = new String[12][12];
    String ImageSelect[] = {"HOLE", "BOX", "KEEPER", "WALL"};
    String CurrentImage = "WALL";
    int Mx, My;
    int indexInc = 0;
    Image Keeper;
    Image Wall;
    Image Box;
    Image Hole;
    FileWriter fw;
    FileReader fr;
    Frame Eframe;

    public EditorBoard(Frame ef) {
    ImageIcon iKeeper = new ImageIcon("Images/keeperdown.gif");
    Keeper = iKeeper.getImage();

    ImageIcon iBox = new ImageIcon("Images/box.jpg");
    Box = iBox.getImage();

    ImageIcon iWall = new ImageIcon("Images/wall.jpg");
    Wall = iWall.getImage();

    ImageIcon iHole = new ImageIcon("Images/hole.png");
    Hole = iHole.getImage();

    Eframe = ef;

    setFocusable(true);

    addMouseListener(this);
    addMouseMotionListener(this);
    addMouseWheelListener(this);
    addKeyListener(this);
    }

    public void paint(Graphics g) {
    super.paint(g);
    Graphics2D g2d = (Graphics2D) g;

    for (int i = 0; i <= 11 ; i++) {
    for (int j = 0; j <= 11 ; j++) {
    if (Editor[i][j] == "WALL") {
    g2d.drawImage(Wall, i * 34, j * 34, null);
    }
    if (Editor[i][j] == "KEEPER") {
    g2d.drawImage(Keeper, i * 34, j * 34, null);
    }
    if (Editor[i][j] == "BOX") {
    g2d.drawImage(Box, i * 34, j * 34, null);
    }
    if (Editor[i][j] == "HOLE") {
    g2d.drawImage(Hole, i * 34, j * 34, null);
    }

    }
    }
    if (CurrentImage == "WALL") {
    g2d.drawImage(Wall, Mx, My, null);
    }
    else if (CurrentImage == "BOX") {
    g2d.drawImage(Box, Mx, My, null);
    }
    else if (CurrentImage == "KEEPER") {
    g2d.drawImage(Keeper, Mx, My, null);
    }
    else if (CurrentImage == "HOLE") {
    g2d.drawImage(Hole, Mx, My, null);
    }
    }

    @Override
    public void mouseDragged(MouseEvent arg0) {


    }

    @Override
    public void mouseMoved(MouseEvent arg0) {
    Mx = arg0.getX() - 17;
    My = arg0.getY() - 17;

    repaint();


    }

    @Override
    public void mouseClicked(MouseEvent arg0) {


    }

    @Override
    public void mouseEntered(MouseEvent arg0) {


    }

    @Override
    public void mouseExited(MouseEvent arg0) {


    }

    @Override
    public void mousePressed(MouseEvent arg0) {


    }

    @Override
    public void mouseReleased(MouseEvent arg0) {
    int x = arg0.getX() / 34;
    int y = arg0.getY() / 34;

    if (arg0.getButton() == MouseEvent.BUTTON1) {
    Editor[x][y] = CurrentImage;

    }
    else if (arg0.getButton() == MouseEvent.BUTTON3) {
    Editor[x][y] = null;
    }
    repaint();


    }

    @Override
    public void keyPressed(KeyEvent arg0) {
    }

    @SuppressWarnings("unused")
    @Override
    public void keyReleased(KeyEvent arg0) {
    char key = arg0.getKeyChar();

    if (key == 's') {
    try {
    fw = new FileWriter (JOptionPane.showInputDialog(null, "Enter the backup path:", "LEVEL CREATOR", JOptionPane.QUESTION_MESSAGE));
    for (int i = 0; i < 12; i++) {
    for (int j = 0; j < 12; j++) {
    if (Editor[j][i] == "WALL") {
    fw.write("0");
    }
    else if (Editor[j][i] == "KEEPER") {
    fw.write("1");
    }
    else if (Editor[j][i] == "BOX") {
    fw.write("2");
    }
    else if (Editor[j][i] == "HOLE") {
    fw.write("3");
    }
    else if (Editor[j][i] == null) {
    fw.write(" ");
    }
    }
    fw.write("\r\n");
    }
    fw.close();
    }
    catch(Exception ex){}
    }
    else if (key == 'l') {
    try {
    fr = new FileReader (JOptionPane.showInputDialog(null, "Enter the path to reading:", "LEVEL CREATOR", JOptionPane.QUESTION_MESSAGE));
    int i = 0;
    int x = 0, y = 0;

    while ((i = fr.read()) != -1) {
    char strImg = (char) i;

    if (strImg == '0') {
    Editor[x][y] = "WALL";
    }
    else if (strImg == '1') {
    Editor[x][y] = "KEEPER";
    }
    else if (strImg == '2') {
    Editor[x][y] = "BOX";
    }
    else if (strImg == '3') {
    Editor[x][y] = "HOLE";
    }
    else if (strImg == ' ') {
    Editor[x][y] = null;
    }
    else if (strImg == '\r' || strImg == '\n') {
    x--;
    }
    if(x == 11) {
    y++;
    x = 0;
    }
    else{
    x++;
    }
    }
    }
    catch (Exception ex) {}
    }
    else if (key == KeyEvent.VK_ESCAPE) {
    MenuFrame frm = new MenuFrame();
    Eframe.dispose();
    }
    }
    @Override
    public void keyTyped(KeyEvent arg0) {


    }

    @Override
    public void mouseWheelMoved(MouseWheelEvent arg0) {
    int rot = arg0.getWheelRotation();
    if (rot < 0) {
    if(indexInc > 0) {
    indexInc--;
    }
    }
    else if (rot > 0) {
    if (indexInc < 3) {
    indexInc++;
    }
    }
    CurrentImage = ImageSelect[indexInc];

    repaint();

    }

    }

    EditorFrame.java

    package sokobanEditor;

    import javax.swing.JFrame;

    @SuppressWarnings("serial")
    public class EditorFrame extends JFrame {
    public EditorFrame() {
    this.setTitle("LEVEL CREATOR");
    this.setSize(412,436);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
    this.add(new EditorBoard(this));
    this.setResizable(false);
    this.setVisible(true);
    }

    }

    Box.java

    package sokobanGame;

    import java.awt.Image;
    import java.awt.Rectangle;

    import javax.swing.ImageIcon;

    public class Box {

    @SuppressWarnings("unused")
    private static final boolean State = false;
    int x,y;
    String state = "PAIR";
    Image Box;
    ImageIcon iBoxPair = new ImageIcon("Images/box.jpg");
    ImageIcon iBoxHole = new ImageIcon("Images/boxonhole.jpg");
    boolean justbox = false;

    public Box(int Startx, int Starty) {
    x = Startx;
    y = Starty;

    ImageIcon iBox = new ImageIcon ("Images/box.jpg");
    Box = iBox.getImage();

    }

    public Rectangle getBounds() {
    Rectangle Box = new Rectangle(x,y, 34,34);
    return Box;
    }
    public int getX() {
    return x;
    }

    public int getY() {
    return y;
    }

    public void setX(int newX) {
    this.x = newX;
    }

    public void setY(int newY) {
    this.y = newY;
    }
    public boolean Justget() {
    return justbox;
    }

    public void Justset(boolean Justnew) {
    this.justbox = Justnew;
    }
    public void setState (String newState) {
    this.state = newState;
    }
    public Image getImage() {
    if (state == "PAIR") {
    Box = iBoxPair.getImage();
    }
    else if(state == "HOLE") {
    Box = iBoxHole.getImage();
    }
    return Box;
    }
    }

    GameBoard.java

    package sokobanGame;

    import java.awt.Color;
    import java.awt.Font;
    import java.awt.Frame;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Rectangle;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.io.FileReader;
    import java.util.ArrayList;

    import javax.swing.JPanel;

    import sokoban.MenuFrame;

    @SuppressWarnings("serial")
    public class GameBoard extends JPanel implements KeyListener{

    String Game[][] = new String[12][12];
    int level = 1;
    private static ArrayList<Wall> Walls;
    private static ArrayList<Hole> Holes;
    private static ArrayList<Box> Boxes;
    Wall wall;
    Keeper keeper;
    Hole hole;
    Box box;
    Font levelFont = new Font("SansSerif", Font.BOLD, 30);
    FileReader fr;
    Frame Gframe;
    public GameBoard(Frame gf) {
    LoadLevel();


    Gframe = gf;

    setFocusable(true);
    addKeyListener(this);
    }

    public void LoadLevel() {
    try {
    fr = new FileReader("Maps/level" + level + ".lvl");
    int x = 0, y = 0, i = 0;

    Walls = new ArrayList<Wall>();
    Boxes = new ArrayList<Box>();
    Holes = new ArrayList<Hole>();

    while ((i = fr.read()) != -1);
    char strImg = (char) i;

    if (strImg == '0') {
    Game[x][y] = "WALL";
    wall = new Wall(x * 34, y * 34);
    Walls.add(wall);
    }
    else if (strImg == '1') {
    Game [x][y] = "KEEPER";
    keeper = new Keeper (x * 34, y * 34);

    }
    else if (strImg == '2') {
    Game [x][y] = "BOX";
    box = new Box (x * 34, y * 34);
    Boxes.add(box);
    }
    else if (strImg == '3') {
    Game [x][y] = "HOLE";
    hole = new Hole (x * 34, y * 34);
    Holes.add(hole);
    }
    else if (strImg == ' ') {
    Game [x][y] = null;
    }
    else if (strImg == '\r' || strImg == '\n') {
    x--;
    }
    if (x==11) {
    y++;
    x = 0;
    }
    else {
    x++;
    }

    }
    catch(Exception ex) {}
    repaint();
    }
    public void paint(Graphics g) {
    super.paint(g);
    Graphics2D g2d = (Graphics2D) g;

    for (int i = 0; i < Walls.size(); i++) {
    wall = (Wall) Walls.get(i);
    g2d.drawImage (wall.getImage(), wall.getX(), wall.getY(), null);
    }

    for (int i = 0; i < Holes.size(); i++) {
    hole = (Hole) Holes.get(i);
    g2d.drawImage (hole.getImage(), hole.getX(), hole.getY(), null);
    }

    for (int i = 0; i < Boxes.size(); i++) {
    box = (Box) Boxes.get(i);
    g2d.drawImage (box.getImage(), box.getX(), box.getY(), null);
    }

    try {
    g2d.drawImage(keeper.getImage(), keeper.getX(), keeper.getY(), null);
    }
    catch(Exception ex) {}

    g.setColor(Color.BLACK);
    g.setFont(levelFont);
    g.drawString("LEVEL : " + level, 10, 25);
    }
    @Override
    public void keyPressed(KeyEvent arg0) {
    }

    @SuppressWarnings("unused")
    @Override
    public void keyReleased(KeyEvent arg0) {
    int Key = arg0.getKeyCode();

    if(Key == KeyEvent.VK_DOWN) {
    keeper.setDir("BOTTOM");
    keeper.Move();
    CheckCollision();
    }
    else if (Key == KeyEvent.VK_UP) {
    keeper.setDir("TOP");
    keeper.Move();
    CheckCollision();
    }
    else if (Key == KeyEvent.VK_LEFT) {
    keeper.setDir("LEFT");
    keeper.Move();
    CheckCollision();
    }
    else if (Key == KeyEvent.VK_RIGHT) {
    keeper.setDir("RIGHT");
    keeper.Move();
    CheckCollision();
    }
    else if (Key == KeyEvent.VK_R) {
    LoadLevel();
    }
    else if(Key == KeyEvent.VK_ESCAPE) {
    MenuFrame frm = new MenuFrame();
    Gframe.dispose();
    }
    repaint();
    VerifierLevelFinished();
    }



    @Override
    public void keyTyped(KeyEvent arg0) {

    }
    public void VerifierLevelFinished() {
    int nbJustBox = 0;

    for (int i = 0; i < Boxes.size(); i++) {
    box = (Box) Boxes.get(i);
    if(box.state == "HOLE") {
    nbJustBox++;
    }

    if (nbJustBox == Boxes.size()) {
    level++;
    LoadLevel();
    }
    }
    }

    public void CheckCollision() {
    Rectangle keeperRec;
    keeperRec = keeper.getBounds();

    for (int i = 0; i < Walls.size(); i++) {
    wall = (Wall) Walls.get(i);
    Rectangle wallRec = wall.getBounds();

    if (keeperRec.intersects(wallRec)) {
    if (keeper.getDir() == "BOTTOM") {
    keeper.setY(keeper.getY() -34);
    }
    else if (keeper.getDir() == "TOP") {
    keeper.setY(keeper.getY() + 34);
    }
    else if (keeper.getDir() == "LEFT") {
    keeper.setX(keeper.getX() + 34);
    }
    else if (keeper.getDir() == "RIGHT") {
    keeper.setX(keeper.getX() - 34);
    }
    }
    }
    for(int i = 0; i < Boxes.size(); i++) {
    box = (Box) Boxes.get(i) ;
    Rectangle boxRec = box.getBounds();

    if (keeperRec.intersects(boxRec)) {
    if (keeper.getDir() == "BOTTOM") {
    if(Game[boxRec.x / 34][(boxRec.y + 34) / 34] != "WALL") {
    box.setY(box.getY() + 34);
    }
    else if(Game[boxRec.x / 34][(boxRec.y + 34) / 34] == "WALL") {
    keeper.setY(keeper.getY() - 34);
    }

    }

    else if(keeper.getDir() == "TOP") {
    if(Game[boxRec.x / 34][(boxRec.y -34) / 34] != "WALL") {
    box.setY(box.getY() - 34);
    }
    else if(Game[boxRec.x / 34][(boxRec.y - 34) / 34] == "WALL") {

    keeper.setY(keeper.getY() +34);

    }
    }

    else if(keeper.getDir() == "LEFT") {
    if(Game[(boxRec.x + 34) / 34][boxRec.y / 34] != "WALL") {
    box.setX(box.getX() +34);
    }
    else if(Game[(boxRec.x + 34) / 34][boxRec.y / 34] == "WALL") {

    keeper.setX(keeper.getX() -34);
    }
    }

    else if(keeper.getDir() == "RIGHT") {
    if(Game[(boxRec.x - 34) / 34][boxRec.y / 34] != "WALL") {
    box.setX(box.getX() - 34);
    }
    else if(Game[(boxRec.x - 34) / 34][boxRec.y / 34] == "WALL") {

    keeper.setX(keeper.getX() + 34);

    }
    }
    }
    }
    for (int i = 0; i < Holes.size(); i++) {
    hole = (Hole) Holes.get(i);
    Rectangle holeRec = hole.getBounds();
    for (int j = 0; j < Boxes.size(); j++) {
    box = (Box) Boxes.get(i);
    Rectangle boxRec = box.getBounds();

    if(boxRec.intersects(holeRec) && !box.Justget() && !hole.getOver()) {
    box.setState("HOLE");
    box.Justset(true);
    hole.setOver(true);
    }
    else if(!boxRec.intersects(holeRec) && !box.Justget() && !hole.getOver()) {
    box.setState("PAIR");
    box.Justset(false);
    hole.setOver(false);
    }
    }
    }
    }
    }

    GameFrame.java

    package sokobanGame;

    import javax.swing.JFrame;

    @SuppressWarnings("serial")
    public class GameFrame extends JFrame {

    public GameFrame () {
    this.setTitle("MY SOKOBAN");
    this.setSize(412,436);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
    this.add(new GameBoard(this));
    this.setResizable(false);
    this.setVisible(true);

    }

    }

    Hole.java

    package sokobanGame;

    import java.awt.Image;
    import java.awt.Rectangle;

    import javax.swing.ImageIcon;

    public class Hole {

    int x,y;
    Image Hole;
    boolean boxOver = false;

    public Hole(int Startx, int Starty) {
    x = Startx;
    y = Starty;
    ImageIcon iHole = new ImageIcon("Images/hole.png");
    Hole = iHole.getImage();
    }

    public Rectangle getBounds() {
    Rectangle Box = new Rectangle(x, y, 34, 34);
    return Box;
    }

    public boolean getOver() {
    return getOver();
    }

    public void setOver(boolean newOver) {
    this.boxOver = newOver;
    }

    public int getX() {
    return x;
    }

    public int getY() {
    return y;
    }

    public void setX(int newX) {
    this.x = newX;
    }

    public void setY(int newY) {
    this.y = newY;
    }

    public Image getImage() {
    return Hole;
    }
    }

    Keeper.java

    package sokobanGame;

    import java.awt.Image;
    import java.awt.Rectangle;

    import javax.swing.ImageIcon;

    public class Keeper {

    int x,y;
    String KeeperDir = "BOTTOM";
    Image Keeper;
    ImageIcon iKeeperDown = new ImageIcon("Images/keeperdown.gif");
    ImageIcon iKeeperUp = new ImageIcon("Images/keeperup.gif");
    ImageIcon iKeeperLeft = new ImageIcon("Images/keeperleft.gif");
    ImageIcon iKeeperRight = new ImageIcon("Images/keeperright.gif");

    public Keeper (int Startx, int Starty) {
    x = Startx;
    y = Starty;
    }


    public Rectangle getBounds() {
    Rectangle Box = new Rectangle(x,y, 34,34);
    return Box;
    }
    public int getX() {
    return x;
    }

    public int getY() {
    return y;
    }
    public String getDir() {
    return KeeperDir;
    }

    public void setX(int newX) {
    this.x = newX;
    }

    public void setY(int newY) {
    this.y = newY;
    }

    public void setDir(String newDir) {
    this.KeeperDir = newDir;
    }

    public Image getImage() {
    if(KeeperDir == "BOTTOM") {
    Keeper = iKeeperDown.getImage();

    }
    else if(KeeperDir == "TOP") {
    Keeper = iKeeperUp.getImage();

    }

    else if(KeeperDir == "LEFT") {
    Keeper = iKeeperLeft.getImage();
    }

    else if(KeeperDir == "RIGHT") {
    Keeper = iKeeperRight.getImage();

    }
    return Keeper;
    }

    public void Move() {
    if(KeeperDir == "BOTTOM") {
    this.y += 34;
    }
    else if(KeeperDir == "RIGHT") {
    this.x += 34;
    }
    else if(KeeperDir == "LEFT") {
    this.x -= 34;
    }
    else if(KeeperDir == "TOP") {
    this.y -= 34;
    }

    }
    }

    Wall

    package sokobanGame;

    import java.awt.Image;
    import java.awt.Rectangle;

    import javax.swing.ImageIcon;

    public class Wall {
    int x,y;
    Image Wall;
    boolean wallAbove = false;

    public Wall(int Startx, int Starty) {
    x = Startx;
    y = Starty;

    ImageIcon iWall = new ImageIcon("Images/wall.jpg");
    Wall = iWall.getImage();
    }

    public Rectangle getBounds() {
    Rectangle Box = new Rectangle(x, y, 34, 34);
    return Box;
    }
    public int getX() {
    return x;
    }

    public int getY() {
    return y;
    }
    public Image getImage() {
    return Wall;
    }

    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default Re: Request

    Moved from a staff-only section.

    Necessary reading: Forum Rules -- particularly the third paragraph.
    Guide For New Members
    BB Code List - Java Programming Forum

    Recommended reading: SSCCE (Short, Self Contained, Correct (Compilable), Example)
    How to ask questions the smart way

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 2
    Last Post: 03-29-2012, 10:32 AM
  2. Get request
    By Lacrim in forum Networking
    Replies: 0
    Last Post: 12-16-2011, 11:38 AM
  3. Replies: 0
    Last Post: 07-06-2011, 01:12 PM
  4. Run JAR from web request
    By benirose in forum New To Java
    Replies: 0
    Last Post: 06-01-2011, 06:02 PM
  5. Http request
    By arthik_babu in forum Advanced Java
    Replies: 2
    Last Post: 06-29-2009, 12:27 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
  •