Results 1 to 2 of 2

Thread: Help!

  1. #1

    Thread Starter
    Member
    Join Date
    Jul 2002
    Posts
    50

    Exclamation Help!

    The method below rescales an image, its basically copied from sun's site (but with a few modifications) ..... it doesn't quite work right though, most of the time it dumps a REALLY long exception stack trace when I try and use it.

    //Rescales an image to the specified dimensions
    public static Image rescaleImage(Image imageSource, int width, int height) {
    //Get Scale Factors
    double wScale=(double)width/(double)imageSource.getWidth(null);
    double hScale=(double)height/(double)imageSource.getHeight(null);
    //Get closets integer scale dimensions
    int wScaled=(int)(wScale*imageSource.getWidth(null));
    int hScaled=(int)(hScale*imageSource.getHeight(null));
    //Create a Buffered Image with our Scale
    BufferedImage imageOut = new BufferedImage(wScaled, hScaled, BufferedImage.TYPE_INT_RGB);
    //Setup Scaling Transformation
    AffineTransform tx = new AffineTransform();
    tx.scale(wScale, hScale);
    //Create Graphics2D object and render the new image
    Graphics2D g2d = imageOut.createGraphics(); //Graphics2D now holds empty buffered pallet
    //g2d.setRenderingHints((RenderingHints)hints); //set the rendering hints
    g2d.drawImage(imageSource, tx, null); //Render image onto our pallet with approiate scaling
    g2d.dispose(); //Clear some memory
    return (Image)imageOut; //Return our newly rescaled image
    }

    Here is the tack dump:

    Exception occurred during event dispatching:

    java.lang.NegativeArraySizeException

    at java.awt.image.DataBufferInt.<init>(DataBufferInt.java:44)

    at java.awt.image.Raster.createPackedRaster(Raster.java:423)

    at java.awt.image.DirectColorModel.createCompatibleWritableRaster(DirectColorModel.java:905)

    at java.awt.image.BufferedImage.<init>(BufferedImage.java:241)

    at bomberboy.ImageTools.rescaleImage(ImageTools.java:81)

    at bomberboy.MapPane.paint(MapPane.java:60)

    at javax.swing.JComponent.paintChildren(JComponent.java:498)

    at javax.swing.JComponent.paint(JComponent.java:696)

    at javax.swing.JLayeredPane.paint(JLayeredPane.java:546)

    at javax.swing.JComponent.paintChildren(JComponent.java:498)

    at javax.swing.JComponent.paint(JComponent.java:669)

    at java.awt.GraphicsCallback$PaintCallback.run(GraphicsCallback.java:23)

    at sun.awt.SunGraphicsCallback.runOneComponent(SunGraphicsCallback.java:54)

    at sun.awt.SunGraphicsCallback.runComponents(SunGraphicsCallback.java:91)

    at java.awt.Container.paint(Container.java:960)

    at javax.swing.JFrame.update(JFrame.java:329)

    at sun.awt.RepaintArea.update(RepaintArea.java:337)

    at sun.awt.windows.WComponentPeer.handleEvent(WComponentPeer.java:200)

    at java.awt.Component.dispatchEventImpl(Component.java:2663)

    at java.awt.Container.dispatchEventImpl(Container.java:1213)

    at java.awt.Window.dispatchEventImpl(Window.java:914)

    at java.awt.Component.dispatchEvent(Component.java:2497)

    at java.awt.EventQueue.dispatchEvent(EventQueue.java:339)

    at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:131)

    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:98)

    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)

    at java.awt.EventDispatchThread.run(EventDispatchThread.java:85)

    ***?

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    It seems you're passing a negative size to the BufferedImage constructor.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width