JAL-3032 adds Java 8 functionality (2/2)
[jalview.git] / src2 / fr / orsay / lri / varna / components / ZoomWindow.java
1 package fr.orsay.lri.varna.components;
2
3 import java.awt.BasicStroke;
4 import java.awt.Color;
5 import java.awt.Graphics;
6 import java.awt.Graphics2D;
7 import java.awt.Point;
8 import java.awt.Polygon;
9 import java.awt.Rectangle;
10 import java.awt.RenderingHints;
11 import java.awt.event.MouseEvent;
12 import java.awt.event.MouseListener;
13 import java.awt.event.MouseMotionListener;
14 import java.awt.geom.GeneralPath;
15 import java.awt.geom.Point2D;
16 import java.awt.geom.Rectangle2D;
17 import java.awt.geom.Point2D.Double;
18 import java.awt.image.BufferedImage;
19 import java.awt.image.ImageObserver;
20
21 import javax.swing.JPanel;
22
23 import fr.orsay.lri.varna.VARNAPanel;
24 import fr.orsay.lri.varna.models.export.SwingGraphics;
25 import fr.orsay.lri.varna.models.export.VueVARNAGraphics;
26
27 public class ZoomWindow extends JPanel implements ImageObserver, Runnable, MouseMotionListener, MouseListener {
28
29         VARNAPanel _vp = null;
30         BufferedImage _bi = null;
31         Rectangle2D.Double rnaRect = null;
32         
33         public ZoomWindow(VARNAPanel vp)
34         {
35                 _vp = vp;
36                 addMouseMotionListener(this);
37                 addMouseListener(this);
38         }
39         
40         
41         public synchronized void setPanel(VARNAPanel vp)
42         {
43                 _vp = vp; 
44         }
45         
46         
47         
48         public synchronized void drawPanel()
49         {
50                 if (getWidth()>0 && getHeight()>0)
51                 {
52                         _bi= new BufferedImage(getWidth(),getHeight(),BufferedImage.TYPE_4BYTE_ABGR);
53                         Graphics2D g2 = _bi.createGraphics();
54                         g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
55                                 RenderingHints.VALUE_ANTIALIAS_ON);
56                         VueVARNAGraphics g2D = new SwingGraphics(g2);
57                         rnaRect =_vp.renderRNA(g2D,new Rectangle2D.Double(0,0,getWidth(),getHeight()),false,true);
58                         
59                         Point2D.Double p1 = _vp.panelToLogicPoint(new Point2D.Double(0.,0.));
60                         Point2D.Double p2 = _vp.panelToLogicPoint(new Point2D.Double(_vp.getWidth(),_vp.getHeight()));
61                         
62                         double w = p2.x-p1.x; 
63                         double h = p2.y-p1.y;
64                                                 
65                         Rectangle2D.Double rnaBox = _vp.getRNA().getBBox();
66                         
67                         double ratiox = w/rnaBox.width;
68                         double ratioy = h/rnaBox.height;
69                         
70                         Rectangle2D.Double rvisible = new Rectangle2D.Double(rnaRect.x+rnaRect.width*(double)(p1.x-rnaBox.x)/(double)rnaBox.width,
71                                         rnaRect.y+rnaRect.height*(double)(p1.y-rnaBox.y)/(double)rnaBox.height,
72                                         ratiox*rnaRect.width,
73                                         ratioy*rnaRect.height);
74                         
75                         //g2D.drawRect(rleft.x,rleft.y,rleft.width,rleft.height);
76                         
77                         Color shade = new Color(.9f,.9f,.9f,.4f);
78                         
79                         g2.setStroke(new BasicStroke(1.0f));
80
81                         g2.setColor(shade);
82                         
83                         /*Polygon north = new Polygon(new int[]{0,getWidth(),(int)rvisible.x,
84                                         (int)(rvisible.x+rvisible.width+1),},new int[]{},1);*/
85                         g2.fillRect(0,0,getWidth(),(int)rvisible.y);
86                         g2.fillRect(0,(int)rvisible.y,(int)rvisible.x,(int)rvisible.height+1);
87                         g2.fillRect((int)(rvisible.x+rvisible.width),(int)rvisible.y,(int)(getHeight()-(rvisible.x+rvisible.width)),(int)(rvisible.height+1));
88                         g2.fillRect(0,(int)(rvisible.y+rvisible.height),getWidth(),(int)(getHeight()-(rvisible.y+rvisible.height)));
89
90                         g2.setColor(new Color(.7f,.7f,.7f,.3f));
91                         g2.draw(rvisible);
92                         g2.drawLine(0,
93                                         0,
94                                         (int)rvisible.x,
95                                         (int)rvisible.y);
96                         g2D.drawLine(getWidth(),
97                                         0,
98                                         rvisible.x+rvisible.width,
99                                         rvisible.y);
100                         g2D.drawLine(getWidth(),
101                                         getHeight(),
102                                         rvisible.x+rvisible.width,
103                                         rvisible.y+rvisible.height);
104                         g2D.drawLine(0,
105                                         getHeight(),
106                                         rvisible.x,
107                                         rvisible.y+rvisible.height);
108
109                         g2.dispose();
110                 }
111         }
112         
113         public void paintComponent(Graphics g)
114         {
115                 setBackground(_vp.getBackground());
116                 super.paintComponent(g);
117                 drawPanel();
118                 if (_bi!=null)
119                 {
120                         g.drawImage(_bi,0,0,this);
121                 }
122         }
123
124         public void run() {
125                 while(true)
126                 {
127                         repaint();
128                         try {
129                                 Thread.sleep(500);
130                         } catch (InterruptedException e) {
131                                 e.printStackTrace();
132                         }
133                         
134                 }
135         }
136
137
138         public void mouseDragged(MouseEvent e) {
139                 // TODO Auto-generated method stub
140                 
141         }
142
143
144         public void mouseMoved(MouseEvent e) {
145                 // TODO Auto-generated method stub
146                 
147         }
148
149
150         public void mouseClicked(MouseEvent e) {
151                 /*if (rnaRect!=null)
152                 {
153                         int x= e.getX();
154                         int y= e.getY();
155                         double ratioX = ((double)(x-rnaRect.getMinX())/((double)rnaRect.width));
156                         double ratioY = ((double)(y-rnaRect.getMinY())/((double)rnaRect.height));
157                         _vp.centerViewOn(ratioX,ratioY );
158                 }*/
159         }
160
161
162         public void mousePressed(MouseEvent e) {
163                 // TODO Auto-generated method stub
164                 
165         }
166
167
168         public void mouseReleased(MouseEvent e) {
169                 // TODO Auto-generated method stub
170                 
171         }
172
173
174         public void mouseEntered(MouseEvent e) {
175                 // TODO Auto-generated method stub
176                 
177         }
178
179
180         public void mouseExited(MouseEvent e) {
181                 // TODO Auto-generated method stub
182                 
183         }
184         
185 }