Results 1 to 14 of 14
  1. #1
    javanoob73 is offline Member
    Join Date
    Nov 2009
    Posts
    7
    Rep Power
    0

    Default need advice for checkers game

    Hi,
    I'm fairly new to java, im taking my first java class this semester in college. For our final project I have to make a checkers game. I have to make it so a human player can play against a computer. I just finished making the checkers board, but I'm not sure how to add the checkers pieces? I'm completely lost at this point and don't know what to do.Oh yeah and I'm using Eclipse. Any advice or links to websites that would be helpful is appreciated, thanks.

  2. #2
    sky
    sky is offline Member
    Join Date
    Nov 2009
    Posts
    96
    Rep Power
    0

    Default

    Well, maybe you can have a class call Piece, with features like the color, if it is dead or not, etc and then place them in the board.

  3. #3
    javanoob73 is offline Member
    Join Date
    Nov 2009
    Posts
    7
    Rep Power
    0

    Default

    ya i tried doing that, I was playing around and tried to just add a simple shape to the board, but it seems that the checkerboard background cover over everything that I add, I even tried adding a text box and it didn't appear

  4. #4
    sky
    sky is offline Member
    Join Date
    Nov 2009
    Posts
    96
    Rep Power
    0

    Default

    Maybe if you put some code or tell us how you are implementing the board and the pieces we can tell you more.

  5. #5
    javanoob73 is offline Member
    Join Date
    Nov 2009
    Posts
    7
    Rep Power
    0

    Default

    okay heres what I have so far...



    Java Code:
    package MyCheckers;
    
    import java.awt.Color;
    import java.awt.Container;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.GridLayout;
    import javax.swing.*;
    
    public class MyCheckerBoard extends JButton{ //extends JPanel{
        
        //private static final long serialVersionUID = 1L;
            public static void main (String args[])
    
        {
        JFrame checkerBoard = new JFrame();
        checkerBoard.setSize(400,400);
        checkerBoard.setTitle("CheckerBoard");
        checkerBoard.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
        int row = 8;
        int col = 8;
         
        Container pane = checkerBoard.getContentPane();
        pane.setLayout(new GridLayout(row,col));
        Color checker;
        for (int x = 1; x <=(row*col); x++)
        
        {
        int altr = 0;
        altr = (x-1) % col;
        altr += (x-1) / col;
        if (altr % 2 == 0)
        
        {
            checker = Color.black;
        }
        else
        
        {
            checker = Color.red;
        }
        
        JPanel panel = new JPanel();
        panel.setPreferredSize(new Dimension(400/row, 400/col));
        panel.setBackground(checker);
        pane.add(panel);
        
        }
        checkerBoard.setVisible(true);
        }
            
        
        public void paintComponent(Graphics g) 
        {
            super.paintComponent(g);
            //g.drawImage(foregroundImage, x, y, this);
            g.setColor(Color.white);
            g.fillOval(70, 70, 100, 100);
    
            //Graphics2D g2d = (Graphics2D) g;
            //g.setColor(Color.white);
            //g.fillOval(70,70,100,100);
        }
    }
    Last edited by javanoob73; 11-28-2009 at 07:06 PM. Reason: Code tags added for readability

  6. #6
    sky
    sky is offline Member
    Join Date
    Nov 2009
    Posts
    96
    Rep Power
    0

    Default

    It's very hard to read the code like that, put some CODE tags please.

  7. #7
    javanoob73 is offline Member
    Join Date
    Nov 2009
    Posts
    7
    Rep Power
    0

    Default

    how do i do that? I'm new to this site

  8. #8
    sky
    sky is offline Member
    Join Date
    Nov 2009
    Posts
    96
    Rep Power
    0

    Default

    When you are writing a post you have a button that looks like an #. If you click it the code tags appear, and you just paste yout code inside them.

  9. #9
    javanoob73 is offline Member
    Join Date
    Nov 2009
    Posts
    7
    Rep Power
    0

    Default

    okay thanks. I changed it

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I added code tags to your code post.

    Some random recommendations:

    * I wouldn't have your class subclass JButton.
    * Consider using a JLayeredPane to hold your board and your pieces. The Sun tutorial on this will help you: How to use a Layered Pane
    * Please check out my signature regarding the use of code tags here.

    Much luck!

  11. #11
    javanoob73 is offline Member
    Join Date
    Nov 2009
    Posts
    7
    Rep Power
    0

    Default

    -Fubarable
    thanks for the link to the website it looks like layered panels is what i need to use, I was playing around with it and I am trying to create a plain green background with the checker board in the foreground, but I'm still a little lost, any advice? I'll post the new code

    Java Code:
    package MyCheckers;
    
    import java.awt.Color;
    import java.awt.Container;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.GridLayout;
    import java.awt.Point;
    
    import javax.swing.*;
    
    public class MyCheckerBoard extends JPanel{
    	
    	//private String[] layerStrings = { "Green (0)"};
    	//private Color[] layerColors = { Color.green };
    	
    	//private JLayeredPane layeredPane;
    
    	
    	//private static final long serialVersionUID = 1L;
    		public static void main (String args[])
    		
    
    	//{
    		//	JFrame background = new JFrame();
    			//background.setSize(600,600);
    			//background.setTitle("background");
    			//background.setColor
    			//background.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	//}
    		
    	{
    	JFrame checkerBoard = new JFrame();
    	JButton StartGame = new JButton("Start Game");
    	JButton resign = new JButton("Resign");
    	checkerBoard.setSize(600,600);
    	checkerBoard.setTitle("CheckerBoard");
    	checkerBoard.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    	int row = 8;
    	int col = 8;
    	 
    	Container pane = checkerBoard.getContentPane();
    	pane.setLayout(new GridLayout(row,col));
    	Color checker;
    	for (int x = 1; x <=(row*col); x++)
    	
    	{
    	int altr = 0;
    	altr = (x-1) % col;
    	altr += (x-1) / col;
    	if (altr % 2 == 0)
    	
    	{
    		checker = Color.black;
    	}
    	else
    	
    	{
    		checker = Color.red;
    	}
    	
    	{
    		JLayeredPane layeredPane = new JLayeredPane();
    		layeredPane.setPreferredSize( new Dimension (600,600));
    		JPanel panel = new JPanel();
    		panel.setBackground(new Color (0,150,0));
    		//pane.add(panel);
    		JPanel board = new JPanel();
    		board.setPreferredSize(new Dimension(400, 400));
    		board.setForeground(checker);
    		panel.add(panel);
    		
    
    
    		
    	}
    	
    	//JPanel panel = new JPanel();
    	//panel.setPreferredSize( new Dimension(600,600) );
    	//panel.setPreferredSize(new Dimension(400/row, 400/col));
    	//panel.setBackground(checker);
    	//panel.setBackground(new Color(0,150,0));
    	//pane.add(panel);
    	
    	//}
    	//{
    	//JPanel board = new JPanel();
    	//board.setPreferredSize(new Dimension(400,400));
    	//board.setForeground(checker);
    	//pane.add(panel);
    	//}
    	//{
    			//JPanel background = new JPanel();
    			//background.setSize(600,600);
    			//background.setTitle("background");
    			//background.setColor
    			//background.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	//}
    	resign.setVisible(true);
    	StartGame.setVisible(true);
    	checkerBoard.setVisible(true);
    	}
    	}
    	
    		
    	
    	public void paintComponent(Graphics g) 
    	{
    		super.paintComponent(g);
    		//g.drawImage(foregroundImage, x, y, this);
    		g.setColor(Color.white);
    		g.fillOval(70, 70, 100, 100);
    
    		//Graphics2D g2d = (Graphics2D) g;
    		//g.setColor(Color.white);
    		//g.fillOval(70,70,100,100);
    	}
    }

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  13. #13
    javanoob73 is offline Member
    Join Date
    Nov 2009
    Posts
    7
    Rep Power
    0

    Default

    for some reason only when i added that code^^ the green background appeared

  14. #14
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by javanoob73 View Post
    for some reason only when i added that code^^ the green background appeared
    The code as posted can't have worked since when you try to add a component to itself, you will cause an error. See for yourself:
    Java Code:
    import javax.swing.JPanel;
    
    public class Fu1 {
      public static void main(String[] args) {
        JPanel panel = new JPanel();
        panel.add(panel);
      }
    }

Similar Threads

  1. Checkers
    By evan42781 in forum New To Java
    Replies: 8
    Last Post: 05-08-2009, 04:07 AM
  2. Some advice please!
    By awebbtt in forum New To Java
    Replies: 3
    Last Post: 02-02-2009, 07:23 PM
  3. Advice needed
    By dineshjf in forum Advanced Java
    Replies: 18
    Last Post: 01-09-2009, 03:17 AM
  4. Help with dice game...student seeking advice
    By waparson in forum New To Java
    Replies: 3
    Last Post: 07-21-2008, 03:31 PM
  5. Advice on best method for....
    By shaungoater in forum Java 2D
    Replies: 1
    Last Post: 06-23-2008, 07:36 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
  •