JIDE 3.5.15

com.jidesoft.chart.util
Class ChartUtils

java.lang.Object
  extended by com.jidesoft.chart.util.ChartUtils

public class ChartUtils
extends Object

Some utility methods for working with charts (and other components)

Note that when exporting charts to a file immediately after they have been created, you should ensure that the chart's animateOnShow property is set to false, as otherwise you may export the first frame of the opening animation instead of the chart that you see after the animation sequence has ended.


Constructor Summary
ChartUtils()
           
 
Method Summary
static void assertEDT()
           
static void clearBufferedImage(BufferedImage img, Color color)
           
static void copyImageToClipboard(Component component)
          This method copies a 'screenshot' of the given component (such as a Chart object) to the system clipboard as a PNG image.
static BufferedImage createImage(Component component)
          Creates a buffered image of type TYPE_INT_RGB from the supplied component.
static BufferedImage createImage(Component component, int imageType)
          Creates a buffered image (of the specified type) from the supplied component.
static Image createImage(String path)
          Creates an image from a file on the classpath
static TexturePaint createTexture(JComponent observer, String fileName)
          Utility method to create a texture paint from a graphics file
static ImageIcon createThumbnail(Component component, int width, int height)
          Creates a thumbnail from the supplied component (such as a chart).
static Image createThumbnailImage(Component component, int width, int height)
          Creates a thumbnail from the supplied component (such as a chart).
static Font deriveResizedFont(Font originalFont, Dimension originalComponentSize, Dimension newComponentSize)
           
static Font deriveResizedFont(Font originalFont, Dimension originalComponentSize, JComponent component)
          Computes a font with a size adjusted to fit the supplied component, based on an original font and component size.
static int[] getPixels(BufferedImage img, int x, int y, int w, int h, int[] pixels)
          Returns an array of pixels, stored as integers, from a BufferedImage.
static boolean isAssertingEDTViolations()
           
static boolean isHardwareAccelerated(Image image)
           
static void runOnEdt(Runnable r)
           
static void setAssertEDTViolations(boolean assertViolations)
           
static void setPixels(BufferedImage img, int x, int y, int w, int h, int[] pixels)
          Writes a rectangular area of pixels in the destination BufferedImage.
static void writeGifToFile(Component c, File file)
          Writes a GIF image of the supplied component to the given file.
static void writeJpegToFile(Component c, File file)
          Writes a JPEG image of the supplied component to the given file.
static void writePngToFile(Component c, File file)
          Writes a PNG image of the supplied component to the given file.
static void writeToStream(Component c, OutputStream stream)
          Paints the component as a PNG image to the supplied output stream
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ChartUtils

public ChartUtils()
Method Detail

setAssertEDTViolations

public static void setAssertEDTViolations(boolean assertViolations)

isAssertingEDTViolations

public static boolean isAssertingEDTViolations()

assertEDT

public static void assertEDT()

runOnEdt

public static void runOnEdt(Runnable r)

writeGifToFile

public static void writeGifToFile(Component c,
                                  File file)
                           throws FileNotFoundException
Writes a GIF image of the supplied component to the given file. In particular, you can use this method to take a 'screen shot' of a Chart component as a GIF Image.

Parameters:
c - the component to save as an image
file - the file to save it to
Throws:
FileNotFoundException

writeJpegToFile

public static void writeJpegToFile(Component c,
                                   File file)
                            throws FileNotFoundException
Writes a JPEG image of the supplied component to the given file. In particular, you can use this method to take a 'screen shot' of a Chart component as a JPEG Image.

Parameters:
c - the component to save as an image
file - the file to save it to
Throws:
FileNotFoundException

writePngToFile

public static void writePngToFile(Component c,
                                  File file)
                           throws FileNotFoundException
Writes a PNG image of the supplied component to the given file. In particular, you can use this method to take a 'screen shot' of a Chart component as a PNG Image.

Parameters:
c - the component to save as an image
file - the file to save it to
Throws:
FileNotFoundException

writeToStream

public static void writeToStream(Component c,
                                 OutputStream stream)
Paints the component as a PNG image to the supplied output stream

Parameters:
c - the component to capture as a PNG image
stream - the stream to write the PNG data to

createImage

public static BufferedImage createImage(Component component)
Creates a buffered image of type TYPE_INT_RGB from the supplied component.

