KeyListener vs. Key Bindings

KeyListener is a much lower level API which requires the component that it is registered to be focused AND have keyboard focus. This can cause issues when you have other components within your game that may grab keyboard focus, for example. KeyListener is generally more difficult to maintain and extend or change, as typically, all … Read more

Porting a Java app that uses AWT and Swing for drawing movies to the server-side

The article Using Headless Mode in the Java SE Platform outlines the limitations imposed on such applications. As a concrete example JFreeChart is a graphic program widely used in both desktop and servlet contexts. For the latter, any of several ChartUtils may be used to stream rendered content in a headless environment. Alternatively, although deprecated, … 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

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

Java: mouseDragged and moving around in a graphical interface

Based on this example, the following program allows the user to drag the axes’ intersection to an arbitrary point, origin, which starts at the center of the panel. import java.awt.Cursor; import java.awt.Dimension; import java.awt.EventQueue; import java.awt.Graphics; import java.awt.Point; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.MouseMotionAdapter; import javax.swing.JFrame; import javax.swing.JPanel; /** * @see https://stackoverflow.com/a/15576413/230513 * @see https://stackoverflow.com/a/5312702/230513 … Read more

How can a Swing WindowListener veto JFrame closing

The right way is set JFrame.setDefaultCloseOperation to DO_NOTHING_ON_CLOSE when the window is created. And then just calling setVisible(false) or dispose() when your user accepts the close, or doing nothing when the close isn’t accepted. The whole purpose of JFrame.setDefaultCloseOperation is only to prevent the need to implement WindowListeners for the most simple actions. The actions … Read more

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

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

Get effective screen size from Java

GraphicsEnvironment has a method which returns the maximum available size, accounting all taskbars etc. no matter where they are aligned: GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds() Note: On multi-monitor systems, getMaximumWindowBounds() returns the bounds of the entire display area. To get the usable bounds of a single display, use GraphicsConfiguration.getBounds() and Toolkit.getScreenInsets() as shown in other answers.