Fractal Softworks Forum

Please login or register.

Login with username, password and session length

Author Topic: Java help (serialization and networking)  (Read 1538 times)

sdmike1

  • Admiral
  • *****
  • Posts: 820
  • Dyslexics of the world, untie!
    • View Profile
Java help (serialization and networking)
« on: April 19, 2013, 07:35:36 AM »

My friends and i are making a tic tac toe game that you can play on different computers, we decided to serialize the objects of the JButtons and send send them over the network to do this, at this point we can't get them to work as we get this error "Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: javax.swing
.JButton cannot be cast to TicButton" anyone have any ideas, i have posted the code below
Thanks! anything helps :)

The class that does the transfer.
Code
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class Transfer
{
    public static void Transfer(String FileName)
    {
    final String largeFile = FileName;
    final int BUFFER_SIZE = 65536;
    new Thread(new Runnable()
    {
    public void run()
    {
    try
    {
    ServerSocket serverSocket = new ServerSocket(12345);
    Socket clientSocket = serverSocket.accept();
    long startTime = System.currentTimeMillis();
    byte[] buffer = new byte[BUFFER_SIZE];
    String read;
    String totalRead = 0;
    InputStream clientInputStream = clientSocket.getInputStream();
    while ((read = clientInputStream.read(buffer)) != -1)
    {
    totalRead += read;
    }
    long endTime = System.currentTimeMillis();
    System.out.println(totalRead + " bytes read in " + (endTime - startTime) + " ms.");
    }
    catch (IOException e)
    {
System.out.println(e);
    }
    }
    }
    ).start();


    new Thread(new Runnable()
    {
    public void run()
    {
    try
    {
    Thread.sleep(5000);
    Socket socket = new Socket("192.168.24.203", 12345);
    FileInputStream fileInputStream = new FileInputStream(largeFile);
    OutputStream socketOutputStream = socket.getOutputStream();
    long startTime = System.currentTimeMillis();
    byte[] buffer = new byte[BUFFER_SIZE];
    int read;
    int readTotal = 0;
    while ((read = fileInputStream.read(buffer)) != -1)
    {
    socketOutputStream.write(buffer, 0, read);
    readTotal += read;
    }
    socketOutputStream.close();
    fileInputStream.close();
    socket.close();
    long endTime = System.currentTimeMillis();
    System.out.println(readTotal + " bytes written in " + (endTime - startTime) + " ms.");
    }
    catch (Exception e)
    {
System.out.println(e);
    }
    }
    }
    ).start();

    }
}
the "actual" game portion.
Code
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.nio.ByteBuffer;
import java.nio.channels.*;

public class TicTacToe3 implements ActionListener
{
JFrame window = new JFrame("TicTacToe");
private JButton buttons[] = new JButton[9];
private String letter;
private int count = 0;
private int[][] wins = new int[][]
{
{0,1,2},{3,4,5},{6,7,8},{0,3,6},{1,4,7},{2,5,8},{0,4,8},{2,4,6}
};
private boolean win = false;
public TicTacToe3()
{
window.setSize(300,300);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLayout(new GridLayout(3,3));
for(int i=0; i <= 8; i++)
{
buttons[i] = new JButton();
window.add(buttons[i]);
buttons[i].addActionListener(this);
}
window.setVisible(true);
}

public void actionPerformed(ActionEvent a)
{

++count;
if(count % 2 == 0)
{
letter = "O";
}
else
{
letter = "X";
}
JButton buttonPressed = (JButton)a.getSource();
buttonPressed.setText(letter);
buttonPressed.setEnabled(false);


//My Stuff
//From this line to line 59 serializes the object
try{

ObjectOutputStream output = new ObjectOutputStream(new FileOutputStream("TicButton.dat"));
TicButton button;
int buttonLoc;
String buttonSym;
output.writeObject(buttonPressed);

Transfer.Transfer("TicButton.dat");

}
catch (IOException z)
{
System.out.println(z);
}
//end of the serialization. the data file is now saved
//start of the reading of the data file where the object is stored --- goes until line 86
try{
ObjectInputStream inR = new ObjectInputStream(new FileInputStream("TicButton.dat"));
TicButton button;
try
{
while(true)
{
button = (TicButton)inR.readObject();
}
}
catch(EOFException e)
{
System.out.println(e);
inR.close();
}
}
catch(IOException z)
{
System.out.println(z);
}
catch(ClassNotFoundException y)
{
System.out.println(y);
}

//end of the reading of the data file where the object is saved

//My Stuff

for(int i = 0; i<= 7; i++)
{
if(buttons[wins[i][0]].getText().equals(buttons[wins[i][1]].getText()) && buttons[wins[i][1]].getText().equals(buttons[wins[i][2]].getText()) && buttons[wins[i][0]].getText() != "")
{
win = true;
}
}
if(win == true)
{
JOptionPane.showMessageDialog(null, letter + " Wins!");
System.exit(0);
}
else if(count == 9 && win == false)
{
JOptionPane.showMessageDialog(null, "Tie!");
System.exit(0);
}
}
}
and the "launcher"
Code
public class Test
{
public static void main(String []args)
{
TicTacToe3 TTT = new TicTacToe3();
}
}