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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
| public class MainDansLaZoneTest extends JPanel {
private static final long TIME = 3000; // le temps d'attente = 3 secondes
private static final long PERIOD = 100; // la période d'observation (toutes les 100 ms)
private final Rectangle rectangle; // stocke la zone (pour la dessiner)
private volatile Point mousePoint; // stocke la position de la souris = la position de la main
private AtomicBoolean mainDansLaZone=new AtomicBoolean(); // un état pour savoir qu'on a valider "main dans la zone pendant 3 secondes"
// sert à afficher un compte à rebours (pour la démo)
private volatile int counter;
private Timer counterTimer;
private TimerTask counterTask;
private AtomicBoolean counterStarted=new AtomicBoolean();
private boolean started;
public MainDansLaZoneTest(Rectangle rectangle) {
this.counterTimer = new Timer();
this.rectangle=rectangle;
// simulation de la détection de la main dans la zone = tes classes C++ de gestion de la kinect
addMouseMotionListener(new MouseAdapter() {
@Override
public void mouseMoved(MouseEvent e) {
if ( !e.getPoint().equals(mousePoint) ) {
mousePoint = e.getPoint();
repaint();
}
}
@Override
public void mouseExited(MouseEvent e) {
if ( mousePoint!=null ) {
mousePoint=null;
stopCounter();
repaint();
}
}
});
}
@Override
public Point getMousePosition() throws HeadlessException {
Point point = super.getMousePosition();
if ( point!=null && rectangle.contains(point) ) {
startCounter();
}
else {
stopCounter();
}
return point;
}
public Point getMousePoint() {
Point point = super.getMousePosition();
if ( point!=null && rectangle.contains(point) ) {
startCounter();
}
else {
stopCounter();
}
return point;
}
private void startCounter() {
if ( counterStarted.compareAndSet(false, true) ) {
counter=(int)TIME/1000;
counterTask = new TimerTask() {
@Override
public void run() {
counter--;
if ( counter==0 ) cancel();
repaint();
}
};
counterTimer.schedule(counterTask, 1000, 1000);
repaint();
}
}
private void stopCounter() {
if ( counterStarted.compareAndSet(true, false) ) {
if ( counterTask!=null) counterTask.cancel();
counter=0;
repaint();
}
}
public void setStarted(boolean started) {
if ( this.started!=started) {
this.started=started;
if ( !started ) {
stopCounter();
}
repaint();
}
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if ( started ) {
if ( mousePoint!=null && rectangle.contains(mousePoint) ) {
if ( mainDansLaZone.get() ) {
g.setColor(Color.GREEN); // on affiche la zone en vert quand la souris est dans la zone et qu'on reçu l'événement
}
else {
g.setColor(Color.YELLOW); // on affiche la zone en jaunne quand la souris est dans la zone
}
}
else {
g.setColor(Color.CYAN);
}
g.fillRect(rectangle.x, rectangle.y, rectangle.width, rectangle.height);
if ( mousePoint!=null ) {
g.setColor(Color.BLACK);
g.drawLine(mousePoint.x-1, mousePoint.y, mousePoint.x+1, mousePoint.y);
g.drawLine(mousePoint.x, mousePoint.y-1, mousePoint.x, mousePoint.y+1);
}
if ( counter>0 ) {
g.setColor(Color.BLACK);
String counterString = String.valueOf(counter);
int size = g.getFont().getSize();
FontMetrics fm = g.getFontMetrics();
Rectangle2D stringBounds = fm.getStringBounds(counterString, g);
double scale = rectangle.getHeight()/size;
AffineTransform scaleTransform = AffineTransform.getScaleInstance(scale, scale);
stringBounds = scaleTransform.createTransformedShape(stringBounds).getBounds2D();
Rectangle2D bounds = new Rectangle2D.Double(0, 0, rectangle.width, rectangle.height);
AffineTransform transform = AffineTransform.getTranslateInstance(rectangle.x + (int)((bounds.getWidth()-stringBounds.getWidth())/2), rectangle.y /*+ (int)((bounds.getHeight()-stringBounds.getHeight())/2)*/);
transform.concatenate(scaleTransform);
((Graphics2D)g).setTransform(transform);
g.drawString(counterString, 0, fm.getAscent()-fm.getDescent());
}
}
else {
g.setColor(Color.DARK_GRAY);
g.drawRect(rectangle.x, rectangle.y, rectangle.width, rectangle.height);
}
}
public void setMainDansLaZone(boolean mainDansLaZone) {
if ( this.mainDansLaZone.compareAndSet(!mainDansLaZone, mainDansLaZone) ) {
repaint();
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(()-> run());
}
private static void run() {
// création de l'UI
JFrame frame = new JFrame("Démo");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setSize(600, 400);
frame.setLocationRelativeTo(null);
Rectangle zone = new Rectangle( 300, 100, 150, 100); // zone d'observation
MainDansLaZoneTest panel = new MainDansLaZoneTest(zone); // simulation de kinect
MainDansLaZoneEventProducer eventProducer = new MainDansLaZoneEventProducer(
new TrucKinect() {
@Override
public Point getPosition() {
return panel.getMousePoint();
}
}, zone, TIME, PERIOD);
// enregistrement de l'écouteur
eventProducer.addListener(()-> {
// ici on fait le code qu'on veut faire quand la main entre dans la zone
System.out.printf("Main dans la zone pendant %d seconde(s)%n", TIME/1000);
panel.setMainDansLaZone(true);
new Thread(){
public void run() {
try {
Thread.sleep(1000);
panel.setMainDansLaZone(false);
} catch (InterruptedException e) {
}
}
}.start();
});
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BorderLayout());
mainPanel.add(panel, BorderLayout.CENTER);
JCheckBox checkBox = new JCheckBox("Ecoute active");
checkBox.addChangeListener(e-> {
if ( checkBox.isSelected() ) {
eventProducer.start();
panel.setStarted(true);
}
else {
eventProducer.stop();
panel.setStarted(false);
}
});
mainPanel.add(checkBox, BorderLayout.SOUTH);
frame.getContentPane().add(mainPanel);
// important : il faut démarrer le composant et l'arrêter
// ici on démarre quand la fenêtre est ouverte, et on arrête quand elle est fermée
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowOpened(WindowEvent e) {
eventProducer.start();
panel.setStarted(true);
checkBox.setSelected(true);
}
@Override
public void windowClosed(WindowEvent e) {
eventProducer.stop();
System.exit(0);
}
});
frame.setVisible(true);
}
} |