add eye tracking benchmark.
[IRC.git] / Robust / src / Benchmarks / SSJava / EyeTracking / InfoPanel.java
1 /*
2  * Copyright 2009 (c) Florian Frankenberger (darkblue.de)
3  * 
4  * This file is part of LEA.
5  * 
6  * LEA is free software: you can redistribute it and/or modify it under the
7  * terms of the GNU Lesser General Public License as published by the Free
8  * Software Foundation, either version 3 of the License, or (at your option) any
9  * later version.
10  * 
11  * LEA is distributed in the hope that it will be useful, but WITHOUT ANY
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
13  * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
14  * details.
15  * 
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with LEA. If not, see <http://www.gnu.org/licenses/>.
18  */
19
20
21 import java.awt.BasicStroke;
22 import java.awt.Color;
23 import java.awt.Graphics;
24 import java.awt.Graphics2D;
25 import java.awt.RenderingHints;
26 import java.awt.image.BufferedImage;
27 import java.io.IOException;
28 import java.io.InputStream;
29 import javax.imageio.ImageIO;
30 import javax.swing.JPanel;
31
32 /**
33  *
34  * @author Florian
35  */
36 abstract class InfoPanel extends JPanel {
37
38         private static final long serialVersionUID = 8311083210432152308L;
39         
40         private String text = "";
41     private BufferedImage image = null;
42     private BufferedImage[] noImageImages = null;
43     private int imageWidth, imageHeight;
44     private int animationTime = 0;
45
46     private AnimationThread animationThread = null;
47
48     private class AnimationThread extends Thread {
49
50         private boolean shutdown = false;
51         private int counter = 0;
52
53         public AnimationThread() {
54             this.start();
55         }
56
57         public void shutdown() {
58             this.shutdown = true;
59         }
60
61         @Override
62         public void run() {
63             while (!shutdown) {
64                 counter = (++counter % noImageImages.length);
65                 image = noImageImages[counter];
66                 repaint();
67
68                 try { Thread.sleep(animationTime); } catch (InterruptedException e) {}
69             }
70         }
71
72     }
73             
74     public InfoPanel(String text, String[] noImageImageFileNames, int fps, int imageWidth, int imageHeight) {
75         this.text = text;
76         this.imageWidth = imageWidth;
77         this.imageHeight = imageHeight;
78         if (noImageImageFileNames == null || noImageImageFileNames.length == 0) {
79             this.noImageImages = null;
80         } else {
81             this.noImageImages = new BufferedImage[noImageImageFileNames.length];
82             for (int i = 0; i < noImageImageFileNames.length; ++i) {
83                 this.noImageImages[i] = loadImage(noImageImageFileNames[i]);
84             }
85         }
86         this.animationTime = (fps == 0 ? 0 : 1000/fps);
87         this.setImage(null);
88     }
89
90     protected static BufferedImage loadImage(String fileName) {
91         BufferedImage image = null;
92         try {
93             InputStream in =
94                     FaceInfoPanel.class.getClassLoader().getResourceAsStream(fileName);
95             if (in != null) {
96                 image = ImageIO.read(in);
97                 in.close();
98             }
99         } catch (IOException ex) {
100             ex.printStackTrace();
101
102         }
103
104         return image;
105     }
106
107     @Override
108     public void paint(Graphics g) {
109         Graphics2D g2d = (Graphics2D) g;
110
111         g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
112         g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
113                     RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
114
115         g2d.setBackground(new Color(0, 0, 0));
116         g2d.clearRect(0, 0, this.getWidth(), this.getHeight());
117
118         g2d.setColor(new Color(39, 53, 66));
119         g2d.fillRoundRect(2, 2, this.getWidth()-4, this.getHeight()-4, 10, 10);
120
121         g2d.setColor(new Color(118, 149, 174));
122         g2d.drawString(text, 10, 20);
123
124         g2d.setColor(new Color(81, 111, 137));
125         g2d.drawRect(this.getWidth()-11-imageWidth, 9, imageWidth+1, imageHeight+1);
126
127         if (image != null) {
128             g2d.drawImage(image,
129                     this.getWidth() - 10 - imageWidth, 10,
130                     imageWidth, imageHeight, null);
131         }
132
133         g2d.setStroke(new BasicStroke(2.0f));
134         g2d.setColor(new Color(81, 111, 137));
135         g2d.drawRoundRect(2, 2, this.getWidth()-4, this.getHeight()-4, 10, 10);
136     }
137
138     protected synchronized void setImage(BufferedImage image) {
139         if (image == null) {
140             if (this.noImageImages != null && this.noImageImages.length > 0) {
141                 image =  this.noImageImages[0];
142                 if (this.animationTime > 0) {
143                     if (this.animationThread == null)
144                         this.animationThread = new AnimationThread();
145                 }
146             }
147         } else {
148             if (this.animationThread != null) {
149                 this.animationThread.shutdown();
150                 this.animationThread = null;
151             }
152         }
153         this.image = image;
154         this.repaint();
155     }
156
157
158 }