May 23
// Load the image

image = new ImageIcon(image).getImage();

// Determine transparency for BufferedImage
// http://helpdesk.objects.com.au/java/how-to-determine-if-image-supports-alpha

boolean hasAlpha = hasAlpha(image);
int transparency = hasAlpha ? Transparency.BITMASK : Transparency.OPAQUE;

// Create the buffered image

GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gs = ge.getDefaultScreenDevice();
GraphicsConfiguration gc = gs.getDefaultConfiguration();
BufferedImage bufferedImage = gc.createCompatibleImage(image.getWidth(null),
		image.getHeight(null), transparency);

if (bufferedImage == null) {

	// if that failed then use the default color model

	int type = hasAlpha ? BufferedImage.TYPE_INT_ARGB : BufferedImage.TYPE_INT_RGB;
	bufferedImage = new BufferedImage(image.getWidth(null), image.getHeight(null), type);
}

// Copy image

Graphics g = bufferedImage.createGraphics();
g.drawImage(image, 0, 0, null);
g.dispose();

written by objects \\ tags: , , ,

Feb 05

The Arrays class has a set of helper toString methods for converting an array to a string representation.

String representation = Arrays.toString(array);

This will work for all array types. For Objects it uses the toString() method of the type to convert array elements.

If you need more control how the array is represented then you will need to implement the conversion yourself using a loop.

written by objects \\ tags: , , ,

Oct 08

Use the toString() method of the Arrays utility class.


Object[] array = new Object[] { "abc", "123", "test" };
String s = Arrays.toString(array);
// s is now "[abc, 123, test]"

written by objects \\ tags: , , , ,