# How do you import a class

**URL:** <https://discourse.processing.org/t/how-do-you-import-a-class/40971>\
**Category:** Beginners\
**Tags:** homework\
**Created:** [February 21, 2023, 3:05am UTC](https://discourse.processing.org/t/how-do-you-import-a-class/40971 "2023-02-21T03:05:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![shoux2](https://avatars.discourse-cdn.com/v4/letter/s/9fc29f/32.png) [@shoux2](https://discourse.processing.org/u/shoux2)\
**Post date:** [February 21, 2023, 3:05am UTC](https://discourse.processing.org/t/how-do-you-import-a-class/40971/1 "2023-02-21T03:05:49Z")

</div>

Main

```auto
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

```auto
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?

---

<div class="post-metadata">

**Author:** ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)\
**Post date:** [February 21, 2023, 5:06am UTC](https://discourse.processing.org/t/how-do-you-import-a-class/40971/2 "2023-02-21T05:06:52Z")

</div>

Try putting the class under a tab.

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [February 21, 2023, 6:32am UTC](https://discourse.processing.org/t/how-do-you-import-a-class/40971/3 "2023-02-21T06:32:48Z")

</div>

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

```auto
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

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

    case 1: 
        ... game 
        break; 

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

```

Chrisir

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [February 21, 2023, 10:53am UTC](https://discourse.processing.org/t/how-do-you-import-a-class/40971/4 "2023-02-21T10:53:15Z")

</div>

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