您好我正在尝试从线程(使用runnable创建)将图像发送到我的GUI
我扩展了一个可观察的
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();
}
}
}
我实现了一个带有update方法的观察者,应该使用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);
}
}
然后在我的主要部分我尝试用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();
}
我绝对不知道为什么但是我收到了这条消息
interfaceGraphique.Fenetre无法强制转换为java.util.Observer
我读了我发现的所有内容,我甚至无法找到问题的原因