Page 2 of 2 FirstFirst 12
Results 21 to 32 of 32
  1. #21
    darthvader45 is offline Member
    Join Date
    Dec 2012
    Posts
    17
    Rep Power
    0

    Default Re: How to make a bouncing ball in Java

    Okay, so I added a return to the Ball constructor, which should be able to pass that over to a class that uses it. How would the RepaintTimer class receive this?

  2. #22
    darthvader45 is offline Member
    Join Date
    Dec 2012
    Posts
    17
    Rep Power
    0

    Default Re: How to make a bouncing ball in Java

    So how would I change this constructor to allow for referencing?

    Java Code:
    public Ball() 
    	{
    		
    	}
    Last edited by darthvader45; 04-17-2018 at 03:43 AM.

  3. #23
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: How to make a bouncing ball in Java

    You don't dereference a constructor. You dereference an instance of a class. You can pass the instance of Ball to a method as an argument of type Ball.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #24
    darthvader45 is offline Member
    Join Date
    Dec 2012
    Posts
    17
    Rep Power
    0

    Default Re: How to make a bouncing ball in Java

    How does one do that? Example?

  5. #25
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: How to make a bouncing ball in Java

    Java Code:
    public class PassingReferenceExample {
       public static void main(String[] args) {
          PassingReferenceExample pre = new PassingReferenceExample();
          Ball ball = new Ball();
          pre.method(ball);
       }
    
    
       public void method(Ball b) {
          b.me();
       }
    }
    
    
    class Ball {
       public void me() {
          System.out.println("I am a ball.");
       }
    }
    If you didn't know how to do this before then I would suggest you review the basics of Java programming. Trying to do animation relies on a solid understanding of the fundamental concepts of Java. And you should experiment with timers and simple graphics, lists and other Java concepts before putting it together to achieve a specific goal.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #26
    darthvader45 is offline Member
    Join Date
    Dec 2012
    Posts
    17
    Rep Power
    0

    Default Re: How to make a bouncing ball in Java

    Okay, I am currently able to get the ball to paint, but now I'm stuck on the moving part.

    New code:

    BallPanel:
    Java Code:
    package com.jlefelhocz.module10;
     
     
     
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
     
    import javax.swing.JPanel;
     
    @SuppressWarnings("serial")
    public class BallPanel extends JPanel implements MouseListener
    {
        Graphics g;
        Ball b = new Ball();
        RepaintTimer rt = new RepaintTimer();
        public BallPanel()
        {
            Thread t = new Thread();
            t.start();
            addMouseListener(this);
                         
                     
        }
        @Override
        public void mouseClicked(MouseEvent e) 
        {
            // TODO Auto-generated method stub
        	Ball b = new Ball(e.getX(), e.getY(), 24, Color.BLUE);
        	b.pHeight = this.getHeight();
        	b.pWidth = this.getWidth();
        	b.x = e.getX();
        	b.y = e.getY();
            b.paint(getGraphics());
        	removeMouseListener(this);
            rt.run();
        }
        @Override
        public void mouseEntered(MouseEvent e) 
        {
            // TODO Auto-generated method stub
             
        }
        @Override
        public void mouseExited(MouseEvent e) 
        {
            // TODO Auto-generated method stub
             
        }
        @Override
        public void mousePressed(MouseEvent e) 
        {
            // TODO Auto-generated method stub
        	
        	
            
        }
        @Override
        public void mouseReleased(MouseEvent e) 
        {
            // TODO Auto-generated method stub
        }
        
    }
    RepaintTimer.java:

    Java Code:
    package com.jlefelhocz.module10;
    
    
    
    public class RepaintTimer implements Runnable 
    {
    
    
    	@Override
    	public void run() 
    	{
    		try {
                Thread.sleep(50);
              } catch (InterruptedException ex) 
              {
            	  ex.printStackTrace();
            	  Thread.currentThread().interrupt();
              }
    	}
    		
    	
    
    }
    BouncingBall.java:
    Java Code:
    package com.jlefelhocz.module10;
     
    import javax.swing.JFrame;
     
    @SuppressWarnings("serial")
    public class BouncingBall extends JFrame 
    {
     
          // Box height and width
    int x,y,dx,dy,diameter;
          
         
        public static void main(String[] args) 
        {
            JFrame.setDefaultLookAndFeelDecorated(true);
            JFrame frame = new JFrame("Bouncing Ball");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setSize(320, 480);
            frame.setContentPane(new BallPanel());
            frame.setVisible(true);
            Thread t = new Thread();
            t.start();
     
        }
        
    }
    Ball.java:
    Java Code:
    package com.jlefelhocz.module10;
     
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.Timer;
    
    
     
    public class Ball implements Runnable 
    {
        int x, y, diameter, centerX, centerY;
        Color Ballcolor;
        int pWidth;
    	  int pHeight;
    	  int radius = diameter/2;
    
    	  
    	 
    	  
    	  int dirX = 3;
    	  int dirY = 3;
         
         public Ball()
         {
        	 
         }
        public Ball(int x,int y,int diameter, Color bColor)
        {
            	this.x = getX();
                this.y = getY();
                this.diameter = diameter;
              
                Ballcolor = bColor;
        }
        
        private int getY() 
        {
    		// TODO Auto-generated method stub
    		return this.y;
    	}
    
    	private int getX() 
    	{
    		// TODO Auto-generated method stub
    		return this.x;
    	}
    
    	
    
    	@Override
        public void run() 
        {
        	
    		Timer t = new Timer(50, new ActionListener()
    		
    		   {
    	
    			@Override
    			public void actionPerformed(ActionEvent ae) 
    			{
    				
    			x += dirX;
    			y += dirY;
    	      
    	              if (x - radius < 0) 
    	              {
    	            	  dirX = -dirX;
    	                x = radius; 
    	              } else if (x + radius > pWidth) 
    	              {
    	            	  dirX = -dirX;
    	                x = pWidth - radius;
    	              }
    	      
    	              if (y- radius < 0) 
    	              {
    	            	  dirY = -dirY;
    	                y = radius;
    	              } else if (y + radius > pHeight) 
    	              {
    	            	  dirY = pHeight-radius;
    	                y = 300 - diameter;
    	              }
    	             
    			}
    			
    		   });         
    			t.start();
        }
        
    	public void paint(Graphics g)
        {
            g.setColor(Ballcolor);
            g.fillOval(x,y,diameter,diameter);
            
        }
     
    }

  7. #27
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,964
    Rep Power
    31

    Default Re: How to make a bouncing ball in Java

    What is the purpose of the Thread class used on lines 21 and 22 in main and in lines 18 and 19 in BallPanel?
    Also what is the purpose of all the run() methods? When are they supposed to be executed?

    I'm stuck on the moving part.
    Is the code executing in the way you expect?
    Try debugging the code by adding some print statements that shows where the code is executing (or not executing) so you can see what is happening when the program is executing. For example add print statements in all the run methods and all the paint methods.
    Last edited by Norm; 04-17-2018 at 06:43 PM.
    If you don't understand my response, don't ignore it, ask a question.

  8. #28
    darthvader45 is offline Member
    Join Date
    Dec 2012
    Posts
    17
    Rep Power
    0

    Default Re: How to make a bouncing ball in Java

    I removed the second one, but the one in Ball is to make a thread for the Ball, which will sleep for 25 milliseconds, as I just now set the timer to in Ball. This is so there will not be an endless loop. How would I call Ball's run() method so that the ball's coordinates get updated as needed?

    Edit: Seems the run one is getting called, but does nothing.


    The run method in Ball.java:

    Java Code:
    public void run() 
        {
        	
    		Timer t = new Timer(25, new ActionListener()
    		
    		   {
    	
    			@Override
    			public void actionPerformed(ActionEvent ae) 
    			{
    				
    			x += dirX;
    			y += dirY;
    	      
    	              if (x - radius < 0) 
    	              {
    	            	  dirX = -dirX;
    	                x = radius; 
    	              } else if (x + radius > pWidth) 
    	              {
    	            	  dirX = -dirX;
    	                x = pWidth - radius;
    	              }
    	      
    	              if (y- radius < 0) 
    	              {
    	            	  dirY = -dirY;
    	                y = radius;
    	              } else if (y + radius > pHeight) 
    	              {
    	            	  dirY = pHeight-radius;
    	                y = 300 - diameter;
    	              }
    	             
    			}
    			
    		   });         
    			t.start();
    			 System.out.println("Print statement");
        }
    The paint method:

    Java Code:
    public void paint(Graphics g)
        {
            g.setColor(Ballcolor);
            g.fillOval(x,y,diameter,diameter);
            System.out.println("Print statement");
        }
    Last edited by darthvader45; 04-17-2018 at 07:00 PM.

  9. #29
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,964
    Rep Power
    31

    Default Re: How to make a bouncing ball in Java

    How would I call Ball's run() method
    Using the reference to the instance of the Ball class: b.run();

    run is a poor name because it is associated with Threads. Ball's run method is for starting the Timer so a better name would be startTimer()
    If you don't understand my response, don't ignore it, ask a question.

  10. #30
    darthvader45 is offline Member
    Join Date
    Dec 2012
    Posts
    17
    Rep Power
    0

    Default Re: How to make a bouncing ball in Java

    However, if Ball implements Runnable, then it requires the inclusion of a method called run().

  11. #31
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,964
    Rep Power
    31

    Default Re: How to make a bouncing ball in Java

    Ball implements Runnable
    Yes, but why does Ball implement Runnable? It is never used.
    If you don't understand my response, don't ignore it, ask a question.

  12. #32
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: How to make a bouncing ball in Java

    Of course, one could just establish a timer to make calls to repaint. The Ball class does not need to have its own timer.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Page 2 of 2 FirstFirst 12

Similar Threads

  1. help! bouncing ball program
    By gryd00 in forum New To Java
    Replies: 2
    Last Post: 05-10-2011, 07:58 AM
  2. Bouncing Ball program
    By lost1000 in forum New To Java
    Replies: 3
    Last Post: 11-18-2010, 03:26 PM
  3. high bouncing ball
    By bouncingball in forum Reviews / Advertising
    Replies: 1
    Last Post: 06-19-2008, 11:21 AM
  4. bouncing ball issue
    By adam405 in forum New To Java
    Replies: 1
    Last Post: 03-18-2008, 03:48 AM
  5. Problem deleting ball from bouncing ball app
    By adlb1300 in forum New To Java
    Replies: 2
    Last Post: 12-03-2007, 09:08 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •