import java.awt.*;
import javax.swing.*;
public class SwingPaintDemo {
public SwingPaintDemo() {
}
public static void createAndShowGui() {
JFrame f = new JFrame("Aim For the Center");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container panel = new BullsEyePanel();
f.getContentPane().add(panel, BorderLayout.CENTER);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
public static void main(final String[] args) {
Runnable gui = new Runnable() {
@Override
public void run() {
createAndShowGui();
}
};
//GUI must start on EventDispatchThread:
SwingUtilities.invokeLater(gui);
}
/**
* A Swing container that renders a bullseye background
* where the area around the bullseye is transparent.
*/
private static class BullsEyePanel extends JPanel {
private Image imageo;
BullsEyePanel() {
super();
setOpaque(false); // we don't paint all our bits
setLayout(new BorderLayout());
setBorder(BorderFactory.createLineBorder(Color.black));
imageo = Toolkit.getDefaultToolkit().getImage("Bomber1.png");
}
@Override
public Dimension getPreferredSize() {
// Figure out what the layout manager needs and
// then add 200 to the largest of the dimensions
// in order to enforce a 'round' bullseye
Dimension layoutSize = super.getPreferredSize();
int max = Math.max(layoutSize.width, layoutSize.height);
return new Dimension(max + 200, max + 200);
}
@Override
protected void paintComponent(final Graphics g) {
super.paintComponent(g);
Dimension size = getSize();
int x = 0;
int y = 0;
int i = 0;
g.drawImage(imageo, 0, 0, this);
}
}
}