13 Replies - 1360 Views - Last Post: 27 June 2011 - 09:28 AM Rate Topic: -----

#1 scopolamine  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 19
  • Joined: 10-April 11

importing and loading an image by socket

Posted 27 June 2011 - 08:31 AM

the problem I've been dealing with is getCodeBase(). how do i deal with this method?
        BufferedImage img = null;
        try {
        img = ImageIO.read(new File("apple.jpg"));
             } 
        catch (IOException e) {
        }
         try {
        URL url = new URL(getCodeBase(), "apple.jpg");
        img = ImageIO.read(url);
             } 
         catch (IOException e) {
}

but if you want the whole code:

the server:
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class Server extends JFrame {
   private JTextArea display;
   ObjectOutputStream output;
   ObjectInputStream input;

   public Server()
   {
      super( "Server" );

      Container c = getContentPane();

      display = new JTextArea();
      c.add( new JScrollPane( display ),
             BorderLayout.CENTER );

      setSize( 300, 150 );
      show();
   }

   public void runServer()
   {
      ServerSocket server;
      Socket connection;
      int counter = 1;

      try {
         // Step 1: Create a ServerSocket.
         server = new ServerSocket( 5000, 100 );

         while ( true ) {
            // Step 2: Wait for a connection.
            display.setText( "Waiting for connection\n" );
            connection = server.accept();

            display.append( "Connection " + counter + " received from: " +
                                 connection.getInetAddress().getHostName() );

            // Step 3: Get input and output streams.
            output = new ObjectOutputStream( connection.getOutputStream() );
            output.flush();
            input = new ObjectInputStream( connection.getInputStream() );
            display.append( "\nGot I/O streams\n" );

            // Step 4: Process connection.
            String message = "SERVER>>> Connection successful";
            output.writeObject( message );
            output.flush();
/*
            do {
               try {
                  message = (String) input.readObject();
                  display.append( "\n" + message );
                  display.setCaretPosition( display.getText().length() );
               }
               catch ( ClassNotFoundException cnfex ) {
                  display.append( "\nUnknown object type received" );
               }
            } while ( !message.equals( "CLIENT>>> TERMINATE" ) );
*/
            // Step 5: Close connection.
            display.append( "\nUser terminated connection" );
            output.close();
            input.close();
            connection.close();

            ++counter;
         }
      }
      catch ( EOFException eof ) {
         System.out.println( "Client terminated connection" );
      }
      catch ( IOException io ) {
         io.printStackTrace();
      }
   }

/*   private void sendData( String s )
   {
      try {
         output.writeObject( "SERVER>>> " + s );
         output.flush();
         display.append( "\nSERVER>>>" + s );
      }
      catch ( IOException cnfex ) {
         display.append( "\nError writing object" );
      }
   }*/

   public static void main( String args[] )
   {
      Server app = new Server();

      app.addWindowListener(
         new WindowAdapter() {
            public void windowClosing( WindowEvent e )
            {
               System.exit( 0 );
            }
         }
      );

      app.runServer();
   }
}

the client:
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import javax.swing.*;


public class Client extends JFrame {
   private JTextArea display;
   ObjectOutputStream output;
   ObjectInputStream input;
   String message = "";

   public Client()
   {
      super( "Client" );

      Container c = getContentPane();

      display = new JTextArea();
      c.add( new JScrollPane( display ), BorderLayout.CENTER );

      setSize( 300, 150 );
      show();
   }

