Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (494)
Games in Android Showcase (113)
games submitted by our members
Games in WIP (562)
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  
  getX() inaccurate for mouselistener?  (Read 492 times)
0 Members and 1 Guest are viewing this topic.
Offline catsaremyreligion

Senior Newbie





« Posted 2014-01-07 17:52:59 »

So I'm using a pretty standard JPanel design for my game. I'm trying to test out using mouselisteners, but the getX() and getY() methods both seem to return inaccurate coordinates. When I draw a test string out on the screen at say, (100,100), and then click the on the area around the string, the getX and getY is returning values almost 100 pixels larger than what it should be. Does anyone know what I'm doing wrong exactly? I'll post some code.


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  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.event.*;

import javax.swing.JPanel;

import managers.Keys;
import managers.Mouse;

public class Game extends JPanel
   implements Runnable, KeyListener, MouseListener{
   
   // dimensions
  public static final int WIDTH = 480;
   public static final int HEIGHT = 320;
   public static final int SCALE = 2;
   
   // game thread
  private Thread thread;
   private boolean running;
   private int FPS = 60;
   private long targetTime = 1000 / FPS;
   
   // image
  private BufferedImage image;
   private Graphics2D g;
   
   
   public Game() {
      super();
      setPreferredSize(
         new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
      setFocusable(true);
      requestFocus();
   }
   
   public void addNotify() {
      super.addNotify();
      if(thread == null) {
         thread = new Thread(this);
         addKeyListener(this);
         addMouseListener(this);
         thread.start();
      }
   }
   
   private void init() {
     
      image = new BufferedImage(
               WIDTH, HEIGHT,
               BufferedImage.TYPE_INT_RGB
            );
      g = (Graphics2D) image.getGraphics();
     
      running = true;
     
   }
   
   public void run() {
     
      init();
     
      long start;
      long elapsed;
      long wait;
     
      // game loop
     while(running) {
         
         start = System.nanoTime();
         
         update();
         draw();
         drawToScreen();
         
         elapsed = System.nanoTime() - start;
         
         wait = targetTime - elapsed / 1000000;
         if(wait < 0) wait = 5;
         
         try {
            Thread.sleep(wait);
         }
         catch(Exception e) {
            e.printStackTrace();
         }
         
      }
     
   }
   
   private void update() {
      Keys.update();
   }
   private void draw() {
      gsm.draw(g);
      g.drawString("point",100,100);
   }
   private void drawToScreen() {
      Graphics g2 = getGraphics();
      g2.drawImage(image, 0, 0,
            WIDTH * SCALE, HEIGHT * SCALE,
            null);
      g2.dispose();
   }
   
   public void keyTyped(KeyEvent key) {}
   public void keyPressed(KeyEvent key) {
      if(key.isControlDown()) {

      }
      Keys.keySet(key.getKeyCode(), true);
   }
   public void keyReleased(KeyEvent key) {
      Keys.keySet(key.getKeyCode(), false);
   }

   public void mouseClicked(MouseEvent e) {
      //System.out.print(e.getX());
       
   }


   public void mouseEntered(MouseEvent arg0) {
     
   }

   public void mouseExited(MouseEvent arg0) {
   }

   public void mousePressed(MouseEvent e) {
      System.out.println(e.getX());
   }

   public void mouseReleased(MouseEvent arg0) {
     
   }
}
 
Offline CodeHead

JGO Knight


Medals: 41


From rags to riches...to rags.


« Reply #1 - Posted 2014-01-07 19:10:56 »

So I'm using a pretty standard JPanel design for my game. I'm trying to test out using mouselisteners, but the getX() and getY() methods both seem to return inaccurate coordinates. When I draw a test string out on the screen at say, (100,100), and then click the on the area around the string, the getX and getY is returning values almost 100 pixels larger than what it should be. Does anyone know what I'm doing wrong exactly? I'll post some code.

Are you sure it's not due to your scaling? Have you tried applying the scaling factor to the mouse coordinates obtained by the mouseClicked() event or conversely have you tried removing the scaling in the drawToScreen() method as a test to see if the co-ordinates line up?

Arthur: Are all men from the future loud-mouthed braggarts?
Ash: Nope. Just me baby...Just me.
Offline catsaremyreligion

Senior Newbie





« Reply #2 - Posted 2014-01-07 20:45:51 »

Thanks a lot. I had a feeling it was something small and stupid that I overlooked. Got it all fixed up though.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline lcass
« Reply #3 - Posted 2014-01-11 23:56:10 »

It can change based on the JFrame position so check to see if it does , if so you can use two Points objects to set your mouse coords
Offline opiop65

JGO Kernel


Medals: 154
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #4 - Posted 2014-01-11 23:57:33 »

No, it doesn't change. The coordinates will alwasys be window based.

Offline lcass
« Reply #5 - Posted 2014-01-12 12:22:17 »

Its changed for me before..
Offline opiop65

JGO Kernel


Medals: 154
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #6 - Posted 2014-01-12 14:55:51 »

If you mean when you move the actual frame, the coordinates change, then no, they don't. getX() and getY() return coordinates relative to the window, not to the window's position. I have no idea what you're talking about.

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 793
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #7 - Posted 2014-01-12 15:54:43 »

For everybody assuming that basic functionality that tens of thousands of developers are using daily, is broken... think again. It's basically guaranteed to be a bug in your code.

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

Junior Member


Exp: 2 years


I like jokes


« Reply #8 - Posted 2014-01-12 16:01:26 »

For everybody assuming that basic functionality that tens of thousands of developers are using daily, is broken... think again. It's basically guaranteed to be a bug in your code.

Id assume more like 10s of millions.

Humor will keep you alive.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

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

The first screenshot will be displayed as a thumbnail.

Dwinin (15 views)
2014-09-12 09:08:26

Norakomi (45 views)
2014-09-10 13:57:51

TehJavaDev (57 views)
2014-09-10 06:39:09

Tekkerue (26 views)
2014-09-09 02:24:56

mitcheeb (49 views)
2014-09-08 06:06:29

BurntPizza (33 views)
2014-09-07 01:13:42

Longarmx (19 views)
2014-09-07 01:12:14

Longarmx (21 views)
2014-09-07 01:11:22

Longarmx (20 views)
2014-09-07 01:10:19

mitcheeb (30 views)
2014-09-04 23:08:59
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!