-2

I have a problem that I don't seem to be able to solve on my own so I turned to stackoverflow.

I think my title says it all. The value I recieve from the inputstream is null the second time I try to get an BufferedImage.

I tried checking if there is a problem in the outputstream in my other java file. However I do not think there is one there since I'm not recieving any errors or problems with the image being sent into the outputstream.

I'm adding the code below

public class controllerSoft {
    Socket connectionSocket;
    public boolean isConnected = false;

    public static void main(String[] args) {
        controllerSoft cont = new controllerSoft();
        cont.gui();
    }

    public void gui() {
        JFrame frame = new JFrame("Controller Software");
        JPanel mainPanel = new JPanel();
        JPanel imagePanel = new JPanel();
        JPanel controllerPanel = new JPanel();
        JButton takePictureBtn = new JButton("Take ScreenShot");
        JButton connectBtn = new JButton("Connect");

        frame.add(mainPanel);
        mainPanel.add(imagePanel);
        mainPanel.add(controllerPanel);
        controllerPanel.add(takePictureBtn);
        controllerPanel.add(connectBtn);
        mainPanel.setLayout(new GridLayout(2,2));
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setSize(700, 700);
        imagePanel.setBorder(BorderFactory.createLoweredBevelBorder());
        controllerPanel.setBorder(BorderFactory.createRaisedBevelBorder());
        setUpActionListener(takePictureBtn, connectBtn, imagePanel);
    }

    public void setUpActionListener(JButton takePictureBtn, JButton connectBtn, JPanel imagePanel) {
        connectBtn.addActionListener(new ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent e) {
                makeConnection();
            }
        });

        takePictureBtn.addActionListener(new ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent e) {
                if (isConnected) {
                    try {
                       boolean shouldStart = true;  // This boolean I send through socket

                       /* Send boolean through socket. Reciever takes the boolean and starts the while-loop... */
                       ObjectOutputStream outputStream = new ObjectOutputStream(connectionSocket.getOutputStream());
                       outputStream.writeObject(shouldStart);
                       outputStream.flush();

                       /* If there are any children of the imagePanel then I should remove them since only one should be present at all times. */
                       imagePanel.removeAll();

                       getAndDisplayImage(imagePanel);
                    } catch (IOException exception) {
                        System.out.println(exception);
                    }
                } else {
                    JOptionPane.showMessageDialog(takePictureBtn, "A connection to the other party has to be made...");
                }
            }
        });
    }

    public void makeConnection() {
        try {
            connectionSocket = new Socket("localhost", 4000);
            System.out.println("Connection successfull!");
            isConnected = true;
        } catch (IOException e) {
            System.out.println("Failed connecting...\t" + e);
        }
    }

    public void getAndDisplayImage(JPanel imagePanel) {
        /* Here I display the image */
        try {   
            BufferedImage img;
            img = ImageIO.read(ImageIO.createImageInputStream(connectionSocket.getInputStream()));

            /* The second time I try to call img it returns as null... why? */
            System.out.println("image is:\t" + img);
            imagePanel.add(new JLabel(new ImageIcon(img)));
            imagePanel.revalidate();
        } catch (IOException e) {
            System.out.println("Couldnt display the image:\t" + e);
        }
    }
}
public class listenUnit {
    ServerSocket serverSocket;
    Socket controllerSocket;
    public boolean shouldEnd = false;
    public boolean shouldStart = false;
    public boolean shouldWait = true;

    public static void main(String[] args) {
        listenUnit unit = new listenUnit();
        unit.makeConnection();
    }

    public void makeConnection() {
        try {
            serverSocket = new ServerSocket(4000);
            controllerSocket = serverSocket.accept();
            System.out.println("Connection successfull!");

            while (!shouldEnd) {
                /* Send the boolean (shouldStart) through the socket to the controllerSoft.java */
                ObjectInputStream objectInputStream = new ObjectInputStream(controllerSocket.getInputStream());

                try {
                    boolean recievedBoolean = (boolean) objectInputStream.readObject();
                    shouldStart = recievedBoolean;
                    System.out.println("shouldStart is: " + shouldStart);
                } catch (ClassNotFoundException e) {
                    System.out.println("Class not found: \t" + e);
                }

                if (shouldStart) {
                    getAndSendImage();
                    shouldStart = false;
                } else {
                    /* If (shouldStart) isnt true yet I just wait until it is... */
                    try {
                        System.out.println("Boolean shouldStart isn't true yet...");
                        TimeUnit.SECONDS.sleep(2); 
                    } catch(InterruptedException e) {
                        System.out.println("Sleep timer got interrupted: \t" + e);
                    }
                }
            }
        } catch (IOException e) {
           System.out.println("Connection failed: \t" + e); 
        }
    }

    public void getAndSendImage() {
        /* Here we take a screenshot */
        int sizeX = 350;
        int sizeY = 650;

        try {
           BufferedImage image = new Robot().createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
           System.out.println("Click! Image taken!" + image);

           try {
               Image tmp = image.getScaledInstance(sizeY, sizeX, Image.SCALE_SMOOTH);
               BufferedImage newImage = new BufferedImage(sizeY, sizeX, BufferedImage.TYPE_INT_ARGB);
               Graphics2D g2d = newImage.createGraphics();

               g2d.drawImage(tmp, 0, 0, null);
               g2d.dispose();
               ImageIO.write(newImage, "png", controllerSocket.getOutputStream());//This one sends the image
               controllerSocket.getOutputStream().flush();
               System.out.println("Click! Image sent!" + newImage);
           } catch (IOException e) {
               System.out.println("Failed to shrink image:\t" + e);
           }
        } catch (AWTException e) {
            System.out.println("Failed to take ScreenShot:\t" + e);
        }
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Buddvar
  • 13
  • 1
  • 5

1 Answers1

0

Okay, I found an answer to my question/problem. If I click once on my takePictureBtn it gives the proper result. The second and third time it gives me a value of null however the fourth time I click the button it decides to give me the proper result...

I do not know what causes this problem however I might be able to solve this on my own from now on.

Thanks Buddvar

I found a solution to my problem now. It works perfectly!

    public void getAndDisplayImage(JPanel imagePanel)
{
    /*Here I display the image*/
    boolean canReadImage = false;
    try
    {   
        while(!canReadImage)
        {
            BufferedImage img;
            img = ImageIO.read(ImageIO.createImageInputStream(connectionSocket.getInputStream()));
            while(img != null)
            {
                /*The second time I try to call img it returns as null...why?*/
                System.out.println("image is:\t" + img);
                imagePanel.add(new JLabel(new ImageIcon(img)));
                imagePanel.revalidate();
                canReadImage = true;
                break;
            }
        }
    }
    catch(IOException e)
    {
        System.out.println("Couldnt display the image:\t" + e);
    }
}
Buddvar
  • 13
  • 1
  • 5