   public void runClient()
   {
      Socket client;

      try {
         // Step 1: Create a Socket to make connection.
         display.setText( "Attempting connection\n" );
         client = new Socket( InetAddress.getByName( "127.0.0.1" ), 5000 );

         display.append( "Connected to: " + client.getInetAddress().getHostName() );

         // Step 2: Get the input and output streams.
         output = new ObjectOutputStream( client.getOutputStream() );
         output.flush();
         input = new ObjectInputStream( client.getInputStream() );
         display.append( "\nGot I/O streams\n" );

         // Step 3: Process connection.

        BufferedImage img = null;
        try {
        img = ImageIO.read(new File("apple.jpg"));
             } 
        catch (IOException e) {
        }
         try {
        URL url = new URL(getCodeBase(), "apple.jpg");
        img = ImageIO.read(url);
             } 
         catch (IOException e) {
}
/*         do {
            try {
               message = (String) input.readObject();
               display.append( "\n" + message );
               display.setCaretPosition( display.getText().length() );
            }
            catch ( ClassNotFoundException cnfex ) {
               display.append( "\nUnknown object type received" );
            }
         } while ( !message.equals( "SERVER>>> TERMINATE" ) );
*/
         
         // Step 4: Close connection.
         display.append( "Closing connection.\n" );
         output.close();
         input.close();
         client.close();
      }
      catch ( EOFException eof ) {
         System.out.println( "Server terminated connection" );
      }
      catch ( IOException e ) {
         e.printStackTrace();
      }
   }

/*   private void sendData( String s )
   {
      try {
         message = s;
         output.writeObject( "CLIENT>>> " + s );
         output.flush();
         display.append( "\nCLIENT>>>" + s );
      }
      catch ( IOException cnfex ) {
         display.append( "\nError writing object" );
      }
   }*/

   public static void main( String args[] )
   {
      Client app = new Client();

      app.addWindowListener(
         new WindowAdapter() {
            public void windowClosing( WindowEvent e )
            {
               System.exit( 0 );
            }
         }
      );

      app.runClient();
   }
}

This post has been edited by scopolamine: 27 June 2011 - 08:32 AM


Is This A Good Question/Topic? 0
  • +

Replies To: importing and loading an image by socket

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10596
  • View blog
  • Posts: 39,259
  • Joined: 27-December 08

Re: importing and loading an image by socket

Posted 27 June 2011 - 08:35 AM

What exactly is the problem? Please be more specific so we can better help you.
Was This Post Helpful? 1
  • +
  • -

#3 scopolamine  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 19
  • Joined: 10-April 11

Re: importing and loading an image by socket

Posted 27 June 2011 - 08:39 AM

Exception in thread "main" java.lang.RuntimeException: Uncompilable source code
at Client.runClient(Client.java:55)

Compile error. It doesn't recognize getCodeBase() method. It should work properly and load the image.
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10596
  • View blog
  • Posts: 39,259
  • Joined: 27-December 08

Re: importing and loading an image by socket

Posted 27 June 2011 - 08:42 AM

The getCodeBase() method isn't defined in the JFrame class. It is only defined in the Applet class. Just pass the exact String for the URL you want to download the Image from to the URL constructor.
Was This Post Helpful? 1
  • +
  • -

#5 scopolamine  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 19
  • Joined: 10-April 11

Re: importing and loading an image by socket

Posted 27 June 2011 - 08:48 AM

View Postmacosxnerd101, on 27 June 2011 - 08:42 AM, said:

The getCodeBase() method isn't defined in the JFrame class. It is only defined in the Applet class. Just pass the exact String for the URL you want to download the Image from to the URL constructor.

Okay. It worked. But how do I display the image downloaded?
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10596
  • View blog
  • Posts: 39,259
  • Joined: 27-December 08

Re: importing and loading an image by socket

Posted 27 June 2011 - 08:49 AM

You could create an ImageIcon from it, and use the setIcon() method on a JComponent. Or you could handle this using custom painting and use the Graphics param in the paint() or paintComponent() methods to draw it.
Was This Post Helpful? 1
  • +
  • -

#7 scopolamine  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 19
  • Joined: 10-April 11

Re: importing and loading an image by socket

Posted 27 June 2011 - 08:56 AM

        BufferedImage img = null;
        try {
        img = ImageIO.read(new File("http://www.google.com/logos/google.jpg"));
             }
        catch (IOException e) {
        }
         try {
        URL url = new URL("http://www.google.com/logos/google.jpg");
        img = ImageIO.read(url);
        ImageIcon icon = new ImageIcon("http://www.google.com/logos/google.jpg");
             }
         catch (IOException e) {
}

When I look my project directory, it doesn't include the downloaded image. Before I start to deal with displaying an image by using ImageIcon, what did I do wrong?
Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10596
  • View blog
  • Posts: 39,259
  • Joined: 27-December 08

