Applet template

Can someone tell me how to change this template to get mouseX, mouseY, left mousebutton (as variables)?

(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 :D)

Well, it kind of depends on what you want exactly, that’s why there are so many methods :slight_smile:

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

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

        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;
   }

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


...
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:


...
case Event.MOUSE_DOWN:
    if (e.getButton() == MouseEvent.BUTTON1) {
        mouseButtonState="down";
    }
    break;
...

and similar for Event.MOUSE_UP

“The method getX() is undefined for the type Event”

Full source:


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;
   }
}

I’m sorry, apparently the x and y values are public. You don’t need to use getters but you can use this:


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.


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.

Works, thanks (maybe by some miracle I will finish it before the deadline :D)