Welcome to Dream.In.Code
Click Here
Getting Java Help is Easy!

Join 117,158 Java Programmers for FREE! Ask your question and get quick answers from experts. There are 2,382 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



URL image display applet

2 Pages V  1 2 >  
Reply to this topicStart new topic

URL image display applet

nombre
post 30 Jun, 2008 - 09:18 AM
Post #1


New D.I.C Head

*
Joined: 7 Jun, 2008
Posts: 49

CODE
try {
            url = new URL("http://xxxxxxxxxxxxxxxxxxxxxxxxxx");
            
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        Toolkit tk = Toolkit.getDefaultToolkit();
        try{
            image = ImageIO.read(url);
        }catch(IOException io){}
        try{
            MediaTracker tracker = new MediaTracker(this);
        

        
        tracker.addImage(image, 1);
        tracker.waitForAll();
        }catch(InterruptedException ie){}
  
        exitButton = new JButton("Exit");
        exitButton.addActionListener(this);


        ImagePanel imagePanel = new ImagePanel(image);
        repaint();
        txtArea.setSize(10, 10);
        contentPane.add(imagePanel, BorderLayout.CENTER);
        contentPane.add(exitButton, BorderLayout.SOUTH);
        contentPane.add(txtArea, BorderLayout.NORTH);
        
  
      
        };


When I try to display the jpeg's in my applet the pictures are not complete they are cut off.

Can anyone help? I switched ImagePanel to Label and I have the same problem.

This post has been edited by nombre: 30 Jun, 2008 - 10:41 AM
User is offlineProfile CardPM

Go to the top of the page


pbl
post 30 Jun, 2008 - 10:43 AM
Post #2


D.I.C Lover

Group Icon
Joined: 6 Mar, 2008
Posts: 2,270



Thanked 131 times

Dream Kudos: 75
My Contributions


Probably ImagePanel() that does not do its job correctly.
What does the class ImagePanel looks like ?
User is offlineProfile CardPM

Go to the top of the page

nombre
post 30 Jun, 2008 - 10:45 AM
Post #3


New D.I.C Head

*
Joined: 7 Jun, 2008
Posts: 49

CODE
class ImagePanel extends JPanel {

    Image image;

    public ImagePanel(Image image) {
        this.image = image;
    }

    public void paintComponent(Graphics g) {

        // paint background
        super.paintComponent(g);
      

        
      


        //Draw image at its natural size
        g.drawImage(image, 0, 0, this); //85x62 image
        
    }


One more question is there a way to do the read with a while loop to make sure it's all there.

This post has been edited by nombre: 30 Jun, 2008 - 10:50 AM
User is offlineProfile CardPM

Go to the top of the page

pbl
post 30 Jun, 2008 - 10:56 AM
Post #4


D.I.C Lover

Group Icon
Joined: 6 Mar, 2008
Posts: 2,270



Thanked 131 times

Dream Kudos: 75
My Contributions


CODE