Parameters:
component - the component to draw
Returns:
an image of the component

createImage

public static BufferedImage createImage(Component component,
                                        int imageType)
Creates a buffered image (of the specified type) from the supplied component.

Parameters:
component - the component to draw
imageType - the type of buffered image to draw
Returns:
an image of the component

createThumbnailImage

public static Image createThumbnailImage(Component component,
                                         int width,
                                         int height)
Creates a thumbnail from the supplied component (such as a chart). If you want to display the thumbnail as a component you can pass the created ImageIcon as a parameter to the constructor of a JLabel.

Parameters:
component - the component from which we would like to generate a thumbnail
width - the width of the thumbnail
height - the height of the thumbnail
Returns:
a thumbnail Image of the supplied component

createThumbnail

public static ImageIcon createThumbnail(Component component,
                                        int width,
                                        int height)
Creates a thumbnail from the supplied component (such as a chart). If you want to display the thumbnail as a component you can pass the created ImageIcon as a parameter to the constructor of a JLabel.

Parameters:
component - the component from which we would like to generate a thumbnail
width - the width of the thumbnail
height - the height of the thumbnail
Returns:
an ImageIcon of the supplied component

createTexture

public static TexturePaint createTexture(JComponent observer,
                                         String fileName)
Utility method to create a texture paint from a graphics file

Parameters:
observer - the observer to be informed when the texture image has been drawn
fileName - the name of a file on the classpath, e.g., com/company/project/images/widget.gif
Returns:
a TexturePaint instance

createImage

public static Image createImage(String path)
Creates an image from a file on the classpath

Parameters:
path - the path to the file
Returns:
an Image object

clearBufferedImage

public static void clearBufferedImage(BufferedImage img,
                                      Color color)

isHardwareAccelerated

public static boolean isHardwareAccelerated(Image image)

getPixels

public static int[] getPixels(BufferedImage img,
                              int x,
                              int y,
                              int w,
                              int h,
                              int[] pixels)

Returns an array of pixels, stored as integers, from a BufferedImage. The pixels are grabbed from a rectangular area defined by a location and two dimensions. Calling this method on an image of type different from BufferedImage.TYPE_INT_ARGB and BufferedImage.TYPE_INT_RGB will unmanage the image.

Parameters:
img - the source image
x - the x location at which to start grabbing pixels
y - the y location at which to start grabbing pixels
w - the width of the rectangle of pixels to grab
h - the height of the rectangle of pixels to grab
pixels - a pre-allocated array of pixels of size w*h; can be null
Returns:
pixels if non-null, a new array of integers otherwise
Throws:
IllegalArgumentException - is pixels is non-null and of length < w*h

setPixels

public static void setPixels(BufferedImage img,
                             int x,
                             int y,
                             int w,
                             int h,
                             int[] pixels)

Writes a rectangular area of pixels in the destination BufferedImage. Calling this method on an image of type different from BufferedImage.TYPE_INT_ARGB and BufferedImage.TYPE_INT_RGB will unmanage the image.

Parameters:
img - the destination image
x - the x location at which to start storing pixels
y - the y location at which to start storing pixels
w - the width of the rectangle of pixels to store
h - the height of the rectangle of pixels to store
pixels - an array of pixels, stored as integers
Throws:
IllegalArgumentException - is pixels is non-null and of length < w*h

copyImageToClipboard

public static void copyImageToClipboard(Component component)
This method copies a 'screenshot' of the given component (such as a Chart object) to the system clipboard as a PNG image. This can then be pasted into another application that can deal with PNG images (such as Windows Paint or PowerPoint)

Parameters:
component - the component to take a screen-dump of

deriveResizedFont

public static Font deriveResizedFont(Font originalFont,
                                     Dimension originalComponentSize,
                                     Dimension newComponentSize)

deriveResizedFont

public static Font deriveResizedFont(Font originalFont,
                                     Dimension originalComponentSize,
                                     JComponent component)
Computes a font with a size adjusted to fit the supplied component, based on an original font and component size. If the component has insets, then the original component size should have the sizes of the original insets subtracted.

Parameters:
originalFont - the original font to be scaled
originalComponentSize - the anticipated size of the component that matches the supplied font
component - the component for which the font is to be tailored and rescaled
Returns:
the original font, rescaled to suit the supplied component

JIDE 3.5.15