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 8 of 8

Thread: mainBall object wont update

  1. #1
    Junior Member
    Join Date
    Aug 2012
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default mainBall object wont update

    ive created an object called mainBall which is just a little black ball that wont change position on the screen. the coordinates of the ball change but the actual ball itself wont change position and i have no idea why

    import java.applet.Applet;
    import java.awt.Color;
    import java.awt.Component;
    import java.awt.Event;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.Rectangle;
    import java.util.ArrayList;
    import java.util.concurrent.TimeUnit;
     
    public class game extends Applet implements Runnable
    {
        static final int WIDTH = 450;
        static final int HEIGHT =  450;
        private Image dbImage;
        private Graphics dbg;
     
        public static long NEW_DOT_FREQ = TimeUnit.SECONDS.toMillis(3);
        public long lastUpdateTime;
        public long timeSinceLastNewDot;
     
     
        public ArrayList<Ball> BALLS;
     
        Color[] color = {Color.red, Color.blue, Color.green, Color.yellow, Color.magenta, Color.black};
        int colorIndex;
     
        static final int NUM_OF_BALLS = 4;  
     
        int i;
        int t;
     
        MainBall mainBall = new MainBall(100, 100, 10, 10, 100, 100, 0, 0);
     
        Thread updateTime = new updateTime();
     
        public void start()
        {
        	lastUpdateTime = System.currentTimeMillis();
     
            Thread th = new Thread(this);
            th.start();//start main game  
     
            updateTime.start();
        }
     
        public void updateGame()
        {
            //Get the current time
            long currentTime = System.currentTimeMillis();
            //Calculate how much time has passed since the last update
            long elapsedTime = currentTime - lastUpdateTime;
            //Store this as the most recent update time
            lastUpdateTime = currentTime;
     
            //Create a new dot if enough time has passed
            //Update the time since last new dot was drawn
            timeSinceLastNewDot += elapsedTime;
     
            if (timeSinceLastNewDot >= NEW_DOT_FREQ)
            {
                int newX = randomNumber();
                int newY = randomNumber();
     
                debugPrint("New dot created at x:" + newX + ", y:" + newY + ".");
     
                BALLS.add(new Ball(newX, newY, 20, 20));
     
                timeSinceLastNewDot = 0;
            }
        }
     
        private void debugPrint(String value)
        {
            System.out.println(value);
        }
     
        public class updateTime extends Thread implements Runnable
        {
            public void run()
            {
                for(t = 0; ; t++)
                {
                    try
                    {
                        Thread.sleep(1000);
                    }
                    catch(InterruptedException e){}
                }
            }
        }
     
        public int randomNumber()
        {
            return (int)(Math.random() * 400);
        }
     
        public int getRandomColor()
        {
        	return (int)(Math.random() * 6);
        }
     
        public class MainBall
        {
        	int x;
        	int y;
        	int width;
        	int height;
            int xpos;
            int ypos;
            int xspeed;
            int yspeed;
     
        	public MainBall(int x, int y, int width, int height, int xpos, int ypos, int xspeed, int yspeed)
        		{
        	        this.x = 100;
        	        this.y = 100;
        	        this.width = 10;
        	        this.height = 10;
        	        this.xpos = 100;
        	        this.ypos = 100;
        	        this.xspeed = 0;
        	        this.yspeed = 0;
        	    }
     
        	public void paintMainBall(Graphics g)
        	{
        		g.setColor(Color.black);
        		g.fillOval(x, y, width, height);
        	    g.drawString(xpos + ", " + ypos, 20, 40);      
        	}	
        }//mainBall
     
        class Ball
        {
            int x;
            int y;
            int width;
            int height;    
     
            public Ball(int x, int y, int width, int height)
            {        
                 this.x = x;
                 this.y = y;
                 this.width = width;
                 this.height = height;
            }//end ball
     
            public void paint(Graphics g)
            {
                    g.setColor(color[getRandomColor()]);
                    g.fillOval(x, y, width, height);
            }                    //end paint
     
     
        }                    //ball class
     
        public void update(Graphics g)                    //double buffer don't touch!!
        {
            if(dbImage == null)
            {
                dbImage = createImage(this.getSize().width, this.getSize().height);
                dbg = dbImage.getGraphics();
            }
     
            dbg.setColor(getBackground());
            dbg.fillRect(0, 0, this.getSize().width, this.getSize().height);
     
            dbg.setColor(getForeground());
            paint(dbg);
     
            g.drawImage(dbImage, 0, 0, this);
        }
     
        public boolean keyDown (Event e, int key)
        {
            if(key == Event.LEFT)
            {
                mainBall.xspeed = -5;
                mainBall.yspeed = 0;
            }
     
            if(key == Event.RIGHT)
            {
                mainBall.xspeed = 5;
                mainBall.yspeed = 0;
            }
     
            if(key == Event.UP)
            {
                mainBall.yspeed = -5;
                mainBall.xspeed = 0;
            }
     
            if(key == Event.DOWN)
            {
                mainBall.yspeed = 5;
                mainBall.xspeed = 0;
            }
            return true;
        }
     
        public void run()
        {
            while(true)
            {
                repaint();
     
                if (mainBall.xpos < 1)
                {
                    mainBall.xpos = 449;
                }
     
                if (mainBall.xpos > 449)
                {
                    mainBall.xpos = 1;
                }
                if (mainBall.ypos < 1)
                {
                    mainBall.ypos = 449;
                }
     
                if (mainBall.ypos > 449)
                {
                    mainBall.ypos = 1;
                }
                mainBall.ypos += mainBall.yspeed;
                mainBall.xpos += mainBall.xspeed;
                try
                {
                    Thread.sleep(20);
                }
                catch(InterruptedException ex){}
            }
          } 
     
        public void init()
        {        
            this.setSize(WIDTH, HEIGHT);
     
            BALLS = new ArrayList<Ball>();
        }
     
        public void paint(Graphics g)
        {    
     
            g.drawString("time: " + t, 20, 20);
     
            mainBall.paintMainBall(g);
     
            for (Ball ball : BALLS)
            {
                ball.paint(g);
            }
     
            updateGame();
        }
     
     
    }
    Last edited by burntcasadilla; August 12th, 2012 at 07:08 PM. Reason: changed the code, still doesnt work


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,645
    Thanks
    45
    Thanked 1,959 Times in 1,933 Posts

    Default Re: mainBall object wont update

    the actual ball itself wont change position
    Where does the code change the location of the ball?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Aug 2012
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: mainBall object wont update

    the paintmainball method

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,645
    Thanks
    45
    Thanked 1,959 Times in 1,933 Posts

    Default Re: mainBall object wont update

    The paintmainball method does not change the values of any variables. What variables control the balls location? Where are those variables changed?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Aug 2012
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: mainBall object wont update

    oh i figured it out lol. i changed the values in paintMainBall to xpos and ypos.

    another unrelated question tho... how do i use the getBounds method? i have an object fixedBall at 250, 250 with height 20 and width 20.
    i know it should look something like this

    public Rectangle getBounds()
    {

    }

    but what would i put in the brackets in order to return the rectangle surrounding the fixedBall object?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,645
    Thanks
    45
    Thanked 1,959 Times in 1,933 Posts

    Default Re: mainBall object wont update

    Define a Rectangle object at the x,y location and size you want it to be using a new statement, assign it to a variable and return the variable (or return what was created by the new statement directly).
    If you don't understand my answer, don't ignore it, ask a question.

  7. The Following User Says Thank You to Norm For This Useful Post:

    burntcasadilla (August 12th, 2012)

  8. #7
    Junior Member
    Join Date
    Aug 2012
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: mainBall object wont update

    alright. now i have this but it does absolutely nothing lol

        public Rectangle getFixedBallBounds()
        {
        	return new Rectangle (250, 250, 20, 20);
        }
     
     
        public Rectangle getMainBallBounds()
        {
        	return new Rectangle(mainBall.xpos, mainBall.ypos, 10, 10); 	
        }
     
     
        public boolean checkCollision()
        {
        	if(mainBallRectangle.intersects(fixedBallRectangle))
        	{
        		mainBall.xspeed = 0;
        		mainBall.yspeed = 0;
        		mainBall.xpos = 100;
        		mainBall.ypos = 100;
        	}
    		return false;
        }

  9. #8
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 262 Times in 231 Posts

    Default Re: mainBall object wont update

    getFixedBallBounds() returns a new rectangle
    getMainBallBounds() returns a new rectangle, I can only assume mainBall.xpos and mainBall.ypos are valid values for your use. Its possible the rectangle is drawn out of sight.
    checkCollision() always returns false, not quite sure this is your desired operation of this method

Similar Threads

  1. Need help my values wont return
    By sajeed in forum What's Wrong With My Code?
    Replies: 6
    Last Post: January 7th, 2012, 07:46 AM
  2. WHY WONT THIS COMPILE!
    By usmc0311 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 12th, 2011, 02:42 PM
  3. [SOLVED] Help with if statement that wont act right...
    By deathpk in forum Loops & Control Statements
    Replies: 2
    Last Post: October 10th, 2011, 02:35 PM
  4. Reading from ResultSet to Object and from object Object Array
    By anmaston in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 7th, 2011, 06:11 AM
  5. pictures wont load
    By wolfgar in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 14th, 2010, 09:34 AM