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

Change the angle/position of a drawing with a algorithm in Java

Try something like… The demo is designed to allow images to be rotated through virtual angels (angles < 0 & > 360), but the basic concept is the same… public class TestFlipImage { protected static final String IMAGE_PATH = “/path/to/your/image”; public static void main(String[] args) { new TestFlipImage(); } public TestFlipImage() { EventQueue.invokeLater(new Runnable() { … 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

Java make a directed line and make it move

This is a basic example that demonstrates the use of a Path2D, to define a custom shape and an AffineTransformation to transform the shape. This example will cause the arrow to point towards the mouse as it moves over the panel import java.awt.BasicStroke; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.EventQueue; import java.awt.Graphics; import java.awt.Graphics2D; … Read more

Having images as background of JPanel

Why not make a single class that takes a Image?? public class ImagePane extends JPanel { private Image image; public ImagePane(Image image) { this.image = image; } @Override public Dimension getPreferredSize() { return image == null ? new Dimension(0, 0) : new Dimension(image.getWidth(this), image.getHeight(this)); } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = … Read more

Implementing a mouse click event on a tile in a map

On a sufficiently small map with a suitable projection, you can transform between coordinate systems using linear interpolation relative to the enclosing panel. Noting the following proportions, you can cross-multiply and solve for the missing coordinate, as shown in this complete example that maps mouse coordinates to pixel coordinates in an image. mouse.x : panelWidthInPixels … Read more

Spring behavior simulation

Here’s a working sscce using FloatSpring. import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.EventQueue; import java.awt.Graphics; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.AbstractAction; import javax.swing.Icon; import javax.swing.JButton; import javax.swing.JComponent; import javax.swing.JFrame; import javax.swing.Timer; import javax.swing.UIManager; /** * @see http://stackoverflow.com/a/11233735/230513 */ public class Test { private static Spring spring = new Spring(); public static void main(String[] args) { EventQueue.invokeLater(new … Read more

Why does calling dispose() on Graphics object cause JPanel to not render any components

The thing is that the Graphics context you are using in paintComponent is created and provided by the caller (the framework), which is also responsible for disposing of it. You only need to dispose of Graphics when you actually create it yourself (for example by calling Component.getGraphics()). In your case, you’re not creating it, you’re … Read more