Click here to Skip to main content
15,889,116 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.geom.*;
public class Ball extends JPanel implements ActionListener,KeyListener {

	
	
	private static final long serialVersionUID = -3622302774061996117L;
	
	
	
//created an obj for the class Timer
	
Timer t=new Timer(5,this);

// created the x y coordinates and made the velocity variables 

	int x=0,y=0,velX=0,velY=0;
	
	//created the method and started the timer and added the KeyListener 
	
	public Ball() {
		t.start();
		addKeyListener(this);
		setFocusable(true);
		setFocusTraversalKeysEnabled(false);
		
	}
	//over rode the paint component method and created the obj that is gonna be controled by the arrow keys
	@Override
	public void paintComponent(Graphics g) {
		super.paintComponent(g);
	   this.setBackground(Color.WHITE);
		g.setColor(new Color(66, 134, 244));
		Graphics2D g2= (Graphics2D) g;
		Ellipse2D circle=new Ellipse2D.Double(x,y,40,40);
		g2.fill(circle);
	}
	//Over rode the action performed method and gave the obj the ability to move
	@Override
	public synchronized void actionPerformed(ActionEvent e) {
		if(x<-40) {
			velX=0;
			x=600;
		}
		if(y<-40) {
			velY=0;
			y=600;
		}
		if(x>600) {
			velX=0;
			x=-40;
		}
		if(y>600) {
			velY=0;
			y=-40;
		}
		x+=velX;
		y+=velY;
		repaint();
	}
	//Over rode the key pressed method so as the user can control the obj by the arrow keys
	@Override
	public synchronized void keyPressed(KeyEvent e) {
		int c=e.getKeyCode();
		if(c==KeyEvent.VK_UP) {
			velY=-3;
			velX=0;
		}
        if(c==KeyEvent.VK_DOWN) {
			velY=3;
			velX=0;
		}
        if(c==KeyEvent.VK_RIGHT) {
	        velX=3;
	        velY=0;
        }
        if(c==KeyEvent.VK_LEFT) {
	        velX=-3;
	        velY=0;
        }
	}
	//Over rode the key released method so as the obj would stop moving when the arrow keys are released
	@Override
	public void keyReleased(KeyEvent e) {
		velY=0;
		velX=0;
	}
	
	@Override
	public void keyTyped(KeyEvent e) {}
	
	//created the main window
	public static void main(String [] args) {
	JFrame frame=new JFrame("moving ball");
	Ball b=new Ball();
	frame.setSize(600,600);
	frame.setVisible(true);
	frame.setResizable(false);
	frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
	frame.setLocationRelativeTo(null);
	frame.add(b);
	JOptionPane.showMessageDialog(null, "let's start\nif it doesn't work then re-run the program please", "message", JOptionPane.DEFAULT_OPTION);
}
}

now this code is supposed to view a cirlcle on a window that can be controled by the arrow keys
it works well
but the problem is when i run it sometimes it doesnt work where the ball doesn't move
when i rerun it it works perfectly
can someone give me an explation and some possible fixes
Thank you.

What I have tried:

I don't know whats the problem so i wish from you guys to help
Posted
Updated 12-Aug-18 6:40am
Comments
Richard MacCutchan 12-Aug-18 9:07am    
I cannot get this to work at all, and it appears that the key presses are not being captured at all. Is there some extra option you use when building the program?
Member 13946476 12-Aug-18 12:16pm    
no just try several times sometimes it takes 5-6 to work idk why

if you can check the code and if u find some possible fixes plz help me
Richard MacCutchan 12-Aug-18 12:42pm    
See below.

1 solution

The code below seems to work. See my commented lines where I have removed or added some code:
Java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.geom.*;
public class Ball extends JPanel implements ActionListener,KeyListener {

	
	
	private static final long serialVersionUID = -3622302774061996117L;
	
	
	
//created an obj for the class Timer
	
Timer t=new Timer(10,this);

// created the x y coordinates and made the velocity variables 

	int x=0,y=0,velX=0,velY=0;
	
	//created the method and started the timer and added the KeyListener 
	
	public Ball() {
		t.start();
		addKeyListener(this);
// remove the following two lines here
//		setFocusable(true);
//		setFocusTraversalKeysEnabled(false);
		
	}
	//over rode the paint component method and created the obj that is gonna be controled by the arrow keys
	@Override
	public void paintComponent(Graphics g) {
		super.paintComponent(g);
	   this.setBackground(Color.WHITE);
		g.setColor(new Color(66, 134, 244));
		Graphics2D g2= (Graphics2D) g;
		Ellipse2D circle=new Ellipse2D.Double(x,y,40,40);
		g2.fill(circle);
	}
	//Over rode the action performed method and gave the obj the ability to move
	@Override
	public synchronized void actionPerformed(ActionEvent e) {
		if(x<-40) {
			velX=0;
			x=600;
		}
		if(y<-40) {
			velY=0;
			y=600;
		}
		if(x>600) {
			velX=0;
			x=-40;
		}
		if(y>600) {
			velY=0;
			y=-40;
		}
		x+=velX;
		y+=velY;
		repaint();
	}
	//Over rode the key pressed method so as the user can control the obj by the arrow keys
	@Override
	public synchronized void keyPressed(KeyEvent e) {
	int c=e.getKeyCode();
		if(c==KeyEvent.VK_UP) {
			velY=-3;
			velX=0;
		}
        if(c==KeyEvent.VK_DOWN) {
			velY=3;
			velX=0;
		}
        if(c==KeyEvent.VK_RIGHT) {
	        velX=3;
	        velY=0;
        }
        if(c==KeyEvent.VK_LEFT) {
	        velX=-3;
	        velY=0;
        }
	}
	//Over rode the key released method so as the obj would stop moving when the arrow keys are released
	@Override
	public synchronized void keyReleased(KeyEvent e) {
		velY=0;
		velX=0;
	}
	
	@Override
	public void keyTyped(KeyEvent e) {}
	
	//created the main window
	public static void main(String [] args) {
	JFrame frame=new JFrame("moving ball");
	Ball b=new Ball();
	frame.setSize(600,600);
	frame.setVisible(true);
	frame.setResizable(false);
	frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
	frame.setLocationRelativeTo(null);
// added the following three lines here
	frame.setFocusable(true);
	frame.setFocusTraversalKeysEnabled(false);
	frame.addKeyListener(b);
	frame.add(b);
	
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900