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

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

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

.gif image doesn’t moves on adding it to the JTabbed pane

Let me demonstrate the hole you are digging yourself into You could do… BufferedImage img = ImageIO.read(this.getClass().getResource(“anigif.gif”)); ImageIcon icon = new ImageIcon(img); JLabel label = new JLabel(icon); add(label); Or you could do… Now, this is woefully inadequate and is designed for example purposes only. It does not support disposal methods or optimized Gifs…so you can … Read more

Difference between paint() and paintcomponent()?

Quoting from documentation of paint() method This method actually delegates the work of painting to three protected methods: paintComponent, paintBorder, and paintChildren. … A subclass that just wants to specialize the UI (look and feel) delegate’s paint method should just override paintComponent. It looks like the paint() method actually draws the component, including the border … Read more

Drawing an object using getGraphics() without extending JFrame

If you want to change the way your component is being drawn (you are adding rectangles), you need to redefine paintComponent() in that component. In your code, you are using getGraphics(). You shouldn’t call getGraphics() on a component. Any painting you do (to the Graphics returned) will be temporary and will be lost the next … Read more

How to draw an image over another image?

Another approach that does not require extending components. import javax.swing.*; import java.awt.*; import java.awt.image.*; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.util.Random; import java.net.URL; import javax.imageio.ImageIO; public class ImageOnImage { ImageOnImage(final BufferedImage bg, BufferedImage fg) { final BufferedImage scaled = new BufferedImage( fg.getWidth()/2,fg.getHeight()/2,BufferedImage.TYPE_INT_RGB); Graphics g = scaled.getGraphics(); g.drawImage(fg,0,0,scaled.getWidth(),scaled.getHeight(),null); g.dispose(); final int xMax = bg.getWidth()-scaled.getWidth(); final int yMax … Read more

tech