create changelog entry
[debian/openrocket] / core / src / net / sf / openrocket / gui / components / ImageDisplayComponent.java
1 package net.sf.openrocket.gui.components;
2
3 import java.awt.Color;
4 import java.awt.Graphics;
5 import java.awt.Graphics2D;
6 import java.awt.RenderingHints;
7 import java.awt.image.BufferedImage;
8 import java.io.File;
9
10 import javax.imageio.ImageIO;
11 import javax.swing.JFrame;
12 import javax.swing.JPanel;
13 import javax.swing.SwingUtilities;
14
15 import net.miginfocom.swing.MigLayout;
16 import net.sf.openrocket.util.MathUtil;
17
18 /**
19  * Draws a BufferedImage centered and scaled to fit to the component.
20  * 
21  * @author Sampo Niskanen <sampo.niskanen@iki.fi>
22  */
23 public class ImageDisplayComponent extends JPanel {
24         
25         private BufferedImage image;
26         
27         public ImageDisplayComponent() {
28                 this(null);
29         }
30         
31         public ImageDisplayComponent(BufferedImage image) {
32                 this.image = image;
33         }
34         
35         
36         @Override
37         protected void paintComponent(Graphics g) {
38                 super.paintComponent(g);
39                 
40                 if (image == null) {
41                         return;
42                 }
43                 
44                 final int width = Math.max(this.getWidth(), 1);
45                 final int height = Math.max(this.getHeight(), 1);
46                 
47                 final int origWidth = Math.max(image.getWidth(), 1);
48                 final int origHeight = Math.max(image.getHeight(), 1);
49                 
50
51                 // Determine scaling factor
52                 double scaleX = ((double) width) / origWidth;
53                 double scaleY = ((double) height) / origHeight;
54                 
55                 double scale = MathUtil.min(scaleX, scaleY);
56                 
57                 if (scale >= 1) {
58                         scale = 1.0;
59                 }
60                 
61
62                 // Center in the middle of the component
63                 int finalWidth = (int) Math.round(origWidth * scale);
64                 int finalHeight = (int) Math.round(origHeight * scale);
65                 
66                 int posX = (width - finalWidth) / 2;
67                 int posY = (height - finalHeight) / 2;
68                 
69
70                 // Draw the image
71                 int dx1 = posX;
72                 int dy1 = posY;
73                 int dx2 = posX + finalWidth;
74                 int dy2 = posY + finalHeight;
75                 int sx1 = 0;
76                 int sy1 = 0;
77                 int sx2 = origWidth;
78                 int sy2 = origHeight;
79                 
80                 Graphics2D g2 = (Graphics2D) g;
81                 g2.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_NORMALIZE);
82                 g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
83                 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
84                 g2.drawImage(image, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, null);
85                 
86         }
87         
88         
89         public BufferedImage getImage() {
90                 return image;
91         }
92         
93         
94         public void setImage(BufferedImage image) {
95                 this.image = image;
96                 this.repaint();
97         }
98         
99         
100         public static void main(String[] args) throws Exception {
101                 final BufferedImage image = ImageIO.read(new File("test.png"));
102                 
103                 SwingUtilities.invokeAndWait(new Runnable() {
104                         @Override
105                         public void run() {
106                                 
107                                 JFrame frame = new JFrame();
108                                 
109                                 JPanel panel = new JPanel(new MigLayout("fill"));
110                                 panel.setBackground(Color.red);
111                                 frame.add(panel);
112                                 
113                                 ImageDisplayComponent c = new ImageDisplayComponent(image);
114                                 panel.add(c, "grow");
115                                 
116                                 frame.setSize(500, 500);
117                                 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
118                                 frame.setVisible(true);
119                                 
120                         }
121                 });
122         }
123         
124 }