Results 1 to 2 of 2
  1. #1
    Majeh is offline Member
    Join Date
    Feb 2011
    Posts
    8
    Rep Power
    0

    Default Need help on Multiple Moving Shapes

    As the title says I need some help trying to figure out how to make multiple shapes that move independently of each other. I managed to make a program that can make one shape move around after spending an hour or two looking up how to use the Thread.sleep() and repaint(). Anyway here's what I got.

    Java Code:
    import java.awt.Graphics;
    import javax.swing.JPanel;
    import java.awt.*;
    import java.util.Random;
    
    public class Shapes extends JPanel implements Runnable
    {
    
        Random myR = new Random();
        int x_pos;
        int y_pos;
        int x_width;
        int y_height;
        int right=5;
        int left= -5;
        int up=-5;
        int down = 5;
        int width, height;
        boolean goDown, goRight;
    
        public Shapes(int x, int y, int xw, int yh)
        {
             x_pos = x;
             y_pos = y;
             x_width = xw;
             y_height = yh;
        }
    
        public void init()
        {
    
        }
    
        public void start ()
        {
    
            Thread th = new Thread (this);
            th.start ();
        }
    
        @Override
        public void paintComponent( Graphics g )
        {
            super.paintComponent( g );
    
            g.setColor(Color.BLUE);
            g.fillRect(0, 0, getWidth(), getHeight());
            g.setColor(new Color(myR.nextInt(255), myR.nextInt(255), myR.nextInt(255)));
            g.fillOval(x_pos, y_pos, x_width, y_height);
    
        }
    
        public void drawShape (int nx, int ny)
        {
            x_pos= nx;
            y_pos= ny;
            repaint();
        }
    
        public void run ()
        {
    
            goRight = true;
            goDown = true;
    
            while (true)
            {
    
                moveShape();
                drawShape(x_pos, y_pos);
                repaint();
    
                try
                {
                    Thread.sleep (25);
                }
                catch (InterruptedException ex)
                {
                        // do nothing
                }
    
            }
            
        }
    
        public void moveShape()
        {
    
            if(goRight)
            {
                x_pos += right;
                if (x_pos >= (getWidth() - x_width))
                    goRight = false;
            }
            else
            {
                x_pos += left;
                if ( x_pos <= 0)
                    goRight =  true;
            }
            if(goDown)
            {
                y_pos += down;
                if(y_pos >= getHeight()-y_height)
                    goDown = false;
            }
            else
            {
                y_pos += up;
                if(y_pos <= 0)
                    goDown = true;
            }
    
        }
        
    }
    Any suggestions on how I would go about this with what I got so far?
    Last edited by Majeh; 03-18-2011 at 11:37 PM.

  2. #2
    Majeh is offline Member
    Join Date
    Feb 2011
    Posts
    8
    Rep Power
    0

Similar Threads

  1. randomly moving shapes
    By marlon in forum New To Java
    Replies: 3
    Last Post: 10-05-2012, 10:37 AM
  2. Moving java shapes with mouse and buttons
    By Haraldjjones in forum New To Java
    Replies: 1
    Last Post: 01-20-2011, 10:26 PM
  3. java shapes
    By Kyle227 in forum New To Java
    Replies: 7
    Last Post: 05-20-2010, 01:21 AM
  4. Inserting multiple shapes using mouseListener
    By thayalan in forum Java 2D
    Replies: 1
    Last Post: 03-14-2009, 03:43 PM
  5. Colors and shapes.
    By Torgero in forum New To Java
    Replies: 14
    Last Post: 10-13-2008, 06:25 PM

Posting Permissions

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