2009penguin 2023-05-13 22:33 采纳率: 54.5%
浏览 40
已结题

客户端图片无法加载,如何解决?

客户端图片无法加载

package SRC;
//RemoteConnection.java
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.*;

import javax.imageio.ImageIO;
import javax.swing.*;

public class RemoteConnection extends JFrame {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    public Socket client,X,Y;
    public DataInputStream dataInputStream;
    public DataOutputStream X_axisCoordinates,Y_axisCoordinates;
    public JLabel Screen;
    public Timer QuickSettingsForScreenIcons;
    public String host;
    public int port;
    public Toolkit tk = Toolkit.getDefaultToolkit();
    public Dimension screenSize = tk.getScreenSize();
    public int Screen_W = screenSize.width;
    public int Screen_H = screenSize.height;
    public JScrollPane scrollpane;
    
    public RemoteConnection(String host,int port) throws UnknownHostException, IOException {
        super("来自 " + host + " 的屏幕");
        getGraphicsConfiguration().getDevice().setFullScreenWindow(this);
        Screen = new JLabel();
        scrollpane = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        scrollpane.getViewport().add(Screen);    
        add(scrollpane);
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                QuickSettingsForScreenIcons.stop();
                try {
                    setVisible(false);
                    client.close();
                } catch (IOException e2) {
                    JOptionPane.showMessageDialog(null,"A Java Exception has occurred","Error",JOptionPane.ERROR_MESSAGE);
                    e2.printStackTrace();
                }
            }
        });
        this.host = host;
        this.port = port;        
    }
    public void update() throws IOException {
        client = new Socket(host,port);
        //X = new Socket(host,5000);
        //Y = new Socket(host,6000);
        dataInputStream = new DataInputStream(client.getInputStream());
        //double height = dataInputStream.readDouble();
        //double width = dataInputStream.readDouble();
        //X_axisCoordinates = new DataOutputStream(X.getOutputStream());
        //Y_axisCoordinates = new DataOutputStream(Y.getOutputStream());
        /*JLabel.addMouseListener(new MouseAdapter() {
             public void mouseClicked(MouseEvent e) {                 
                 try {
                     X_axisCoordinates.writeInt(e.getX());
                     Y_axisCoordinates.writeInt(e.getY());
                } catch (Exception e1) {
                    JOptionPane.showMessageDialog(null,"A Java Exception has occurred","Error",JOptionPane.ERROR_MESSAGE);
                    e1.printStackTrace();
                }                 
             }
        });*/
        QuickSettingsForScreenIcons = new Timer(0,(e ->{
            try {
                int len = dataInputStream.readInt();
                byte[] ImageData = new byte[len];
                dataInputStream.readFully(ImageData);
                ByteArrayInputStream in = new ByteArrayInputStream(ImageData);
                BufferedImage OriginalImage = ImageIO.read(in);
                Image ModifiedImage = OriginalImage.getScaledInstance(Screen_W,Screen_H,Image.SCALE_DEFAULT);
                ImageIcon ICON = new ImageIcon(ModifiedImage);                
                Screen.setIcon(ICON);
                Screen.updateUI();
                Screen.repaint();
            } catch (Exception e1) {
                JOptionPane.showMessageDialog(null,"A Java Exception has occurred","Error",JOptionPane.ERROR_MESSAGE);
                e1.printStackTrace();
                QuickSettingsForScreenIcons.stop();
                try {
                    client.close();
                } catch (IOException e2) {
                    JOptionPane.showMessageDialog(null,"A Java Exception has occurred","Error",JOptionPane.ERROR_MESSAGE);
                    e2.printStackTrace();
                }
            }
        }));
        try {
            QuickSettingsForScreenIcons.start();
        } catch (Exception e2) {
            client.close();
            QuickSettingsForScreenIcons.stop();
            JOptionPane.showMessageDialog(null,"A Java Exception has occurred","Error",JOptionPane.ERROR_MESSAGE);
            e2.printStackTrace();
        }
        
    }
    /*@SuppressWarnings("static-access")
    public static void main(String[] args) throws UnknownHostException, IOException, AWTException {
        Toolkit TK = Toolkit.getDefaultToolkit();
        Dimension D = TK.getScreenSize();
        Rectangle R = new Rectangle(D);
        Robot rr = new Robot();
        BufferedImage BI = rr.createScreenCapture(R);
        RemoteConnection r = new RemoteConnection("20",12);    
        r.setVisible(true);
        r.Screen.setIcon(new ImageIcon(BI));
    }*/    
}


package SRC;
//ModeSettings.java
import java.awt.*;
import javax.swing.*;

public class ModeSettings extends JFrame{
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    public JOptionPane P = new JOptionPane(null, JOptionPane.INFORMATION_MESSAGE);    
    public ButtonGroup bp = new ButtonGroup();
    public JRadioButton ScreenProjection = new JRadioButton("仅投屏");
    public JRadioButton RemoteControl = new JRadioButton("远程控制");
    public JButton Yes = new JButton("连接");
    public JButton Cancellation = new JButton("返回上一步");
    public JButton Exit = new JButton("退出");
    
