1414import java .awt .event .ActionListener ;
1515import java .awt .event .KeyEvent ;
1616import java .awt .event .KeyListener ;
17- import java .awt .event .MouseEvent ;
1817import java .awt .event .MouseListener ;
1918import java .awt .event .MouseMotionListener ;
2019import java .awt .geom .Arc2D ;
4140import javax .swing .JMenuItem ;
4241import 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 )
0 commit comments