X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/graphlib_java.git/blobdiff_plain/2bf0d02e7c40d2b3af46579ffc8e9e04f58944d0..147524588337beed8ccc0b3981a6e06341082348:/DrawingWindow.java diff --git a/DrawingWindow.java b/DrawingWindow.java index 4cb330d..08f85e5 100644 --- a/DrawingWindow.java +++ b/DrawingWindow.java @@ -1,8 +1,23 @@ -import java.awt.*; -import java.awt.event.*; -import java.awt.image.*; -import javax.swing.*; -import java.lang.reflect.*; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +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.Rectangle; +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; +import javax.swing.JFrame; +import javax.swing.JPanel; /** * Fenêtre de dessin @@ -19,16 +34,8 @@ import java.lang.reflect.*; * fenêtre. Comme pour la plupart des applications, il est également * possible de fermer la fenêtre via le gestionnaire de fenêtres. * - *
Télécharger le code: DrawingWindow.java - * - *
Télécharger des exemples d'utilisation:
- * Hello.java
- * Exemple1.java
- * Exemple2.java
- * Exemple3.java
- *
* @author Arnaud Giersch <arnaud.giersch@univ-fcomte.fr>
- * @version Thu Oct 9 16:03:46 2014 +0200
+ * @version 20141104
*/
public class DrawingWindow {
@@ -54,6 +61,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();
@@ -80,6 +92,7 @@ public class DrawingWindow {
* @param color couleur
*
* @see java.awt.Color
+ * @see #setColor(int)
* @see #setColor(String)
* @see #setColor(float, float, float)
* @see #setBgColor(Color)
@@ -91,23 +104,36 @@ public class DrawingWindow {
/**
* Change la couleur de dessin.
*
- * Le nom de couleur peut être "black", "blue", "cyan",
- * "darkGray", "gray", "green", "lightGray", "magenta", "orange",
- * "pink", "red", "white", ou "yellow".
+ * La couleur est un entier, tel que retourné par {@link #getPointColor}.
+ * Normalement de la forme #00RRGGBB.
+ *
+ * @param rgb couleur
+ *
+ * @see #setColor(String)
+ * @see #setColor(float, float, float)
+ * @see #setBgColor(int)
+ * @see #getPointColor
+ */
+ public void setColor(int rgb) {
+ setColor(new Color(rgb));
+ }
+
+ /**
+ * Change la couleur de dessin.
*
* @param name nom de couleur
*
- * @see #setColor(Color)
+ * @see #setColor(int)
* @see #setColor(float, float, float)
* @see #setBgColor(String)
+ * @see liste des noms de couleurs
*/
public void setColor(String name) {
- try {
- Field field = Class.forName("java.awt.Color").getField(name);
- graphics.setColor((Color)field.get(null));
- } catch (Exception e) {
+ Color color = colorMap.get(name);
+ if (color != null)
+ setColor(color);
+ else
System.err.println("Warning: color not found: " + name);
- }
}
/**
@@ -121,7 +147,7 @@ public class DrawingWindow {
* @param green composante de vert
* @param blue composante de bleu
*
- * @see #setColor(Color)
+ * @see #setColor(int)
* @see #setColor(String)
* @see #setBgColor(float, float, float)
*/
@@ -134,32 +160,47 @@ public class DrawingWindow {
*
* @param color couleur
*
+ * @see #setBgColor(int)
* @see #setBgColor(String)
* @see #setBgColor(float, float, float)
* @see #setColor(Color)
- * @see #clearGraph()
+ * @see #clearGraph
*/
public void setBgColor(Color color) {
bgColor = color;
}
+ /** Change la couleur de fond.
+ *
+ * @param rgb couleur
+ *
+ * @see #setBgColor(String)
+ * @see #setBgColor(float, float, float)
+ * @see #setColor(int)
+ * @see #getPointColor
+ * @see #clearGraph
+ */
+ public void setBgColor(int rgb) {
+ setBgColor(new Color(rgb));
+ }
+
/**
* Change la couleur de fond.
*
* @param name nom de couleur
*
- * @see #setBgColor(Color)
+ * @see #setBgColor(int)
* @see #setBgColor(float, float, float)
* @see #setColor(String)
- * @see #clearGraph()
+ * @see #clearGraph
+ * @see liste des noms de couleurs
*/
public void setBgColor(String name) {
- try {
- Field field = Class.forName("java.awt.Color").getField(name);
- bgColor = (Color)field.get(null);
- } catch (Exception e) {
+ Color color = colorMap.get(name);
+ if (color != null)
+ setBgColor(color);
+ else
System.err.println("Warning: color not found: " + name);
- }
}
/** Change la couleur de fond.
@@ -168,13 +209,13 @@ public class DrawingWindow {
* @param green composante de vert
* @param blue composante de bleu
*
- * @see #setBgColor(Color)
+ * @see #setBgColor(int)
* @see #setBgColor(String)
* @see #setColor(float, float, float)
- * @see #clearGraph()
+ * @see #clearGraph
*/
public void setBgColor(float red, float green, float blue) {
- bgColor = new Color(red, green, blue);
+ setBgColor(new Color(red, green, blue));
}
/**
@@ -202,6 +243,8 @@ public class DrawingWindow {
* @see #setColor
*/
public void drawPoint(int x, int y) {
+ if (x < 0 || y < 0 || x >= width || y >= height)
+ return;
synchronized (image) {
image.setRGB(x, y, graphics.getColor().getRGB());
}
@@ -316,7 +359,9 @@ public class DrawingWindow {
synchronized (image) {
graphics.drawPolygon(poly);
}
- panel.repaint(poly.getBounds());
+ Rectangle bounds = poly.getBounds();
+ bounds.setSize(bounds.width + 1, bounds.height + 1);
+ panel.repaint(bounds);
}
/**
@@ -338,6 +383,8 @@ public class DrawingWindow {
graphics.drawPolygon(poly);
graphics.fillPolygon(poly);
}
+ Rectangle bounds = poly.getBounds();
+ bounds.setSize(bounds.width + 1, bounds.height + 1);
panel.repaint(poly.getBounds());
}
@@ -359,9 +406,93 @@ public class DrawingWindow {
* Retourne la couleur du pixel de coordonnées (x, y).
*
* @return couleur du pixel
+ *
+ * @see #setColor(int)
+ * @see #setBgColor(int)
*/
public int getPointColor(int x, int y) {
- return image.getRGB(x, y);
+ return (x < 0 || y < 0 || x >= width || y >= height) ?
+ 0 : image.getRGB(x, y) & 0x00ffffff;
+ }
+
+ /**
+ * Attend l'appui sur un des boutons de la souris.
+ *
+ * @return vrai (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 vrai (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;
}
/**
@@ -391,8 +522,7 @@ public class DrawingWindow {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
WindowEvent ev =
- new WindowEvent(frame,
- WindowEvent.WINDOW_CLOSING);
+ new WindowEvent(frame, WindowEvent.WINDOW_CLOSING);
Toolkit.getDefaultToolkit()
.getSystemEventQueue().postEvent(ev);
}
@@ -404,8 +534,11 @@ public class DrawingWindow {
* Suspend l'exécution pendant un certain temps.
*
* @param secs temps d'attente en seconde
+ *
+ * @see #msleep
+ * @see #usleep
*/
- static void sleep(long secs) {
+ public static void sleep(long secs) {
try {
Thread.sleep(secs * 1000);
}
@@ -417,8 +550,11 @@ public class DrawingWindow {
* Suspend l'exécution pendant un certain temps.
*
* @param msecs temps d'attente en millisecondes
+ *
+ * @see #sleep
+ * @see #usleep
*/
- static void msleep(long msecs) {
+ public static void msleep(long msecs) {
try {
Thread.sleep(msecs);
}
@@ -430,8 +566,11 @@ public class DrawingWindow {
* Suspend l'exécution pendant un certain temps.
*
* @param usecs temps d'attente en microsecondes
+ *
+ * @see #sleep
+ * @see #msleep
*/
- static void usleep(long usecs) {
+ public static void usleep(long usecs) {
try {
Thread.sleep(usecs / 1000, (int)(usecs % 1000) * 1000);
}
@@ -441,35 +580,234 @@ public class DrawingWindow {
/* PRIVATE STUFF FOLLOWS */
+ private static final Map