I'm learning Java here and trying to get to grips with classes and how best to catch mouse events etc. for my little game, so please be patient and give my some hints.
The main class of my project extends JFrame
and upon construction adds to itself an object of a class which extends JPanel
. This JPanel
covers the whole window and it is this I am using to paint stuff to the screen. Then I have a class called Scene
which extends nothing but which stores all the references to the current objects on the screen and is responsible for compiling them all and passing them back to the JPanel
. I didn't want to put the full mouse event code in the JPanel because it would be so messy so I though I'd create another class for that called MEs
and let each scene have one. My thinking was, this way each mes
object can access the objects in each scene easily. So my code is looking like this:
class DPanel extends JPanel {
Scene scCurrent;
public DPanel() {
scCurrent = new Scene();
addMouseMotionListener(new MouseAdapter() {
public void mouseMoved(MouseEvent me) { scCurrent.mes.moved(me); }
});
...
}
...
but of course, inside scCurrent.mes.moved()
I don't even know how to change the cursor. It doesn't recognise setCursor()
in there. How can I change the cursor and access objects that are higher up the tree like I'd need to to switch scene? Or is there some other place I can tuck my mouse event handling code that will not bumph-out out my JPanel?