Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-03-2008, 07:26 PM
Senior Member
 
Join Date: Dec 2007
Location: Spain
Posts: 326
willemjav is on a distinguished road
image download from server
I am aware of the Sun article page "How to Use Icons", but I do not understand it fully. The advanced Sun texts are informative but not instructive to me.
So here is what I did for the moment, in order to test what is really going on. I divided the Image loading part form the image displaying part, by triggering the download and the image paint under two different buttons (first button downloads the file and second button paints the image).
I found out that it takes very long to get the image (of an average size of 40 kb) from the server (takes several seconds) and after downloading it, it seams still not ready to paint because I get erros according the image size… (can not get size, see try and catch).
I am just trying to understand how things work. From the first button on the image should be stored under the var image or....
So ones image points to the photo I should be able to get the size through image.getWidth(null); ......
Why is that still not working?
here is part of the code



private class EventHandler implements ActionListener {

public void actionPerformed(ActionEvent evt) {
// images are download before display
if (evt.getSource() == firstButton) {
count++;
if (count==maxcount)
count=0;
image = downloadImage(img[count].name); // the download
}

if (evt.getSource() == nextButton) // display image
try {
imgwidth = image.getWidth(null);
}
catch (Exception e) {
System.out.println("can not get image width " + imgwidth);
//e.printStackTrace();
}
try {
imgheight = image.getHeight(null);
}
catch (Exception e) {
System.out.println("can not get image height " + imgheight);
//e.printStackTrace();
}
x = (int)(framew - imgwidth)/2; // centre the image on the window
y = (int)(frameh - imgheight)/2;

displayPanel.repaint();
}

}
}



private Image downloadImage(String filename) { // the image file reader
Image images=null;
URL url;
url = getClass().getResource("Imagestore/" + filename);
try {
images = ImageIO.read(url);
} catch (IOException ex) {
System.out.println("can not read the file: " + filename);
ex.printStackTrace();
}

return images;
}





private class ImageDisplay extends JPanel { // Defines the display panel.
public void paintComponent(Graphics g) { // for painting the images
this.setBackground(Color.WHITE);
super.paintComponent(g);
g.drawImage(image,x, y, imgwidth, imgheight,this);
// the image infotext
textLabel.setBounds(img[count].textx,img[count].texty, 300 , 50);
// color, position of the tet
settextColor(img[count].textcolor);
textLabel.setText(img[count].info);
}
}
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 04-03-2008, 09:32 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,189
hardwired is on a distinguished road
Code:
import java.awt.*; import java.awt.event.*; import java.awt.image.BufferedImage; import java.io.*; import java.net.URL; import javax.imageio.ImageIO; import javax.swing.*; public class DownloadingImages { ImageDisplay displayPanel; String[] paths = { "http://antwrp.gsfc.nasa.gov/apod/image/0712/m74_hst_800c.jpg", "http://antwrp.gsfc.nasa.gov/apod/image/0711/pleiades_fs.jpg", "http://antwrp.gsfc.nasa.gov/apod/image/0711/crescent_bugnet.jpg", "http://antwrp.gsfc.nasa.gov/apod/image/0712/" + "HindsVariable_goldman800.jpg" }; private class EventHandler implements ActionListener { BufferedImage image; int count = 0; public void actionPerformed(ActionEvent evt) { String ac = evt.getActionCommand(); // images are download before display if (ac.equals("load image")) { count++; if (count == paths.length) count=0; long start = System.currentTimeMillis(); image = downloadImage(paths[count]); String name = getName(paths[count]); long end = System.currentTimeMillis(); double time = (end - start)/1000.0; System.out.printf("load time for %s was: %.1f seconds%n", name, time); } if (ac.equals("show image")) { int imgwidth = 0, imgheight = 0; try { imgwidth = image.getWidth(); } catch (Exception e) { System.out.println("can not get image width " + imgwidth); System.out.println(e.getMessage()); } try { imgheight = image.getHeight(); } catch (Exception e) { System.out.println("can not get image height " + imgheight); System.out.println(e.getMessage()); } displayPanel.setImage(image); } if(ac.equals("load all")) loadAllImages(); } private String getName(String s) { int lastSlash = s.lastIndexOf("/"); if(lastSlash > -1 && lastSlash < s.length()-1) return s.substring(lastSlash+1); return null; } } private BufferedImage downloadImage(String path) { BufferedImage image = null; try { URL url = new URL(path); image = ImageIO.read(url); } catch (IOException ex) { System.out.println("can not read : " + path); System.out.println(ex.getMessage()); } return image; } private class ImageDisplay extends JPanel { BufferedImage image; public ImageDisplay() { // this belongs here this.setBackground(Color.WHITE); } protected void paintComponent(Graphics g) { // This next line will fill component background // with background color specified in constructor // above. super.paintComponent(g); if(image != null) { int x = (getWidth() - image.getWidth())/2; int y = (getHeight() - image.getHeight())/2; g.drawImage(image, x, y, this); } } public void setImage(BufferedImage image) { this.image = image; Dimension d = new Dimension(image.getWidth(), image.getHeight()); setPreferredSize(d); revalidate(); repaint(); } } private void loadAllImages() { Thread thread = new Thread(new Runnable() { public void run() { long start = System.currentTimeMillis(); BufferedImage[] images = new BufferedImage[paths.length]; for(int i = 0; i < images.length; i++) { try { URL url = new URL(paths[i]); images[i] = ImageIO.read(url); } catch(IOException e) { System.out.println("read error for: " + paths[i] + ": " + e.getMessage()); } } long end = System.currentTimeMillis(); double time = (end - start)/1000.0; String s = "Images loaded in " + time + " seconds"; JOptionPane.showMessageDialog(null, s, "images loaded", -1); /* JPanel panel = new JPanel(new GridLayout(2,0)); JScrollPane scrollPane = new JScrollPane(panel); scrollPane.setPreferredSize(new Dimension(500,500)); for(int i = 0; i < images.length; i++) { panel.add(new JLabel(new ImageIcon(images[i]))); } JOptionPane.showMessageDialog(null, scrollPane, "images", -1); */ } }); thread.setPriority(Thread.NORM_PRIORITY); thread.start(); } private JScrollPane getImagePanel() { displayPanel = new ImageDisplay(); return new JScrollPane(displayPanel); } private JPanel getButtonPanel() { String[] ids = { "load image", "show image", "load all" }; EventHandler handler = new EventHandler(); JPanel panel = new JPanel(); for(int j = 0; j < ids.length; j++) { JButton button = new JButton(ids[j]); button.setActionCommand(ids[j]); button.addActionListener(handler); panel.add(button); } return panel; } public static void main(String[] args) { DownloadingImages test = new DownloadingImages(); JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(test.getImagePanel()); f.add(test.getButtonPanel(), "Last"); f.setSize(400,400); f.setLocation(200,200); f.setVisible(true); } }
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 04-03-2008, 11:13 PM
Senior Member
 
Join Date: Dec 2007
Location: Spain
Posts: 326
willemjav is on a distinguished road
Thanks hardwired

impressive (your code), I pulled it all ready through the compiler and it works! So now I need some time to study it all (I might ask some questions?) and to digest it very carefully.

thanks again this might be the missing link to my applet
willemjav
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Converting multiple banded image into single banded image... Image enhancement archanajathan Advanced Java 0 01-08-2008 06:29 PM
Server socket - send image to client Hinty Networking 0 11-30-2007 09:15 PM
I could download JDK 1.5 Albert New To Java 2 07-13-2007 04:36 PM
Download JDK 1.5 Nick15 New To Java 2 05-27-2007 10:52 AM
How to download an image from a HTTP URL Valeriano Networking 1 05-20-2007 09:35 PM


All times are GMT +3. The time now is 12:41 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org