I tried to separate the main class (the only class) into input and screen, as well as the remaining main class. Before, I could see a colored window with a rectangle and title but now it’s just blank, even though it doesn’t say I have errors. Here are the 3 classes:
Game.java
package com.izipizi.net;
import java.awt.Color;
import javax.swing.JFrame;
public class Game extends JFrame {
private int x=200;
private int y=150;
public Game(){
addKeyListener(new InputKey());
}
public static void main(String[] args) {
new Screen();
new Game();
}
public void setX(int x){
this.x=x;
}
public int getX(){
return this.x;
}
public void setY(int y){
this.y=y;
}
public int getY(){
return this.y;
}
}
//////////////////////////////////////////////////////////
InputKey.java
package com.izipizi.net;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class InputKey extends KeyAdapter {
int speed=3;
Game game;
public void keyPressed(KeyEvent e){
switch(e.getKeyCode()){
case KeyEvent.VK_UP:
game.setY(game.getY()-speed);
break;
case KeyEvent.VK_DOWN:
game.setY(game.getY()+speed);
break;
case KeyEvent.VK_LEFT:
game.setX(game.getX()-speed);
break;
case KeyEvent.VK_RIGHT:
game.setX(game.getX()+speed);
break;
}
}
}
////////////////////////////////////////////
Screen.java
package com.izipizi.net;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.JFrame;
public class Screen extends JFrame {
int WIDTH=480;
int HEIGHT=360;
private Graphics dbg;
private Image dbi;
Font font=new Font(Font.SERIF, Font.BOLD,30);
Game game;
public Screen(){
setBackground(Color.cyan);
setSize(WIDTH,HEIGHT);
setTitle("Simple Game");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setResizable(false);
setLocationRelativeTo(null);
setVisible(true);
}
public void paint (Graphics g){
dbi=createImage(WIDTH,HEIGHT);
dbg=dbi.getGraphics();
paintComponent(dbg);
g.drawImage(dbi, 0, 0, this);
}
public void paintComponent(Graphics g){
g.setFont(font);
g.setColor(Color.red);
g.drawString("Epic Square of Epicness", 70, 70);
g.fillRect(game.getX(), game.getY(), 18, 18);
repaint();
}
}
/////////////////////////////////////////
The solution is probably something simple but i’m a newbie so please explain me.
By the way, when the screen appears i get this message in console:
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)