keyPressed and keyReleased with java(eclipse)

Oopsie! Just now I’ve noticed the end of your thread title better: “Java (Eclipse)”! :woozy_face:

When using Eclipse and other similar IDEs, we need to use real Java syntax in files w/ “.java” extension. :coffee:

Processing’s IDE (PDE) uses “.pde” files. Although it recognizes “.java” files too. :innocent:

If you wanna, I can convert the class Player to actual Java syntax inside file “Player.java”. :nerd_face:

But b/c I don’t have Eclipse, I won’t be able to test if it’s actually compilable outside PDE. :see_no_evil:


I’ve just converted class Player to proper Java syntax in its own “Player.java” file: :coffee:

Player.java:

package sketches.classes;

import processing.core.PApplet;
import static processing.core.PApplet.constrain;
import static processing.core.PApplet.parseInt;
import static processing.core.PConstants.*;

public class Player {
  public static final int INK = 0xff008000, OUTLINE = 0;
  public static final float BOLD = 2f;

  protected final PApplet p;
  protected boolean isLeft, isRight, isUp, isDown;

  public int x, y;
  public final int d, v;

  public Player(PApplet pa, int xx, int yy, int dd, int vv) {
    p = pa;
    x = xx;
    y = yy;
    d = dd;
    v = vv;
  }

  public void display() {
    p.circle(x, y, d);
  }

  public void move() {
    final int r = d >> 1;
    x = constrain(x + v*(parseInt(isRight) - parseInt(isLeft)), r, p.width - r);
    y = constrain(y + v*(parseInt(isDown) - parseInt(isUp)), r, p.height - r);
  }

  public boolean setMove(final int k, final boolean b) {
    switch (k) {
    case 'W':
    case UP:
      return isUp = b;

    case 'S':
    case DOWN:
      return isDown = b;

    case 'A':
    case LEFT:
      return isLeft = b;

    case 'D':
    case RIGHT:
      return isRight = b;

    default:
      return b;
    }
  }
}

What’s still left is the main “Player_Move.pde” file: :neutral_face:

Player_Move.pde:

/**
 * Player Move (v1.3.1)
 * by GoToLoop (2014/Dec/07)
 *
 * Discourse.Processing.org/t/
 * keypressed-and-keyreleased-with-java-eclipse/12994/5
 *
 * Forum.Processing.org/two/discussion/8511/movement-code-messed-up#Item_6
 * Studio.ProcessingTogether.com/sp/pad/export/ro.91tcpPtI9LrXp
 */

//package sketches.main;

import sketches.classes.Player;

static final int DIAM = 48, SPD = 4, FPS = 60;
static final color BG = 0350;

Player p;

void setup() {
  size(800, 600);

  smooth(3);
  frameRate(FPS);
  ellipseMode(CENTER);

  fill(Player.INK);
  stroke(Player.OUTLINE);
  strokeWeight(Player.BOLD);

  //p = new Player(width >> 1, height >> 1, DIAM, SPD);
  p = new Player(this, width >> 1, height >> 1, DIAM, SPD);
}

void draw() {
  background(BG);
  p.move();
  p.display();
}

void keyPressed() {
  p.setMove(keyCode, true);
}

void keyReleased() {
  p.setMove(keyCode, false);
}

A very quick way to convert it to Java syntax is by using PDE’s “Export Application” option (hit CTRL+SHIFT+E). :bulb:

The “Player_Move.pde” will be output as “Player_Move.java”. :wink:

I believe you can move it to Eclipse and hopefully very little or even no changes will be needed. :new_moon:

2 Likes