Results 1 to 5 of 5
Thread: The mouse and the cheese
- 08-14-2007, 06:41 AM #1
Member
- Join Date
- Aug 2007
- Posts
- 2
- Rep Power
- 0
- 08-14-2007, 08:37 AM #2
How do you have in mind to make the mouse move? With keyboard input, animation, dragging with the mouse? I would say that you can move the mouse with any of these means.
- 08-15-2007, 02:17 AM #3
Senior Member
- Join Date
- Jul 2007
- Posts
- 130
- Rep Power
- 0
If ur gonna move the mouse (ur application mouse) using the mouse (the computer mouse that is) u can use the MouseListener and MouseMotionListener, though further details are available in the tutorials
But if ur gonna use keyboard, well, i was about to ask the same thing too, how do u capture keyboard inputs actually?:confused:
What listener to use? an example would be great :)
- 08-15-2007, 09:50 AM #4
See How to Use Key Bindings
and How to Use the Focus Subsystem for more information.
Java Code:import java.awt.*; import java.awt.event.*; import java.awt.geom.Ellipse2D; import javax.swing.*; public class MouseKeyMotion extends JPanel { Ellipse2D.Double ball = new Ellipse2D.Double(200,200,40,40); double dx = 3.0; double dy = 3.0; public MouseKeyMotion() { registerKeys(); setFocusable(true); } protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D)g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2.setPaint(Color.red); g2.fill(ball); } private void registerKeys() { int c = JComponent.WHEN_IN_FOCUSED_WINDOW; getInputMap(c).put(KeyStroke.getKeyStroke("UP"), "UP"); getActionMap().put("UP", upAction); getInputMap(c).put(KeyStroke.getKeyStroke("LEFT"), "LEFT"); getActionMap().put("LEFT", leftAction); getInputMap(c).put(KeyStroke.getKeyStroke("DOWN"), "DOWN"); getActionMap().put("DOWN", downAction); getInputMap(c).put(KeyStroke.getKeyStroke("RIGHT"), "RIGHT"); getActionMap().put("RIGHT", rightAction); getInputMap(c).put(KeyStroke.getKeyStroke("ESCAPE"), "ESCAPE"); getActionMap().put("ESCAPE", escapeAction); } private void moveBall(int xInc, int yInc) { double x = ball.x + dx*xInc; double y = ball.y + dy*yInc; ball.setFrameFromDiagonal(x, y, x+ball.width, y+ball.height); repaint(); } public static void main(String[] args) { JFrame f = new JFrame("Use arrow keys"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.getContentPane().add(new MouseKeyMotion()); f.setSize(400,400); f.setLocation(200,200); f.setVisible(true); } private Action upAction = new AbstractAction() { public void actionPerformed(ActionEvent e) { moveBall(0,-1); } }; private Action leftAction = new AbstractAction() { public void actionPerformed(ActionEvent e) { moveBall(-1,0); } }; private Action downAction = new AbstractAction() { public void actionPerformed(ActionEvent e) { moveBall(0,1); } }; private Action rightAction = new AbstractAction() { public void actionPerformed(ActionEvent e) { moveBall(1,0); } }; private Action escapeAction = new AbstractAction() { public void actionPerformed(ActionEvent e) { System.exit(0); } }; }
- 08-15-2007, 10:55 PM #5
Member
- Join Date
- Aug 2007
- Posts
- 2
- Rep Power
- 0
Answer
Yes, it was something like this that i had in mind... Thanks alot fellow... Now i just have to implement the code for the colision detection, cause i want to create a labyrinth, and the rat is suppose to chase the cheese...
Got the idea, well alone this time, i'll try to do it... if i have problems i'll post something...
Thanks
Similar Threads
-
Mouse dragg not working
By Preethi in forum AWT / SwingReplies: 1Last Post: 02-08-2008, 04:51 AM -
How to mouse-drag a JWindow?
By cruxblack in forum New To JavaReplies: 3Last Post: 08-06-2007, 09:52 AM -
mouse over on JButton
By gradon in forum Java AppletsReplies: 1Last Post: 08-04-2007, 05:50 AM -
Use the mouse position
By susan in forum Java AppletsReplies: 1Last Post: 07-28-2007, 11:10 PM -
Mouse over JButton
By sandor in forum AWT / SwingReplies: 1Last Post: 05-17-2007, 09:15 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks