aboutsummaryrefslogtreecommitdiffstats
path: root/src/jake2/imageio/ImageFrame.java
blob: 52cf9e4330e5c52de8d54076846a249255efef45 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
 * Created on Apr 26, 2003
 *
 */
package jake2.imageio;

import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;

/**
 * @author cwei
 *
 */
public class ImageFrame extends JFrame {

	BufferedImage image;
	Component pane;

	public ImageFrame(BufferedImage image) {
		super();
		this.image = image;

		pane = getContentPane();
		setIconImage(image);
		setSize(640, 480);

		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
	}

	public void paint(Graphics g) {
		super.paint(g);
		Graphics2D g2 = (Graphics2D) pane.getGraphics();
		if (this.image != null) {
			g2.drawImage(
				image,
				Math.max(0, (getWidth() - image.getWidth()) / 2),
				Math.max(0, (getHeight() - image.getHeight()) / 2),
				Color.LIGHT_GRAY,
				pane);
		} else {
			g2.drawString(
				"EMPTY IMAGE",
				this.getWidth() / 4,
				this.getHeight() / 2);
		}
	}

	public void showImage(BufferedImage image) {
		this.image = image;
		this.repaint();
	}

}