Results 1 to 1 of 1
Thread: MouseListener issues
- 01-18-2011, 09:16 AM #1
Member
- Join Date
- Jan 2011
- Posts
- 16
- Rep Power
- 0
MouseListener issues
In the following code I implemented three ways of handling mouse events:
-Game Applet is MouseListener by itself
-Control is MouseListener Thread added to Game
-Control sets variable mouseClicked to true and Game Thread checks whether it's true 30 times per second.
None of these seems to work. Your help would be appreciated.
Java Code:import java.applet.Applet; import javax.swing.AbstractAction; import java.awt.event.ActionEvent; import java.awt.Image; import java.awt.event.MouseListener; import java.awt.event.MouseEvent; import java.awt.Graphics; import java.awt.Color; /** * * @author Warmonger */ public class Game extends Applet implements Runnable, MouseListener { StopGame pause; SwarmShooterView parentPanel; private Thread ticker; private Control control; private boolean running = false; MyCanvas canvas; Game(SwarmShooterView pp) { parentPanel = pp; pause = new StopGame("Pause"); resize(SwarmShooterView.guihandler.dimension); setBackground(SwarmShooterView.guihandler.backgroundColor); canvas = new MyCanvas(); add(canvas); } @Override public synchronized void start () { if (ticker == null || !ticker.isAlive()) running = true; ticker = new Thread(this); control = new Control(this); ticker.setPriority(Thread.MIN_PRIORITY + 1); control.setPriority(Thread.MAX_PRIORITY); ticker.start(); control.start(); addMouseListener(control); addKeyListener(control); } public void run () { int i = 100; while (running && i > 0) { repaint(); try { if (control.mouseClicked) { setBackground(Color.BLACK); repaint(); } ticker.sleep(1000 / 30); } catch (InterruptedException e) {} i--; } pause.actionPerformed(null); } public synchronized void stop () { running = false; control = null; } public void init() { Image offscreenimage = createImage(getMaximumSize().width, getMaximumSize().height); } public synchronized void paint(Graphics g) { g.setColor(Color.red); g.fillRect(getX(), getY(), getWidth(), getHeight()); } public void mousePressed (MouseEvent evt){} public void mouseEntered(MouseEvent evt) {} public void mouseExited(MouseEvent evt) {} public void mouseClicked(MouseEvent evt) { pause.actionPerformed(null); setBackground(Color.BLACK); System.out.println("MouseClicked"); } public void mouseReleased(MouseEvent evt) {} class StopGame extends AbstractAction { public StopGame(String text) { super(text); } public void actionPerformed(ActionEvent e) { parentPanel.swapView(parentPanel.gamePanel, parentPanel.mainPanel); stop(); } } }Before I decide final architecture and threading of project, I need to see that something works at least.Java Code:import java.awt.event.MouseListener; import java.awt.event.MouseEvent; import java.awt.event.KeyListener; import java.awt.event.KeyEvent; import java.awt.Color; /** * * @author Warmonger */ public class Control extends Thread implements KeyListener, MouseListener { Game game; boolean mouseClicked = false; public Control(Game target) { super(target); game = target; } public void run() { game.setBackground(Color.blue); } public void mousePressed(MouseEvent evt){} public void mouseReleased(MouseEvent evt){} public void mouseClicked(MouseEvent evt) { System.out.println("MouseClicked"); mouseClicked = true; game.setBackground(Color.red); } public void mouseEntered(MouseEvent evt){} public void mouseExited(MouseEvent evt){} public void keyPressed(KeyEvent e) { System.out.println("Key Pressed!!!"); if(e.getKeyCode() == 27) { // nacisniecie ESC: koniec dzialania programu System.exit(0); } } public void keyReleased(KeyEvent e) { System.out.println("Key Released!!!"); } public void keyTyped(KeyEvent e) { } }
Similar Threads
-
Problem with mouseListener
By js91723 in forum AWT / SwingReplies: 12Last Post: 05-31-2010, 07:49 PM -
can't get x and y from mouselistener
By j2me64 in forum Java 2DReplies: 3Last Post: 04-24-2010, 04:57 PM -
MouseListener & GUI
By Suurisa in forum New To JavaReplies: 2Last Post: 10-27-2009, 12:52 AM -
i need help for MouseListener
By sfaxianovic in forum New To JavaReplies: 2Last Post: 08-21-2008, 03:30 AM -
MouseListener
By Aswq in forum New To JavaReplies: 12Last Post: 07-18-2008, 08:10 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks