Creating an image from a panel

Hi everybody,
I have a panel which extends JPanel, and wrote an image() method to extract the drawn image off the panel. For some reason, though, when I call this method, I get a dialog with a black box centered in it, and not the appropriate image.
My SSCCE is as follows:
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
public class MyPanel extends JPanel {
    private BufferedImage image;
    public BufferedImage image() {
        int x = getWidth();
        int y = getHeight();
        //image = new BufferedImage(x, y, BufferedImage.TYPE_INT_RGB);
        image = new BufferedImage(200, 200, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2 = image.createGraphics();
        SwingUtilities.paintComponent( g2, this, new JPanel(), 0, 0, x, y );
        g2.dispose();
        return image;
    public static void main( String args[] ) {
         MyPanel canvas = new MyPanel() {
              public void paintComponent( Graphics g ) {
                   Graphics2D g2 = (Graphics2D)g;
                   for( int i = 1; i <= 20; i++ ) {
                        int pos = i*50;
                        g2.drawString( "This is a test", pos, pos );
         JDialog test = new JDialog();
        JLabel label = new JLabel( new ImageIcon( canvas.image() ) );
        JScrollPane scroller = new JScrollPane( label );
        scroller.setPreferredSize( new Dimension( 500, 500 ) );
        test.add( scroller );
        test.pack();
        test.setVisible( true );
}I'm pretty sure it has something to do with the sizing of the image and the timing of the repainting--when I uncomment the commented line in image(), I get an error saying width and height can't be less than or equal to 0. But anyway, I need to paint and then show an image of what was painted, not the actual painted panel--that's what I'm having trouble with. If anyone could help me out, I'd appreciate it. Can anyone help me fix this, please?
Thanks,
Jezzica85
Edited by: jezzica85 on Jun 1, 2009 6:16 AM

Odd choice to both have the panel with a getImage() method, and also override paintComponent(Graphics).
Here is an alternate form of your code.
import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
public class MyPanel extends JPanel {
  private BufferedImage image;
  public BufferedImage image() {
    int x = 200;
    int y = 200;
    image = new BufferedImage(x,y, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2 = image.createGraphics();
    g2.setColor(Color.yellow);
    g2.fillRect(0,0,x,y);
    g2.setColor(Color.black);
    for( int i = 1; i <= 20; i++ ) {
      int pos = i*50;
      g2.drawString( "This is a test", pos, pos );
    g2.dispose();
    return image;
  public static void main( String args[] ) {
    Runnable r = new Runnable() {
      public void run() {
        MyPanel canvas = new MyPanel();
        JDialog test = new JDialog();
        JLabel label = new JLabel( new ImageIcon( canvas.image() ) );
        JOptionPane.showMessageDialog(null, label);
        JScrollPane scroller = new JScrollPane( label );
        scroller.setPreferredSize( new Dimension( 500, 500 ) );
        JOptionPane.showMessageDialog(null, scroller);
    EventQueue.invokeLater(r);
}

Similar Messages

  • Creating an image from a Mac and restoring it in another Mac

    Hi,
    I've spent a couple of hours trying to make a copy of one of my macminis, trying to copy it in another macmini to have the SAME machine "duplicated".
    I've read some tutorials online, but something is going wrong....
    First of all, I went to my "master" macmini, DiskUtility, create new image from folder, select my macintosh HD and I created the image of the whole HD.
    Almost a hour after, the image was created.
    Then I moved the .dmg to a USB drive, and I connected the USB pen to the machine that I want to be a "clone" of the master. There, I opened Disk Utility, Restore, I selected the Source image from my USB, but I can't selected (=drag) the Macintosh HD into the "destination" field...
    What I'm doing wrong?
    Thanks, any help is appreciated :))

    Hi sr.richie;
    If you are booted from Macintosh HD then the system will not allow this cloning operation to take place. You must boot from another disk in order to do that.
    Allan

  • How to create an image from another one with midp1.0 as in midp 2.0

    hi:
    we can create an image from part of another one in midp2.0 width the following method
    createImage(Image image, int x,int y,int width,int height,int transform)
    but have to work with midp1.0, then how?
    regards

    but i have six icons in one picture, (tow row ,three column, each size 14*14)
    it's ok to get the top-left icon with the following code
    Image image = Image.createImage("/myicon.png");
    Image tmp = Image.createImage(14, 14);
    Graphics g = tmp.getGraphics();
    g.drawImage(image, 0, 0, Graphics.TOP|Graphics.LEFT);
    but how to get other icon?
    regards

  • Create an image from Component

    I have created an ImageLoader that stores images loaded from the filesystem. I do however need a default image that is not reliant on loading an image from the filesystem, in case any images on the filesystem are not found.
    I have tried to use Components createImage() method, until I read that this can only be used if the Component is visible on screen, which my Component never will be.
    What I really want is a grey rectangle as an ImageIcon by what means I get this I'm not bothered,
    hopefully someone can help

    Use the java.awt.image.BufferedImage class. To create a BufferedImage object, no component need to be visible. Here is some sample code:       BufferedImage image = new BufferedImage(wid, ht, BufferedImage.TYPE_INT_RGB);
           Graphics2D gc = image.createGraphics();
           gc.drawRect(x,y, width, height);Note that BufferedImage is a subclass of Image. So, you can create ImageIcon from a BufferedImage object.
    If you don't want to use BufferedImage, an option would be to create the image from an array of bytes.

  • [SOLVED] How to Create an Image from UTF8 Text via Command-line

    As the title points out, I'm trying to create an image from unicode text via command line. I tried...
    convert -pointsize 48 -size 400 caption:测试用 text.png
    But that results in question marks for the Chinese characters. So searching around online I discovered that I needed to specify a font which could display the characters. The characters show up just fine in Firefox, KDE, Kate, Terminal, etc so I know I have a font which can render them. I thought it might be DejaVu but this also resulted in question marks...
    convert -font /usr/share/fonts/TTF/DejaVuSerif.ttf -pointsize 48 -size 400 caption:测试用 text.png
    Any ideas?
    Last edited by tony5429 (2011-01-31 23:17:41)

    DejaVu doesn't contain those Chinese glyphs at all, so please don't blame ImageMagick for not rendering them.
    So, Firefox, Kate, Terminal and the others you stated to use DejaVu, if encounter these characters, fall back to some other fonts to render them. These fonts are, however, not vector, but bitmap fonts. (This can be seen if you increase text size (Ctrl++ in Firefox): the Chinese characters don't change, they remain of their inherent size.)
    Actually, e.g. /usr/share/fonts/misc/18x18ko.pcf.gz definitely contains the three example characters, so the mentioned apps may use this font as fall back.
    Apparently ImageMagick doesn't handle bitmap fonts (I'm not sure), so you won't be able to hit your original target. Anyway, since you tried to parse "-pointsize 48", you wouldn't be satisfied with the font size.
    Your only choice seems to be using the above mentioned CJK-approved TTFs.
    EDIT: typo
    Last edited by barto (2011-01-28 21:52:33)

  • Clearing an Image from a panel

    I made an Image object and drew it on a panel using method paintComponent. How can I remove the Image from the panel afterwards?
    Thanks

    Just use the remove method of the JPanel.
    JPanel.remove(imageRefernce);You may also need to call the repaint method as well if it doesn't disapear.

  • Create an Image from a Component

    Can anybody tell me how to create an Image from a Component?
    I intend to create an Image from a JTable now but I want to have a more generic solution of course to be able to create an Image from any Java / Swing component (Applet, JList, JButton, ...).
    Thank you,
    Dirk

    2 ways:
    1) java.awt.Toolkit.createScreenCapture(Rectangle bounds)
    2) This:
         public static BufferedImage grabByPaint(Component comp) {
              boolean visible = comp.isVisible();
              // if not visible, need to make visible to make sure sizes are correct. 
              if(!visible) {
                   comp.setVisible(true);
              Dimension size = comp.getSize();
              BufferedImage bi = new BufferedImage(size.width, size.height, BufferedImage.TYPE_INT_ARGB);
              Graphics2D g2d = bi.createGraphics();
              comp.paintAll(g2d);
              if(!visible) {
                   comp.setVisible(false);
              g2d.dispose();
              return bi;
         }

  • Create still images from video files?

    from http://gadgetwise.blogs.nytimes.com/2009/04/16/5-new-photoshop-tips-for-photographers/ , "You can now preview video in Bridge and create still images from video files."
    I just installed the 5.4 Camera Raw Update.
    I can preview the AVI files from my Nikon D5000, but have not figured out how I might create a still image ( other than stopping the movie, and dragging to copy ) Have tried searching adobe, this forum, have not found more info ion this.

    Stop the video at the desired point, press the Shift, Command, and 4 keys, drag the cursor so that the whole picture is highlighted, let go of the button, and open the PNG on the desktop.
    (35300)

  • Create transparency image from shape

    Hi,
    I'd like to create the application that create the transparency image from drawing data. the code is as follows.
    BufferedImage img = new BufferedImage(350,350,BufferedImage.TYPE_INT_RGB);
    Graphics2D g2 = img.createGraphics();
    GeneralPath gp = new GeneralPath(GeneralPath.WIND_NON_ZERO);
    gp.moveTo(100,100);     gp.lineTo(100,200);
    gp.lineTo(200,200);     gp.lineTo(200,100); gp.closePath();
    g2.setPaint(Color.yellow);
    g2.fill(gp);
    g2.setStroke(new BasicStroke(2));
    g2.setPaint(Color.red);
    g2.draw(gp);
    File outFile = new File("outimg.png");
    ImageIO.write(img,"png",outFile);
    I can create the image from such code, but the image is not transparent since the background is all black. What additional coding I need to make it transparent?
    Thanks,
    Sanphet

    1. use a PixelGrabber to convert the image into a pixel array
    2. (assuming pixel array called pixelArray) :
    if (pixelArray[loopCounter] == 0xFFFFFFFF) { //assuming the black your seeing is not a fully transparent surface
        pixelArray[loopCounter] = pixelArray[loopCounter] & 0x00FFFFFF; //sets alpha channel for all black area to 0, thus making it fully transparent
    }3. recreate an Image using MemoryImageSource
    I'm sure there is a quicker solution using some built in java functions...but hey, I dont' know it!! :)
    Here's a sample class that utilizes these techniques:
    * To start the process, click on the window
    * Restriction (next version upgrade) :
    *     - firstImage must be larger
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.image.*;
    import javax.swing.*;
    public class AdditiveBlendingTest extends javax.swing.JFrame implements MouseListener, ActionListener, WindowListener {
        static final int ALPHA = 0xFF000000; // alpha mask
        static final int MASK7Bit = 0xFEFEFF; // mask for additive/subtractive shading
        static final int ZERO_ALPHA = 0x00FFFFFF; //zero's alpha channel, i.e. fully transparent
        private Image firstImage, secondImage, finalImage; //2 loades image + painted blended image
        private int firstImageWidth, firstImageHeight, secondImageWidth, secondImageHeight;
        private int xInsets, yInsets; //insets of JFrame
        //cliping area of drawing, and size of JFrame
        private int clipX = 400;
        private int clipY = 400;
        //arrays representing 2 loades image + painted blended image
        private int[] firstImageArray;
        private int [] secondImageArray;
        private int [] finalImageArray;
        //system timer, used to cause repaints
        private Timer mainTimer;
        //used for double buffering and drawing the components
        private Graphics imageGraphicalSurface;
        private Image doubleBufferImage;
        public AdditiveBlendingTest() {
            firstImage = Toolkit.getDefaultToolkit().getImage("Image1.jpg");
            secondImage = Toolkit.getDefaultToolkit().getImage("Image2.gif");
         //used to load image, MediaTracker process will not complete till the image is fully loaded
            MediaTracker tracker = new MediaTracker(this);
            tracker.addImage(firstImage,1);
         try {
             if(!tracker.waitForID(1,10000)) {
              System.out.println("Image1.jpg Load error");
              System.exit(1);
         } catch (InterruptedException e) {
             System.out.println(e);
         tracker = new MediaTracker(this);
         tracker.addImage(secondImage,1);
         try {
             if(!tracker.waitForID(1,10000)) {
              System.out.println("Image2.gif Load error");
              System.exit(1);
         } catch (InterruptedException e) {
             System.out.println(e);
         //calculate dimensions
         secondImageWidth = secondImage.getWidth(this);
         secondImageHeight = secondImage.getHeight(this);
         firstImageWidth = firstImage.getWidth(this);
         firstImageHeight = firstImage.getHeight(this);
         //creates image arrays
         firstImageArray = new int[firstImageWidth * firstImageHeight];
            secondImageArray = new int[secondImageWidth * secondImageHeight];
         //embeded if statements will be fully implemented in next version
         finalImageArray = new int[((secondImageWidth >= firstImageWidth) ? secondImageWidth : firstImageWidth) *
                 ((secondImageHeight >= firstImageHeight) ? secondImageHeight : firstImageHeight)];
         //PixelGrabber is used to created an integer array from an image, the values of each element of the array
         //represent an individual pixel.  FORMAT = 0xFFFFFFFF, with the channels (FROM MSB) Alpha, Red, Green, Blue
         //each taking up 8 bits (i.e. 256 possible values for each)
         try {
             PixelGrabber pgObj = new PixelGrabber(firstImage,0,0,firstImageWidth,firstImageHeight,firstImageArray,0,firstImageWidth);
             if(pgObj.grabPixels() && ((pgObj.getStatus() & ImageObserver.ALLBITS) != 0)){
         } catch (InterruptedException e) {
             System.out.println(e);
         try {
             PixelGrabber pgObj = new PixelGrabber(secondImage,0,0,secondImageWidth,secondImageHeight,secondImageArray,0,secondImageWidth);
             if (pgObj.grabPixels() && ((pgObj.getStatus() & ImageObserver.ALLBITS) !=0)) {
         } catch (InterruptedException e) {
             System.out.println(e);
         //adds the first images' values to the final painted image.  This is the only time the first image is involved
         //with the blend
         for(int cnt = 0,large = 0; cnt < (secondImageWidth*secondImageHeight);cnt++, large++){
             if (cnt % 300 == 0 && cnt !=0){
              large += (firstImageWidth - secondImageWidth);
             finalImageArray[cnt] = AdditiveBlendingTest.ALPHA + (AdditiveBlendingTest.ZERO_ALPHA & firstImageArray[large]) ;
         //final initializing
         this.setSize(clipX,clipY);
         this.enable();
         this.setVisible(true);
         yInsets = this.getInsets().top;
         xInsets = this.getInsets().left;
         this.addMouseListener(this);
         this.addWindowListener(this);
         doubleBufferImage = createImage(firstImageWidth,firstImageHeight);
         imageGraphicalSurface = doubleBufferImage.getGraphics();
        public void mouseEntered(MouseEvent e) {}
        public void mouseExited(MouseEvent e) {}
        public void mousePressed(MouseEvent e) {}
        public void mouseReleased(MouseEvent e) {}
        public void windowActivated (WindowEvent e) {}
        public void windowDeiconified (WindowEvent e) {}
        public void windowIconified (WindowEvent e) {}
        public void windowDeactivated (WindowEvent e) {}
        public void windowOpened (WindowEvent e) {}
        public void windowClosed (WindowEvent e) {}
        //when "x" in right hand corner clicked
        public void windowClosing(WindowEvent e) {
         System.exit(0);
        //used to progress the animation sequence (fires every 50 ms)
        public void actionPerformed (ActionEvent e ) {
         blend();
         repaint();
        //begins animation process and set's up timer to continue
        public void mouseClicked(MouseEvent e) {
         blend();
         mainTimer = new Timer(50,this);
         mainTimer.start();
         repaint();
         * workhorse of application, does the additive blending
        private void blend () {
         int pixel;
         int overflow;
         for (int cnt = 0,large = 0; cnt < (secondImageWidth*secondImageHeight);cnt++, large++) {
             if (cnt % 300 == 0 && cnt !=0){
              large += (firstImageWidth - secondImageWidth);
             //algorithm for blending was created by another user, will give reference when I find
             pixel = ( secondImageArray[cnt] & MASK7Bit ) + ( finalImageArray[cnt] & MASK7Bit );
             overflow = pixel & 0x1010100;
             overflow -= overflow >> 8;
             finalImageArray[cnt] = AdditiveBlendingTest.ALPHA|overflow|pixel ;
         //creates Image to be drawn to the screen
         finalImage = createImage(new MemoryImageSource(secondImageWidth, secondImageHeight, finalImageArray,
              0, secondImageWidth));
        //update overidden to eliminate the clearscreen call.  Speeds up overall paint process
        public void update(Graphics g) {
         paint(imageGraphicalSurface);
         g.drawImage(doubleBufferImage,xInsets,yInsets,this);
         g.dispose();
        //only begins painting blended image after it is created (to prevent NullPointer)
        public void paint(Graphics g) {
         if (finalImage == null){
             //setClip call not required, just added to facilitate future changes
             g.setClip(0,0,clipX,clipY);
             g.drawImage(firstImage,0,0,this);
             g.drawImage(secondImage,0,0,this);
         } else {
             g.setClip(0,0,clipX,clipY);
             g.drawImage(finalImage,0,0,this);
        public static void main( String[] args ) {
         AdditiveBlendingTest additiveAnimation = new AdditiveBlendingTest();
         additiveAnimation.setVisible(true);
         additiveAnimation.repaint();

  • Unable to create Disc Image from DVD

    I get the following error when trying to make a copy of a family wedding DVD. I'm using Disc Utility and creating the image as a DVD/CD master (.cdr) file. It gets to about 3/4 of the way and then gives the following error.
    The internal drive is a PIONEER DVD-RW DVR-K05
    Sep 10 22:13:24: Disk Utility started.
    Creating Image “Wedding of xxx & xxx.cdr”
    Reading VOLUME_IDENTIFIER (Apple_UDF : 0)...
    Unable to create “Wedding of Sarah & Federico.cdr” - Input/output error.
    I've tried twice now and the same error. Looks like I might have to use Toast/Popcorn etc?
    Thanks,
    Darren
    iMac G5 2.1GHz 1Gb RAM 260Gb HDD   Mac OS X (10.4.7)  

    I do not know what that error means. However, if you open up the Disk Utility & go into it's Help Menu & in the search field type duplicating a CD or DVD. There you will find detailed instructions on how to create a disc image from a DVD.
    Good possibility your answer may be found there and/or there may have been something you missed in creating a disc image.
    Good luck.

  • System Image Utility 10.6.3 - fails when creating NetBoot image from DVD

    System Image Utility 10.6.3, trying to create a NetBoot image from a bundled installer disc that came with a 27" Late 2009 iMac (iMac11,1). Image creation fails consistently, since the image that System Image Utility creates is only 901M.
    Anyone see this before?
    Don
    --------- System Image Utility log ----------
    Workflow Started (2010-06-16 14:03:02 -0700)
    Starting action: Define Image Source
    Finished running action: Define Image Source
    Starting action: Create Image
    Starting image creation process...
    Create NetBoot Image
    Initiating NetBoot from Install Media.
    Creating working path at /Library/NetBoot/NetBootSP0/NetBoot of Mac OS X Install DVD
    Creating disk image (Size: 901 MB)
    Finalizing disk image.
    created: /Library/NetBoot/NetBootSP0/NetBoot of Mac OS X Install DVD/NetBoot.dmg
    Attaching disk image
    Installing to destination volume
    2010-06-16 14:03:38.126 installer[2365:6f03] Looking for system packages
    2010-06-16 14:03:38.129 installer[2365:6f03] no system packages found
    2010-06-16 14:03:38.130 installer[2365:6f03] No or Invalid system receipts found on /private/tmp/mnt.LjFArn
    2010-06-16 14:03:38.130 installer[2365:6f03] Attempting fallback using: /System/Library/PrivateFrameworks/SystemMigration.framework/Resources/FallbackS ystemFiles.plist
    2010-06-16 14:03:38.175 installer[2365:6f03] Finding system files...
    2010-06-16 14:03:38.619 installer[2365:6f03] Writing system path cache.
    2010-06-16 14:03:38.623 installer[2365:6f03] Error writing cache to /private/tmp/mnt.LjFArn/Library/Caches/com.apple.FindSystemFiles.plist
    2010-06-16 14:03:38.625 installer[2365:6f03] Failed to enumerate /tmp/mnt.LjFArn/Library/Caches, cannot prune (
    "com.apple.userpictureCache"
    installer: Package name is Mac OS X
    installer: Installing at base path /private/tmp/mnt.LjFArn
    installer: The install failed (There is not enough space on this disk to install the selected items. Deselect at least 6.46 GB and try again.)
    Script is done.
    NetBoot creation failed.
    Image creation process finished...
    Stopping image creation.
    Image creation failed.

    Brian Nesse wrote:
    Hi Don, here's my guess...
    The 901 number is additional space added in the scripts. This indicates that the source image size was 0.
    Since you are making a NetBoot from Install media, under the covers the installer process is being run to create a NetBoot volume. The media shipped with the 27" iMac is most likely CPU specific and thus the installation fails because you are trying to create the image (i.e. install the system) on an unsupported CPU.
    In order to produce a NetBoot from the install media, you'll have to create it on the 27" iMac.
    Hi Brian,
    Thanks for the response. This makes perfect sense. I'll give this a try and shout back!
    Thanks,
    Don

  • Problem In moving 1 image from 1 panel to next and maintain size of original image?

    Why is it when i drag one image from the bottom panel over to another picture(solid color) i will only get 50% of the image and not the full image. Same thing when i copy and paste that image over the gray solid colored image also i will get only 50% of the image.  The original image of the cat(full view) is about 24.79%  and picture is 11.733 x 15.644 in size.  the other image (solid color) is at 43.63%  or 6.667 x 8.889". size is in inches. Do i have to resize the images and if so how as i am a stupie in this matter. The images are aligned side by side on the main panel or display.I am trying to place the cat over the gray background image  and with a layer mask delete the present baackground of the cat to uncover the gray of the bottom layer.  Thanks!!

    Probably the resolution of each image has a different value. You can check this in Image>resize>image size. The resolution is expressed in px/in.
    If you are attempting to place the cat on a plain gray background, try this:
    Set your foreground color chip  (lower left) to the shade of gray desired
    Open the picture with the cat
    Using one of the selection tools (e.g. Selection brush, lasso), select the cat
    Place the cat on a separate layer (Layer>new>layer via copy)
    Place a blank layer between the background layer and the cat layer
    Fill the blank layer with gray ( Edit>fill layer>foreground color)
    Use the move tool to position the cat, and resize, if necessary, with the corner handles of the bounding box.

  • Create an image from float array?

    Dear All,
    I have a float array containing pixel values. From that array I want to create an image (let's say in JPEG format).
    How should I do that?
    thanks

    Hi musti168,
    You're going through your entire image pixel-by-pixel and getting each of their values - why don't you just use the IMAQ ImageToArray function?  
    On this forum post I found an example of IMAQ ArrayToImage: http://forums.ni.com/t5/LabVIEW/IMAQ-arraytoimage-​example/td-p/68418
    You can use some of the IMAQ Image Processing palette to change the image to gray scale - possibly a threshold.
    Julian R.
    Applications Engineer
    National Instruments

  • Can iDVD create disc images from VIDEO_TS folder, and burn them?

    That first question is pretty self-explanatory.
    Background is this: I've downloaded some video folders/files from forum/torrent trading websites. When finished, I have a VIDEO_TS folder, often an EXTRAS_TS folder as well. A while back, I downloaded the app DVD IMAGER to create a disc image. Yesterday that stopped working effectively, so maybe I need to re-download, but I was wondering if there's just a better way? I also downloaded the app called BURN, but can't see yet how that creates disc images.
    Once I create that disc image, I need to burn it, and I was using Disk Utility for that. I'm really looking for some help and advice on the best way to perform these processes.
    Thank you, and let me know if I need to post in another forum.

    Additionally, do you have experience w/ DVD Imager?
    Nope. Like I said, Roxio's Toast Titanium is the top application (and is probably the one that most experienced people use).

  • Problem creating an image from a remote url

    I am creating a servlet which does the following:
    1. retrieves an image from a remote url
    2. resizes the image
    3. sends the image to the client.
    The servlet works fine when I run it on my PC, but when I run it on the server, it doesn't. Here's the problematic code
        private Image getRemoteImage(String imageUrl) throws Exception {
            System.err.println("in getRemoteImage()");
            if (!imageUrl.startsWith("http://")) imageUrl = "http://" + imageUrl;
            URL u = new URL(imageUrl);
            System.err.println("imageUrl: " + imageUrl);
            HttpURLConnection huc = (HttpURLConnection) u.openConnection();
            huc.setRequestMethod("GET");
            System.err.println("connecting...");
            huc.connect();
            System.err.println("Getting DataInputStream...");
            DataInputStream in = new DataInputStream(u.openStream());
            //DataInputStream in = new DataInputStream(huc.getInputStream());
            System.err.println("...got");
            int numBytes = huc.getContentLength(); //1063
            System.err.println("numBytes: " + numBytes);
            byte[] imageBytes = new byte[numBytes];
            System.err.println("reading fully...");
            in.readFully(imageBytes);
            System.err.println("...read");
            in.close();
            System.err.println("creating imageIcon");
            ImageIcon imageIcon = new ImageIcon(imageBytes);  //   <-- problem here
            System.err.println("** THIS LINE IS NOT REACHED **");
            System.err.println("creating image");
            Image image = imageIcon.getImage();
            System.err.println("returning image");
            return image;
        }When I access the servlet with FF, the browser reports:
    The image �<my servlet url>� cannot be displayed, because it contains errors.
    When I access the servlet with IE, I get:
    Apache Tomcat/4.0.3 - HTTP Status 500 - Internal Server Error
    type Exception report
    message Internal Server Error
    description The server encountered an internal error (Internal Server Error) that prevented it from fulfilling this request.
    exception
    javax.servlet.ServletException: Servlet execution threw an exception
         at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
         at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)
         at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:243)
         at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)
         at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
         at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
         at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:190)
         at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)
         at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
         at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
         at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2343)
         at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:180)
         at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)
         at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:170)
         at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564)
         at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:170)
         at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564)
         at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:468)
         at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564)
         at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
         at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
         at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:174)
         at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)
         at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
         at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
         at org.apache.ajp.tomcat4.Ajp13Processor.process(Ajp13Processor.java:429)
         at org.apache.ajp.tomcat4.Ajp13Processor.run(Ajp13Processor.java:495)
         at java.lang.Thread.run(Thread.java:534)
    root cause
    java.lang.NoClassDefFoundError
         at java.lang.Class.forName0(Native Method)
         at java.lang.Class.forName(Class.java:141)
         at java.awt.Toolkit$2.run(Toolkit.java:748)
         at java.security.AccessController.doPrivileged(Native Method)
         at java.awt.Toolkit.getDefaultToolkit(Toolkit.java:739)
         at javax.swing.ImageIcon.(ImageIcon.java:205)
         at myservlets.GetImage.getRemoteImage(GetImage.java:283)
         at myservlets.GetImage.processRequest(GetImage.java:73)
         at myservlets.GetImage.doGet(GetImage.java:394)
         at javax.servlet.http.HttpServlet.service(HttpServlet.java:740)
         at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
         at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:247)
         at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)
         at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:243)
         at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)
         at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
         at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
         at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:190)
         at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)
         at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
         at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
         at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2343)
         at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:180)
         at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)
         at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:170)
         at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564)
         at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:170)
         at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564)
         at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:468)
         at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564)
         at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
         at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
         at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:174)
         at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)
         at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
         at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
         at org.apache.ajp.tomcat4.Ajp13Processor.process(Ajp13Processor.java:429)
         at org.apache.ajp.tomcat4.Ajp13Processor.run(Ajp13Processor.java:495)
         at java.lang.Thread.run(Thread.java:534)
    Any ideas?
    Cheers,
    James

    Cheers rebol.
    I've tried that (along with all other kinds of techniques I never knew existed). I think it might be a problem with the Tomcat setup - it may need to run headless (sounds a bit severe!...).

Maybe you are looking for