Results 1 to 2 of 2
  1. #1
    Paul_Boland is offline Member
    Join Date
    Jan 2013
    Posts
    1
    Rep Power
    0

    Question ChessMate GUI Program Issues.

    Hi Folks.

    I am studying Java at college and am really enjoying it. I have a project to do and I picked to create a Chess program. Not a program that plays Chess, but one that records moves in a Chess game. I have the GUI set up and it looks great, see attached screenshot (can't seem to attach a screenshot for some reason?!?). But now I'm stuck...

    When ChessMate starts, it displays a splash screen for five seconds and then the board. The Quit button works perfectly. But the New button... When clicked, the board array is reset to defaults for a new game and I then repaint() the screen. But for some reason, when I do this, the splash screen comes back up and stays up. Move the mouse across the top of the screen causes a number of New and Quit buttons to appear!?!

    I don't know what is causing this and was wondering if someone here can help me out please. There are three classes in my program, I'm including the code below.

    ChessMate.java
    Java Code:
    //ChessMate
    //by Paul Boland (00815667)
    import javax.swing.*;  //Import GUI Javax Swing library.
    //import java.awt.*;  //Import graphic controls from the AWT library.
    //import java.awt.image.BufferedImage;  //Import controls for loading images.
    //import java.io.*;  //Import general file controls.
    //import javax.imageio.*;  //Import image input/output controls.
    public class ChessMate
    {
    	//Declare two 2D arrays for keeping track of what's going on on the board.
    	public static int boardSquares[][]=new int[8][8];  //Declares a 2D array of board squares, 8x8.
    	public static int boardPieces[][]=new int[8][8];  //Declares a 2D array of board pieces, 8x8.
    	//Establish the window, accessible throughout the whole program.
    	public static JFrame f=new JFrame("ChessMate");
    	//Main.
    	public static void main(String[] args)
    	{
    		//Call method CreateBoard to create the game board.
    		CreateBoard();
    		//Call method SetPieces to set the pieces to their starting poitions for a new game.
    		SetPieces();
    		//Call the GUI.
    		TheSplashScreen();  //Call the TheSplashScreen method.
    		//Delay for five seconds.
    		Delay(5000);
    		TheMainScreen();  //Call the The MainScreen method.
    	}
    	//Delay method.
    	//The int 'dt' passed into the method is the delay time in 1000th's of a second.
    	//So to delay for 1 second you would pass in 1000 to dt.
    	public static void Delay(int dt)
    	{
    		try
            {
                Thread.currentThread().sleep(dt);
            }
            catch(Exception e)
            {
                System.out.println("Error!!  "+e);
            }
    	}
    	//CreateBoard method.
    	public static void CreateBoard()
    	{
    		//Declare variables.
    		int x;  //For moving through horizontal tiles.
    		int y;  //For moving through vertical tiles.
    		int remainderX, remainderY;  //Used in calculating tile offsets per row.
    		int currentTile;  //Used to determine what the tile is.
    		//Set up the board tiles.  1 = white.  2 = black.
    		//Use a nested For loop to move through the tiles.
    		//First we move through vertical Y columns.
    		for(y=0;y<8;y++)
    		{
    			//Calculate the Y remainder.
    			remainderY=y%2;  //Remainder is 0 or 1.
    			//Second we move through the horizontal X rows.
    			for(x=0;x<8;x++)
    			{
    				//Calculate the X remainder.
    				remainderX=x%2;  //Remainder is 0 or 1.
    				//To determine the current tile we are on we add both remainders together.
    				currentTile=remainderX+remainderY;
    				//If currentTile is 0, set it to 2.
    				if(currentTile==0)
    				{
    					currentTile=2;
    				}
    				//Set the tile to currentTile.
    				boardSquares[x][y]=currentTile;
    			}
    		}
    	}
    	//SetPieces method.
    	public static void SetPieces()
    	{
    		//Declare variables.
    		int x;  //For moving through horizontal tiles.
    		int y;  //For moving through vertical tiles.
    		//Piece values:
    		//10 = White Pawn.  20 = Black Pawn.
    		//11 = White Rook.  21 = Black Rook.
    		//12 = White Knight.  22 = Black Knight.
    		//13 = White Bishop.  23 = Black Bishop.
    		//14 = White Queen.  24 = Black Queen.
    		//15 = White King.  25 = Black King.
    		//0 = blank square, nothing on this tile.
    		//Pawns.
    		for(x=0;x<8;x++)
    		{
    			boardPieces[x][1]=20;
    		}
    		for(x=0;x<8;x++)
    		{
    			boardPieces[x][6]=10;
    		}
    		//Rooks.
    		boardPieces[0][0]=21;
    		boardPieces[7][0]=21;
    		boardPieces[0][7]=11;
    		boardPieces[7][7]=11;
    		//Knights.
    		boardPieces[1][0]=22;
    		boardPieces[6][0]=22;
    		boardPieces[1][7]=12;
    		boardPieces[6][7]=12;
    		//Bishops.
    		boardPieces[2][0]=23;
    		boardPieces[5][0]=23;
    		boardPieces[2][7]=13;
    		boardPieces[5][7]=13;
    		//Queens.
    		boardPieces[3][0]=24;
    		boardPieces[3][7]=14;
    		//Kings.
    		boardPieces[4][0]=25;
    		boardPieces[4][7]=15;
    		//Blank sqaures.
    		for(y=2;y<6;y++)  //For moving down through the four rows in the centre of the board.
    		{
    			for(x=0;x<8;x++)  //For moving across the rows.
    			{
    				boardPieces[x][y]=0;
    			}
    		}	
    	}
    	//SplashScreen method.
    	public static void TheSplashScreen()
    	{
    		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  //Set default action for when the windows closes.
    		SplashScreen s=new SplashScreen();  //Creating an instant of the splash screen.
    		f.add(s);  //Add the splash screen to the window.
    		f.setSize(640, 480);  //Set the window size.
    		f.setVisible(true);  //Make the window appear.
    		
    	}
    	public static void TheMainScreen()
    	{
    		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  //Set default action for when the windows closes.
    		MainScreen m=new MainScreen();  //Creating an instant of the main screen.
    		f.add(m);  //Add the main screen to the window.
    		f.invalidate();  //Invalidate the old screen.
    		f.repaint();  //Repaint the screen.
    		f.validate();  //Validate the new screen.
    	}
    }
    SplashScreen.java
    Java Code:
    //SplashScreen
    //by Paul Boland (00815667)
    import java.awt.*;
    import java.awt.image.BufferedImage;
    import javax.swing.*;
    import java.io.*;
    import javax.imageio.*;
    public class SplashScreen extends JPanel
    {
    	public void paintComponent(Graphics g)
    	{
    		//Load default paint components.
    		super.paintComponent(g);
    		//Set the backgroun colour of the window.
    		this.setBackground(Color.WHITE);
    		//Load image.
    		BufferedImage ss=null;
    		try
    		{
    			ss=ImageIO.read(new File("Images\\ChessTitle.jpg"));
    		}
    		catch (IOException e)
    		{
    			System.out.println("Error!  "+e);
    		}
    		//Display image centred horizontally at the top.
    		g.drawImage(ss, (640-ss.getWidth())/2, 0, null);
    	}
    }
    MainScreen.java
    Java Code:
    //MainScreen
    //by Paul Boland (00815667)
    import java.awt.*;
    import java.awt.image.BufferedImage;
    import javax.swing.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.*;
    import javax.imageio.*;
    public class MainScreen extends JPanel
    {
    	public void paintComponent(Graphics g)
    	{
    		//Load default paint components.
    		super.paintComponent(g);
    		//Set the backgroun colour of the window.
    		this.setBackground(Color.WHITE);
    		//Declare variables.
    		int x, y;  //For moving across and down the Chess board.
    		//Load board tiles images.
    		//Black Tile.
    		BufferedImage tb=null;
    		try
    		{
    			tb=ImageIO.read(new File("Images\\TileBlack.jpg"));
    		}
    		catch (IOException e)
    		{
    			System.out.println("Error!  "+e);
    		}
    		//White Tile.
    		BufferedImage tw=null;
    		try
    		{
    			tw=ImageIO.read(new File("Images\\TileWhite.jpg"));
    		}
    		catch (IOException e)
    		{
    			System.out.println("Error!  "+e);
    		}
    		//Green Tile.
    		BufferedImage tg=null;
    		try
    		{
    			tg=ImageIO.read(new File("Images\\TileGreen.jpg"));
    		}
    		catch (IOException e)
    		{
    			System.out.println("Error!  "+e);
    		}
    		//Now draw the board.
    		for(y=0;y<8;y++)  //Move vertically from top to bottom.
    		{
    			for(x=0;x<8;x++)  //Move horizontally left to right.
    			{
    				if(ChessMate.boardSquares[x][y]==1)
    				{
    					//Draw a white tile.
    					g.drawImage(tw, x*50, y*50, null);
    				}
    				else if(ChessMate.boardSquares[x][y]==2)
    				{
    					//Draw a black tile.
    					g.drawImage(tb, x*50, y*50, null);
    				}
    				else
    				{
    					//Draw a green tile.
    					g.drawImage(tg, x*50, y*50, null);
    				}
    			}
    		}
    		//Load Chess pieces images.
    		//Pawn.
    		BufferedImage Pwp=null;
    		try
    		{
    			Pwp=ImageIO.read(new File("Images\\WhitePawn.png"));
    		}
    		catch (IOException e)
    		{
    			System.out.println("Error!  "+e);
    		}
    		BufferedImage Pbp=null;
    		try
    		{
    			Pbp=ImageIO.read(new File("Images\\BlackPawn.png"));
    		}
    		catch (IOException e)
    		{
    			System.out.println("Error!  "+e);
    		}
    		//Rook.
    		BufferedImage Pwr=null;
    		try
    		{
    			Pwr=ImageIO.read(new File("Images\\WhiteRook.png"));
    		}
    		catch (IOException e)
    		{
    			System.out.println("Error!  "+e);
    		}
    		BufferedImage Pbr=null;
    		try
    		{
    			Pbr=ImageIO.read(new File("Images\\BlackRook.png"));
    		}
    		catch (IOException e)
    		{
    			System.out.println("Error!  "+e);
    		}
    		//Knight.
    		BufferedImage Pwkn=null;
    		try
    		{
    			Pwkn=ImageIO.read(new File("Images\\WhiteKnight.png"));
    		}
    		catch (IOException e)
    		{
    			System.out.println("Error!  "+e);
    		}
    		BufferedImage Pbkn=null;
    		try
    		{
    			Pbkn=ImageIO.read(new File("Images\\BlackKnight.png"));
    		}
    		catch (IOException e)
    		{
    			System.out.println("Error!  "+e);
    		}
    		//Bishop.
    		BufferedImage Pwb=null;
    		try
    		{
    			Pwb=ImageIO.read(new File("Images\\WhiteBishop.png"));
    		}
    		catch (IOException e)
    		{
    			System.out.println("Error!  "+e);
    		}
    		BufferedImage Pbb=null;
    		try
    		{
    			Pbb=ImageIO.read(new File("Images\\BlackBishop.png"));
    		}
    		catch (IOException e)
    		{
    			System.out.println("Error!  "+e);
    		}
    		//Queen.
    		BufferedImage Pwq=null;
    		try
    		{
    			Pwq=ImageIO.read(new File("Images\\WhiteQueen.png"));
    		}
    		catch (IOException e)
    		{
    			System.out.println("Error!  "+e);
    		}
    		BufferedImage Pbq=null;
    		try
    		{
    			Pbq=ImageIO.read(new File("Images\\BlackQueen.png"));
    		}
    		catch (IOException e)
    		{
    			System.out.println("Error!  "+e);
    		}
    		//King.
    		BufferedImage Pwk=null;
    		try
    		{
    			Pwk=ImageIO.read(new File("Images\\WhiteKing.png"));
    		}
    		catch (IOException e)
    		{
    			System.out.println("Error!  "+e);
    		}
    		BufferedImage Pbk=null;
    		try
    		{
    			Pbk=ImageIO.read(new File("Images\\BlackKing.png"));
    		}
    		catch (IOException e)
    		{
    			System.out.println("Error!  "+e);
    		}
    		//Now draw the pieces.
    		//Piece values:
    		//10 = White Pawn.  20 = Black Pawn.
    		//11 = White Rook.  21 = Black Rook.
    		//12 = White Knight.  22 = Black Knight.
    		//13 = White Bishop.  23 = Black Bishop.
    		//14 = White Queen.  24 = Black Queen.
    		//15 = White King.  25 = Black King.
    		//0 = blank square, nothing on this tile.
    		for(y=0;y<8;y++)  //Move vertically from top to bottom.
    		{
    			for(x=0;x<8;x++)  //Move horizontally left to right.
    			{
    				if(ChessMate.boardPieces[x][y]==10)
    				{
    					//White Pawn.
    					g.drawImage(Pwp, (x*50)+((50-Pwp.getWidth())/2), (y*50)+((50-Pwp.getHeight())/2), null);
    				}
    				else if(ChessMate.boardPieces[x][y]==20)
    				{
    					//Black Pawn.
    					g.drawImage(Pbp, (x*50)+((50-Pbp.getWidth())/2), (y*50)+((50-Pbp.getHeight())/2), null);
    				}
    				else if(ChessMate.boardPieces[x][y]==11)
    				{
    					//White Rook.
    					g.drawImage(Pwr, (x*50)+((50-Pwr.getWidth())/2), (y*50)+((50-Pwr.getHeight())/2), null);
    				}
    				else if(ChessMate.boardPieces[x][y]==21)
    				{
    					//Black Rook.
    					g.drawImage(Pbr, (x*50)+((50-Pbr.getWidth())/2), (y*50)+((50-Pbr.getHeight())/2), null);
    				}
    				else if(ChessMate.boardPieces[x][y]==12)
    				{
    					//White Knight.
    					g.drawImage(Pwkn, (x*50)+((50-Pwkn.getWidth())/2), (y*50)+((50-Pwkn.getHeight())/2), null);
    				}
    				else if(ChessMate.boardPieces[x][y]==22)
    				{
    					//Black Knight.
    					g.drawImage(Pbkn, (x*50)+((50-Pbkn.getWidth())/2), (y*50)+((50-Pbkn.getHeight())/2), null);
    				}
    				else if(ChessMate.boardPieces[x][y]==13)
    				{
    					//White Bishop.
    					g.drawImage(Pwb, (x*50)+((50-Pwb.getWidth())/2), (y*50)+((50-Pwb.getHeight())/2), null);
    				}
    				else if(ChessMate.boardPieces[x][y]==23)
    				{
    					//Black Bishop.
    					g.drawImage(Pbb, (x*50)+((50-Pbb.getWidth())/2), (y*50)+((50-Pbb.getHeight())/2), null);
    				}
    				else if(ChessMate.boardPieces[x][y]==14)
    				{
    					//White Queen.
    					g.drawImage(Pwq, (x*50)+((50-Pwq.getWidth())/2), (y*50)+((50-Pwq.getHeight())/2), null);
    				}
    				else if(ChessMate.boardPieces[x][y]==24)
    				{
    					//Black Queen.
    					g.drawImage(Pbq, (x*50)+((50-Pbq.getWidth())/2), (y*50)+((50-Pbq.getHeight())/2), null);
    				}
    				else if(ChessMate.boardPieces[x][y]==15)
    				{
    					//White King.
    					g.drawImage(Pwk, (x*50)+((50-Pwk.getWidth())/2), (y*50)+((50-Pwk.getHeight())/2), null);
    				}
    				else if(ChessMate.boardPieces[x][y]==25)
    				{
    					//Black King.
    					g.drawImage(Pbk, (x*50)+((50-Pbk.getWidth())/2), (y*50)+((50-Pbk.getHeight())/2), null);
    				}
    				else
    				{
    					//The tile is empty, draw nothing.
    				}
    			}
    		}
    		//Draw the border.
    		g.setColor(new Color(194, 233, 234));
    		g.fillRect(0, 400, 400, 20);
    		g.fillRect(400, 0, 20, 400);
    		//Load the borders letters and numbers and display them.
    		//A.
    		BufferedImage BlA=null;
    		try
    		{
    			BlA=ImageIO.read(new File("Images\\A.png"));
    		}
    		catch (IOException e)
    		{
    			System.out.println("Error!  "+e);
    		}
    		g.drawImage(BlA, 19, 402, null);
    		//B.
    		BufferedImage BlB=null;
    		try
    		{
    			BlB=ImageIO.read(new File("Images\\B.png"));
    		}
    		catch (IOException e)
    		{
    			System.out.println("Error!  "+e);
    		}
    		g.drawImage(BlB, 70, 402, null);
    		//C.
    		BufferedImage BlC=null;
    		try
    		{
    			BlC=ImageIO.read(new File("Images\\C.png"));
    		}
    		catch (IOException e)
    		{
    			System.out.println("Error!  "+e);
    		}
    		g.drawImage(BlC, 120, 402, null);
    		//D.
    		BufferedImage BlD=null;
    		try
    		{
    			BlD=ImageIO.read(new File("Images\\D.png"));
    		}
    		catch (IOException e)
    		{
    			System.out.println("Error!  "+e);
    		}
    		g.drawImage(BlD, 169, 402, null);
    		//E.
    		BufferedImage BlE=null;
    		try
    		{
    			BlE=ImageIO.read(new File("Images\\E.png"));
    		}
    		catch (IOException e)
    		{
    			System.out.println("Error!  "+e);
    		}
    		g.drawImage(BlE, 220, 402, null);
    		//F.
    		BufferedImage BlF=null;
    		try
    		{
    			BlF=ImageIO.read(new File("Images\\F.png"));
    		}
    		catch (IOException e)
    		{
    			System.out.println("Error!  "+e);
    		}
    		g.drawImage(BlF, 270, 402, null);
    		//G.
    		BufferedImage BlG=null;
    		try
    		{
    			BlG=ImageIO.read(new File("Images\\G.png"));
    		}
    		catch (IOException e)
    		{
    			System.out.println("Error!  "+e);
    		}
    		g.drawImage(BlG, 319, 402, null);
    		//H.
    		BufferedImage BlH=null;
    		try
    		{
    			BlH=ImageIO.read(new File("Images\\H.png"));
    		}
    		catch (IOException e)
    		{
    			System.out.println("Error!  "+e);
    		}
    		g.drawImage(BlH, 369, 402, null);
    		//1.
    		BufferedImage Bn1=null;
    		try
    		{
    			Bn1=ImageIO.read(new File("Images\\1.png"));
    		}
    		catch (IOException e)
    		{
    			System.out.println("Error!  "+e);
    		}
    		g.drawImage(Bn1, 407, 367, null);
    		//2.
    		BufferedImage Bn2=null;
    		try
    		{
    			Bn2=ImageIO.read(new File("Images\\2.png"));
    		}
    		catch (IOException e)
    		{
    			System.out.println("Error!  "+e);
    		}
    		g.drawImage(Bn2, 406, 317, null);
    		//3.
    		BufferedImage Bn3=null;
    		try
    		{
    			Bn3=ImageIO.read(new File("Images\\3.png"));
    		}
    		catch (IOException e)
    		{
    			System.out.println("Error!  "+e);
    		}
    		g.drawImage(Bn3, 405, 267, null);
    		//4.
    		BufferedImage Bn4=null;
    		try
    		{
    			Bn4=ImageIO.read(new File("Images\\4.png"));
    		}
    		catch (IOException e)
    		{
    			System.out.println("Error!  "+e);
    		}
    		g.drawImage(Bn4, 405, 217, null);
    		//5.
    		BufferedImage Bn5=null;
    		try
    		{
    			Bn5=ImageIO.read(new File("Images\\5.png"));
    		}
    		catch (IOException e)
    		{
    			System.out.println("Error!  "+e);
    		}
    		g.drawImage(Bn5, 405, 167, null);
    		//6.
    		BufferedImage Bn6=null;
    		try
    		{
    			Bn6=ImageIO.read(new File("Images\\6.png"));
    		}
    		catch (IOException e)
    		{
    			System.out.println("Error!  "+e);
    		}
    		g.drawImage(Bn6, 405, 117, null);
    		//7.
    		BufferedImage Bn7=null;
    		try
    		{
    			Bn7=ImageIO.read(new File("Images\\7.png"));
    		}
    		catch (IOException e)
    		{
    			System.out.println("Error!  "+e);
    		}
    		g.drawImage(Bn7, 404, 67, null);
    		//8.
    		BufferedImage Bn8=null;
    		try
    		{
    			Bn8=ImageIO.read(new File("Images\\8.png"));
    		}
    		catch (IOException e)
    		{
    			System.out.println("Error!  "+e);
    		}
    		g.drawImage(Bn8, 405, 17, null);
    		//Buttons.
    		//Quit.
    		JButton btnQuit=new JButton("Quit");
    		btnQuit.setLocation(490, 400);
    		btnQuit.setSize(120, 32);
    		add(btnQuit);
    		btnQuit.setVisible(true);
    		btnQuit.addActionListener(new ActionListener()
    		{
                public void actionPerformed(ActionEvent e)
                {
                    ChessMate.f.dispose();
                }
            });
    		//New.
    		JButton btnNew=new JButton("New Game");
    		btnNew.setLocation(490, 10);
    		btnNew.setSize(120, 32);
    		add(btnNew);
    		btnNew.setVisible(true);
    		btnNew.addActionListener(new ActionListener()
    		{
                public void actionPerformed(ActionEvent e)
                {
                	//Reset the board.
                	ChessMate.CreateBoard();
                	//Reset the pieces.
                	ChessMate.SetPieces();
                	ChessMate.f.invalidate();
                	ChessMate.f.repaint();
                	ChessMate.f.validate();
                }
            });
    	}
    }

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

    Default Re: ChessMate GUI Program Issues.

    Quote Originally Posted by Paul_Boland View Post
    Hi Folks.

    I am studying Java at college
    Moved from Advanced Java. Let's keep homework where it belongs.

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

Similar Threads

  1. New Tabbed Program issues
    By Logik22 in forum New To Java
    Replies: 3
    Last Post: 08-16-2011, 06:24 PM
  2. GUI Issues
    By larry_d1990 in forum Advanced Java
    Replies: 2
    Last Post: 03-17-2011, 02:35 PM
  3. jdk issues
    By artemff in forum New To Java
    Replies: 3
    Last Post: 01-02-2010, 03:18 AM
  4. small issues with a program
    By jimJohnson in forum New To Java
    Replies: 6
    Last Post: 04-25-2008, 08:28 AM
  5. Issues with Jva I.O
    By Annatar01 in forum New To Java
    Replies: 0
    Last Post: 02-08-2008, 01:16 AM

Posting Permissions

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