KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > snow > utils > gui > ImageViewerPanel


1 package snow.utils.gui;
2
3 import javax.swing.*;
4 import javax.swing.event.*;
5 import javax.imageio.*;
6 import javax.swing.text.*;
7 import javax.swing.text.html.*;
8 import java.io.*;
9 import java.nio.*;
10 import java.awt.*;
11 import java.awt.geom.*;
12 import java.awt.event.*;
13 import java.awt.image.*;
14 import java.util.*;
15
16 /** an image viewer
17 */

18 public class ImageViewerPanel extends JPanel
19 {
20   ImagePanel ip = new ImagePanel();
21   Image im = null;
22   JScrollPane scrollPane = new JScrollPane(ip);
23
24   JPanel controlPanel = new JPanel(new FlowLayout(FlowLayout.LEFT,4,4));
25   JButton zoomPlus = new JButton("+");
26   JButton zoomMinus = new JButton("-");
27   JCheckBox limitToFrameDims = new JCheckBox("Limit to frame size", true);
28
29   double zoomFact = 1;
30   AffineTransform imTr = new AffineTransform(1,0,0,1,0,0);
31
32   private final ComponentAdapter resizeListener;
33
34   public ImageViewerPanel()
35   {
36      super(new BorderLayout());
37      add(scrollPane, BorderLayout.CENTER);
38      add(controlPanel, BorderLayout.NORTH);
39      controlPanel.add(zoomPlus);
40      zoomPlus.setMargin(new Insets(0,2,0,2));
41      zoomPlus.setFocusPainted(false);
42      controlPanel.add(zoomMinus);
43      zoomMinus.setMargin(new Insets(0,3,0,3));
44      zoomMinus.setFocusPainted(false);
45
46      controlPanel.add(limitToFrameDims);
47
48      zoomPlus.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae)
49      {
50         zoomFact *= 1.1;
51         update();
52      }});
53
54      zoomMinus.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae)
55      {
56         zoomFact /= 1.1;
57         update();
58      }});
59
60      limitToFrameDims.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae)
61      {
62         update();
63      }});
64
65
66      resizeListener = new ComponentAdapter()
67      {
68        @Override JavaDoc public void componentResized(ComponentEvent ce)
69        {
70          if(limitToFrameDims.isSelected())
71          {
72            update();
73          }
74        }
75      };
76      ip.addComponentListener(resizeListener);
77
78   } // Constructor
79

80   public void terminate()
81   {
82     ip.removeComponentListener(resizeListener);
83     this.im = null;
84   }
85
86   public JPanel getToolBar() { return controlPanel; }
87
88   public void setImage(Image im)
89   {
90     this.im = im;
91     this.zoomFact = 1.0;
92     update();
93   }
94
95   void update()
96   {
97     if(im!=null)
98     {
99       double imw = im.getWidth(this)+5;
100       double imh = im.getHeight(this)+5;
101       System.out.println(""+scrollPane.getWidth()+" "+this.scrollPane.getPreferredSize().getWidth());
102       System.out.println(""+this.getWidth()+" "+this.getPreferredSize().getWidth());
103
104       if(this.limitToFrameDims.isSelected())
105       {
106          double f = 1;
107
108          double wp = scrollPane.getWidth();
109          if(wp<=0) wp = this.getPreferredSize().getWidth();
110          if(wp < imw) f = wp / imw;
111
112          double hp = scrollPane.getHeight();
113          if(hp<=0) hp = this.getPreferredSize().getHeight();
114          if(hp < imh)
115          {
116            double f2 = hp / imh;
117            if(f2<f) f = f2;
118          }
119          this.zoomFact = f;
120
121          System.out.println("reduce "+zoomFact);
122          //scrollPane.setHorizontalScrollBarPolicy( JScrollPane.HORIZONTAL_SCROLLBAR_NEVER );
123
}
124       else
125       {
126         //scrollPane.setHorizontalScrollBarPolicy( JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED );
127
}
128
129       Dimension dim = new Dimension((int)(im.getWidth(this)*zoomFact), (int)(im.getHeight(this)*zoomFact));
130       ip.setSize( dim );
131       ip.setPreferredSize( dim ); // don't forget, or the scroll pane will never appear !
132

133
134     }
135     else
136     {
137       ip.setPreferredSize(new Dimension(2,2));
138     }
139     repaint();
140   }
141
142
143   class ImagePanel extends JPanel
144   {
145      public ImagePanel()
146      {
147        super();
148        setBackground(Color.black);
149      }
150
151      @Override JavaDoc public void paintComponent(Graphics g)
152      {
153        // background
154
super.paintComponent(g);
155        Graphics2D g2 = (Graphics2D) g;
156        imTr.setToScale( zoomFact, zoomFact);
157        g2.transform( imTr );
158        if(im!=null)
159        {
160          double x0 = 0;
161          double y0 = 0;
162          if(im.getWidth(this)*zoomFact < scrollPane.getWidth()) x0 = (scrollPane.getWidth()-im.getWidth(this)*zoomFact )/2.0;
163          if(im.getHeight(this)*zoomFact < scrollPane.getHeight()) y0 = (scrollPane.getHeight()-im.getHeight(this)*zoomFact )/2.0;
164          g.drawImage(im, (int) (x0/zoomFact), (int) (y0/zoomFact), ImageViewerPanel.this);
165        }
166      }
167   }
168
169
170 } // ImageViewerPanel
Popular Tags