2011-12-12 111 views
0

你好我使用JApplet來顯示一張Jpanel的圖像,我從網絡上得到這個圖像,但是這個applet正在加載並且沒有顯示圖像。JApplet不顯示帶圖像的JPanel

代碼:

package com.ntenisot;

import java.io.*; 
import java.net.*; 
import javax.swing.*; 

import java.awt.event.*; 
import java.awt.*; 
import java.applet.*; 

public class Cliente extends JApplet { 

JTextField tf; 
Lienzo lienzo; 
Socket con; 
ObjectOutputStream salida; 
ObjectInputStream entrada; 

public void init(){ 
    System.out.println("initializing"); 
    tf = new JTextField(10); 
    lienzo=new Lienzo();  
    setSize(1000,1000); 
    lienzo.setSize(900,900); 
    lienzo.setVisible(true); 
    setContentPane(lienzo); 

    Container container = getContentPane(); 
    container.setBackground(Color.pink); 
    container.setLayout(new FlowLayout()); 

    // Text area 1 
    String string = "Some text in here, Some text in here, Some text in here"; 
    JTextArea textArea1 = new JTextArea(string, 10, 15); 
    container.add(new JScrollPane(textArea1)); 
} 

public void start() { 
    ejecutar(); 
} 

void ejecutar(){ 
    System.out.println("executing1"); 

    try{ 

     con = new Socket("127.0.0.1",5700); 
     salida = new ObjectOutputStream(con.getOutputStream()); 
     salida.flush(); 
     entrada = new ObjectInputStream(con.getInputStream()); 
     System.out.println("executing"); 
     procesar(); 


    } 
    catch(IOException e){ 
     System.out.println("error"); 
    } 

} 

void procesar() throws IOException { 
    System.out.println("processing"); 

    try{ 

     while(true){ 
      ImageIcon img = (ImageIcon) entrada.readObject(); 
      escribir(img); 
     } 
    }catch(ClassNotFoundException e){} 

} 

void escribir(final ImageIcon img){ 

    SwingUtilities.invokeLater(
      new Runnable(){ 
       public void run(){ 
        lienzo.pinta(img); 
       } 

      } 
      ); 

} 

class Lienzo extends JPanel{ 

    ImageIcon img=null ; 

    public void pinta(ImageIcon img){ 
     this.img=img; 
     repaint(); 
    } 

    public void paintComponent(Graphics g){ 

     super.paintComponent(g); 

     if(img!=null) 
      img.paintIcon(this,g,10,10); 

    } 


} 

} 

回答

1

我沒有測試你的代碼,但似乎方法procesar不能終止。因此,init方法也不會終止。我認爲你可以在(真實)循環中移除周圍環境,因爲一次設置圖像就足夠了。

+0

更新了代碼.. removed ejecutar();從init和我把它放到開始(),但仍然不顯示任何東西! – glarkou

+0

爲什麼你需要把while(true)放在方法procesar中?你是否正在加載多個圖像並製作動畫? – dsboger

+0

是的,我通過互聯網流媒體制作視頻.. – glarkou