    public ModeSettings() {
        super("模式设置");                
        Yes.addActionListener((e) ->{
            if(ScreenProjection.isSelected() || RemoteControl.isSelected()) {
                String IP = ClientMain.SI.ip.getText();
                int PORT = Integer.parseInt(ClientMain.SI.port.getText());
                ClientMain.MS.setVisible(false);                
                try {
                    ClientMain.RC = new RemoteConnection(IP,PORT);
                    ClientMain.RC.setVisible(true);
                    ClientMain.RC.update();                    
                } catch (Exception e1) {
                    JOptionPane.showMessageDialog(null,"A Java Exception has occurred","Error",JOptionPane.ERROR_MESSAGE);
                    e1.printStackTrace();
                    System.exit(0);
                }
            }else {
                JOptionPane.showMessageDialog(null,"请选择选项","警告",JOptionPane.WARNING_MESSAGE);
            }            
        });
        Cancellation.addActionListener((e) ->{
            this.setVisible(false);
            ClientMain.SI.setVisible(true);            
        });
        Exit.addActionListener((e) ->{
            System.exit(0);
        });
        bp.add(ScreenProjection);
        bp.add(RemoteControl);
        Object[] Options = {ScreenProjection,RemoteControl,Yes,Cancellation,Exit};
        P.setMessage(Options);
        setLayout(new FlowLayout(FlowLayout.LEFT));
        add(P);
        Toolkit kit = Toolkit.getDefaultToolkit();
        Dimension screenSize = kit.getScreenSize();
        int screenw = screenSize.width;
        int screenh = screenSize.height;
        setLocation((screenw - 300) / 2, (screenh - 200) / 2);
        setSize(300,200);
    }
}


package SRC;
//ClientMain.java

public class ClientMain {    
    static StartInterface SI = new StartInterface();
    static ModeSettings MS = new ModeSettings();
    static RemoteConnection RC;
    public static void main(String[] args) {        
        SI.setVisible(true);        
    }    

}


package SRC;
//StartInterface.java
import java.awt.*;
import java.util.regex.*;
import javax.swing.*;

public class StartInterface extends JFrame{
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    public JOptionPane P = new JOptionPane(null, JOptionPane.INFORMATION_MESSAGE);    
    public JLabel RemoteLink = new JLabel("是否进行远程连接?");
    public JLabel IP = new JLabel("IP : ");
    public JTextField ip = new JTextField();
    public JLabel Port = new JLabel("Port : ");
    public JTextField port = new JTextField();
    public JButton Link = new JButton("下一步");
    public JButton Exit = new JButton("退出");
    
    public StartInterface() {
        super("远程连接");            
        Object[] Options = {RemoteLink,IP,ip,Port,port,Link,Exit};
        P.setLayout(new GridLayout(3,2,5,5));
        P.setMessage(Options);
        setLayout(new FlowLayout(FlowLayout.LEFT));
        add(P);
        Link.addActionListener((e) -> {
            if(port.getText() != null) {
                if(IsTheDataLegal(ip.getText(),port.getText())) {                        
                    System.out.println(true);
                    ClientMain.SI.setVisible(false);
                    ClientMain.MS.setVisible(true);
                }
            }
        });
        Exit.addActionListener((e) -> {
            System.exit(0);        
        });        
        Toolkit kit = Toolkit.getDefaultToolkit();
        Dimension screenSize = kit.getScreenSize();
        int screenw = screenSize.width;
        int screenh = screenSize.height;
        setLocation((screenw - 300) / 2, (screenh - 225) / 2);
        setSize(300,225);
    }
    public static boolean IsTheDataLegal(String IP,String Port) {
        String regex = "^((\\d|[1-9]\\d|1\\d\\d|2[0-4]\\d|25[0-5]|[*])\\.){3}(\\d|[1-9]\\d|1\\d\\d|2[0-4]\\d|25[0-5]|[*])$";
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(IP);
        if(m.find()){
            try {
                if(Pattern.compile("\\d{6}").matcher(Port).find() || 
                   Pattern.compile("\\d{5}").matcher(Port).find() || 
                   Pattern.compile("\\d{4}").matcher(Port).find()) {
                    int PORT = Integer.parseInt(Port);
                    if(PORT > 1024 && PORT < 49151) {
                        if(PORT == 5000 || PORT == 6000 || PORT == 7000 || PORT == 8000) {
                            JOptionPane.showMessageDialog(null,"请勿输入为5000,6000,7000,8000的Port值","警告",JOptionPane.WARNING_MESSAGE);
                            return false;
                        }else {
                            return true;
                        }                        
                    }else{
                        JOptionPane.showMessageDialog(null,"Port输入不合法","警告",JOptionPane.WARNING_MESSAGE);
                        return false;
                    }
                }else {
                    JOptionPane.showMessageDialog(null,"Port输入不合法","警告",JOptionPane.WARNING_MESSAGE);
                }
                
            }catch(Exception e) {
                e.printStackTrace();
                return false;
            }            
        }else{
            JOptionPane.showMessageDialog(null,"IP输入不合法","警告",JOptionPane.WARNING_MESSAGE);
            return false;
        }
        return false;                
    }
}


  • 写回答

1条回答 默认 最新

  • 徐州蔡徐坤 2023-05-14 08:39
    关注

    问问题描述清楚一点,什么场景,报错信息是什么

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 5月28日
  • 已采纳回答 5月20日
  • 创建了问题 5月13日

悬赏问题

  • ¥15 vb6.0+webbrowser无法加载某个网页求解
  • ¥15 RPA财务机器人采购付款流程
  • ¥15 计算机图形多边形及三次样条曲线绘制
  • ¥15 根据protues画的图用keil写程序
  • ¥200 如何使用postGis实现最短领规划?
  • ¥15 pyinstaller打包错误
  • ¥20 cesm的气溶胶排放文件
  • ¥15 逐月累计,月份不连续,补齐月份
  • ¥15 应用简单的Python代码完成一个学生成绩管理系统
  • ¥15 用matlab求微分方程初值问题