Results 1 to 11 of 11

Thread: Java board game

  1. #1
    sumys is offline Member
    Join Date
    Mar 2012
    Posts
    8
    Rep Power
    0

    Default Java board game

    Hi,

    I am working on a board game which is a 8x8 grid with 5 images of different colours. The objects can swap as long as they are next to each other in a row or column. If the switch results 3 or more objects are same in a row or column, they will be removed. if the switch does not result in a match, the switch will not happen. when the object is removed, the board collapses so that the objects positioned above the removed one falls down to take its place and new one falls in to the top to fill in the spaces.
    I have started programming, but stuck at a point. here is the program I have written so far.
    I need help with the following questions:
    1. I have not implemented the dragging part yet. Since I first want to see when I move my mouse as a drag, does it give me the right location. But the mouse drag itself is not recognised when I drag the mouse from one location to another. Don't know why?
    2. I click at a grid, the mouse location is correct. The mouse pressed also give the right location but the mouse release doesnot give the right object that I am printing when the mouse is released at a different location. It still gives the same object as mouse presses. I am not able to understand why?
    3. Which is a good way to implement drag operation and falling down of the objects, Should I use layered pane?

    I will be grateful if some one help me . thanks
    Java Code:
    package com.games;
    
    	import java.awt.BorderLayout;
    	import java.awt.Color;
    	import java.awt.Component;
    	import java.awt.Container;
    	import java.awt.Dimension;
    	import java.awt.GridLayout;
    	import java.awt.Point;
    	import javax.swing.ImageIcon;
    	import javax.swing.JFrame;
    	import javax.swing.JLabel;
    	import javax.swing.JLayeredPane;
        import javax.swing.JPanel;
        import java.util.Random;
    import java.awt.event.*;
    
    	@SuppressWarnings("serial")
    	public class GameBoard extends JFrame  {
    	
    			String []arrPic = {"Blue.png","Green.png","Purple.png","Red.png","Yellow.png"};
    	    	String prefix = "C:/Users/Public/Documents/test/";
    	    	JLabel[][] gameArray=new JLabel[8][8];
    	    	JLabel gamepiece;
    	    	GameImage gImage=new GameImage();
    	    	GameImage[][] gimage=new GameImage[8][8];
    	    	GameListener listener;
    	    	GameImage gImagestart = new GameImage();
    	    	GameImage gImageend = new GameImage();
    	    	
    		 JPanel gameboard=new JPanel();
    		
    		 class GameImage {  
    			 
    			 int gridrow;  
    			 int gridcolumn; 
    			 JLabel boardimage; 
    			 public GameImage()
    			 {
    				 
    			 }
    			 public GameImage(int X, int Y, JLabel img) {  
    			 gridrow = X;  
    			 gridcolumn = Y;   
    			 boardimage = img;  
    			 }  
    			    
    			 public int getRow() {  
    			 return gridrow;  
    			 }
    			 public int getColumn() {  
    			 return gridcolumn;  
    				 }  
    			 public JLabel getImage(){
    				 return boardimage;  
    			 }
    		
    			 } 
    		 class GameListener implements MouseListener, MouseMotionListener {
    			    GameImage gimage;
    			    
    			 
    			    public GameListener(GameImage gimage) {
    			        this.gimage=gimage;
    			        
    			    }
    			 
    			    public void mouseMoved(MouseEvent e) {
    					System.out.println("mouse is moved");
    					  
    					  
    					  }
    					  public void mouseDragged(MouseEvent event) {
    					  System.out.println("mouse is dragged");
    					  }
    					  
    					  public void mouseClicked(MouseEvent e) {
    							System.out.println("The frame was clicked.");
    							 JLabel label = (JLabel)e.getSource();
    								
    							    System.out.println("label="+ label);  
    						//	 if (label.equals(gimage)) System.out.println("hurray");
    							// gImagestart=gimage;
    						   
    							// System.out.println(" clicked at " + gimage.getRow() + gimage.getColumn());
    							// System.out.println(" image is" + gimage.getImage());
    
    					
    						}
    
    						public void mouseEntered(MouseEvent e) {
    						//	System.out.println("The mouse entered the frame.");
    							
    						}
    
    						public void mouseExited(MouseEvent e) {
    						//	System.out.println("The mouse exited the frame.");
    							
    						}
    
    						public void mousePressed(MouseEvent e) {
    							
    							System.out.println("mouse is pressed");
    							//Component c =  gameboard.findComponentAt(e.getX(), e.getY());
    							//System.out.println("c="+ c);
    							 JLabel label = (JLabel)e.getSource();
    							
    						    System.out.println("label="+ label);   
    							// gImagestart=gimage;
    							// System.out.println(" clicked at " + gimage.getRow() + gimage.getColumn());
    							// System.out.println(" pressed at " + e.getX() + e.getY());
    							// System.out.println(" image is" + gimage.getImage());
    						}
    
    						public void mouseReleased(MouseEvent e) {
    							System.out.println("The left mouse button was released.");
    							 JLabel label = (JLabel)e.getSource();
    							
    						    System.out.println("label="+ label);  
    							//System.out.println(" released at " + gimage.getRow() + gimage.getColumn());
    							//System.out.println(" released at " + e.getX() + e.getY());
    			}
    		 }
    		 public GameBoard(){
    	    	
    	   
    	    getContentPane().add(gameboard);
    	    gameboard.setLayout(new GridLayout(8,8));
    	  
        	Random randomGenerator = new Random();
    	    	
    	    	int count=0;
    	    	int current = randomGenerator.nextInt(4);
    	    	for (int i=0;i<8;i++)
    	    		for (int j=0;j<8;j++)
    	    	{
    	    		int previous=current;
    	    	
    	    		System.out.println("Generated : " + current);
    	    		System.out.println("i="+i);
    	    		System.out.println("j="+j);
    	    		String images=prefix+arrPic[current];
    	    		gameArray[i][j]=new JLabel(new ImageIcon(images));
    	    		System.out.println("Generated path: " + images);
    	    		current = randomGenerator.nextInt(4);
    	    		gimage[i][j]=new GameImage(i,j,gameArray[i][j]);
    	    		
    	    		listener = new GameListener(gimage[i][j]);
    	    		gimage[i][j].getImage().addMouseListener(listener);
    	    		gameboard.add(gimage[i][j].getImage());
    	    		if( current == previous )
    	    		{
    	    		   count++;
    	    		   if (count==1){
    	    		     current = randomGenerator.nextInt(4);
    	    		     count=0;
    	    		               }
    	    		 }
    	    		       
    	    	}
    	    	
    	    		     
    	    	System.out.println("Done.");		       	    				
    		 }
    
    	    	
    		 
    	    		public static void main(String[] args) {
    	    			GameBoard frame = new GameBoard();
    	    			frame.setSize(500,500);
    	    	        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	    	        frame.pack();
    	    	        frame.setVisible(true);	    			
    	    		}
    	    	}
    Last edited by sumys; 03-15-2012 at 01:33 PM. Reason: added code tags

  2. #2
    sumys is offline Member
    Join Date
    Mar 2012
    Posts
    8
    Rep Power
    0

    Default Re: Java board game

    Just one more, the link I gave has given for the game midas miner seems to be printed some thing else. It is wrong. Please donit use that link.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,463
    Rep Power
    25

    Default Re: Java board game

    when I move my mouse as a drag, does it give me the right location.
    Can you post the output that shows what you mean and explain what is wrong with it.

    the mouse release doesnot give the right object that I am printing when the mouse is released at a different location.
    Same for this.

  4. #4
    sumys is offline Member
    Join Date
    Mar 2012
    Posts
    8
    Rep Power
    0

    Default Re: Java board game

    1. When I move the mouse as a drag, The print statement "mouse is dragged" is not printed. So I assume drag is not working
    public void mouseDragged(MouseEvent event) {
    System.out.println("mouse is dragged");}

    2. Mouse is pressed and released points to same object, even though I have pressed and released mouse in different locations

    mouse is pressed
    label=javax.swing.JLabel[,175,0,35x36,alignmentX=0.0,alignmentY=0.0,border= ,flags=8388608,maximumSize=,minimumSize=,preferred Size=,defaultIcon=C:/Users/Public/Documents/test/Blue.png,disabledIcon=,horizontalAlignment=CENTER, horizontalTextPosition=TRAILING,iconTextGap=4,labe lFor=,text=,verticalAlignment=CENTER,verticalTextP osition=CENTER]

    The left mouse button was released.
    label=javax.swing.JLabel[,175,0,35x36,alignmentX=0.0,alignmentY=0.0,border= ,flags=8388608,maximumSize=,minimumSize=,preferred Size=,defaultIcon=C:/Users/Public/Documents/test/Blue.png,disabledIcon=,horizontalAlignment=CENTER, horizontalTextPosition=TRAILING,iconTextGap=4,labe lFor=,text=,verticalAlignment=CENTER,verticalTextP osition=CENTER]

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,463
    Rep Power
    25

    Default Re: Java board game

    Where do you add the mouse motion listener?

    Print out the mouse event instead of the label for more debug information.
    Last edited by Norm; 03-14-2012 at 10:20 PM.

  6. #6
    sumys is offline Member
    Join Date
    Mar 2012
    Posts
    8
    Rep Power
    0

    Default Re: Java board game

    Hehe..I have not added mouse motion listener? Now that part is working.

    Still I am not able to understand why the mouse release is not pointing at the right location where I have released it.
    Mouse listener only recognizes where I have pressed it.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,463
    Rep Power
    25

    Default Re: Java board game

    Print out the mouse event instead of the label for more debug information.

    Read the API doc to see what to expect from the Event object for press and release.

  8. #8
    sumys is offline Member
    Join Date
    Mar 2012
    Posts
    8
    Rep Power
    0

    Default Re: Java board game

    I will look into that.. thanks
    I would also like know good or an easy way to implement drag operation and falling down of the objects, Should I use layered pane?

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,257
    Rep Power
    19

    Default Re: Java board game

    Don't ask the same question in more than o ne place. The new thread you started has been closed.

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

  10. #10
    sumys is offline Member
    Join Date
    Mar 2012
    Posts
    8
    Rep Power
    0

    Default Re: Java board game

    Sorry about that. I really need help to finish my assignment. It will be great if any one answers about a good way to implement drag ad drop of images from one grid to another and also to show the falling of images in the grid as I mentioned earliar. I have looked at use of layered pane in chessgame implementation. I am wondering if there is any other way suitable for my board game

  11. #11
    sumys is offline Member
    Join Date
    Mar 2012
    Posts
    8
    Rep Power
    0

    Default Re: Java board game

    Can any one give suggestions? Some ideas to drag and drop and also show the movement.

Similar Threads

  1. Board Game arrays?
    By linc186 in forum New To Java
    Replies: 1
    Last Post: 03-19-2011, 09:19 PM
  2. Replies: 0
    Last Post: 03-17-2011, 09:50 PM
  3. Help with JAVA board game
    By corky1501 in forum New To Java
    Replies: 5
    Last Post: 02-09-2011, 03:18 AM
  4. need help making a game board
    By Don k in forum New To Java
    Replies: 2
    Last Post: 04-30-2010, 12:09 AM
  5. game board design
    By tomitu in forum New To Java
    Replies: 12
    Last Post: 02-27-2010, 05:31 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
  •