Re: importing and loading an image by socket

Posted 27 June 2011 - 08:58 AM

Print the Stack traces for your Exceptions to see if anything is going wrong. Also, the read() method just reads the Image. You have to write it to your local File system.
Was This Post Helpful? 1
  • +
  • -

#9 scopolamine  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 19
  • Joined: 10-April 11

Re: importing and loading an image by socket

Posted 27 June 2011 - 09:07 AM

View Postmacosxnerd101, on 27 June 2011 - 08:58 AM, said:

Print the Stack traces for your Exceptions to see if anything is going wrong. Also, the read() method just reads the Image. You have to write it to your local File system.

Well, you were right. I forgot to add write method.
        BufferedImage img = null;
         try {
        URL url = new URL("http://www.google.com/logos/google.jpg");
        img = ImageIO.read(url);
        ImageIO.write(img, "jpg",new File("C:\\google.jpg"));
        JLabel icon1 = new JLabel();
        ImageIcon icon = new ImageIcon("C:\\google.jpg");
        icon1.setIcon(icon);
             }

Now, it downloads the image. I use ImageIcong to display the image but still can't display the image.
Was This Post Helpful? 0
  • +
  • -

#10 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10596
  • View blog
  • Posts: 39,259
  • Joined: 27-December 08

Re: importing and loading an image by socket

Posted 27 June 2011 - 09:09 AM

What is icon1? Did you add it to your JPanel, and the JPanel to a JFrame?
Was This Post Helpful? 1
  • +
  • -

#11 scopolamine  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 19
  • Joined: 10-April 11

Re: importing and loading an image by socket

Posted 27 June 2011 - 09:20 AM

View Postmacosxnerd101, on 27 June 2011 - 09:09 AM, said:

What is icon1? Did you add it to your JPanel, and the JPanel to a JFrame?

I created a JLabel and thought it would display on the same frame. I changed the code because it didn't work.
        BufferedImage img = null;
         try {
        URL url = new URL("http://www.google.com/logos/google.jpg");
        img = ImageIO.read(url);
        ImageIO.write(img, "jpg",new File("C:\\google.jpg"));
        JLabel icon1 = new JLabel();
        ImageIcon icon = new ImageIcon("C:\\google.jpg");
        icon1.setIcon(icon);
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setSize(400,400);
        f.setLocation(200,200);
        f.setVisible(true);
             }

JFrame still doesn't show the image. How can I add the image to my JFrame?
Was This Post Helpful? 0
  • +
  • -

#12 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10596
  • View blog
  • Posts: 39,259
  • Joined: 27-December 08

Re: importing and loading an image by socket

Posted 27 June 2011 - 09:22 AM

Use the JFrame add() method to add the JLabel to the JFrame.
Was This Post Helpful? 1
  • +
  • -

#13 scopolamine  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 19
  • Joined: 10-April 11

Re: importing and loading an image by socket

Posted 27 June 2011 - 09:27 AM

View Postmacosxnerd101, on 27 June 2011 - 09:22 AM, said:

Use the JFrame add() method to add the JLabel to the JFrame.

        BufferedImage img = null;
         try {
        URL url = new URL("http://www.google.com/logos/google.jpg");
        img = ImageIO.read(url);
        ImageIO.write(img, "jpg",new File("C:\\google.jpg"));
        JLabel icon1 = new JLabel();
        ImageIcon icon = new ImageIcon("C:\\google.jpg");
        icon1.setIcon(icon);
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setSize(400,400);
        f.setLocation(200,200);
        f.setVisible(true);
        f.add(icon1);
             }

This is the final version of the code. Thanks for help.

If you hadn't helped me, I could have never pulled it off. I'm so thankful to you. Thank you, again.
Was This Post Helpful? 1
  • +
  • -

#14 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10596
  • View blog
  • Posts: 39,259
  • Joined: 27-December 08

Re: importing and loading an image by socket

Posted 27 June 2011 - 09:28 AM

Thanks for sharing your code. Glad I could help! :)
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1