Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Collision Testing Code isnt working.

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Collision Testing Code isnt working.

    For some reason the ball is still going off the top of the frame. Im not sure why.




    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.event.KeyAdapter;
    import java.awt.event.KeyEvent;
     
    import javax.swing.JFrame;
     
     
    public class Frame extends JFrame
    {
    	int ovalSpeed = 5;
    	int xCoord = 50; 
    	int yCoord = 50;
    	private final int ovalWidth = 25;
    	private final int ovalHeight = 25;
    	private final int WIDTH = 800;
    	private final int HEIGHT = WIDTH / 5 * 4;
    	private Dimension borderlessSize;
    	private Image dbImage;
    	private Graphics dbG;
    	private Dimension d = new Dimension(WIDTH, HEIGHT);
     
    	//Constructor for the class, and makes the frame.
    	Frame()
    	{
    		addKeyListener(new AL());
    		setTitle("Frame");
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setSize(d);
    		setResizable(false);
    		setVisible(true);
    		setBackground(Color.WHITE);
     
    		borderlessSize = this.getContentPane().getSize();
     
    		System.out.println(borderlessSize.getWidth() + " " + borderlessSize.getHeight());
    	}
    	//Draws graphics on the screen.
    	public void paint(Graphics g)
    	{
    		dbImage = createImage(getWidth(), getHeight());
    		dbG = dbImage.getGraphics();
    		paintComponent(dbG);
    		g.drawImage(dbImage, 0, 0, this);
    	}
     
    	public void paintComponent(Graphics g)
    	{
    		g.fillOval(xCoord, yCoord, ovalWidth, ovalHeight);
     
    		repaint();
    	}
    	public class AL extends KeyAdapter
    	{
    		public void keyPressed(KeyEvent e)
    		{
    			int keyCode = e.getKeyCode();
     
    			//If left arrow is pressed.
    			if(keyCode == e.VK_LEFT)
    			{
    				//Tests they're past the left hand portion of the screen, of so, then set its xCoord to 0.
    				if(xCoord <= 0)
    				{
    					xCoord = 0;
    				}
    				//Moves the ball ovalSpeed pixels to the left.
    				else
    				{
    					xCoord += -ovalSpeed;
    				}
    			}
    			//If right arrow is pressed.
    			if(keyCode == e.VK_RIGHT)
    			{
    				//Tests they're past the right hand portion of the screen, of so, then set its xCoord to the width of the frame - the width of the oval.
    				if(xCoord >= borderlessSize.getWidth() - ovalWidth)
    				{
    					xCoord = (int) (borderlessSize.getWidth() - ovalWidth);
    				}
    				//Moves the ball ovalSpeed pixels to the right.
    				else
    				{
    					xCoord += ovalSpeed;
    				}
    			}
    			//If up arrow is pressed.
    			if(keyCode == e.VK_UP)
    			{
    				//Tests they're higher then the upper hand portion of the screen, of so, then set its yCoord to 0.
    				if(yCoord <= 0)
    				{
    					System.out.println(yCoord);
    					System.out.println("top collision");
    					yCoord = 0;
    				}
    				//Moves the ball ovalSpeed pixels up.
    				else
    				{
    					System.out.println(yCoord);
    					yCoord += -ovalSpeed;
    				}
    			}
    			//If down arrow is pressed.
    			if(keyCode == e.VK_DOWN)
    			{
    				//Tests they're lower the lower hand portion of the screen, of so, then set its yCoord to the height of the frame - the height of the oval.
    				if(yCoord >= borderlessSize.getHeight() - ovalHeight)
    				{
    					yCoord = (int) (borderlessSize.getHeight() - ovalHeight);
    				}
    				//Moves the ball ovalSpeed pixels down.
    				else
    				{
    					yCoord += ovalSpeed;
    				}
    			}
    		}
    	}
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Collision Testing Code isnt working.

    There are so many basic mistakes in painting/graphics here. For example, calling repaint() from inside paintComponent() is just plain wrong, if you were actually overriding paintComponent(), which you're not. You should learn why it's wrong, why you should be overriding paintComponent() and how when you read the Custom Painting Lesson, (the whole thing). After you've applied what you learn in that lesson to your source code, come back with your changes and when you're ready for help.

    Also, don't name your classes the same as existing core Java classes. It's bad form and will eventually (if not immediately) result in headaches.

Similar Threads

  1. [SOLVED] What wrong with my code? count++ isnt incrementing.
    By godofdoom999 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: May 24th, 2012, 12:37 PM
  2. JInternalFrame isnt working why
    By justyStepi in forum AWT / Java Swing
    Replies: 1
    Last Post: May 17th, 2012, 01:42 PM
  3. collision detection not working...
    By skberger21 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 10th, 2011, 09:02 PM
  4. keylistner + applet isnt working
    By brandon95 in forum AWT / Java Swing
    Replies: 3
    Last Post: January 4th, 2011, 07:16 AM
  5. hmm for some reason the char.At isnt working
    By dvsumosize in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 2nd, 2010, 04:27 AM

Tags for this Thread