Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (475)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (529)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  mouseMoved is never called  (Read 752 times)
0 Members and 1 Guest are viewing this topic.
Offline TheDirectShift

Senior Newbie





« Posted 2014-02-02 00:30:27 »

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

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
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);
    }


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
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();
    }


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
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("f**k");
    }


Got any tips? thanks!
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 741
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #1 - Posted 2014-02-02 00:31:13 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
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();
    }

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline TheDirectShift

Senior Newbie





« Reply #2 - Posted 2014-02-02 00:34:36 »

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?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Agro
« Reply #3 - Posted 2014-02-02 00:35:49 »

add this in your jpanel 

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

that should work

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 741
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #4 - Posted 2014-02-02 00:39:27 »

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

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline TheDirectShift

Senior Newbie





« Reply #5 - Posted 2014-02-02 00:54:09 »

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.
Offline SwordsMiner

Senior Member


Medals: 2
Projects: 2
Exp: 1 year


Head of the Indie Developing team Revereor.


« Reply #6 - Posted 2014-02-02 00:55:10 »

eww. Jframe.

- The one and only, SwordsMiner.
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 741
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #7 - Posted 2014-02-02 01:05:00 »

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.

So, you're saying that
System.out.println("f**k");
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.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Dxu1994
« Reply #8 - Posted 2014-02-02 01:06:23 »

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.

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 741
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #9 - Posted 2014-02-02 01:08:14 »

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.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Dxu1994
« Reply #10 - Posted 2014-02-02 01:10:34 »

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

Offline TheDirectShift

Senior Newbie





« Reply #11 - Posted 2014-02-02 01:18:27 »

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:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
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.
Offline Dxu1994
« Reply #12 - Posted 2014-02-02 01:23:29 »

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

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
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.

Offline TheDirectShift

Senior Newbie





« Reply #13 - Posted 2014-02-02 02:29:31 »

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.
Offline opiop65

JGO Kernel


Medals: 153
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #14 - Posted 2014-02-02 03:36:39 »

eww. Jframe.
I'm starting to think you're just a troll.

Sorry for the off topic comment.

Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 128
Projects: 4
Exp: 3 years



« Reply #15 - Posted 2014-02-02 03:38:37 »

eww. Jframe.
It would be more constructive if you posted why JFrame is "eww"...
Offline Dxu1994
« Reply #16 - Posted 2014-02-02 04:04:27 »

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.

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

Offline TheDirectShift

Senior Newbie





« Reply #17 - Posted 2014-02-07 03:59:31 »

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!
Pages: [1]
  ignore  |  Print  
 
 

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

ctomni231 (31 views)
2014-07-18 06:55:21

Zero Volt (27 views)
2014-07-17 23:47:54

danieldean (23 views)
2014-07-17 23:41:23

MustardPeter (24 views)
2014-07-16 23:30:00

Cero (39 views)
2014-07-16 00:42:17

Riven (41 views)
2014-07-14 18:02:53

OpenGLShaders (28 views)
2014-07-14 16:23:47

Riven (28 views)
2014-07-14 11:51:35

quew8 (25 views)
2014-07-13 13:57:52

SHC (61 views)
2014-07-12 17:50:04
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!