Skip to content

Commit f3bb7fa

Browse files
committed
refactoring StdDraw w/ @samanthalangit
1 parent 8249d80 commit f3bb7fa

2 files changed

Lines changed: 98 additions & 85 deletions

File tree

src/main/java/org/teachingextensions/logo/utils/MazeUtils/StdDraw.java

Lines changed: 16 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import java.awt.event.ActionListener;
1515
import java.awt.event.KeyEvent;
1616
import java.awt.event.KeyListener;
17-
import java.awt.event.MouseEvent;
1817
import java.awt.event.MouseListener;
1918
import java.awt.event.MouseMotionListener;
2019
import java.awt.geom.Arc2D;
@@ -41,7 +40,7 @@
4140
import javax.swing.JMenuItem;
4241
import javax.swing.KeyStroke;
4342

44-
public final class StdDraw implements ActionListener, MouseListener, MouseMotionListener, KeyListener
43+
public final class StdDraw implements ActionListener, KeyListener
4544
{
4645
private static final int DEFAULT_SIZE = 512;
4746
private static int width = DEFAULT_SIZE;
@@ -55,17 +54,13 @@ public final class StdDraw implements ActionListener, MouseListener, MouseMotion
5554
private static final double DEFAULT_YMIN = 0.0;
5655
private static final double DEFAULT_YMAX = 1.0;
5756
private static double xmin, ymin, xmax, ymax;
58-
private static Object mouseLock = new Object();
59-
private static Object keyLock = new Object();
6057
private static final Font DEFAULT_FONT = new Font("SansSerif", Font.PLAIN, 16);
6158
private static Font font;
6259
private static BufferedImage offscreenImage, onscreenImage;
6360
private static Graphics2D offscreen, onscreen;
6461
private static StdDraw stdDraw = new StdDraw();
6562
private static JFrame frame;
66-
private static boolean mousePressed = false;
67-
private static double mouseX = 0;
68-
private static double mouseY = 0;
63+
private static Object keyLock = new Object();
6964
private static LinkedList<Character> keysTyped = new LinkedList<Character>();
7065
private static TreeSet<Integer> keysDown = new TreeSet<Integer>();
7166
private static long nextDrawInMS = -1;
@@ -128,18 +123,23 @@ private static void init()
128123
offscreen.addRenderingHints(hints);
129124
ImageIcon icon = new ImageIcon(onscreenImage);
130125
JLabel draw = new JLabel(icon);
131-
draw.addMouseListener(stdDraw);
132-
draw.addMouseMotionListener(stdDraw);
133126
frame.setContentPane(draw);
134-
frame.addKeyListener(stdDraw); // JLabel cannot get keyboard focus
127+
frame.addKeyListener(stdDraw);
135128
frame.setResizable(false);
136-
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // closes all windows
129+
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // close all windows
137130
// frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); // closes only current window
138131
frame.setTitle("Amazing Maze!");
139132
frame.setJMenuBar(createMenuBar());
140133
frame.pack();
141134
frame.requestFocusInWindow();
142135
frame.setVisible(true);
136+
// TODO: This is not working, so mouse events won't work yet...
137+
//draw.addMouseListener(stdDraw);
138+
//draw.addMouseMotionListener(stdDraw);
139+
MouseListener l = null;
140+
draw.addMouseListener(l);
141+
MouseMotionListener m = null;
142+
draw.addMouseMotionListener(m);
143143
}
144144
private static JMenuBar createMenuBar()
145145
{
@@ -171,7 +171,7 @@ public static void setXscale(double min, double max)
171171
double size = max - min;
172172
if (size == 0.0)
173173
throw new IllegalArgumentException("the min and max are the same");
174-
synchronized (mouseLock)
174+
synchronized (StdDrawMouseEvents.mouseLock)
175175
{
176176
xmin = min - BORDER * size;
177177
xmax = max + BORDER * size;
@@ -182,7 +182,7 @@ public static void setYscale(double min, double max)
182182
double size = max - min;
183183
if (size == 0.0)
184184
throw new IllegalArgumentException("the min and max are the same");
185-
synchronized (mouseLock)
185+
synchronized (StdDrawMouseEvents.mouseLock)
186186
{
187187
ymin = min - BORDER * size;
188188
ymax = max + BORDER * size;
@@ -193,7 +193,7 @@ public static void setScale(double min, double max)
193193
double size = max - min;
194194
if (size == 0.0)
195195
throw new IllegalArgumentException("the min and max are the same");
196-
synchronized (mouseLock)
196+
synchronized (StdDrawMouseEvents.mouseLock)
197197
{
198198
xmin = min - BORDER * size;
199199
xmax = max + BORDER * size;
@@ -217,11 +217,11 @@ private static double factorY(double h)
217217
{
218218
return h * height / Math.abs(ymax - ymin);
219219
}
220-
private static double userX(double x)
220+
static double userX(double x)
221221
{
222222
return xmin + x * (xmax - xmin) / width;
223223
}
224-
private static double userY(double y)
224+
static double userY(double y)
225225
{
226226
return ymax - y * (ymax - ymin) / height;
227227
}
@@ -949,75 +949,6 @@ public void actionPerformed(ActionEvent e)
949949
StdDraw.save(chooser.getDirectory() + File.separator + chooser.getFile());
950950
}
951951
}
952-
public static boolean isMousePressed()
953-
{
954-
synchronized (mouseLock)
955-
{
956-
return mousePressed;
957-
}
958-
}
959-
public static double mouseX()
960-
{
961-
synchronized (mouseLock)
962-
{
963-
return mouseX;
964-
}
965-
}
966-
public static double mouseY()
967-
{
968-
synchronized (mouseLock)
969-
{
970-
return mouseY;
971-
}
972-
}
973-
@Override
974-
public void mouseClicked(MouseEvent e)
975-
{
976-
}
977-
@Override
978-
public void mouseEntered(MouseEvent e)
979-
{
980-
}
981-
@Override
982-
public void mouseExited(MouseEvent e)
983-
{
984-
}
985-
@Override
986-
public void mousePressed(MouseEvent e)
987-
{
988-
synchronized (mouseLock)
989-
{
990-
mouseX = StdDraw.userX(e.getX());
991-
mouseY = StdDraw.userY(e.getY());
992-
mousePressed = true;
993-
}
994-
}
995-
@Override
996-
public void mouseReleased(MouseEvent e)
997-
{
998-
synchronized (mouseLock)
999-
{
1000-
mousePressed = false;
1001-
}
1002-
}
1003-
@Override
1004-
public void mouseDragged(MouseEvent e)
1005-
{
1006-
synchronized (mouseLock)
1007-
{
1008-
mouseX = StdDraw.userX(e.getX());
1009-
mouseY = StdDraw.userY(e.getY());
1010-
}
1011-
}
1012-
@Override
1013-
public void mouseMoved(MouseEvent e)
1014-
{
1015-
synchronized (mouseLock)
1016-
{
1017-
mouseX = StdDraw.userX(e.getX());
1018-
mouseY = StdDraw.userY(e.getY());
1019-
}
1020-
}
1021952
public static boolean isNextKeyTyped()
1022953
{
1023954
synchronized (keyLock)
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package org.teachingextensions.logo.utils.MazeUtils;
2+
3+
import java.awt.event.MouseEvent;
4+
import java.awt.event.MouseListener;
5+
import java.awt.event.MouseMotionListener;
6+
7+
public class StdDrawMouseEvents implements MouseListener, MouseMotionListener
8+
{
9+
public static Object mouseLock = new Object();
10+
private static boolean mousePressed = false;
11+
private static double mouseX = 0;
12+
private static double mouseY = 0;
13+
public static boolean isMousePressed()
14+
{
15+
synchronized (mouseLock)
16+
{
17+
return mousePressed;
18+
}
19+
}
20+
public static double mouseX()
21+
{
22+
synchronized (mouseLock)
23+
{
24+
return mouseX;
25+
}
26+
}
27+
public static double mouseY()
28+
{
29+
synchronized (mouseLock)
30+
{
31+
return mouseY;
32+
}
33+
}
34+
@Override
35+
public void mouseClicked(MouseEvent e)
36+
{
37+
}
38+
@Override
39+
public void mouseEntered(MouseEvent e)
40+
{
41+
}
42+
@Override
43+
public void mouseExited(MouseEvent e)
44+
{
45+
}
46+
@Override
47+
public void mousePressed(MouseEvent e)
48+
{
49+
synchronized (mouseLock)
50+
{
51+
mouseX = StdDraw.userX(e.getX());
52+
mouseY = StdDraw.userY(e.getY());
53+
mousePressed = true;
54+
}
55+
}
56+
@Override
57+
public void mouseReleased(MouseEvent e)
58+
{
59+
synchronized (mouseLock)
60+
{
61+
mousePressed = false;
62+
}
63+
}
64+
@Override
65+
public void mouseDragged(MouseEvent e)
66+
{
67+
synchronized (mouseLock)
68+
{
69+
mouseX = StdDraw.userX(e.getX());
70+
mouseY = StdDraw.userY(e.getY());
71+
}
72+
}
73+
@Override
74+
public void mouseMoved(MouseEvent e)
75+
{
76+
synchronized (mouseLock)
77+
{
78+
mouseX = StdDraw.userX(e.getX());
79+
mouseY = StdDraw.userY(e.getY());
80+
}
81+
}
82+
}

0 commit comments

Comments
 (0)