2011-12-12 71 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