From 5bb657301359fbd1d141b15cb68d2e9adb9bf617 Mon Sep 17 00:00:00 2001 From: Arnaud Giersch Date: Tue, 14 Oct 2014 22:10:45 +0200 Subject: [PATCH] Add methods waitMousePress & Co. --- DrawingWindow.java | 103 +++++++++++++++++++++++++++++++++++++++++++++ Test.java | 6 +++ overview.html | 3 +- 3 files changed, 111 insertions(+), 1 deletion(-) diff --git a/DrawingWindow.java b/DrawingWindow.java index b73cf7e..9fb03b5 100644 --- a/DrawingWindow.java +++ b/DrawingWindow.java @@ -5,10 +5,13 @@ import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics2D; import java.awt.Graphics; +import java.awt.Point; import java.awt.Polygon; import java.awt.Toolkit; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.awt.image.BufferedImage; @@ -57,6 +60,11 @@ public class DrawingWindow { this.width = width; this.height = height; + mouseLock = new Object(); + mouseEvent = null; + mousePos = new Point(0, 0); + mouseButton = 0; + image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); graphics = image.createGraphics(); @@ -399,6 +407,86 @@ public class DrawingWindow { return image.getRGB(x, y); } + /** + * Attend l'appui sur un des boutons de la souris. + * + * @return #true si un bouton a été pressé + * + * @see #waitMousePress(long) + * @see #getMouseX() + * @see #getMouseY() + * @see #getMouseButton() + */ + public boolean waitMousePress() { + return waitMousePress(-1); + } + + /** + * Attend l'appui sur un des boutons de la souris. + * + * @param timeout temps maximal d'attente (millisecondes) + * + * @return #true si un bouton a été pressé + * + * @see #waitMousePress() + * @see #getMouseX() + * @see #getMouseY() + * @see #getMouseButton() + */ + public boolean waitMousePress(long timeout) { + boolean result = false; + synchronized (mouseLock) { + if (timeout != 0) { + mouseEvent = null; + try { + if (timeout > 0) + mouseLock.wait(timeout); + else // (timeout < 0) + mouseLock.wait(); + } + catch (InterruptedException e) { + } + } + if (mouseEvent != null) { + mousePos.setLocation(mouseEvent.getPoint()); + mouseButton = mouseEvent.getButton(); + mouseEvent = null; + result = true; + } + } + return result; + } + + /** + * Retourne la position (x) de la souris la dernière fois qu'un + * bouton a été pressé pendant l'appel à {@link #waitMousePress()}. + * + * @return position (x) + */ + public int getMouseX() { + return mousePos.x; + } + + /** + * Retourne la position (y) de la souris la dernière fois qu'un + * bouton a été pressé pendant l'appel à {@link #waitMousePress()}. + * + * @return position (y) + */ + public int getMouseY() { + return mousePos.y; + } + + /** + * Retourne le numéro du bouton de la souris pressé pendant + * le dernier appel à {@link #waitMousePress()}. + * + * @return numéro de bouton (1: gauche, 2: milieu, 3: droit) + */ + public int getMouseButton() { + return mouseButton; + } + /** * Synchronise le contenu de la fenêtre. * @@ -640,6 +728,10 @@ public class DrawingWindow { private Graphics2D graphics; // graphics associated with image private Color bgColor; // background color, for clearGraph() private boolean isClosed; // is the window closed ? + private Object mouseLock; // lock for mouse events + private MouseEvent mouseEvent; // last mouse event + private Point mousePos; // last mouse position + private int mouseButton; // last mouse click // To be run on the Event Dispatching Thread void createGUI() { @@ -651,6 +743,7 @@ public class DrawingWindow { frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); frame.addWindowListener(new DWWindowHandler()); frame.addKeyListener(new DWKeyHandler()); + frame.addMouseListener(new DWMouseHandler()); frame.setLocationByPlatform(true); frame.setVisible(true); } @@ -685,6 +778,16 @@ public class DrawingWindow { } } + private class DWMouseHandler extends MouseAdapter { + public void mousePressed(MouseEvent ev) { + DrawingWindow w = DrawingWindow.this; + synchronized (w.mouseLock) { + w.mouseEvent = ev; + mouseLock.notifyAll(); + } + } + } + private class DWPanel extends JPanel { DWPanel() { DrawingWindow w = DrawingWindow.this; diff --git a/Test.java b/Test.java index bcc7f92..65a891f 100644 --- a/Test.java +++ b/Test.java @@ -44,5 +44,11 @@ class Test{ } } w2.closeGraph(); + + while (w1.waitMousePress(5 * 1000)) + System.out.println("[ " + w1.getMouseX() + " ; " + w1.getMouseY() + + " ] -- button " + w1.getMouseButton()); + + System.out.println("Done!"); } } diff --git a/overview.html b/overview.html index 89e27f4..bd8e2f3 100644 --- a/overview.html +++ b/overview.html @@ -39,11 +39,12 @@

Modifications

--- lun. 14 oct. 2014 14:09:22 +0200
+-- lun. 14 oct. 2014 22:10:45 +0200
 
         * Ajout des méthodes setColor(int) et setBgColor(int).
         * Liste de noms de couleurs plus complète.
         * Ne termine l'application que lorsque la dernière fenêtre est fermée.
+        * Ajout des méthodes waitMousePress() et getMouse{X,Y,Button}().
     
-- 2.20.1