How do you import a class

Main

import java.awt.Image;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;

PImage gawd;
PImage god;
PImage lt33;

void setup() {
  size(500, 500);
  background(50);
  scene1();
  gawd = loadImage("gawd.png");
  god = loadImage("god.png");
  image(god, -230, -110);
  lt33 = loadImage("33.jpg");
  welcomeMessage();
}

void draw() {
  fill(0);
  rect(0, 0, width, 30);
  fill(255);
  textAlign(CENTER);
  text("MouseX: "+mouseX+" & "+ "MouseY: "+mouseY, 250, 20);
  if (mousePressed==true) {
    mousePressed();
    //import player here
  }
}


void welcomeMessage() {
  textSize(20);
  textAlign(CENTER);
  text("Welcome... to... my CHALLENGE! My name is gawd, ", 250, 450);
  text("nice to meet you! Now, click ME!", 250, 480);
}



void mousePressed() {
  background(50);
  scene1();
  image(lt33, 0, 0);
  
}

void scene1() {
  background(0);
}

void scene2() {
}

'

Player

public class player {
  private int dx;
  private int dy;
  private int x = 40;
  private int y = 60;
  private int w;
  private int h;
  private Image image;

  public player() {
    loadImage();
  }

  private void loadImage() {
    ImageIcon ii = new ImageIcon("player.png");
    image = ii.getImage();

    w = image.getWidth(null);
    h = image.getHeight(null);
  }

  public void move() {
    x += dx;
    y += dy;
  }

  public int getX() {
    return x;
  }

  public int getY() {
    return y;
  }

  public int getWidth() {
    return w;
  }

  public int getHeight() {
    return h;
  }

  public Image getImage() {
    return image;
  }

  public void keyPressed(KeyEvent e) {
    int key = e.getKeyCode();
    if (key == KeyEvent.VK_LEFT) {
      dx = -2;
    }

    if (key == KeyEvent.VK_RIGHT) {
      dx = 2;
    }

    if (key == KeyEvent.VK_UP) {
      dy = -2;
    }

    if (key == KeyEvent.VK_DOWN) {
      dy = 2;
    }
  }

  public void keyReleased(KeyEvent e) {
    int key = e.getKeyCode();

    if (key == KeyEvent.VK_LEFT) {
      dx = 0;
    }

    if (key == KeyEvent.VK_RIGHT) {
      dx = 0;
    }

    if (key == KeyEvent.VK_UP) {
      dy = 0;
    }

    if (key == KeyEvent.VK_DOWN) {
      dy = 0;
    }
  }
}

How do you import the player class into main.java?

Try putting the class under a tab.

1 Like

No need to import.

Just make a new tab and copy paste the code there.

You can also drag and drop the class (as a pde file) onto the processing Editor IDE.

Remark

There are a few issues in your code.

The Welcome screen

Allow me to comment on the Welcome screen.

For example I recommend
in draw():

if (welcomeFlag){
  call welcome screen 
}else{
  scene1();
  and what you have in draw now 
}

In mousePressed() say welcomeFlag=false;

Before setup() say

boolean welcomeFlag=true;

Explanation

The variable welcomeFlag indicates whether you show the welcome screen or the game.
When you have multiple screens, use a int variable int state = 0; to distinguish the screens in draw(), mousePressed, keyPressed() etc. using

switch(state){
    
    case 0: 
        ...welcome screen
        break; 

    case 1: 
        ... game 
        break; 

    case 2: 
        ...high score / Game over screen
       break; 
    
}

Chrisir

1 Like

And you need to call keyPressed and keyReleased from outside the class into the class to make them work

1 Like