2 Copyright (C) 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package gnu.classpath.examples.CORBA.swing.x5;
41 import java.awt.Color;
42 import java.awt.Dimension;
43 import java.awt.Graphics;
44 import java.awt.Point;
45 import java.awt.event.MouseEvent;
46 import java.awt.event.MouseListener;
47 import java.util.ArrayList;
48 import java.util.Collection;
49 import java.util.Iterator;
51 import javax.swing.JComponent;
52 import javax.swing.JFrame;
53 import javax.swing.JScrollPane;
56 * The purpose of this simple example is to check if the mouse events are
57 * correctly received in a scrollable canvas and also if the canvas are
58 * correctly repainted. The similar canvas are used in various games and
59 * interactive demonstrations.
61 * The user can set one of the three possible figures with the different
62 * mouse buttons. The figure must be set where the user have clicked the
65 * @author Audrius Meskauskas (AudriusA@Bioinformatics.org)
67 public class CanvasWorld
69 implements MouseListener, State
72 * Use serialVersionUID for interoperability.
74 private static final long serialVersionUID = 1;
77 * Red oval, set by the left mouse button.
79 public static final int RED = 0;
82 * Black cross, set by the right mouse button.
84 public static final int BLACK = 1;
87 * Blue and smaller oval, set by the middle mouse button.
89 public static final int HINT = 2;
92 * The message string is displayed at the top of the window.
94 String message = "Click left, right or middle button in to set the figure";
97 * The additinal message, related to the mouse events.
99 String mouse = "No mouse event so far";
107 * The radius of the dots being painted.
109 static int R = W / 3;
112 * The collection of the red dots.
114 ArrayList reds = new ArrayList();
117 * The collection of the black crosses.
119 ArrayList blacks = new ArrayList();
122 * The collection of the smaller blue crosses.
124 ArrayList hints = new ArrayList();
130 addMouseListener(this);
134 throw new AssertionError(e);
139 * Paint this component.
141 public void paintComponent(Graphics g)
146 g.setColor(Color.white);
147 g.fillRect(0, 0, w, h);
151 g.setColor(Color.black);
153 g.drawString(message, W, W);
154 g.drawString(mouse, W, 2*W);
160 * Check for the presence of the given point in the collection.
162 public final boolean pointPresent(int x, int y, Collection in)
164 Iterator iter = in.iterator();
166 while (iter.hasNext())
168 p = (Point) iter.next();
169 if (p.x == x && p.y == y)
175 public void drawGrid(int w, int h, Graphics g)
177 g.setColor(Color.lightGray);
181 // Draw vertical lines:
182 for (int x = 0; x < w; x += W)
184 g.drawLine(x, xs, x, h);
187 // Draw horizontal lines:
188 for (int y = 3*W; y < h; y += W)
190 g.drawLine(0, y, w, y);
193 g.setColor(Color.gray);
196 public void drawFigures(Graphics g)
198 g.setColor(Color.red);
199 drawDots(reds, g, RED);
201 g.setColor(Color.black);
202 drawDots(blacks, g, BLACK);
204 g.setColor(Color.blue);
205 drawDots(hints, g, HINT);
208 public Point makePoint(int x, int y)
210 return new Point(x / W, y / W);
214 * Draw a collection of dots (the collor must be set before calling the
217 public void drawDots(Collection dots, Graphics g, int mode)
219 Iterator iter = dots.iterator();
227 while (iter.hasNext())
229 p = (Point) iter.next();
234 g.drawOval(x - R, y - R, RR, RR);
235 else if (mode == BLACK)
237 g.drawLine(x - R, y - R, x + R, y + R);
238 g.drawLine(x - R, y + R, x + R, y - R);
243 g.drawOval(x - hR, y - hR, R, R);
248 public void mouseClicked(MouseEvent e)
253 Point p = makePoint(x, y);
255 // Ignore clicks on the occupied cells.
256 if (pointPresent(p.x, p.y, reds) || (pointPresent(p.x, p.y, blacks)))
258 message = "Clicked on the occupied cell.";
262 message = "Figure set at ["+p.x+","+p.y+"]";
264 if (e.getButton() == MouseEvent.BUTTON1)
266 else if (e.getButton() == MouseEvent.BUTTON3)
268 else if (e.getButton() == MouseEvent.BUTTON2)
273 public void mouseEntered(MouseEvent m)
275 mouse = "Mouse entered.";
279 public void mousePressed(MouseEvent m)
281 mouse = "Mouse pressed at "+m.getX()+","+m.getY();
285 public void mouseReleased(MouseEvent m)
287 mouse = "Mouse released at "+m.getX()+","+m.getY();
291 public void mouseExited(MouseEvent m)
293 mouse = "Mouse exited";
297 public static void main(String[] args)
299 JFrame frame = new JFrame();
300 CanvasWorld world = new CanvasWorld();
301 world.setPreferredSize(new Dimension(1000,1000));
302 frame.add(new JScrollPane(world));
303 frame.setSize(400, 200);
304 frame.setVisible(true);