I am working if application and I cannot figure out how to make a box move forward one at a time when the spacebar is pressed. Here are my codes:

package DrawingInApplications;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.*;

import javax.swing.JFrame;

public class Program extends Drawing implements KeyListener{

	Drawing d;
	
	
	public Program (){
		JFrame frame = new JFrame ("Program.java");
		frame.setSize (300,200);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible (true);
		d = new Drawing();         // goes to class called Drawing and to call you add d
		frame.add(d);                    // adds drawing class
		d.Drawing();                     // calls drawing METHOD!!! in drawing class (sry I put the same name lol)
		
	}
	
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Program p = new Program ();
	}


	@Override
	public void keyPressed(KeyEvent arg0) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void keyReleased(KeyEvent arg0) {
		// TODO Auto-generated method stub
		int key = arg0.getKeyCode();
		if (key == KeyEvent.VK_SPACE){
			move += 1;
			d.Drawing();
		}
		
		
		
	}


	@Override
	public void keyTyped(KeyEvent arg0) {
		// TODO Auto-generated method stub
		
	}

}

and

package DrawingInApplications;
import javax.swing.*;
import java.awt.*;


public class Drawing extends JPanel{

	int move = 10;
	
	public void Drawing (){
		repaint();    // calls paint method because you cannot call it directly from Program class
	}
	public void paintComponent (Graphics g){
		super.paintComponent(g);                 // a line that idk what it does, but you need to have it
		g.fillRect(move, 10, 10, 10);              // you know what this is
	}

}

I am having trouble communicating through the different classes. When the spacebar (key event) is pressed then move += 1; and repaint();

I know how to do that. like I mentioned I believe it's the communicating between the classes.

Thanks for any help.

Recommended Answers

All 3 Replies

In your drawing class, move is set to 10 and never changes. There are a few ways to fix this the first is:

//in the Drawing class
public void paintComponent (Graphics g){
	super.paintComponent(g); //This line calls the parent class, check out inheritance if you need a better explanation
        move++; //this is the same as move += 1; in your code             
	g.fillRect(move, 10, 10, 10);              
	}

You don't have to increment move in the keyListener if you use this way, just call the method.
The above is probably the easiest way to fix your problem, but you could also try adding a parameter to the paintComponent method:

public void paintComponent (Graphics g, int paramMove){
		super.paintComponent(g);                 
		g.fillRect(paramMove, 10, 10, 10);            
	}

If you use this, you should increment move in the keyListener and send move in as a parameter.
I would suggest using the first way.

or you can try

BufferStrategy bs = getBufferStrategy();
Graphics g = bs.getDrawGraphics();
//draw anything
bs.show();

@Mattox. Sorry, but both your suggestions are poorly thought out.

move++ in the paintComponent method means that the box will move every time paintComponent is called. You have no control over how often that is, so the moves are unpredictable, and certainly have nothing to do with the space bar.

Changing the method to paintComponent (Graphics g, int paramMove) means that it does not override the inherited paintComponent (Graphics g) method and thus will never be called.

If you're not certain its a good idea to check out your code before posting in public.

@sirlink99:
You define a keylistener but you don't add it to any of your GUI objects, so it won't get called.
You need to increment move in the keylistener, but use it in the Drawing class, which suggests passing it as a parameter, eg in Drawing

public void drawAt(int position) {
   move = position;
   repaint();
}

Now declare move in the main class, increment that when the space bar is pressed, and call the drawAt method to update the Drawing instance. Don't increment move in Drawing, just use the value you are passed.
Finally, I see no reason to make the main class extend Drawing, but some reasons for not doing that.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.