1

Hello so i’m trying to send an image to my GUI from thread (created using runnable)

i’v extended an observable

public class receiveRunnable extends Observable implements Runnable{
   public void run() {
       try {
           //crée un serverSocket branché sur le meme port que send
           ServerSocket serverSocket = new ServerSocket(5000);
           Socket socket = serverSocket.accept();
           //reçoit l'image serialisée
           InputStream inputStream = (InputStream) socket.getInputStream();
           System.out.println("Reading: " + System.currentTimeMillis());
           //déserialise l'image
           byte[] sizeAr = new byte[4];
           inputStream.read(sizeAr);
           int size = ByteBuffer.wrap(sizeAr).asIntBuffer().get();
           byte[] imageAr = new byte[size];
           inputStream.read(imageAr);
           BufferedImage image = ImageIO.read(new ByteArrayInputStream(imageAr));
           System.out.println("Received " + image.getHeight() + "x" + image.getWidth());
           Object img = (Object) image;
           setChanged();
           notifyObservers(img);
           System.out.println("yeah");
       }catch(IOException e){
           e.printStackTrace();
       }
   }
}

i’ve implemented an observer with an update method who should be called with notifyObserver(image )

class win extends JFrame implements Observer {

public void update(Observable receivedRunnable, Object arg) {
  System.out.println("ok" );
  JFrame ji = new JFrame();
  JPanel po = new JPanel();
  JLabel c = new JLabel();
  BufferedImage image = (BufferedImage) arg;
  ImageIcon icoff = new ImageIcon(image);
  c.setIcon(icoff);
  po.add(c);
  ji.setVisible(true);
  ji.setContentPane(po);
}

}

and then in my main i tried to link then with addObservers()

public static void main(String [] args){
  Fenetre a = new Fenetre();
  receiveRunnable runnable = new receiveRunnable();
  runnable.addObserver((Observer) a);
  Thread receiveThread = new Thread();
  receiveThread.run();
}

and I absolutely have no idea why but i get this message

interfaceGraphique.Fenetre cannot be cast to java.util.Observer

I’v read everything i found and i can’t even find a lead on what could the problem be

Monsieur Sam
  • 333
  • 1
  • 6
  • 20
  • 4
    I'm going to guess it's because a `Fenetre` is not an `Observer`. – Joe C Apr 27 '17 at 21:25
  • Possible duplicate of [Explanation of "ClassCastException" in Java](http://stackoverflow.com/questions/907360/explanation-of-classcastexception-in-java) – Joe C Apr 27 '17 at 21:25
  • and what about putting the runnable inside the thread Thread receiveThread = new Thread(runnable); – Yahya Apr 27 '17 at 21:35

0 Answers0