+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: Simple thread - Bouncing ball

  1. #1
    Join Date
    May 2008
    Location
    Hell
    Posts
    3,852
    Blog Entries
    4
    Rep Power
    49

    Simple thread - Bouncing ball

    Hey Codecall !

    I saw some threads with ping pong games and such, however most of them were broken.
    So I thought, well let people have something simple to work with?
    Maybe a running thread?

    Anyways here it is !

    Classes needed
    Code:
    import java.awt.*;
    First class, our own Thread class that has a function we want...
    Code:
    class XThread extends Thread {
        public static boolean delay(long millis) {
            if (interrupted())
                return false;
            try {
                sleep(millis);
            } catch (InterruptedException e) {
                return false;
            }
            return true;
        }
    }
    Firstly, we inherit from Thread, since I didn't want to do something heavy, just made something simple. XThread will be the one to run later on...
    The ball class
    Code:
    public class Ball extends Panel implements Runnable {
    	protected Thread activity;
    	private int r,x0,y0;
    	private int xStep,yStep;
    So, our ball, extends its area from Panel and we implement the runnable interface for our ball to since we are going to work with one thread...
    Thread activity is our thread for the main dish...
    While r,x0,y0 will represent the r for radius of a ball and x0 y0 start points.
    Balls everywhere !
    Code:
    public Ball(int radius,int xSpeed, int ySpeed) {
    		r = radius; xStep = xSpeed; yStep = ySpeed;
    		x0 = r; y0 = r;
    	}
    Our constructor that will create the main ball every time we want to have balls.
    Thread activity - Start || Stop
    Code:
    public void start() {
    		if(activity == null) {
    			activity = new Thread(this);
    			activity.start();
    		}
    	}
    	public void stop() {
    		if(activity != null) {
    			activity.interrupt();
    			}
    		}
    As for our methods that will start of the execution of tasking we want(moving a ball...)
    We will have to see when the activity is null. Thats when we will start a new thread which will allow the ball to interact with out panel area...
    As stop checks if activity isn't null it will interrupt the activity, inherit from Thread applied to our own class called XThread.
    Running with a Runnable interface you will need a method called run
    Code:
    public void run() {
    		while(XThread.delay(100)) {
    			if(x0-r+xStep<0 || x0+r+xStep > getSize().width)
    				xStep = -xStep;
    				x0 += xStep;
    			if(y0-r+yStep<0 || y0+r+yStep > getSize().height)
    				yStep = - yStep;
    				y0 += yStep;
    				repaint();
    		}
    	}
    So ass mentioned, XThread is just a small adjustment of our own taste, my tasted I gotten to taste... Allowing the execution happen while the thread isn't destroyed or interrupted...
    So, how does it move, while executing the process we have to have conditions it can follow or be bound to... ifx0-r+xStep<0 or x0+r+xStep>getSize().width Follows to, if start point minus radius+the speed in x axle, is less than 0 or invers, meaning from |<-->|. Literally from first point of wall to the last...
    Same goes for in the y axle...
    Drawing the ball...
    Code:
    public void paint(Graphics g) {
    		g.fillOval(x0-r,y0-r,2*r,2*r);
    	}
    }
    Drawing the ball depending from the radius point of x0 and y0. The rest is simple to get...Math...meh
    Demonstration of our ball, first off classes needed
    Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import static javax.swing.JOptionPane.*;
    Nothing much to pinpoint at...
    Our class
    Code:
    public class BallDemo extends JFrame implements ActionListener {
    Inheriting everything from JFrame as well using the interface ActionListener.
    Every item we come to need to create a simple interface...
    Code:
    private Ball b;
    	private JPanel panel = new JPanel();
    	private JMenuBar JMB = new JMenuBar();
    	private JMenu JM = new JMenu("Edit");
    	private JMenuItem[] JMI = new JMenuItem[3];
    	int r,sp;
    As we connect a bridge with ball, and gave it the name b, we will be able to reach every method(public or protected) and instances...
    Constructor
    Code:
    public  BallDemo() {
    		JMI[0] = new JMenuItem("Start");
    		JMI[1] = new JMenuItem("Stop");
    		JMI[2] = new JMenuItem("Close");
    		JMB.setSize(640, 20);
    		String s = showInputDialog(null,"Radie");
    		r = Integer.parseInt(s);
    		s = showInputDialog(null,"Speed");
    		sp = Integer.parseInt(s);
    		b = new Ball(r,sp,sp);
    		setLayout(null);
    		add(panel);
    		panel.setBackground(Color.white);
    		panel.setSize(640,440);
    		panel.setLocation(JMB.getX(), JMB.getY()+20);
    		b.setLocation(b.getWidth(), b.getHeight());
    		panel.setLayout(new BorderLayout());
    		panel.add(b);
    		add(JMB);
    		JMB.add(JM);
    		JM.add(JMI[0]);
    		JM.add(JMI[1]);
    		JM.add(JMI[2]);
    		setSize(640,480);
    		setResizable(false);
    		JMI[0].addActionListener(this);
    		JMI[1].addActionListener(this);
    		JMI[2].addActionListener(this);
    		setDefaultCloseOperation(EXIT_ON_CLOSE);
    		show();
    	}
    As we used an array for our buttons, we will have to define every position of the array with a new button option thus allowing us to give them proper name as well use them correct...
    Also, as I inheriting everything from JFrame, I can easily just use the methods without a reference variable.
    Methods calling the balls stop and start methods.
    Code:
    public void start() {
    		b.start();
    	}
    	public void stop() {
    		b.stop();
    	}
    Nothing much to see, just using our reference to grab .stop and .start for our simple bouncing ball.
    Our actionlistener...
    Code:
    public void actionPerformed(ActionEvent e) {
    		if(e.getSource()==JMI[0]) {
    				start();
    			
    		}
    		else if(e.getSource()==JMI[1]) {
    			stop();
    		}
    		else {
    			System.exit(0);
    		}
    	}
    As we used buttons, with names that supposedly do, so while pressing Start we activate our own method which activates the Balls method called start weird right?
    Main
    Code:
    public static void main(String[] arg) {
    		new BallDemo();
    	}
    	
    }
    So, just create a new BallDemo and run it while having fun !
    More or less this is it
    Grab it, evolve it or just reconstruct it the way you want it or, just make your own without looking at this one !

    Run it...
    Simple thread - Bouncing ball-run-.png
    Running...
    Simple thread - Bouncing ball-run-it2.png
    Stopping...
    Simple thread - Bouncing ball-run-it3.png
    Closing it...
    Simple thread - Bouncing ball-run-it4.png

    Cheers !

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Jordan Guest

    Re: Simple thread - Bouncing ball

    Neat! Well done, +rep!

  4. #3
    Join Date
    Jul 2008
    Location
    Somewhere that is shorter to write than "In the gloomy shadows of my personal namespace"
    Posts
    10,725
    Blog Entries
    2
    Rep Power
    90

    Re: Simple thread - Bouncing ball

    Neat! +rep
    Hey! Check out my new Toyota keyboaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

  5. #4
    Join Date
    May 2008
    Location
    Hell
    Posts
    3,852
    Blog Entries
    4
    Rep Power
    49

    Re: Simple thread - Bouncing ball

    Quote Originally Posted by Jordan View Post
    Neat! Well done, +rep!
    Thank you
    Quote Originally Posted by marwex89 View Post
    Neat! +rep
    Thank you too

  6. #5
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: Simple thread - Bouncing ball

    Nicely done, +rep
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  7. #6
    Join Date
    May 2008
    Location
    Hell
    Posts
    3,852
    Blog Entries
    4
    Rep Power
    49

    Re: Simple thread - Bouncing ball

    Quote Originally Posted by WingedPanther View Post
    Nicely done, +rep
    Haha thank you

  8. #7
    Join Date
    Aug 2007
    Location
    Gizeh, Al Jizah, Egypt, Egypt
    Posts
    8,675
    Blog Entries
    12
    Rep Power
    81

    Re: Simple thread - Bouncing ball

    nice one, was waiting for it !
    +rep
    yo homie i heard you like one-line codes so i put a one line code that evals a decrypted one line code that prints "i love one line codes"
    Code:
    eval(base64_decode("cHJpbnQgJ2kgbG92ZSBvbmUtbGluZSBjb2Rlcyc7"));
    www.amrosama.com | the unholy methods of javascript

  9. #8
    Join Date
    May 2008
    Location
    Hell
    Posts
    3,852
    Blog Entries
    4
    Rep Power
    49

    Re: Simple thread - Bouncing ball

    Quote Originally Posted by amrosama View Post
    nice one, was waiting for it !
    +rep
    Haha, sounds great

  10. #9
    Impulser's Avatar
    Impulser is offline Newbie
    Join Date
    Aug 2009
    Posts
    5
    Rep Power
    0

    Re: Simple thread - Bouncing ball

    Very nice explanation and overall a great tutorial. I learned I bit from this.

    Thanks.

  11. #10
    Join Date
    May 2008
    Location
    Hell
    Posts
    3,852
    Blog Entries
    4
    Rep Power
    49

    Re: Simple thread - Bouncing ball

    Quote Originally Posted by Impulser View Post
    Very nice explanation and overall a great tutorial. I learned I bit from this.

    Thanks.
    Great to know

+ Reply to Thread
Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 0
    Last Post: 06-22-2011, 09:57 PM
  2. Bouncing Image Help!
    By jsolutions in forum JavaScript and CSS
    Replies: 0
    Last Post: 06-05-2011, 12:04 PM
  3. Need help with a third ball in game.
    By vlan in forum Java Help
    Replies: 1
    Last Post: 06-05-2010, 10:41 AM
  4. Java Bouncing Balls Thread Problem
    By H2O Pure in forum Java Help
    Replies: 2
    Last Post: 05-24-2010, 03:08 AM
  5. JS Ball Trick
    By tachyon in forum JavaScript and CSS
    Replies: 1
    Last Post: 04-03-2010, 05:36 PM

Tags for this Thread

Bookmarks

Posting Permissions

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