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


ADVERTISEMENT

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

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

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

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

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

Unable To Print A Loop Inside Array Of Greater Size Than Loop Itself

Jan 27, 2015

I am trying to print a loop inside an array of a greater size than the loop itself. Like for example I have an array of size 7 but it has only 3 elements.

int[] array=new int[7]
array[0]=2;
array[1]=3;
array[2]=4;

now what I want to do is print these three numbers in a loop so that my array[3]=2;array[4]=3;array[5]=4 ...... till the last one. Also the array could be any size and not just 7.

View Replies View Related

While Loop Inside A For Loop To Determine Proper Length Of Variable

Feb 25, 2014

Here's the code: it's while loop inside a for loop to determine the proper length of a variable:

for (int i = 0; i < num; i++) {
horse[i]=new thoroughbred();
boolean propernamelength = false;
while (propernamelength==false){
String name = entry.getUserInput("Enter the name of horse "

[code]....

I was just wondering what was going on here -- I've initialized the variable, so why do I get this message? (actually the carat was under the variable name inside the parentheses.

View Replies View Related

When Type Quit To Close Outer Loop / It Still Runs Inner Loop

Nov 2, 2014

I have everything else working. My problem is that when i type "quit" to close the outer loop. It still runs the inner loop. The National Bank manager wants you to code a program that reads each clients charges to their Credit Card account and outputs the average charge per client and the overall average for the total number of clients in the Bank.

Hint: The OUTER LOOP in your program should allow the user the name of the client and end the program when the name entered is QUIT.In addition to the outer loop, you need AN INNER LOOP that will allow the user to input the clients charge to his/her account by entering one charge at a time and end inputting the charges whenever she/he enters -1 for a value. This INNER LOOP will performed the Add for all the charges entered for a client and count the number of charges entered.

After INNER LOOP ends, the program calculates an average for this student. The average is calculated by dividing the Total into the number of charges entered. The program prints the average charge for that client, adds the Total to a GrandTotal, assigns zero to the Total and counter variables before it loops back to get the grade for another client.Use DecimalFormat or NumberFormat to format the numeric output to dollar amounts.

The output of the program should something like this:

John Smith average $850.00
Maria Gonzalez average $90.67
Terry Lucas average $959.00
Mickey Mouse course average $6,050.89
National Bank client average $1,987.67

Code:

public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String name = "";
int charge = 0;
int count = -1;
int total = 1;
int grandtotal = 0;
int average = 0;
 
[code]....

View Replies View Related

Convert From While Loop To For Loop

Mar 8, 2014

How to convert this program from a while loop to a for loop.

import java.util.Scanner;
public class LongDivision {
public static void main(String arguments[]){
Scanner input = new Scanner(System.in);
System.out.println("Enter the dividend: ");

[Code] ....

View Replies View Related

Converting For Loop To While And Do While Loop

Feb 17, 2014

How do I convert my for loop below to a while and do while loop?

for(int a=1;a<=9;a++){
for(int b=1;b<=a;b++){
System.out.print(a);
}
System.out.println();

[Code] .....

View Replies View Related

How To Store Value From Loop To Add To New Value From Loop

Feb 9, 2015

I am trying to make a program add values from a loop. So what its supposed to do is search through tokens on an imported file. The file lists State, Capital, and then capital population. Then take the population string, turn it into numbers, and then do stuff with the numbers. First I'm supposed to find the Highest and lowest population of the places in the file (which I did without problem), but the finally thing is I'm supposed to add each found population to the last so I can find the average of the populations.

I just cannot seem to grasp how to do that. I THINK I'm supposed to some how store the given value into a variable, but how do I get that variable to add to the new value?

like...?
Get token -> a
b = a
c = a + b

or wait no.....

Java Code :

import java.io.*;
import java.util.Scanner;
public class CapPopS
{
public static void main(String[] args) throws IOException
{
File stateCAP = new File("state-capital-2004population.txt");
if (!stateCAP.exists())

[Code] ....

View Replies View Related

How To Add Within Loop

Feb 9, 2015

My teacher wants me to make a program that counts the amount of steps a student takes a day. The program asks for other info such as name, age, college. However I need to write a loop that will allow the user to enter how many ever steps they took and convert them to miles.how exactly to make the steps entered by the user within the loop be their own individual days like monday tuesday etc. Like the loop will ask how many steps did you take monday.. tuesday.. etc for each time it runs.

package StudentInfo;
import java.util.Scanner;
public class studentinfo {
public static void main (String [] args){
Scanner scan = new Scanner(System.in);

[code]....

View Replies View Related

How To Use While Loop

Apr 5, 2014

I need to write a program that measures how long it will take someone to make a million dollars if he is being paid $5.75 an hour, but the pay rate is increase by 0.2% each week after the third week.

View Replies View Related

Why Won't FOR Loop End

Feb 18, 2014

This is the first time I've ever gotten an infinite loop with a FOR loop. This program is supposed to let you enter five integer numbers and draw a bar chart based on those numbers. After the fifth number is entered, guess what? It wraps back around to zero again and starts over! Why the bleep doesn't it stop? The code is below:

Java Code: import java.awt.Graphics;
import javax.swing.JPanel;
import java.util.Scanner;

[Code]...

View Replies View Related

Inner If Loop Code

Mar 23, 2015

Can i write inner if loop without { and } if it has more lines of code as below

public class TEst111 {
/**
* @param args
*/
public static void main(String[] args) {
int num=11;
// TODO Auto-generated method stub
if (num < 10)

[code]....

I tested to see outer if which does not need { and } even though it has multiple lines in that

View Replies View Related







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