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

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 to print a JTable object in the Java application

You obviously didn’t read the links provided in your previous question. From the Printing section of How to use Tables Printing JTable provides a simple API for printing tables. The easiest way to print out a table is to invoke JTable.print with no arguments: try { if (! table.print()) { System.err.println(“User cancelled printing”); } } … Read more

How can I make this JButton visible? When I have progressive scan background JWindow()?

It’s not clear how your video source works, but it appears to be incompatible with Swing due to Mixing Heavyweight and Lightweight Components. Although awt components aren’t transparent, you can always draw your own translucent text on the Frame and do manual hit testing, as suggested below. You might also check to see if your … 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

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.