Java keyPressed() method only works after player presses tab - java

I am making a little game applet, and I have gotten to where the player should move around with the controls.
Main:
public class Main extends JApplet implements Runnable, KeyListener {
private static final long serialVersionUID = 1L;
private static DoubleBufferedCanvas canvas = new DoubleBufferedCanvas();
public static int width = 900;
public static int height = 600;
public static int fps = 60;
public static Ailoid ailoid = new Ailoid();
public static Player player = new Player();
// Initialize
public void init() {
setSize(width, height);
setBackground(Color.white);
add(canvas);
addKeyListener(this);
setFocusable(true);
setFocusTraversalKeysEnabled(false);
ailoid.setLocation(new Location(100, 100));
AlienManager.registerAlien(ailoid);
player.setLocation(new Location(400, 400));
}
// Paint graphics
public void paint(Graphics g) {
super.paint(g);
}
// Thread start
#Override
public void start() {
Thread thread = new Thread(this);
thread.start();
}
// Thread stop
#Override
public void destroy() {
}
// Thread run
#Override
public void run() {
Thread thread = new Thread(this);
while (thread != null) {
Updater.run();
canvas.repaint();
try {
// 1000 divided by fps to get frames per second
Thread.sleep(1000 / fps);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}
#Override
public void keyPressed(KeyEvent evt) {
KeyPress.run(evt);
System.out.println("DEBUG");
}
#Override
public void keyReleased(KeyEvent evt) {
}
#Override
public void keyTyped(KeyEvent evt) {
}
}
What happens is that it only starts saying "debug" after I press tab. How would I make it work even if the player doesn't press tab before?

Your Applet most likely does not have the input focus.Try adding
this.requestFocus ()
to your init method.
Also note that a JApplet is a part of the Swing framework and thus not threadsafe as mentioned in the documentation

public static void main(String[] args) {
JFrame frame = new JFrame(GAME_TITLE);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
sGame = new Game(GAME_WIDTH, GAME_HEIGHT);
frame.add(sGame);
frame.pack();
frame.setVisible(true);
frame.setIconImage(Resources.iconimage);
}
I had the same problem, it was caused in main by not calling everything in the correct order.
Originally I had setVisible(true) before I had passed sGame to .add and called frame.pack().
This code is what worked for me.

Related

Trouble calling a JAVA file from a JAVA File

I'm fairly new to java and am having trouble calling a mini tennis game I coded, following a tutorial, into another java file. Essentially a button in the Main File will call my game and display it in another JFrame. I haven't been able to get my game frame to open and I know it has something to do with the layout of my code for the game but I have tried rearranging and I haven't been able to call it because the main function throws an interrupted exception because there is a thread in there that keeps the game looping. What would I put in my main program to call it and how can I modify my mini tennis game to be able to be called? Basically, how can I modify the code below to be able to be called using a Game2 game = new Game2(); in another program? Or is there another method?
#SuppressWarnings("serial")
public class Game2 extends JPanel {
Ball ball = new Ball(this);
Racquet racquet = new Racquet(this);
int speed = 1;
private int getScore() {
return speed - 1;
}
public Game2() {
addKeyListener(new KeyListener() {
#Override
public void keyTyped(KeyEvent e) {
}
#Override
public void keyReleased(KeyEvent e) {
racquet.keyReleased(e);
}
#Override
public void keyPressed(KeyEvent e) {
racquet.keyPressed(e);
}
});
setFocusable(true);
Sound.BACK.loop();
}
private void move() {
ball.move();
racquet.move();
}
#Override
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
ball.paint(g2d);
racquet.paint(g2d);
g2d.setColor(Color.GREEN);
g2d.setFont(new Font("Verdana", Font.BOLD, 30));
g2d.drawString(String.valueOf(getScore()), 10, 30);
}
public void gameOver() {
Sound.BACK.stop();
Sound.GAMEOVER.play();
JOptionPane.showMessageDialog(this,
"You caught it this many times " + getScore(),
"Game Over",
JOptionPane.YES_NO_OPTION);
System.exit(ABORT);
}
public static void main(String[] args) throws InterruptedException {
JFrame frame = new JFrame("Catch the Snowball");
Game2 game = new Game2();
frame.add(game);
frame.setSize(300, 400);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
while (true) {
game.move();
game.repaint();
Thread.sleep(10);
}
}
}
I am not sure if i understand you correctly, but if you want to access Game2 game = new Game2(); from another class you can create static object game.
public class Game2 extends JPanel {
public static Game2 game;
...
Then you need to initialize it.
public static void main(String[] args) throws InterruptedException {
JFrame frame = new JFrame("Catch the Snowball");
game = new Game2();
and then you can access it by
Game2.game.something();

The background of the Jpanel is grey

Im trying to create a game in java.
At the first I've made a Jpanel and made a FPS counter and the background was white.
after that I've tried to made that when you pressing ESCAPE, the Jpanel shutting down and then I've tried to make that when you pressing Space, the Background moving to green but after that, when Im trying to start, the Jpanel moving to grey background and the FPS counter has been disappear.
this is the frame class
public class Frame extends JFrame{
public static void main(String[] args) {
new Frame();
}
public Frame(){
new JFrame();
this.setTitle("Tower Defence - By Sahar Haine");
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setExtendedState(MAXIMIZED_BOTH);
this.setUndecorated(true);
this.setResizable(false);
this.setVisible(true);
Screen screen = new Screen(this);
this.add(screen);
}
this is the screen class:
public class Screen extends JPanel implements Runnable{
Thread thread = new Thread(this);
Frame frame;
private int fps = 0;
public int scene;
public Boolean running = false;
public Screen(Frame frame){
this.frame = frame;
this.frame.addKeyListener(new KeyHandller(this));
thread.start();
}
#Override
public void paintComponent(Graphics g){
g.clearRect(0, 0, this.frame.getWidth(), this.frame.getHeight());
g.drawString(fps + "", 10, 10);
if(scene == 0){
g.setColor(Color.BLUE);
}else{
}
g.setColor(Color.GREEN);
g.fillRect(0, 0, this.frame.getWidth(), this.frame.getHeight());
}
public void run() {
System.out.println("Success");
long lastFrame = System.currentTimeMillis();
int frames = 0;
running = true;
scene = 0;
while(running){
try {
repaint();
frames++;
if(System.currentTimeMillis() - 1000>= lastFrame){
fps = frames;
frames = 0;
lastFrame = System.currentTimeMillis();
}
Thread.sleep(1);
} catch (InterruptedException ex) {
Logger.getLogger(Screen.class.getName()).log(Level.SEVERE, null, ex);
}
}
System.exit(0);
}
public class KeyTyped{
public void keyESC(){
running = false;
}
public void keySPACE() {
scene = 1;
}
and this is the key handler class:
public class KeyHandler implements KeyListener{
private Screen screen;
private Screen.KeyTyped keyTyped;
public KeyHandler(Screen screen){
this.screen = screen;
this.keyTyped = this.screen.new KeyTyped();
}
public void keyTyped(KeyEvent e) {
}
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
System.out.println(e.getKeyCode());
if(keyCode == 27){
this.keyTyped.keyESC();
}
if(keyCode == 27){
this.keyTyped.keySPACE();
}
}
public void keyReleased(KeyEvent e) {
}
One issue that jumps out immediately is that you have hard coded "keyCode = 27" for both Escape and Space which is not correct. "27 - Escape" and "32 - Space". Rather than hard code the values, please use KeyEvent.VK_ESCAPE and KeyEvent.VK_SPACE as documented here, KeyEvents
EDIT 1
Also make certain that you draw the string AFTER the "Fill" or else the "Fill" will over paint the FPS string and you will not see it.
I think you have to call
super.paintComponent(g)
as the first line in your paintComponent method override.

Java simple JFrame is null

I have made simple JFrame that works fine except when I am trying to access it from other classes it returns null. So I made getter for it (public static JFrame getWindow()) it returns null. If I set JFrame to public and try to access it that way it returns null. When I create it becomes null right after the game engine starts.
Main:
public class Main {
private static String title = "2D SquareWorld 0.";
private static String version = "";
private static JFrame window;
private static Container container;
public static void main(String[] args) {
GameEngine game = new GameEngine();
window = new JFrame();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setTitle(title + Version.newVersion());
window.setResizable(false);
window.add(game);
window.setSize(1000, 720);
window.setLocationRelativeTo(null);
window.setVisible(true);
game.start();
}
public static Container getContainer() {
return container;
}
public static JFrame getWindow() {
return window;
}
}
GameEngine:
package GameEngine;
public class GameEngine extends Canvas implements Runnable, KeyListener, MouseListener, MouseMotionListener {
private static final long serialVersionUID = 1L;
private Thread thread;
private boolean running;
private GameStateManager gsm;
public GameEngine() {
gsm = new GameStateManager();
}
public void start() {
thread = new Thread(this, "Game");
thread.start();
running = true;
init();
}
public void init() {
Data.setValue("ScreenWidth", getWidth());
Data.setValue("ScreenHeight", getHeight());
addKeyListener(this);
}
public void update(double delta) {
}
public void render() {
BufferStrategy bs = getBufferStrategy();
if(bs == null) {
createBufferStrategy(2);
return;
}
Graphics g = bs.getDrawGraphics();
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.white);
g2.fillRect(0, 0, getWidth(), getHeight());
gsm.draw(g2);
g2.dispose();
bs.show();
}
public void run() {
long lastLoopTime = System.nanoTime();
long lastFpsTime = 0;
final int TARGET_FPS = Fps.TargetFPS;
final long OPTIMAL_TIME = 1000000000 / TARGET_FPS;
int CurrentFps = 0;
while(running) {
long now = System.nanoTime();
long updateLength = now - lastLoopTime;
lastLoopTime = now;
double delta = updateLength / ((double) OPTIMAL_TIME);
lastFpsTime += updateLength;
CurrentFps++;
if (lastFpsTime >= 1000000000) {
Fps.FPS = CurrentFps;
lastFpsTime = 0;
CurrentFps = 0;
}
update(delta);
render();
try {
if(((lastLoopTime-System.nanoTime() + OPTIMAL_TIME) / 1000000) >= 1) {
Thread.sleep((lastLoopTime-System.nanoTime() + OPTIMAL_TIME) / 1000000);
}
}catch(InterruptedException IE) {
}
}
}
public void mouseDragged(MouseEvent me) {
}
public void mouseMoved(MouseEvent me) {
}
public void mouseClicked(MouseEvent me) {
}
public void mouseEntered(MouseEvent me) {
}
public void mouseExited(MouseEvent me) {
}
public void mousePressed(MouseEvent me) {
}
public void mouseReleased(MouseEvent me) {
}
public void keyPressed(KeyEvent ke) {
gsm.keyPressed(ke.getKeyCode());
}
public void keyReleased(KeyEvent ke) {
}
public void keyTyped(KeyEvent ke) {
}
}
I called getWindow from this class:
public class LoginState extends GameState {
private GameStateManager gsm;
public LoginState(GameStateManager gsm) {
this.gsm = gsm;
}
private JTextField username;
private JTextField password;
public void init() {
username = new JTextField(25);
username.setVisible(true);
username.setBounds(20, 20, 50, 50);
Main.getWindow().add(username);
}
public void draw(Graphics2D g) {
g.setColor(Color.gray);
//g.fillRect(0, 0, Data.getIntegerValue("ScreenWidth"), Data.getIntegerValue("ScreenHeight"));
}
GameStateManager:
public class GameStateManager {
private ArrayList<GameState> gameStates;
public static final int LOGINSTATE = 0;
public static final int MENUSTATE = 1;
public static final int PLAYSTATE = 2;
private static int currentState;
public GameStateManager() {
gameStates = new ArrayList<GameState>();
currentState = LOGINSTATE;
gameStates.add(new LoginState(this));
gameStates.add(new MenuState(this));
gameStates.add(new PlayState(this));
gameStates.get(currentState).init();
}
Please help.
Thanks for the update, but... you still haven't shown where LoginWindow is being initialized.
A guess -- you're program is starting from a different main method from the one you're showing, and so the main method which creates and assigns your JFrame to the static window field is never called. I suggest that you avoid using static methods and fields in this way, that depend on a main method to initialize. Java is an OOP language -- so make your code OOP compliant and create needed objects within code guaranteed to be called, and then assign them to non-static fields.
edit:
simply swap the two lines:
GameEngine game = new GameEngine();
window = new JFrame();
to
window = new JFrame();
GameEngine game = new GameEngine();
Your JFrame gets initialized in the main method of Main. You are better off using a static initialization block instead.
static {
window = new JFrame();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setTitle(title + Version.newVersion());
window.setResizable(false);
window.add(game);
window.setSize(1000, 720);
window.setLocationRelativeTo(null);
window.setVisible(true);
}

Java JApplet doesn't remove an old rectangle once it is moved

I am new to graphics and japplets, and I made a rectangle that goes across the screen. But for some reason, it just draws a line across, not removing the old instance of the rectangle once it moves.
Main:
public class Main extends JApplet implements Runnable {
private static final long serialVersionUID = 1L;
private static int width = 900;
private static int height = 600;
public static int fps = 60;
public Thread thread = new Thread(this);
public static Ailoid ailoid = new Ailoid();
public void init() {
setSize(width, height);
setBackground(Color.white);
ailoid.setLocation(new Location(100, 100));
AlienManager.registerAlien(ailoid);
}
public void paint(Graphics g) {
g.setColor(Color.green);
for (Alien alien : AlienManager.getAliens()) {
Location loc = alien.getLocation();
int x = loc.getX();
int y = loc.getY();
g.fillRect(x, y, 10, 20);
}
}
// Thread start
#Override
public void start() {
thread.start();
}
// Thread stop
#SuppressWarnings("deprecation")
#Override
public void destroy() {
thread.stop();
}
#Override
public void run() {
while (true) {
Updater.run();
repaint();
try {
// 1000 divided by fps to get frames per millisecond
Thread.sleep(1000 / fps);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
Updater:
public class Updater {
public static void run() {
for (Alien alien : AlienManager.getAliens()) {
Location loc = alien.getLocation();
int x = loc.getX();
int y = loc.getY();
alien.setLocation(new Location(x, y));
}
}
}
Why doesn't it remove the old graphics? thank you!
Your main problem is that your paint(...) method does not call the super method, the method that allows the component to redraw its contents:
public void paint(Graphics g) {
super.paint(g);
//....
Having said that, you're far better off not drawing in a top level window but rather in the paintComponent method of a JPanel that is displayed by the applet. If you do this correction, then do the same thing -- call the super method.
class MyPanel extends JPanel {
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
//....
As an aside, this code:
public class Updater {
public static void run() {
for (Alien alien : AlienManager.getAliens()) {
Location loc = alien.getLocation();
int x = loc.getX();
int y = loc.getY();
alien.setLocation(new Location(x, y));
}
}
}
Doesn't look like it's moving things all that much. In fact per this code, your aliens should stay completely still.
Edit
Also, this is never code, code you should never have in your application:
#SuppressWarnings("deprecation")
#Override
public void destroy() {
thread.stop();
}
There's a reason that thread's stop() method is deprecated and should never be called. If you're curious as to why, please check out the API.

Start timer on keypress, java

I want to, when i click the "d" button, start a timer. This is to animate a player walking. The timer doesn't start when i press the key, how do i do that?
The code I have is this:
public class Game extends JPanel implements KeyListener {
//Player variables
private BufferedImage playerStanding;
private BufferedImage playerWalking;
private BufferedImage playerFrame;
private boolean walking = false;
private final int PLAYER_HEIGHT = 100;
private final int PLAYER_WIDTH = 100;
private final int INITIAL_X = 0;
private final int INITIAL_Y = 500;
private int x = INITIAL_X;
private int y = INITIAL_Y;
//The timer I want to start on keypress-> "d"
private Timer playerAnimationTimer;
public Game() {
setPreferredSize(new Dimension(800, 800));
setBackground(Color.CYAN);
//Player
try {
playerStanding = ImageIO.read(getClass().getResource("player1.gif"));
playerWalking = ImageIO.read(getClass().getResource("player2.gif"));
playerFrame = playerStanding;
}
catch (IOException ex) {
ex.printStackTrace();
}
playerAnimationTimer = new Timer(500, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
walking = !walking;
playerFrame = walking ? playerWalking : playerStanding;
x += 10;
if (x > getWidth() - PLAYER_WIDTH) {
x = INITIAL_X;
}
repaint();
}
});
playerAnimationTimer.setRepeats(true);
}
public Dimension setPreferredSize() {
return new Dimension(800, 800);
}
#Override
public void paintComponent(Graphics graphics) {
super.paintComponent(graphics);
Graphics2D graphics2D = (Graphics2D) graphics;
if (playerFrame != null) {
graphics2D.drawImage(playerFrame, x, y, PLAYER_WIDTH, PLAYER_HEIGHT, this);
}
graphics2D.dispose();
}
#Override
public void keyTyped(KeyEvent e) {
//This doesn't work
playerAnimationTimer.start();
}
#Override
public void keyPressed(KeyEvent e) {
}
#Override
public void keyReleased(KeyEvent e) {
}
}
//The class to hold the gamepanel
public class StartGame extends JFrame implements ActionListener {
private static JButton startGame = new JButton();
StartGame() {
setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
setSize(200, 100);
setVisible(true);
setBackground(Color.BLUE);
setLocationRelativeTo(null);
startGame.setText("Play!");
startGame.setSize(100, 25);
startGame.addActionListener(this);
add(startGame);
}
#Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == startGame) {
this.setVisible(false);
new GameWindow();
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new StartGame();
}
});
}
}
How could I make the timer start when I click the "d" button?
Your KeyListener doesn't work because you never add the KeyListener to anything much less to a component that has focus, which is needed for a KeyListener to work.
I suggest that you instead use Key Bindings as a cleaner safer way to capture the desired key press.
As an aside, never dispose of a Graphics object that is given to you from the JVM.
For a better answer, please edit your code to make it comply with the mcve standard. You should use no images files, and it should compile and run for us unaltered.
You could set the private Timer like you did and start it like this...
public void startTimer(){
timer.start();
timer.setRepeats(true);
}

Categories