Results 1 to 7 of 7
Thread: mouseListeners
- 08-12-2010, 04:35 PM #1
Senior Member
- Join Date
- Aug 2010
- Posts
- 127
- Rep Power
- 0
- 08-12-2010, 04:50 PM #2
Senior Member
- Join Date
- Jul 2010
- Posts
- 125
- Rep Power
- 0
You need to need import:
javax.swing.event.*;
Then, let your class implement MouseListener. Attach a listener to each JComponent:
myJComponent.addMouseListener(this);
Finally, add the unimplemented methods, which are all in the javadocs. Cheers,
Crikey
-
If you're still stuck, you may wish to show us your code, if it isn't too big.
- 08-12-2010, 05:07 PM #4
Senior Member
- Join Date
- Aug 2010
- Posts
- 127
- Rep Power
- 0
Did everything except the JComponent. This is just me being a noob but, how does a JComponent work? This is the code for my constructor:
Eclipse automaticly added the unimplemented methodes, but I don't have a JComponent I can use as far as I know.Java Code:public class editor extends JFrame implements MouseListener { public editor() { setTitle("tower defence"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(608,634); setLocation(0,0); getContentPane().setBackground(Color.white); setVisible(true); } }
-
Your JFrame's contentPane is a JPanel which extends from JComponent (most all Swing components inherit from JComponent either directly or indirectly). I would either get the contentPane via getContentPane() and add the MouseListener to that. But having said that, does your class create JPanels and populate it with components? If so, it's often better to add the mouse listener to one of the JPanels.... hard to say though without knowing more of your program's logic.
- 08-12-2010, 05:23 PM #6
Senior Member
- Join Date
- Aug 2010
- Posts
- 127
- Rep Power
- 0
Well, Im making a tower defence game. The game is going to consist of a play area and some information printed below that. The play area consists of squares (which are instances of the class square) which are drawn as rectangle polygons (I later found out I could have used the drawRect methode) when the paint methode is called. This class editor is going to be the map editor (I make this one first because the "game" class is going to take in a pre-made field of squares). The user has to decide where to put the castle (the destination for the creeps) and square by square where the path to the castle is going to be. They have to do this by clicking on the screen. Using the coordinates I calculate if, and if so which, square they pressed and if that square can be turned into the castle or path and when that is possible, repaint the square.
-
Then your MouseListener(s) will be added to whichever component needs to respond to the mouse, probably the JPanel that is doing the drawing.


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks