mouseMoved is never called

Hi everyone.
The problem is simple: the mouseMoved event is never called.
Let’s see the code.

public class Window extends javax.swing.JFrame {

    public Window() {
        initComponents();
        setVisible(true);
        setResizable(false);
        jPanel1.setVisible(true);
        jPanel1.setBounds(0,0,300,300);
        
        Game g = new Game();
        jPanel1.add(g);
    }
public Game()
    {
        setVisible(true);
        setBounds(0,0,300,300);
        map = new Map();
        drawer = new Drawer(map);
        listener = new Listener(map);
        t = new Thread(this);
        
        this.add(drawer);
        addMouseListener(listener);
        t.start();
    }
public class Listener extends MouseAdapter{

    int mouseX, mouseY;
    Map map;
    public Listener(Map copyMap)
    {
        mouseX = 0;
        mouseY = 0;
        map = copyMap;   
    }
    
    @Override
    public synchronized void mouseMoved(MouseEvent evt)
    {
        GetMouseCords(evt);
        ShouldMoveView();
        System.out.println("fuck");
    }

Got any tips? thanks!

public Game()
    {
        setVisible(true);
        setBounds(0,0,300,300);
        map = new Map();
        drawer = new Drawer(map);
        listener = new Listener(map);
        t = new Thread(this);
        
        this.add(drawer);
        addMouseListener(listener);
+       addMouseMotionListener(listener);
        t.start();
    }

Gosh, that’s a fast reply!!
Already tried that, but didn’t work.

Could be it that the Listener never obtains focus? Will help if I tell you that the game calls repaints functions?

add this in your jpanel

@Override
public void addNotify() {
setFocusable(true);
this.requestFocus();
}

that should work

It might not have worked, but it is mandatory for it to work, once you solved the other issues.

I’m surprised! it didn’t work either!
I also found that if I put breakpoint on listener and I go into debug mode… the code Works good.

Any other ideas?

thanks.

So, you’re saying that [icode]System.out.println(“f**k”);[/icode] is executed, but nothing appears in the console?

  1. Does anything appear in the console? (put some prints in the Game-constructor)
  2. If so, maybe some other part of your code actually closes the System.out.

Sometimes people use a typical method that copies an InputStream into and OutputStream, and closing the streams to finalize the copy. If you were to use such a method on System.out, it will effectively mute it, from then on.

If nothing appears in the console then how do you know it executed?

Im guessing its because youre running an old classfile. Try a clean and build, in NetBeans or a Clean and Rebuild All in Eclipse.

Also remember that Java2D/Swing is not thread safe.

Does the Game-constuctor get executed on the Event Dispatch Thread? If not, that may very well cause all kinds of trouble.

Your usage of the synchronized keyword there is a sure giveaway of concurrency problems - please read into how Java AWT/Swing uses the E.D.T., and how you interact with it. You basically have to ensure that all your GUI manipulating code runs on that very thread, not your own.

To add to what Riven said, make sure you create your entire GUI/window thing in a SwingUtilities.invokeLater or invokeAndWait runnable.

Ok, now it Works. I added Agros’ code wrong before.
Now I’m having another problem (I always have problems with two things: eventListeners and Backbuffers)

I’m implementing backbuffer on a Drawer, like this:

public class Drawer extends Applet {
    
    Image backbuffer;
    Graphics graphics;
    Map map;
    
    boolean firstTime;
    
    public Drawer(Map mapCopy)
    {
        setVisible(true);
        setBounds(0,0,300,300);
        map = mapCopy;
        firstTime = true;
    }

    public void Draw()
    {
        //draw lots of things into graphics        
        repaint();
        
    }
    
    public void update( Graphics g ) 
    {
        g.drawImage( backbuffer, 0, 0, this );
    }

    public void paint( Graphics g ) 
    {  
       if(firstTime)
       {
            backbuffer = createImage(getSize().width,getSize().height);
            graphics = backbuffer.getGraphics();
       }
       update( g );
    }
}

I’ve read tons of examples of how to implement it, but all sites use different methods, and I just tried to use the most convenient.
The problem is simple: when Draw() method is called, graphics returns null, and doesn’t let me draw anything into it.

Thanks.

I dont know what youre trying to accomplish, but you have an Applet in a JPanel in a JPanel in a JFrame…

Not sure if thats nesessary, I wrote a piece of code that renders fine, detects mouse movements and everything, feel free to use it



import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author David
 */
public class Lol {

    static class Window extends JFrame {

        public Window() {
            setVisible(true);
            setResizable(false);
            Game g = new Game();
            add(g);
            
            pack();
            
            g.requestFocusInWindow();
        }
    }

    static class Game extends JPanel implements Runnable {
        int i = 0;

        public Game() {
            setVisible(true);
            setPreferredSize(new Dimension(300, 300));
            Listener listener = new Listener();
            Thread t = new Thread(this);
            addMouseMotionListener(listener);
            addMouseListener(listener);
            t.start();
        }

        public void run() {
            while (true) {
                repaint();
                try {
                    Thread.sleep(10);
                } catch (InterruptedException ex) {
                    Logger.getLogger(Lol.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        
        @Override
        public void paint(Graphics g) {
            super.paint(g);
            g.drawString("Count:"+(i++), 40, 40);
        }
    }

    static class Listener extends MouseAdapter {

        int mouseX, mouseY;

        public Listener() {
            mouseX = 0;
            mouseY = 0;
        }

        @Override
        public void mouseMoved(MouseEvent evt) {
            System.out.println("f**k");
        }
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                new Window().setVisible(true);
            }
        });
    }

}


If you want to use proper active-rendering, you’ll need to use BufferStrategy.

Ok, I’ve taken your advice into having the Game extends from JPanel. But what if I want the paint methods to be in another class? that’s why I created a Drawer. what do you recommend here? Drawer should extend from what??

I’m implementing BufferStrategy, as son as I have it working I’m going to post here with results.

thanks.

I’m starting to think you’re just a troll.

Sorry for the off topic comment.

It would be more constructive if you posted why JFrame is “eww”…

You can just make Drawer extend Object, and have a method like doRender(Graphics2D) and call that from paint.

Ok, so I basically redesigned the hierarchy of classes.
Now is like so:

Game -> Drawer (JFrame) -> Listener

Game is a class, which instantiates a Drawer (JFrame), and this contains the Listener.
Also, I’m implementing BufferStrategy on Drawer.

Now it Works perfectly, using the same help you gave me =).

Thanks!