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

about drawing a Polygon in java

JFrame does not have a paintComponent(Graphics g) method. Add the @Override annotation and you will get a compile time error. 1) Use JPanel and override paintComponent (you would than add JPanel to the JFrame viad JFrame#add(..)) 2) Override getPreferredSize() to return correct Dimensions which fit your drawing on Graphics object or else they wont be … Read more

Java 2D Drawing Optimal Performance

A synthesis of the answers to this post, the answers to Consty’s, and my own research: What works: Use GraphicsConfiguration.createCompatibleImage to create images compatible with what you’re drawing on. This is absolutely essential! Use double-buffered drawing via Canvas.createBufferStrategy. Use -Dsun.java2d.opengl=True where available to speed up drawing. Avoid using transforms for scaling. Instead, cache scaled versions … Read more

Java2D: Increase the line width

You should use setStroke to set a stroke of the Graphics2D object. The example at http://www.java2s.com gives you some code examples. The following code produces the image below: import java.awt.*; import java.awt.geom.Line2D; import javax.swing.*; public class FrameTest { public static void main(String[] args) { JFrame jf = new JFrame(“Demo”); Container cp = jf.getContentPane(); cp.add(new JComponent() … Read more

Draggable rectangles in Java 2D [duplicate]

JHotDraw was designed as “a Java GUI framework for technical and structured Graphics.” The linked JHotDraw Pattern Language: JHotDraw Domain Overview illustrates how to customize drawing editors. The sample org.jhotdraw.samples.draw.Main is a reasonable starting point, and JModeller is a simple UML editor built using the framework.

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

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