Java Game KeyListener Not Working In FSEM

Sep 5, 2014

I had a game written in a 500x400 window using JFrame and a threaded JPanel. It used KeyListener to detect user input and worked just fine, problem was it was very click dependent and therefore I wanted to put it into fullscreen mode so I would no longer come across the problem of clicking outside the window and being taken out of the game. But when I ported the game to Full Screen Exclusive Mode the KeyListener no longer seems to be working and I have no clue why. I tried using KeyBindings as well, that code didn't work and is left commented out in the FSEM code.Here is my code for the windowed application:

import java.awt.Container;
import javax.swing.*;
public class Main extends JFrame{
private GamePanel panel;
public Main(){
panel = new GamePanel();

[code]....

View Replies


ADVERTISEMENT

KeyListener Is Not Working?

May 31, 2014

I'm programming a game, but the keyListener doesn't work. Here is the source code:

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
 public class InputHandler implements KeyListener{
 public InputHandler(Game game) {
game.addKeyListener(this);

[code]....

My Question is: Why will it not say "up", if I press "w"?

View Replies View Related

Simple Snake Game - Timer And KeyListener

May 25, 2014

I'm trying to create a simple snake game, but I can't get my timer and keyListener to work...

//Timer
Timer tm = new Timer(5, this);
int x = 50, y = 250, vel = 2;
// The snake
g.setColor(Color.black);
g.fillRect(x, y, snakeSize, snakeSize);

[Code] .....

View Replies View Related

KeyListener And GObject To Create BreakOut Game

May 14, 2012

I am trying to create the Breakout game and cannot figure how to get the paddle to move and make constraints to bricks disappear when the ball hits them but not the paddle. I tried this:

public void keyPressed(KeyEvent press)
{
obj = paddle;
int key = press.getKeyCode();
if(key == KeyEvent.VK_LEFT)
{
while(true)
{
paddle.move();
}
}
}

but I get an error for the "paddle.move();". Below is the entire code that I have gotten so far:

import acm.program.GraphicsProgram;
import acm.graphics.*;
import java.awt.Color;
import java.awt.event.*;
import acm.util.RandomGenerator;

[code]...

View Replies View Related

Key Presses In Game Not Working

Jan 25, 2014

I finished a game. It works perfectly fine on my computer. Sent it to a friend. On his computer, for some reason, one of the keys that are used to play doesn't work inside the game. (It works in any other context, like a web browser).

He tried pushing this key with no other keys held. Still didn't work. So it's not a 'key combination' issue. We both use Windows and the most updated JRE version.

The key that doesn't work on his computer, is: ; (semicolon)

View Replies View Related

Text Based RPG Game - Working With Multiple ArrayLists

Feb 22, 2014

I have come across an issue with arraylists. I am writing a text based RPG game as something to start with ...

Initially I had a single zone which was all stored in an arraylist and everything was working in regards to the player moving around. The problem I now have is how to add further zones to my game. Ideally I would like an arraylist for each zone, and would use the below to create each arraylist

public static ArrayList<RoomsClass> castleMap = new ArrayList<>();

The problem I now have is how to handle the player moving, initially with a single zone/arraylist I could reference that arraylist directly

public void findRoomCoords(int ID) {
for (int i = 0; i < castleMap.size(); i++) {
if (castleMap.get(i).roomID.equals(ID)) {
PLAYER.setCurrentRoomZone(castleMap.get(i).roomZone);
PLAYER.setCurrentRoomX(castleMap.get(i).roomX);
PLAYER.setCurrentRoomY(castleMap.get(i).roomY);
PLAYER.setCurrentRoomZ(castleMap.get(i).roomZ);
}
}
}

My initial thought was to use a getter/setter to remove the reference of castleMap from my movement code in order to access different arraylists, however this is where things have fallen over, I can't seem to work out how to get the arraylist name to change, depending on the outcome of the setZoneMap() method.

public void setZoneMap() {
switch (PLAYER.getCurrentRoomZone()) {
case 0: {
zoneMap = Castle.castleMap;
break;

[Code] ....

View Replies View Related

How To Add A KeyListener

Oct 30, 2014

I'm trying to add a KeyListener, but every time i do i have to implement methods that i will not use.

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class GameFrame extends JFrame implements KeyListener{
private GamePanel panel;
*/
public GameFrame()

[code]....

it works just concerned about the unimplemented methods.I'm supposed to make a row of images all the way across the screeen and down it unless the row contains the player. which is always at the bottom.

private void drawRow(int y, int blockWidth, int blockHeight, GameRow row, Graphics g) {
Image img = Toolkit.getDefaultToolkit().getImage("blocks.png");
for(int i = rows.size()-1; i >= 0; i--) {
if(rows.get(i).isEmpty()) {
g.drawImage(img, width,y,blockWidth, blockHeight, null);
}
}
}

As of right now it will only paste on the FIRST column not the rows.

View Replies View Related

Keylistener In A Loop?

Jun 16, 2014

I am trying to program a version of the "Worlds Hardest Game" using ready to program and applets for a final class project. Unfortunately I cannot figure out how to get my keylistener to work because I am using a loop for the enemies to go back and forth.

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
 public class HardestGame extends Applet
implements KeyListener, MouseListener {
final int WIDTH = 400;
final int HEIGHT = 123;
int myX = 400;
int myY = 400;

[code]...

View Replies View Related

Another Way To Add Keylistener To JComponent?

Jan 13, 2014

I wrote this program that makes a little square move around with the wasd keys. A huge problem that I had while creating it is adding the keylistener to my JFrame. In the end, I ended up creating an anonymous KeyListener class and adding that anonymous class to the JFrame. However, this anonymous class requires two other methods that I don't use, so any other thing I could have done to add these keylisteners to the JFrame without creating an anonymous class? I had to create 4...

Java Code:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

[Code] .....

View Replies View Related

Add KeyListener To Thread

Feb 13, 2014

I want to try and run a thread that starts running on start and quits after pressing a key.I cant seem to detect keypresses. ci cant seem to add code: Post denied. New posts are limited by number of URLs it may contain and checked if it doesn't contain forbidden words.

its just a code snippit without links or anything..this is a small part... but how do i add a keyListener in here that listends to a random key?The class implements KeyListener and the overrided method "keyPressed" sets the isRunning boolean to false... but the keyPressed method is never executed.

public static void main(String[] args) {
Thread thread = new Thread() {
public void run() {
while(isRunning){
System.out.println("running");
try {
sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
};
thread.start();
}

View Replies View Related

How To Access Keylistener If It Is Within A Method

Feb 1, 2014

I am trying to make a program where a ball moves up continuously when you press space, then begins to move down when you reach a certain point. My method for this is to have a timer, and have a method that does this: When you press space, add 10 y coords every second (using a timer), and if you reach 470 y, then begin to drop 10 y coords. I made a method to hold the keylistener, and am running that method inside the actionPerformed method which is within another class. However, since it is a method, I cannot add my keylistener to my frame in the main method.

main
error line 9
Java Code: import javax.swing.*;
public class Main {

[code]...

View Replies View Related

Accessing Keylistener From Thread

Mar 23, 2014

I start my thread, it's for a real basic game I'm learning. Everything was working fine, until I got to recognizing keys. It runs, and I can close using the mouse on the close command, but the keys aren't being generated from the keyboard. I copied it to a working sample, and here are the two files.

package testkeys;
import java.awt.Canvas;
import java.awt.Dimension;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;
import javax.swing.JFrame;

[Code] ....

The idea was to set the return value to true if any key is pressed, thus quitting, for this short sample program. Problem, it never recognizes any keys pressed on the keyboard. New to java and threading.

View Replies View Related

Combining JMenuItem And KeyListener

Jun 9, 2014

I have a JMenuItem "Find" in the Edit Menu. I want to add a shortcut key to the JMenuItem.

For Find for example i want to use Ctrl + F

Here is my 'Action' for 'Find' which can be used via Edit->Find. It works.

Find = new AbstractAction(){
public void actionPerformed(ActionEvent e){ 
String word = JOptionPane.showInputDialog(new MainWindow(),"","Find",PLAIN_MESSAGE);
new WordSearcher(textArea,word);
}
};

WordSearcher() is a class i am using to search the word

Now i want to add a KeyListener for ctrl+F, for doing the same purpose. Even this one works.

private KeyListener k1 = new KeyAdapter()
{
public void keyPressed(KeyEvent e)
{
if(e.isControlDown())

if(e.getKeyCode()== KeyEvent.VK_F)

[Code] ....

But the problem here is i have to write the same code twice. Is there some way by which I can Use the already written Action Find in the KeyListener.

View Replies View Related

How To Identify Up Down Left And Right Keys With Keylistener

Apr 9, 2014

I Want to identify up, down, left and right keys with the keylistener() WHATE NAME DO I IDENTIFY IT WITH

View Replies View Related

Servlets :: Detect Keystroke In JSP Textarea And Keylistener

May 22, 2014

i can use this current code to change it so that it use TextArea in jsp and the keylistener code in servlet. i know i have to take out the JFrame,JTextfield and ContentPane but still i could not do it.

Servlet:

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.IOException;

[code]....

View Replies View Related

Creating A Keylistener That Reacts Across The Entire Application?

Aug 13, 2014

make the keylistener "listen" across the entire application? Currently whenever I've used it I have always attached it to something else, like a JTextField or the like. However that doesn't work all that well if I for example want to close down the current JDialog by pressing escape, because no object that has the keylistener attached to it is currently focused.

When I searched around for the answer if found similar questions on stackoverflow, but those seemed to focus on listening globally, even when the application itself is not focused, which isn't quite what I want.

Here is a simple piece of code that I'm currently experimenting with:

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.IOException;
import java.security.spec.InvalidKeySpecException;

[Code].....

It's supposed to simply create a frame and give me a message whenever I press the A or D buttons, as well as notify me whenever they are released. As of currently it obviously doesn't work because the keylistener hasn't been attached to anything, but that's also where I'm unsure of how to proceed.

View Replies View Related

KeyListener Doesn't Work - Button Is Not Displayed

Apr 14, 2015

Why the keyTyped function isn't triggered, when i type a key?

Here is my code:

package main;
import java.awt.Component;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
 
[Code] .....

I have noticed, that if i call setVisible(true) before i add the button, then it works, but the button is not displayed... How can i achieve that the button is displays AND the KeyListener works?

View Replies View Related

Swing/AWT/SWT :: Paint Circle In Frame On Key Press - Cannot Get KeyListener To Work

Nov 3, 2014

I wanted to try out using a KeyListener to read what key I press so it can paint a circle in my frame, but I can't get it to work?

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;

[Code] ....

View Replies View Related

Java SetProperty Not Working

Apr 17, 2015

I have a file called config.dedserver and on a JButton click I am editing the values in it using setProperty but it didn't work. It removed everything from the entire file and then added the properties.

This is my code:

String newGems = gem.getText();
String newGold = gold.getText();
String newElixir = elixir.getText();
String newDE = de.getText();
try {
Properties props = new Properties();
props.setProperty("startingGems", newGems);

[Code] ....

And this is what the original file is:

Quote
# Main configuration file for DEDServer

# Starting resource values
startingGold=1000000
startingElixir=1000000
startingDarkElixir=1000000
startingGems=1077

# Saving method. Valid values are:
# -none: database disabled
# -h2: use h2 database (default)
saveMethod=h2

# Save interval (milliseconds)
saveInterval=60000

View Replies View Related

Java Import Not Working In Package?

May 20, 2015

I am trying to understand the package and import functions.

The path to access the file " library.book" is

C:UsersKameshDesktopJava SessionJAVAOCA7ooklibrary

and i can see 3 txt files

Book.class
Book.ctxt
Book.java

while compiling i am getting <identifier>expected error.

Here is the code :

package library;
public class Book {
public String isbn;
public void printBook() {}
}
package building;

[Code] .....

The error i get is <identifier>expected.

View Replies View Related

How To Create PDF File While Working In Java

Feb 22, 2013

how to create pdf file while working in java.

View Replies View Related

Java Merge Sort Not Working

Dec 10, 2014

I have been working on getting the merge sort working, I have a complteted code but am not getting the right results... Here is my code and the results are "876323149" ...

public class MergeSort
{
private static int[] local; // for use in copying
private static int[] a;
public static void main(String[] args) {
int[] test = {2,3,1,4,7,8,6,3,9};

[Code] ....

View Replies View Related

Working With Custom JList Component In Java

Aug 21, 2014

I am trying to use the JList to display a list of students. Now, each student has a first name, last name and course taken. each courses taken by the student has its' own name, level and idNumber. For now, I am just trying to create my own custom JList model for the students. I have the custom model as well as the button event calling it. Unfortunately, when I click the button to display the student info added, the component is NOT fired up!..

I am not sure what I have done wrong regarding creating the component because I can display the information on the console and the list contains everything I have added but just to display it on the component is NOT working. I have broken the codes into sub classes, you can add the classes to the same package or create sub packages to insert individual classes.

The student class
 
public class Student {
 private String studentName;
private String studentID ;
 public String getStudentName(){
return studentName;

[Code] ....

View Replies View Related

Java UDP Client Server Program Not Working

Apr 18, 2014

I am currently writing two java classes (client and server). The client takes an input number form keyboard and sends it to the server. The server then multiplies this number by two and sends it back to the client. The numbers should also be printed to screen along the way, for example if I input the number 3 I should get

"From Client: 3" "From Server: 6"

They should continuously do this unless a negative number is received by the client, say for example the number -3 is sent to the server and it returns -6.

The code I have for the two classes so far is:

import java.io.*;
import java.net.*;
import java.nio.ByteBuffer;
import java.util.Scanner;
class Client {
public static void main(String args[]) throws Exception {
DatagramSocket clientSocket = new DatagramSocket();

[Code] .....

Currently, when I run the program all I get is an output of the number first entered. I am aware it requires a loop but I don't know where and what the condition should be.

Also if I wanted to adapt this so that it would take the integer from client and subtract two at the server and return to client who sends back to server to keep subtracting two unless it reaches a negative number at which point the client will terminate the program - how might I do this.

I do realise there needs to be a while loop in the above code, but I wanted to test it sent the number from client to server and its not doing it. All I get is a print screen of 'enter number' and then the number I enter.

View Replies View Related

Java Iterate Over Collection Not Working As Expected

Aug 6, 2014

I am currently working on a Java project, below are my attempts at coding so far:

public class MyZoo
{
// zoo identifier
private String zooId;
// a number used in generating a unique identifier for the next animal to be added to the zoo
private int nextAnimalIdNumber;
// zstorage for the Animal objects
private TreeMap<String, Animal> animals;

[Code] ....

I currently cannot get the printAllAnimals() method to work as it should.

When executing the method printAllAnimals(), it does not do anything, when it is supposed to use the Collection object c, so that animals stored in the zoo can easily be checked.

View Replies View Related

Java Database Embedded Exe File Why Not Working

Jul 7, 2014

I have written my whole java code in netbeans IDE and create database in MYSQL work bench and connected java Gui with this DBMS through requried driver.when i run this program from Netbeans IDE , my program successfully access the data from DBMS. But when i created this java gui exe file its not working and not accessing data from DBMS, and each times gives exception "Driver not found ".if there is no driver loaded in this program how this file is working when i run this file from netbeans .

View Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved