use of java.awt.Graphics in project ChatGameFontificator by GlitchCog.
the class AnimatedGifUtil method loadDittoAnimatedGif.
/**
* Fix (ditto) for Java's animated GIF interpretation
*
* Adapted from http://stackoverflow.com/questions/26801433/fix-frame-rate-of-animated-gif-in-java#answer-26829534
*
* @param url
* The URL for the animated GIF to be loaded
* @param dim
* The dimension object to be filled by the width and height of the loaded animated GIF
* @return The loaded animated GIF
* @throws Exception
*/
public static Image loadDittoAnimatedGif(final URL url, Dimension dim) {
final Image dimImage = new ImageIcon(url).getImage();
Image image = null;
try {
ImageReader gifReader = ImageIO.getImageReadersByFormatName(GIF_EXTENSION).next();
InputStream imageStream = url.openStream();
gifReader.setInput(ImageIO.createImageInputStream(imageStream));
IIOMetadata imageMetaData = gifReader.getImageMetadata(0);
String metaFormatName = imageMetaData.getNativeMetadataFormatName();
final int frameCount = gifReader.getNumImages(true);
ByteArrayOutputStream baoStream = new ByteArrayOutputStream();
ImageOutputStream ios = ImageIO.createImageOutputStream(baoStream);
ImageWriter writer = ImageIO.getImageWriter(gifReader);
writer.setOutput(ios);
writer.prepareWriteSequence(null);
final int imgWidth = dimImage.getWidth(null);
final int imgHeight = dimImage.getHeight(null);
dim.setSize(imgWidth, imgHeight);
for (int i = 0; i < frameCount; i++) {
// This read method takes into account the frame's top and left coordinates
BufferedImage frame = gifReader.read(i);
IIOMetadataNode nodes = (IIOMetadataNode) gifReader.getImageMetadata(i).getAsTree(metaFormatName);
for (int j = 0; j < nodes.getLength(); j++) {
IIOMetadataNode node = (IIOMetadataNode) nodes.item(j);
if (GRAPHIC_CTRL_EXT.equalsIgnoreCase(node.getNodeName())) {
int delay = Integer.parseInt(node.getAttribute(ATTRIBUTE_DELAY_TIME));
// Minimum delay for browsers, which delay animated GIFs much more than would be to spec
if (delay < MIN_ANI_GIF_DELAY) {
node.setAttribute(ATTRIBUTE_DELAY_TIME, Integer.toString(MIN_ANI_GIF_DELAY));
}
// at least not for BTTV's (ditto)
if (node.getAttribute(ATTRIBUTE_DISPOSAL_METHOD).equals(DISPOSE_RESTORE_TO_PREVIOUS)) {
node.setAttribute(ATTRIBUTE_DISPOSAL_METHOD, DISPOSE_RESTORE_TO_BGCOLOR);
}
}
}
IIOMetadata metadata = writer.getDefaultImageMetadata(new ImageTypeSpecifier(frame), null);
metadata.setFromTree(metadata.getNativeMetadataFormatName(), nodes);
// This modified frame is necessary to get the correct image placement, width, and height in the final GIF
BufferedImage frameMod = new BufferedImage(imgWidth, imgHeight, BufferedImage.TYPE_INT_ARGB);
Graphics big = frameMod.getGraphics();
big.drawImage(frame, 0, 0, null);
IIOImage fixedFrame = new IIOImage(frameMod, null, metadata);
writer.writeToSequence(fixedFrame, writer.getDefaultWriteParam());
}
writer.endWriteSequence();
if (ios != null) {
ios.close();
}
image = Toolkit.getDefaultToolkit().createImage(baoStream.toByteArray());
} catch (Exception e) {
// If anything goes wrong, just load it normally
logger.error("Error loading animated GIF (ditto) from " + url, e);
image = new ImageIcon(url).getImage();
dim.setSize(image.getWidth(null), image.getHeight(null));
}
return image;
}
use of java.awt.Graphics in project ChatGameFontificator by GlitchCog.
the class CollagePanel method addCapture.
private void addCapture() {
final String imageText = getImageText();
final int labelHeight = imageText.isEmpty() ? 0 : 24;
final int width = chat.getWidth();
final int height = chat.getHeight() + labelHeight;
BufferedImage chatImage = new BufferedImage(width, height, transparencyBox.isSelected() ? BufferedImage.TYPE_INT_ARGB : BufferedImage.TYPE_INT_RGB);
Graphics chatGraphics = chatImage.getGraphics();
chat.paint(chatGraphics);
if (!imageText.isEmpty()) {
try {
chatGraphics.setColor(drawBlockColor);
chatGraphics.fillRect(1, chat.getHeight(), width - 2, labelHeight - 1);
chatGraphics.setColor(drawTextColor);
chatGraphics.setFont(font);
chatGraphics.drawString(imageText, 6, height - 8);
} catch (Exception e) {
logger.error("Unable to write text to image", e);
}
}
images.add(chatImage);
}
use of java.awt.Graphics in project adempiere by adempiere.
the class Env method getGraphics.
// getFrame
/**
* Get Graphics of container or its parent.
* The element may not have a Graphic if not displayed yet,
* but the parent might have.
* @param container Container
* @return Graphics of container or null
*/
public static Graphics getGraphics(Container container) {
Container element = container;
while (element != null) {
Graphics g = element.getGraphics();
if (g != null)
return g;
element = element.getParent();
}
return null;
}
use of java.awt.Graphics in project hid-serial by rayshobby.
the class StyledString method getParagraghSpacer.
/**
* Create a graphic image character to simulate paragraph breaks
*
* @param ww
* @return a blank image to manage paragraph ends.
*/
private ImageGraphicAttribute getParagraghSpacer(int ww) {
if (ww == Integer.MAX_VALUE)
ww = 1;
BufferedImage img = new BufferedImage(ww, 10, BufferedImage.TYPE_INT_ARGB);
Graphics g = img.getGraphics();
g.setColor(new Color(255, 255, 255, 0));
g.fillRect(0, 0, img.getWidth(), img.getHeight());
return new ImageGraphicAttribute(img, GraphicAttribute.TOP_ALIGNMENT);
}
use of java.awt.Graphics in project jgnash by ccavanaugh.
the class IconUtils method createImageIcon.
/**
* Creates an {@code ImageIcon} from any Icon. Useful for caching complex icon paints into a buffered Icon
*
* @param icon Icon with complex painting operation
* @return {@code ImageIcon} based on a {@code BufferedImage}
*/
public static ImageIcon createImageIcon(final Icon icon) {
BufferedImage bufferedImage = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics g = bufferedImage.createGraphics();
// paint the icon into the BufferedImage
// pass a JLabel; the Metal based icons want a GraphicsConfiguration from the passed component
icon.paintIcon(new JLabel(), g, 0, 0);
g.dispose();
return new ImageIcon(bufferedImage);
}
Aggregations