Java-Gaming.org Hi !
Featured games (81)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (575)
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  
  Applet template  (Read 2290 times)
0 Members and 1 Guest are viewing this topic.
Offline Archibald

Junior Duke


Projects: 1



« Posted 2012-02-20 15:24:49 »

Can someone tell me how to change this template to get mouseX, mouseY, left mousebutton (as variables)?
http://www.java-gaming.org/topics/applet-templates/21626/msg/181270/view.html#msg181270

(I started learning Java like 4 days ago and I got a headache when I saw how many possible methods to obtain mouse position there are Cheesy)
Offline Aeroren

Senior Newbie


Medals: 1



« Reply #1 - Posted 2012-02-20 15:58:40 »

Well, it kind of depends on what you want exactly, that's why there are so many methods Smiley

Anyway, you should probably check out these two tutorials:
http://docs.oracle.com/javase/tutorial/uiswing/events/mouselistener.html
http://docs.oracle.com/javase/tutorial/uiswing/events/mousemotionlistener.html
Offline Archibald

Junior Duke


Projects: 1



« Reply #2 - Posted 2012-02-20 16:13:16 »

I need the simpliest one, just filling the ??? parts.

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  
        public boolean handleEvent(Event e) {
            switch (e.id) {
                  case Event.KEY_PRESS:
                  case Event.KEY_ACTION:
                      // key pressed
                      break;
                  case Event.KEY_RELEASE:
                      // key released
                      break;
                  case Event.MOUSE_DOWN:
                      // mouse button pressed
mouseButtonState=???;
                      break;
                  case Event.MOUSE_UP:
                      // mouse button released
mouseButtonState=???;
                      break;
                  case Event.MOUSE_MOVE:
 mouseX=???; mouseY=???;
                      break;
                  case Event.MOUSE_DRAG:
                      break;
             }
             return false;
   }
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Aeroren

Senior Newbie


Medals: 1



« Reply #3 - Posted 2012-02-20 16:30:14 »

The event that is passed to the handler has all the information, so in case of the movement:

1  
2  
3  
4  
5  
6  
...
case Event.MOUSE_MOVE:
    mouseX=e.getX();
    mouseY=e.getY();
    break;
...


And in the case of the mouse_down and mouse_up, e.getButton() returns an int corresponding to the mousebutton the event applies to, so:

1  
2  
3  
4  
5  
6  
7  
...
case Event.MOUSE_DOWN:
    if (e.getButton() == MouseEvent.BUTTON1) {
        mouseButtonState="down";
    }
    break;
...


and similar for Event.MOUSE_UP
Offline Archibald

Junior Duke


Projects: 1



« Reply #4 - Posted 2012-02-20 17:05:30 »

"The method getX() is undefined for the type Event"

Full source:
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  
import java.applet.Applet;
import java.awt.Component;
import java.awt.Event;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;

public class G extends Applet implements Runnable {

   public void start() {
      new Thread(this).start();
   }

   int mouseX=0;
   int mouseY=0;
   
   public void run() {
      setSize(800, 600); // For AppletViewer, remove later.


      // Set up the graphics stuff, double-buffering.
      BufferedImage screen = new BufferedImage(800, 600, BufferedImage.TYPE_INT_RGB);
      Graphics g = screen.getGraphics();
      Graphics appletGraphics = getGraphics();

      // Some variables to use for the fps.
      int tick = 0, fps = 0, acc = 0;
      long lastTime = System.nanoTime();

      // Game loop.
      while (true) {
         long now = System.nanoTime();
         acc += now - lastTime;
         tick++;
         if (acc >= 1000000000L) {
            acc -= 1000000000L;
            fps = tick;
            tick = 0;
         }

         // Update
         // TODO add some update logic here.

         lastTime = now;
     
         // Render
         g.setColor(Color.black);
         g.fillRect(0, 0, 800, 600);
         g.setColor(Color.white);
         g.drawString("FPS " + String.valueOf(fps), 20, 30);
         g.drawString("Mouse ("+mouseX+','+mouseY+')', 20,60);
         
         // Draw the entire results on the screen.
         appletGraphics.drawImage(screen, 0, 0, null);

         try {
            Thread.sleep(10);
         } catch (Exception e) { /* best practice */ };

         if (!isActive()) {
            return;
         }
      }
   }

        public boolean handleEvent(Event e) {
            switch (e.id) {
                  case Event.KEY_PRESS:
                  case Event.KEY_ACTION:
                      // key pressed
                      break;
                  case Event.KEY_RELEASE:
                      // key released
                      break;
                  case Event.MOUSE_DOWN:
                      // mouse button pressed
                      break;
                  case Event.MOUSE_UP:
                      // mouse button released
                      break;
                  case Event.MOUSE_MOVE:
                      mouseX=e.getX();
                      mouseY=e.getY();
                      break;
                  case Event.MOUSE_DRAG:
                      break;
             }
             return false;
   }
}
Offline Aeroren

Senior Newbie


Medals: 1



« Reply #5 - Posted 2012-02-20 18:55:43 »

I'm sorry, apparently the x and y values are public. You don't need to use getters but you can use this:
1  
mouseX=e.x;

this is all deprecated code by the way. In case of the 4k competition you can still use this (because it requires less code this way) but in other applications I recommend using mouseListener and mouseMotionListener.

EDIT:
The code I gave for the button doesn't work with the old events either. In order to differentiate between mouse buttons you need to check e.modifiers.
1  
2  
3  
4  
case Event.MOUSE_DOWN:
    if (e.modifiers == 0) //left button
    if (e.modifiers == Event.ALT_MASK // middle button
    if (e.modifiers == Event.META_MASK // right button

Unfortunately, this method counts pressing the left button while holding the alt key as pressing the middle mouse button. If that's a problem, you'll need to use the newer listeners.
Offline Archibald

Junior Duke


Projects: 1



« Reply #6 - Posted 2012-02-20 23:07:44 »

Works, thanks (maybe by some miracle I will finish it before the deadline Cheesy)
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.

Longarmx (35 views)
2014-10-17 03:59:02

Norakomi (26 views)
2014-10-16 15:22:06

Norakomi (24 views)
2014-10-16 15:20:20

lcass (26 views)
2014-10-15 16:18:58

TehJavaDev (50 views)
2014-10-14 00:39:48

TehJavaDev (52 views)
2014-10-14 00:35:47

TehJavaDev (40 views)
2014-10-14 00:32:37

BurntPizza (63 views)
2014-10-11 23:24:42

BurntPizza (36 views)
2014-10-11 23:10:45

BurntPizza (76 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

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
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!