Click here to Skip to main content
15,892,161 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to save the current position of the pieces on the board after the user clicks on the save button. I have implemented 'Serializable' in the Board class but when I click on the save button, there are some runtime errors. Can you help me solve this kind of problem? I'm new to this java GUI. Thank you in advance.

What I have tried:

public class Board extends JPanel implements ActionListener, Serializable
{
	JPanel cardss;
	JButton[] square = new JButton[42];
	JButton save = new JButton("Save");
	JButton quit = new JButton("Quit");
	MenuPage menupage;
	Tile tile = Tile.getInstance();
	boolean firstClick = true;
	int count = 0;
	int player = 1;
	JLabel player_label;
	JLabel player_status;
	JFrame frame = new JFrame();
	JFrame f;
	private Board()
	{
		// super("Chess Board");
		
		// setJMenuBar(MyChessBar);
		Dimension boardSize = new Dimension(600, 650); //(width, height)
		Dimension player_panel = new Dimension(180, 650); //(width, height)

		JPanel topPanel = new JPanel(new FlowLayout());
		JPanel boardPanel = new JPanel(new GridLayout(0,7));
		JPanel player_turn = new JPanel(new FlowLayout());
		player_turn.setBackground(Color.yellow);
		
		player_label = new JLabel("Moved : " + count);
		player_status = new JLabel("TURN : PLAYER " + player);
		// JTextField text = new JTextField(15);
		// text.setText( "Player :" + count + " turn ");
		player_turn.add(player_label);
		player_turn.add(player_status);
		save.addActionListener(this);
		quit.addActionListener(this);		
		
		topPanel.add(save);
		topPanel.add(quit);
		// setPreferredSize(boardSize);
		
		for(int i=0; i<42; i++)
		{
			square[i] = new JButton();
			if(i%2==0)
			{
				square[i].setBackground(Color.black);
				tile.setTileFalse(i);
				square[i].addActionListener(this);
			}
			else
			{
				square[i].setBackground(Color.white);
				tile.setTileFalse(i);
				square[i].addActionListener(this);
			}
			boardPanel.add(square[i]);
			tile.setTileLoc(i);

		} // close for loop
		
		boardPanel.setPreferredSize( boardSize );
		player_turn.setPreferredSize( player_panel);
		setLayout(new BorderLayout(2,2));
		add(topPanel, BorderLayout.NORTH);
		add(boardPanel, BorderLayout.WEST);
		add(player_turn, BorderLayout.EAST);
		
		// cardss = new JPanel(new CardLayout());
		// menupage = new MenuPage();
		// cardss.add(menupage, "Menu");
		// add(cardss);
		
		setSize(800,800);
		setVisible(true);

	} //close for board
	
	private static class SingletonHolder {
        private static final Board INSTANCE = new Board();
    }
	
	public static Board getInstance() {
        return SingletonHolder.INSTANCE;
    }

	public void actionPerformed(ActionEvent e)
	{
		String choice_in_game = e.getActionCommand();
		
		if(choice_in_game.equals("Save"))
		{
			
			// save game
			// saveGameDataToFile(chess);
			try
			{
                           FileOutputStream file = new FileOutputStream("chess.txt");
				ObjectOutputStream object = new ObjectOutputStream(file);
				object.writeObject(new Board());
				object.close();

			 // create a dialog Box 
            JDialog d = new JDialog(f, "Chess Game"); 
  
            // create a label 
            JLabel l = new JLabel("Game is successfully saved!"); 
  
            d.add(l); 
  
            // setsize of dialog 
            d.setSize(200,100); 
			d.setLocationRelativeTo(null);
  
            // set visibility of dialog 
            d.setVisible(true); 

			}
			catch(Exception ex)
			{
				ex.printStackTrace();
			}
		}
}

	public void setPiecePic(int i, String name){
		square[i].setIcon(loadImage(name));
		tile.setTileTrue(i);
	}
	
	public void setPieceNull(int i){
		square[i].setIcon(null);
		tile.setTileFalse(i);
	}

	private ImageIcon loadImage(String path){
        Image image = new ImageIcon(this.getClass().getResource(path)).getImage();
        Image scaledImage = image.getScaledInstance(82, 82,  java.awt.Image.SCALE_SMOOTH);
        return new ImageIcon(scaledImage);
    }
	
	public void setEveryPiece(){
		
		setPiecePic(0,"PlusB.png");
		setPiecePic(1,"TriangleB.png");
		setPiecePic(2,"ChevronB.png");
		setPiecePic(3,"SunB.png");
		setPiecePic(4,"ChevronB.png");
		setPiecePic(5,"TriangleB.png");
		setPiecePic(6,"PlusB.png");
		
		setPiecePic(35,"PlusR.png");
		setPiecePic(36,"TriangleR.png");
		setPiecePic(37,"ChevronR.png");
		setPiecePic(38,"SunR.png");
		setPiecePic(39,"ChevronR.png");
		setPiecePic(40,"TriangleR.png");
		setPiecePic(41,"PlusR.png");
		
		tile.setAllPiece();
	}

	public JButton getButton(int x){
		return square[x];
	}
	
	public JButton[] getSquare(){
		return square;
	}
	
	public void getStatus(int count, int player) {
		player_label.setText("Moved: " + count);
		player_status.setText("TURN : PLAYER " + player);	
	}
}
Posted
Updated 22-Sep-18 1:28am
Comments
Patrice T 22-Sep-18 4:56am    
And you plan to tell the error messages and positions ?

1 solution

Java
object.writeObject(new Board());

Why are you trying to save a new Board object, instead of the one that exists in the application?

You also need to explain exactly what runtime errors you get, we cannot guess.
 
Share this answer
 
Comments
Member 13620781 22-Sep-18 14:27pm    
sorry for not giving the details of the errors, but I've changed the code and have some problems with the load data from file. I will submit the question and provide the errors. Do you mind to help me? Thanks.
Richard MacCutchan 23-Sep-18 3:05am    
Mark this one as solved and open a new question.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900