Pacman open/close mouth animation

Something like this might work for PacMan images. It uses a Java 2D based Shape instance to represent the form, and an AffineTransform to produce the different orientations. import java.awt.*; import java.awt.event.*; import java.awt.geom.*; import java.awt.image.BufferedImage; import javax.swing.*; import java.io.*; import javax.imageio.ImageIO; class PacManShape { private double size; private double rotation; final int maxSize = … Read more

Get mouse detection with a dynamic shape

Use Shape.contains(Point2D) – something like this: This example uses overlapping ellipses to show how the contains(..) method will accurately identify which ovals the mouse click falls inside. But the kind of map you are referring to will probably be made of a number of GeneralPath objects (one for each country) that do not overlap. import … Read more

How to serialize Java 2D Shape objects as XML?

Unfortunately, naive encoding/decoding of a Shape to XML using XMLEncoder/Decoder often destroys all the vital information of the Shape! So to do this, still using the above mentioned classes, we serialize and restore properly constructed beans that represent the parts of the shape as obtained from a PathIterator. These beans are: PathBean which stores the … Read more

Java2D Performance Issues

I think I found a solution by researching and putting bits and pieces together from too many Google searches. Here it is, comments and all: private BufferedImage toCompatibleImage(BufferedImage image) { // obtain the current system graphical settings GraphicsConfiguration gfxConfig = GraphicsEnvironment. getLocalGraphicsEnvironment().getDefaultScreenDevice(). getDefaultConfiguration(); /* * if image is already compatible and optimized for current system … Read more

How to draw a directed arrow line in Java?

Although Pete’s post is awesomely comprehensive, I’m using this method to draw a very simple line with a little triangle at its end. // create an AffineTransform // and a triangle centered on (0,0) and pointing downward // somewhere outside Swing’s paint loop AffineTransform tx = new AffineTransform(); Line2D.Double line = new Line2D.Double(0,0,100,100); Polygon arrowHead … Read more

Drawing a Component to BufferedImage causes display corruption

Summary: The original JScrollNavigator uses the Swing opacity property to render a convenient green NavBox over a scaled thumbnail of the component in an adjacent JScrollPane. Because it extends JPanel, the (shared) UI delegate’s use of opacity conflicts with that of the scrollable component. The images seen in edit 5 above typify the associated rendering … Read more

setOpaque(true/false); Java

The short answer to your question is that “opaque” is defined in English as completely non-transparent. Therefore an opaque component is one which paints its entire rectangle, and every pixel is not at all translucent to any degree. However, the Swing component opacity API is one of those mis-designed and therefore often mis-used APIs. What’s … Read more

How to resize text in java

One way is to use an AffineTransform (this variant also fades the color). import java.awt.*; import java.awt.geom.AffineTransform; import java.awt.image.BufferedImage; import javax.swing.*; import java.io.File; import javax.imageio.ImageIO; public class StretchText { public static void main(String[] args) throws Exception { // used to stretch the graphics instance sideways AffineTransform stretch = new AffineTransform(); int w = 640; // … Read more

Java2D Graphics anti-aliased

Assuming you actually want smooth (non-aliased) text, TextLayout may make this easier. The FontRenderContext constructor can manage the anti-aliasing and fractional metrics settings. Addendum: Using g2d.setColor(Color.blue) seems to produce the expected effect. Addendum: On Mac OS X, the Pixie application in /Developer/Applications/Graphics Tools/ is convenient for examining the anti-alias pixels. On other platforms, Zoom may … Read more

Smoothing a jagged path

This is a big subject. You might find Depixelizing Pixel Art1 by Johannes Kopf & Dani Lischinski useful: it’s readable, recent, includes a summary of previous work, and explains their approach in detail. See also slides covering similar background and video(!). Here are some screenshots from the document of ‘nearest neighbor’ vs. ‘their technique’.