I am currently, for fun, making a pong game however I am currently stuck trying to get the value of my boolean "goingup" to a true state via the w key press. I have the boolean going up in my render class ,as well a int x and int y, and is set to false. In this class i draw a square , with x as int x and y as int y. In another class(Framemake.java) i have a key listener set to my Jframe. In this class i have the keylistener listening for the w key press and release. Here is where my problem arises: If the W key is pressed i want it to change goingup to true and if its released i want it to change going up to false; this works fine but the problem is in my render class i have setup a timer , this is then started in the graphics method (not sure of the proper name). In the timers action performed i have set: if going up is true i want it to print out "going up is true- render". Here is the problem- even though going up is true because of the w key being pressed, the print is not printed onto the screen. (Thankyou for reading this, i needed you to fully understand whats going on- even though it may or may not be really obvious).
Here is the code: Starter.java
public class Starter {
public static void main(String[] args) {
Frame frame = new Frame();
frame.Start();
}
}
Here is the Frame.java
public class Frame implements Runnable {
Framemake r = new Framemake();
public void Start(){
new Thread(this).start();//
}
public void run() {
try {
r.framemade();
}
catch (Exception e) { //
e.printStackTrace();
}
}
}
Here is parts of Render.java as i was having troubles pasting into this post
public class Render extends JPanel implements ActionListener {
boolean goingup = false;
boolean goingdown = false;
int x = 0; //starting pos of x .
int y = 150;//starting pos of y
Timer tm = new Timer(7, this); //The timer is created
public void paintComponent(Graphics g) {
Framemake frames = new Framemake();
super.paintComponent(g);
tm.start();
g.setColor(Color.GREEN);
g.fillRect(x, y, 20, 150);
g.dispose();
}
public void actionPerformed(ActionEvent e){ //timer
if(goingup){
System.out.println("Going up is true- render");
x++;//for example...
repaint();
}
}
Here is Framemake.java
public class Framemake implements KeyListener {
int WIDTH = 500;
int HEIGHT = 500;
Render c = new Render();
public void framemade() {
System.out.println("Frame starting");
//Frame is created here
Render render = new Render();
JFrame frame = new JFrame();
frame.setSize(WIDTH, HEIGHT);
frame.setVisible(true);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setTitle("Animation");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Window w = frame;
w.addKeyListener(this);
frame.add(render); //displays all graphics from render to the screen
}
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
if (keyCode == KeyEvent.VK_W) {
System.out.println("Going up");
c.goingup = true;
}
if (keyCode == KeyEvent.VK_S) {
System.out.println("Going down");
c.goingdown = true;
}
}
public void keyReleased(KeyEvent e) {
int keyCode = e.getKeyCode();
if (keyCode == KeyEvent.VK_W) {
System.out.println("Up terminated");
c.goingup = false;
}
if (keyCode == KeyEvent.VK_S) {
System.out.println("Down terminated");
c.goingdown = false;
}
}
public void keyTyped(KeyEvent e) {}
}
I hope the question is clear here (Sorry if I have posted my code incorrectly), any help is appreciated.