Infinite background for game

Have a look through this source which behaves in a more predictable manner, and also includes a nice tweak to the chopper image (animated). 😉 import java.awt.*; import java.awt.event.*; import java.awt.geom.*; import java.awt.image.BufferedImage; import java.net.URL; import java.io.IOException; import javax.imageio.ImageIO; import javax.swing.*; public class MainFrame { public MainFrame() { JFrame f = new JFrame(“Helicopter Background Test”); … Read more

Exporting a JPanel to an image

The panel needs to be laid out based on it’s requirements. If the panel hasn’t being realized on the screen yet, it may not render the way you expect it do The following example assumes that the panel has not being displayed on the screen… setSize(getPreferredSize()); BufferedImage image = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB); Graphics2D g … Read more

Using addMouseListener() and paintComponent() for JPanel

This is not an immediate answer to your question, but knowing (or at least suspecting) what it is that you wish to offer (a simple paint program), I suggest starting with this approach based around a BufferedImage as the painting surface.. import java.awt.*; import java.awt.RenderingHints.Key; import java.awt.event.*; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.util.HashMap; … Read more