    public void paintComponent(Graphics g) {

        // paint background
        super.paintComponent(g);
        //Draw image at its natural size
        g.drawImage(image, 0, 0, this); //85x62 image
        
    }


As you say yourself in the comments you draw it "at its natural size" so if if does not fit in the panel it will be truncated
If you want to resize the image to fit you'll have to use

g.drawImage(image, 0, 0, getWidth(), getHeight(), imageObserver);
User is offlineProfile CardPM

Go to the top of the page

pbl
post 30 Jun, 2008 - 11:01 AM
Post #5


D.I.C Lover

Group Icon
Joined: 6 Mar, 2008
Posts: 2,270



Thanked 131 times

Dream Kudos: 75
My Contributions


QUOTE(nombre @ 30 Jun, 2008 - 10:45 AM) *

One more question is there a way to do the read with a while loop to make sure it's all there.


Would be easier with a JLabel... they you just create a ImageIcon and the coinstructor of ImageIcon(URL) will returned only when to read is complete. No neeed to use ToolKit and ImageIO.read();

Another DIC had the same problem few days ago

http://www.dreamincode.net/forums/showtopic55126.htm

User is offlineProfile CardPM

Go to the top of the page

nombre
post 30 Jun, 2008 - 11:10 AM
Post #6


New D.I.C Head

*
Joined: 7 Jun, 2008
Posts: 49

The picture is cut off abnormally. There is a place for the picture to fit but the picture is unfinished. Like it didn't finish downloading. Is there a way to make sure that the whole picture is transferred.
User is offlineProfile CardPM

Go to the top of the page

pbl
post 30 Jun, 2008 - 11:45 AM
Post #7


D.I.C Lover

Group Icon
Joined: 6 Mar, 2008
Posts: 2,270



Thanked 131 times

Dream Kudos: 75
My Contributions


QUOTE(nombre @ 30 Jun, 2008 - 11:10 AM) *

The picture is cut off abnormally. There is a place for the picture to fit but the picture is unfinished. Like it didn't finish downloading. Is there a way to make sure that the whole picture is transferred.

MediaTracker should do that... thsi why it was created
Did you try to System.out.println("Error: " + e); in your catch() statements ?
Did you try to resize() your Applet so see if a second repaint() does the job ?
You should also test isErrorAny() on the meia tracker.

This post has been edited by pbl: 30 Jun, 2008 - 11:58 AM
User is offlineProfile CardPM

Go to the top of the page

nombre
post 30 Jun, 2008 - 01:00 PM
Post #8


New D.I.C Head

*
Joined: 7 Jun, 2008
Posts: 49

CODE
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.awt.image.RescaleOp;

import javax.swing.*;

import java.applet.*;
import javax.imageio.*;

import java.io.*;
import java.awt.geom.AffineTransform;
import java.awt.image.ImageObserver;



public class applet extends JApplet
                             implements ActionListener {
    
    private JButton  exitButton = null;
    private URL     url        = null;
    JLabel label=null;    
    /**
     * Noarg constructor
     */
    public applet() {


        Container contentPane = this.getContentPane();
        contentPane.setLayout(new BorderLayout());
        StringBuffer strBuff;
        String line;
        TextArea txtArea= new TextArea();
        Image image=null;
        File fima;
        URL url2=null;
        try
        {
            url = new URL("xxxxxxxxxx");
        }
        catch(MalformedURLException e){e.printStackTrace();}

        try{
            
            InputStream in = url.openStream();
            BufferedReader bf = new BufferedReader(new InputStreamReader(in));
            strBuff = new StringBuffer();
            while((line = bf.readLine()) != null){
              strBuff.append(line + "\n");
            }
          System.out.println(strBuff.toString());
          txtArea.append(strBuff.toString());
          }
          catch(IOException e){
            e.printStackTrace();
          }
          
        // ---------------------------------------------------------------------
        // Now get the Image (graphic) from the given URL
        // ---------------------------------------------------------------------
        try {
            url2 = new URL("xxxxxxxxxxxxxxxxxxxxxxx");
            
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
      
        try{
            image = ImageIO.read(url2);
        }catch(IOException io){System.out.println(io);}
        try{
            MediaTracker tracker = new MediaTracker(this);
        

        
        tracker.addImage(image, 1);
        tracker.waitForAll();
        }catch(InterruptedException ie){System.out.println(ie);}
  
        exitButton = new JButton("Exit");
        exitButton.addActionListener(this);

    
        
        // ---------------------------------------------------------------------
        // Exit button
        // ---------------------------------------------------------------------
        exitButton = new JButton("Exit");
        exitButton.addActionListener(this);


        // ---------------------------------------------------------------------
        // Finally create a ImagePanel to display the image and exit button
        // ---------------------------------------------------------------------
        


        ImagePanel i = new ImagePanel(image);
        txtArea.setSize(5, 5);
        contentPane.add(i, BorderLayout.CENTER);
        contentPane.add(exitButton, BorderLayout.SOUTH);
        contentPane.add(txtArea, BorderLayout.NORTH);
        i.setAlignmentY(JLabel.CENTER_ALIGNMENT);
        repaint();
        // ------------------------------------------------------------
        // Window listener to close application when Window gets closed
        // ------------------------------------------------------------
      
        };

    


    /**
     * The action listener for user events like buttons.
     * @param ae Event that was performed by the user
     */
    public void actionPerformed(ActionEvent ae) {

        String action = ae.getActionCommand();
                
        if (action.equals("Exit")) {
            
            System.out.println("Exiting.");
            System.exit(0);
        } else {
            System.out.println(action);
        }

    }
            

    /**
     * Sole entry point to the class and application.
     * @param args Array of String arguments.
     */
    public static void main(String[] args) {

        applet mainFrame = new applet();
        mainFrame.setVisible(true);
        mainFrame.setSize(1000,800);
        
    }

}


class ImagePanel extends JPanel {

    Image image;
    ImageObserver im;
    public ImagePanel(Image image) {
        this.image = image;
    }

    public void paintComponent(Graphics g) {

        // paint background
        super.paintComponent(g);
      

        
      


        //Draw image at its natural size
        g.drawImage(image, 0, 0, this);  

        
    }
}



I still can't get the whole picture to display it gets cut off before its finished. This is my whole code. Please help.

This post has been edited by nombre: 30 Jun, 2008 - 01:01 PM
User is offlineProfile CardPM

Go to the top of the page

pbl
post 30 Jun, 2008 - 01:20 PM
Post #9


D.I.C Lover

Group Icon
Joined: 6 Mar, 2008
Posts: 2,270



Thanked 131 times

Dream Kudos: 75
My Contributions


You run that as an Application or as an Applet ?
If it is an Applet you doin't have the init() method
If it is an Application you don't build a JFrame
User is offlineProfile CardPM

Go to the top of the page

nombre
post 30 Jun, 2008 - 01:29 PM
Post #10


New D.I.C Head

*
Joined: 7 Jun, 2008
Posts: 49

I'm running it as an applet. What do I need in init()?

In a previous program I had to do something like this. Are there comparable commands for my situation.

CODE
InputStream in = new FileInputStream(files.elementAt(x));
                OutputStream out = new FileOutputStream(temp2);
                byte[] buf = new byte[1024];
                int len;
                while ((len = in.read(buf)) > 0) {
                    out.write(buf, 0, len);
                }
                out.flush();
                in.close();
                out.close();


This post has been edited by nombre: 30 Jun, 2008 - 02:07 PM
User is offlineProfile CardPM

Go to the top of the page

pbl
post 30 Jun, 2008 - 02:38 PM
Post #11


D.I.C Lover

Group Icon
Joined: 6 Mar, 2008
Posts: 2,270



Thanked 131 times

Dream Kudos: 75
My Contributions


QUOTE(nombre @ 30 Jun, 2008 - 01:29 PM) *

I'm running it as an applet. What do I need in init()?

Applet don't have a constructor they have an init() method
I replaced your constructor of applet() by void init()
I remove the double creation of your JButton
and it worked well

CODE

import java.net.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.awt.image.RescaleOp;

import javax.swing.*;

import java.applet.*;
import javax.imageio.*;

import java.io.*;
import java.awt.geom.AffineTransform;
import java.awt.image.ImageObserver;



public class applet extends JApplet
                             implements ActionListener {
    
    private JButton  exitButton = null;
    private URL     url        = null;
    JLabel label=null;    
    /**
     * Noarg constructor
     */
    public void init() {


        Container contentPane = this.getContentPane();
        contentPane.setLayout(new BorderLayout());
        StringBuffer strBuff;
        String line;
        TextArea txtArea= new TextArea();
        Image image=null;
        File fima;
        URL url2=null;
        try
        {
            url = new URL("http://www.pblinc.ca/index.html");
        }
        catch(MalformedURLException e){e.printStackTrace();}

        try{
            
            InputStream in = url.openStream();
            BufferedReader bf = new BufferedReader(new InputStreamReader(in));
            strBuff = new StringBuffer();
            while((line = bf.readLine()) != null){
              strBuff.append(line + "\n");
            }
//          System.out.println(strBuff.toString());
          txtArea.append(strBuff.toString());
          }
          catch(IOException e){
            e.printStackTrace();
          }
          
        // ---------------------------------------------------------------------
        // Now get the Image (graphic) from the given URL
        // ---------------------------------------------------------------------
        try {
            url2 = new URL("http://www.pblinc.ca/images/pbl_bourse.jpg");
            
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
      
        try{
            image = ImageIO.read(url2);
        }catch(IOException io){System.out.println(io);}
        try{
            MediaTracker tracker = new MediaTracker(this);
        

        
        tracker.addImage(image, 1);
        tracker.waitForAll();
        }catch(InterruptedException ie){System.out.println(ie);}
  
        // ---------------------------------------------------------------------
        // Exit button
        // ---------------------------------------------------------------------
        exitButton = new JButton("Exit");
        exitButton.addActionListener(this);


        // ---------------------------------------------------------------------
        // Finally create a ImagePanel to display the image and exit button
        // ---------------------------------------------------------------------
        


        ImagePanel i = new ImagePanel(image);
//        txtArea.setSize(5, 5);
        contentPane.add(i, BorderLayout.CENTER);
        contentPane.add(exitButton, BorderLayout.SOUTH);
        contentPane.add(new JScrollPane(txtArea), BorderLayout.NORTH);
//        i.setAlignmentY(JLabel.CENTER_ALIGNMENT);
        repaint();
        // ------------------------------------------------------------
        // Window listener to close application when Window gets closed
        // ------------------------------------------------------------
      
        };



    /**
     * The action listener for user events like buttons.
     * @param ae Event that was performed by the user
     */
    public void actionPerformed(ActionEvent ae) {

        String action = ae.getActionCommand();
                
        if (action.equals("Exit")) {
            
            System.out.println("Exiting.");
            System.exit(0);
        } else {
            System.out.println(action);
        }

    }
            

    /**
     * Sole entry point to the class and application.
     * @param args Array of String arguments.
     */
    public static void main(String[] args) {

        applet mainFrame = new applet();
        mainFrame.setVisible(true);
        mainFrame.setSize(1000,800);
        
    }

}


class ImagePanel extends JPanel {

    Image image;
    ImageObserver im;
    public ImagePanel(Image image) {
        this.image = image;
    }

    public void paintComponent(Graphics g) {

        // paint background
        super.paintComponent(g);
      
        //Draw image at its natural size
        g.drawImage(image, 0, 0, this);  

        
    }
}

User is offlineProfile CardPM

Go to the top of the page

nombre
post 30 Jun, 2008 - 03:30 PM
Post #12


New D.I.C Head

*
Joined: 7 Jun, 2008
Posts: 49

Thank you very much for your help pbl!

I still have the problem. It's probably the server. Is there a way that I could get the file piece by piece and then display it.

Thanks again for your time.
User is offlineProfile CardPM

Go to the top of the page

2 Pages V  1 2 >
Fast ReplyReply to this topicStart new topic
Time is now: 10/6/08 11:02AM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites