Click here to Skip to main content
13,739,356 members
Rate this:
 
Please Sign up or sign in to vote.
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 12-Aug-18 2:27am
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?
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

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

The code below seems to work. See my commented lines where I have removed or added some code:
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);
	
    }
}
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web01-2016 | 2.8.180920.1 | Last Updated 12 Aug 2018
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100