Key Event in Loop *Solved*
Hi, I am very new (3 weeks) to java and i am wondering how I would get a key event inside while loop. I am trying to create a very simple version of pong with some basic code (creates a screen and draws a ball) that I modified. I tried putting the key event outside the while loop but it doesnt work and I have no idea how to move it inside. Thanks for any help.
Code:
[B]BallSimple.java[/B]
package Ball;
// import built in classes that you need to use
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import java.awt.event.*;
import java.util.Random;
//import java.util.Random;
public class BallSimple {
Boolean running = true;
//set x and y position
int x_pos = 20;
int y_pos = 20;
int paddle_posx = 600;
//set x and y velocity
int velocityx = 3;
int velocityy = 3;
int velopaddle = 5;
//set background and ball color
Color BackCol = Color.cyan;
Color BallCol = Color.red;
//define paddle movement
public void keyPressed(KeyEvent evt) {
int key = evt.getKeyCode();
if (key == KeyEvent.VK_UP){
System.out.println("Key Pressed");
}
}
// Constructor - accepts arguments and builds object
public BallSimple(JFrame window) {
// get graphics object from JFrame so we can draw on it
Graphics g = window.getGraphics();
//while loop to run program
while(running){
g.setColor(BackCol);
g.fillRect(0, 0, window.getWidth(), window.getHeight()); // this is bad coding practice, use descriptively named variables
g.setColor(BallCol);
g.fillOval(x_pos, y_pos, 25, 25);
g.setColor(Color.white);
g.drawLine(paddle_posx, 200, paddle_posx, 280);
//right side collision
if (x_pos < 0){
velocityx*=-1;
ColorChangeBG();
ColorChangeBall();
}
else if (x_pos + 12.5 > window.getWidth()){
velocityx*=-1;
ColorChangeBG();
ColorChangeBall();
}
if (y_pos + 12.5 > window.getHeight() || y_pos - 12.5 < 0){
velocityy*=-1;
ColorChangeBG();
ColorChangeBall();
}
//move ball
x_pos += velocityx;
y_pos += velocityy;
// puts current application thread to sleep for specified number of milliseconds
// try-catch block checks for error exception
try {
Thread.sleep(15);
} catch (InterruptedException ex) {
System.out.println("Could not sleep!");
}
} // end for loop
} // end constructor
private void ColorChangeBG(){
Random rand = new Random();
int RandomColor = rand.nextInt(5);
switch (RandomColor) {
case 0:
BackCol = Color.green;
break;
case 1:
BackCol = Color.black;
break;
case 2:
BackCol = Color.white;
break;
case 3:
BackCol = Color.blue;
break;
case 4:
BackCol = Color.white;
break;
}
}
private void ColorChangeBall(){
Random rand = new Random();
int RandomColor = rand.nextInt(5);
switch (RandomColor) {
case 0:
BallCol = Color.orange;
break;
case 1:
BallCol = Color.magenta;
break;
case 2:
BallCol = Color.yellow;
break;
case 3:
BackCol = Color.cyan;
break;
case 4:
BackCol = Color.DARK_GRAY;
break;
}
}
} // end ball class
Code:
[B]Screen.java[/B]
package Ball;
import javax.swing.JFrame;
// new class screen is an extension of built in class JFrame and inherits
// its properties, like setSize, setLocation etc.
public class Screen extends JFrame {
// constructor, where properties of object are set
public Screen(String frameLabel) {
setSize(640, 480);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
// main method to start object
public static void main(String[] args){
// create a screen and a simple ball animation on it
Screen window = new Screen("new screen");
new BallSimple(window);
}
}