SwingGUI.java

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.imageio.*;
import java.net.*;
import java.io.*;
import java.awt.image.*;

public class SwingGUI
{
 public static void main(String[] args)
 {
  JFrame.setDefaultLookAndFeelDecorated(true);
  JDialog.setDefaultLookAndFeelDecorated(true);
  
  SimpleFrame frame = new SimpleFrame();
  
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.setResizable(false);
  try {
   //UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel") ;
   //UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel") ; 
   //UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel") ;
   javax.swing.UIManager.setLookAndFeel("com.birosoft.liquid.LiquidLookAndFeel");
   //com.birosoft.liquid.LiquidLookAndFeel.setLiquidDecorations(true);
   //UIManager.setLookAndFeel("javax.swing.plaf.mac.MacLookAndFeel");
  
   SwingUtilities.updateComponentTreeUI(frame);
  }
  catch(ClassNotFoundException cnfe) {
      cnfe.printStackTrace( );
  } catch(InstantiationException ie) {
       ie.printStackTrace( );
  } catch(IllegalAccessException iae) {
      iae.printStackTrace( );
  } catch(UnsupportedLookAndFeelException ulafe) {
      ulafe.printStackTrace( );
  }
 
  frame.show();
  
 }
}
class SimpleFrame extends JFrame

 JMenuBar menuBar;
 JMenu operationMenu;
 JMenu linkMenu;
 JMenuItem linkItem1;
 JMenuItem linkItem2;
 JMenuItem linkItem3;
 JMenuItem linkItem4;
 JMenuItem quitItem;
 JLabel m_label1=new JLabel();
 JLabel m_label2=new JLabel();
 private inputSer_IP dialog1=null;
 private inputSer_IP dialog2=null;
 String content1,content2;
 boolean DEBUGGING=true;
 private SwingWorker m_worker1 = null;
 private SwingWorker m_worker2 = null;
 private final int HDRLEN = 50;
    private final int SZOFS = 29;
    private boolean m_stop = false;
    JPanel topPanel=new JPanel();
 
 public SimpleFrame()
 {
  
  Toolkit kit = Toolkit.getDefaultToolkit();
  Dimension screenSize = kit.getScreenSize();
  int screenHeight = screenSize.height;
  int screenWidth = screenSize.width;
  //setSize(screenSize.width/2,screenSize.height/2);
  setLocation(screenSize.width/4,screenSize.height/4);
  setSize(720,520);
  
  //set frame icon and title
  Image icon = kit.getImage("camera.jpg");
  setIconImage(icon);
  setTitle("WebCamplayer");
  //add menu
  menuBar = new JMenuBar();
     operationMenu = new JMenu("连接服务器");
     menuBar.add(operationMenu);
     linkMenu = new JMenu("服务器选择");
     operationMenu.add(linkMenu);
     linkItem1 = new JMenuItem("一号服务器");
     linkMenu.add(linkItem1);
     linkItem1.addActionListener(new ConnectServerAction1());
     operationMenu.add(linkMenu);
     linkItem2 = new JMenuItem("二号服务器");
     linkMenu.add(linkItem2);
     linkItem2.addActionListener(new ConnectServerAction2());
     operationMenu.add(linkMenu);
     linkItem3 = new JMenuItem("三号服务器");
     linkMenu.add(linkItem3);
     operationMenu.add(linkMenu);
     linkItem4 = new JMenuItem("四号服务器");
     linkMenu.add(linkItem4);
     //linkItem.addActionListener(new ConnectServerAction());
     operationMenu.addSeparator();
     quitItem = new JMenuItem("退出");
     operationMenu.add(quitItem);
     quitItem.addActionListener(new ActionListener()
     {
      public void actionPerformed(ActionEvent act_e)
     {
    System.exit(0);
   }
     });
     this.setJMenuBar(menuBar);
  //this.setContentPane(m_label1);
  topPanel.setLayout(new GridLayout(2,2,3,3));
  ImageIcon icon1=new ImageIcon("camera.jpg");
        m_label1.setIcon(icon1);
        ImageIcon icon2=new ImageIcon("camera.jpg");
        m_label2.setIcon(icon2);
  topPanel.add(m_label1);
  topPanel.add(m_label2);
  topPanel.add(new Button("3"));
  topPanel.add(new Button("4"));
  
        add(topPanel);
       
        //m_label1.setBounds(0,0,320,240);
 
  
 }
 public int unsignedByteToInt( byte b )
    {
     return (int) b & 0xff; //byte to int
    }
 private class ConnectServerAction1 implements ActionListener
 {
  public void actionPerformed(ActionEvent event)
  {
   if(dialog1==null) dialog1=new inputSer_IP();
   if(dialog1.showDialog(SimpleFrame.this,"输入服务器IP"))
   {
    
    content1=dialog1.Ser_IP.getText();
    if (DEBUGGING) System.out.println(content1);
    m_worker1 = new SwingWorker() {
           public Object construct() {
   Socket connection;
         InputStream in;
         OutputStream out;
   try {
               int port=7070;
                 connection = new Socket(content1, port);//socket 会话
                 if (DEBUGGING)
                    System.out.println("have create a socket");
                  in=connection.getInputStream();//方法获得网络连接输入
                  if (DEBUGGING)
                    System.out.println("have done in");
                  out = connection.getOutputStream();//方法连接的另一端将得到输入
                  if (DEBUGGING)
                   System.out.println("have done out");
                }
            catch (IOException e) {
                 
                  m_label1.setText("Connection failed with error: " + e);
                  m_label1.setIcon(null);//显示图标
                  if (DEBUGGING)
                   System.out.println(e);
                  return null;
                }
                byte [] buffer_label = new byte [512*1024];
               
            m_label1.setText("");
           
            while(!m_stop)
                {
                    byte [] b = {'O','K',0,0, 0,0,0,0,0,0,0,0,0};
                   
         
                    int n = 0;
                    int siz = 0;

                    try {
                     out.write(b);
                   if (DEBUGGING)
                    System.out.println("do read 50");
                        n = in.read(buffer_label, 0, HDRLEN);//将输入流中最多 50 个数据字节读入字节数组,0 is buffer's index
                   System.out.println(n);
                   if (DEBUGGING)
                     System.out.println("have done read 50");
                     }
                    catch(IOException e) {
                        e.printStackTrace();
                        m_stop = true;
                    }
                   
                    if (n<HDRLEN)
                    {
                        if (n<=0)
                            m_stop = true;

                        continue;
                    }
                    else //n>=50
                    {
                        // can someone point out a more straightforward way to convert 4 bytes
                        // to a java integer? this is just awful...
                        int ssz = SZOFS;//29
            siz += unsignedByteToInt(buffer_label[ssz+3]) << 24;
            siz += unsignedByteToInt(buffer_label[ssz+2]) << 16;
            siz += unsignedByteToInt(buffer_label[ssz+1]) << 8;
            siz += unsignedByteToInt(buffer_label[ssz]) ;
                        if (DEBUGGING)
                            System.out.println("** reading jpeg, size="+Integer.toString(siz));

                        n = HDRLEN;
                        if (buffer_label[0]!='S' || buffer_label[1]!='P' || buffer_label[2]!='C' || buffer_label[3]!='A')
                        {
                            if (DEBUGGING)
                                System.out.println("*Header missing 'SPCA'");
                           
                            continue;
                        }
                        else if (siz<=0 || siz>(512*1024))
                        {
                            siz = 0;
                            try {
                                n = in.read(buffer_label, 0, HDRLEN); //重新读取
                            }  
                            catch(Exception e) {
                                //if (DEBUGGING)
                                    //e.printStackTrace();
                                    m_stop = true;
                            }
                           
                            //if (DEBUGGING)
                                //System.out.println("*Illegal image size");
                           
                            continue;
                        }
                        else
                        {
                            do
                            {
                                int r = 0;
                                try {
                                    //r = in.read(buffer, n, HDLREN + siz - n);
                                    r = in.read(buffer_label, n, buffer_label.length - n);//读取数据
                                }  
                                catch(Exception e)
                                {
                                    e.printStackTrace();
                                }

                                n += r;

                                //if (DEBUGGING)
                                    //System.out.println(" chunk read, size="+Integer.toString(r)+" total="+Integer.toString(n));

                                /*if (r<=0)
                                {
                                    n = r;
                                    break;
                                }*/
                            }
                            while(n<(siz+HDRLEN));
                        }

                        //if (DEBUGGING)
                            //System.out.println("jpeg read, size="+Integer.toString(n));
                    }//读取完毕
                
                    byte [] buffer2 = new byte [n];
                   
                    for(int i=0; i<n; i++)
                        buffer2[i] = buffer_label[i+HDRLEN];

          
     try{
       BufferedImage image = ImageIO.read(new ByteArrayInputStream(buffer2));//读取图片数据,除格式头                     
       
                      
       ImageIcon ii = new ImageIcon(image);

//                  
 
       /*if (ii.getIconHeight()<=1 || ii.getIconWidth()<=1)
        {
             //if (DEBUGGING)
             //  System.out.println("--invalid image got");
         continue;
        }
       else
        {   
             //if (DEBUGGING)
               //System.out.println("--VALID image got");
        }*/
       
      
       m_label1.setIcon(ii);//显示不带overlay的图片
      } 
     catch (IOException e) {}
                }//while
               
                try {
                    connection.close();
                    
                      // (Alternatively, you might depend on the server
                      //  to close the connection.)
                 }
                catch (IOException e) {
                 }

                m_label1.setIcon(null);
                m_label1.setText("JDialogcamPlayer - Connection closed");
               
                    
                m_stop = true;
               
                return null;
               }
        };
        m_worker1.start();
        }
  
  
   
  }
 }
 private class ConnectServerAction2 implements ActionListener
 {
  public void actionPerformed(ActionEvent event)
  {
   if(dialog2==null) dialog2=new inputSer_IP();
   if(dialog2.showDialog(SimpleFrame.this,"输入服务器IP"))
   {
    
    content2=dialog2.Ser_IP.getText();
    if (DEBUGGING) System.out.println(content2);
    m_worker2 = new SwingWorker() {
           public Object construct() {
   Socket connection;
         InputStream in;
         OutputStream out;
   try {
               int port=7070;
                 connection = new Socket(content2, port);//socket 会话
                 if (DEBUGGING)
                    System.out.println("have create a socket");
                  in=connection.getInputStream();//方法获得网络连接输入
                  if (DEBUGGING)
                    System.out.println("have done in");
                  out = connection.getOutputStream();//方法连接的另一端将得到输入
                  if (DEBUGGING)
                   System.out.println("have done out");
                }
            catch (IOException e) {
                 
                  m_label2.setText("Connection failed with error: " + e);
                  m_label2.setIcon(null);//显示图标
                  if (DEBUGGING)
                   System.out.println(e);
                  return null;
                }
                byte [] buffer_label = new byte [512*1024];
               
            m_label2.setText("");
           
            while(!m_stop)
                {
                    byte [] b = {'O','K',0,0, 0,0,0,0,0,0,0,0,0};
                   
         
                    int n = 0;
                    int siz = 0;

                    try {
                     out.write(b);
                   if (DEBUGGING)
                    System.out.println("do read 50");
                        n = in.read(buffer_label, 0, HDRLEN);//将输入流中最多 50 个数据字节读入字节数组,0 is buffer's index
                   System.out.println(n);
                   if (DEBUGGING)
                     System.out.println("have done read 50");
                     }
                    catch(IOException e) {
                        e.printStackTrace();
                        m_stop = true;
                    }
                   
                    if (n<HDRLEN)
                    {
                        if (n<=0)
                            m_stop = true;

                        continue;
                    }
                    else //n>=50
                    {
                        // can someone point out a more straightforward way to convert 4 bytes
                        // to a java integer? this is just awful...
                        int ssz = SZOFS;//29
            siz += unsignedByteToInt(buffer_label[ssz+3]) << 24;
            siz += unsignedByteToInt(buffer_label[ssz+2]) << 16;
            siz += unsignedByteToInt(buffer_label[ssz+1]) << 8;
            siz += unsignedByteToInt(buffer_label[ssz]) ;
                        if (DEBUGGING)
                            System.out.println("** reading jpeg, size="+Integer.toString(siz));

                        n = HDRLEN;
                        if (buffer_label[0]!='S' || buffer_label[1]!='P' || buffer_label[2]!='C' || buffer_label[3]!='A')
                        {
                            if (DEBUGGING)
                                System.out.println("*Header missing 'SPCA'");
                           
                            continue;
                        }
                        else if (siz<=0 || siz>(512*1024))
                        {
                            siz = 0;
                            try {
                                n = in.read(buffer_label, 0, HDRLEN); //重新读取
                            }  
                            catch(Exception e) {
                                //if (DEBUGGING)
                                    //e.printStackTrace();
                                    m_stop = true;
                            }
                           
                            //if (DEBUGGING)
                                //System.out.println("*Illegal image size");
                           
                            continue;
                        }
                        else
                        {
                            do
                            {
                                int r = 0;
                                try {
                                    //r = in.read(buffer, n, HDLREN + siz - n);
                                    r = in.read(buffer_label, n, buffer_label.length - n);//读取数据
                                }  
                                catch(Exception e)
                                {
                                    e.printStackTrace();
                                }

                                n += r;

                                //if (DEBUGGING)
                                    //System.out.println(" chunk read, size="+Integer.toString(r)+" total="+Integer.toString(n));

                                /*if (r<=0)
                                {
                                    n = r;
                                    break;
                                }*/
                            }
                            while(n<(siz+HDRLEN));
                        }

                        //if (DEBUGGING)
                            //System.out.println("jpeg read, size="+Integer.toString(n));
                    }//读取完毕
                
                    byte [] buffer2 = new byte [n];
                   
                    for(int i=0; i<n; i++)
                        buffer2[i] = buffer_label[i+HDRLEN];

          
     try{
       BufferedImage image = ImageIO.read(new ByteArrayInputStream(buffer2));//读取图片数据,除格式头                     
       
                      
       ImageIcon ii = new ImageIcon(image);

//                  
 
       /*if (ii.getIconHeight()<=1 || ii.getIconWidth()<=1)
        {
             //if (DEBUGGING)
             //  System.out.println("--invalid image got");
         continue;
        }
       else
        {   
             //if (DEBUGGING)
               //System.out.println("--VALID image got");
        }*/
       
      
       m_label2.setIcon(ii);//显示不带overlay的图片
      } 
     catch (IOException e) {}
                }//while
               
                try {
                    connection.close();
                    
                      // (Alternatively, you might depend on the server
                      //  to close the connection.)
                 }
                catch (IOException e) {
                 }

                m_label2.setIcon(null);
                m_label2.setText("JDialogcamPlayer - Connection closed");
               
                    
                m_stop = true;
               
                return null;
               }
        };
        m_worker2.start();
        }
  
  
   
  }
 }
}
class inputSer_IP extends JPanel
{
 JTextField Ser_IP;
 private JButton okButton;
 private JButton cancelButton;
 private JDialog dialog;
 private boolean ok;
 public inputSer_IP()
 {
  //create a panel with textfield
  setLayout(new BorderLayout());
  JPanel panel = new JPanel();
  panel.setLayout(new GridLayout(2,2));
  panel.add(new JLabel("服务器IP地址"));
  panel.add(Ser_IP=new JTextField("192.168.1.111"));
  add(panel,BorderLayout.CENTER);
  //create ok and cancel
  okButton = new JButton("确定");
  okButton.addActionListener(new ActionListener()
  {
   public void actionPerformed(ActionEvent event)
   {
    ok=true;
    dialog.setVisible(false);
   }
  });
  cancelButton =new JButton("取消");
  cancelButton.addActionListener(new ActionListener()
  {
   public void actionPerformed(ActionEvent event)
   {
    dialog.setVisible(false);
   }
  });
  JPanel buttonpanel = new JPanel();
  buttonpanel.add(okButton);
  buttonpanel.add(cancelButton);
  add(buttonpanel,BorderLayout.SOUTH);
 }
 public boolean showDialog(Component parent,String title)
 {
  ok = false;
  Frame owner = null;
  if(parent instanceof Frame)
   owner = (Frame) parent;
  else
   owner = (Frame)SwingUtilities.getAncestorOfClass(Frame.class,parent);
  if(dialog == null||dialog.getOwner()!=owner)
  {
   dialog =new JDialog(owner,true);
   dialog.getContentPane().add(this);
   dialog.getRootPane().setDefaultButton(okButton);
   dialog.pack();
  }
  dialog.setTitle(title);
  Toolkit kit = Toolkit.getDefaultToolkit();
  Dimension screenSize = kit.getScreenSize();
  int screenHeight = screenSize.height;
  int screenWidth = screenSize.width;
  dialog.setLocation(screenSize.width/2,screenSize.height/2);
  dialog.show();
  return ok;
 }
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值