I made a client server program to send an image from the server to the client. The image is being sent being but its not showing up in client window immediately. If i click send on the server then i minimize (or maximize) the client window then open again the image will show up. But if i just click send and dont do anything to the client window nothing shows up until i resize it in some way. I'm not sure why its not just showing up in the client window. Any help would be really appreciated. thanks

Server Code

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Date;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class SendImage extends JFrame {
    BufferedImage bi;
    JButton btn;
    ServerSocket serverSocket;
    Socket socket;

    public SendImage() {
        super("SendImage");
        bi = createImage();
        this.add(btn = new JButton("Send"), BorderLayout.NORTH);
        this.add(new JLabel(new ImageIcon(bi)), BorderLayout.CENTER);
        this.setSize(200, 180);
        this.setVisible(true);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);

        try{
             serverSocket = new ServerSocket(8000);
             socket = serverSocket.accept();
        }
        catch(IOException ex){
            System.err.println(ex);
        }

        btn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {

                try {
                    ImageIO.write(bi, "PNG", socket.getOutputStream());
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });
    }


    public BufferedImage createImage() {
        BufferedImage bi = new BufferedImage(100, 100,
                BufferedImage.TYPE_INT_RGB);
        Graphics2D g2 = (Graphics2D) bi.getGraphics();
        g2.setPaint(Color.YELLOW);
        g2.fillRect(0, 0, 100, 100);
        g2.setPaint(Color.red);
        g2.fillRect(10, 10, 60, 60);
        g2.setPaint(Color.CYAN);
        g2.fillOval(30, 30, 60, 60);
        return bi;
    }

    public static void main(String[] args) {        
                new SendImage();
    }

}

Client Code

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.Socket;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class ReceiveImage extends JFrame {

    public ReceiveImage() {
        super("ReceiveImage");
        this.setSize(200, 180);
        this.setVisible(true);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);

        try {
            Socket socket = new Socket("127.0.0.1", 8000);
            BufferedImage image = ImageIO.read(socket.getInputStream());
            this.add(new JLabel(new ImageIcon(image)), BorderLayout.CENTER);

        } catch (IOException e) {
            // TODO Auto-generated catch block
            System.err.println(e);
        }
    }


    public static void main(String[] args) {        
                new ReceiveImage();
    }

}

Maybe its because your layout manager isn't updating to include the new JLabel until you force it by re-sizing the window. Try calling revalidate() (and maybe also repaint()) after adding the JLabel

thanks! revalidate() worked! :)

<post deleted>

Do not hijack old threads. Start your own new thread if you have a question.

JC

@ Neeraj_9
You asked something in IcyFire's post from 3 years ago. It doesn't appear to be an answer but a necropost. Avoid that. Create your own new question.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.