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

Thread: For Loop Delay Help, Visibility Manipulation Assistance

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Location
    Utah
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default For Loop Delay Help, Visibility Manipulation Assistance

    Hey All!
    I am a beginner programmer (actually am technically not even a programmer, I am a designer). My major is multimedia so I am a bit of a jack of all trades, thus, am required to learn how to manipulate basic code so that I will be able to better communicate with any real programmers I encounter and work with on the job! Our final project was to create a simple program of our own. Mine is below, but I have 2 problems I can't seem to solve. They are:

    1. Slowing down the "movement" of the eggs.

    2. Hiding the "busted egg" components until all prior for loops for the whole eggs have executed their movements down their respective ramps

    If any of you can solve ANY 1 of these issues, I will be forever grateful. I have been trying for days with no luck. Here is my code:

    import java.awt.Color;
    import java.awt.Graphics;
    import javax.swing.JPanel;
    import javax.swing.Timer;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import java.util.Random;
     
    public class finalB extends JPanel
    {
     
     //integer variable setup
    	int x, y, x1, y1, i, del; // these are all defined as integers  
     
    	// display library class-- load premade java component code
       public void paintComponent( Graphics g )
       {
          super.paintComponent( g ); //call superclass' paint method from library
     
    		this.setBackground( Color.BLACK );
     
    		//draw walls
    		g.setColor( Color.BLUE );
    		g.fillRoundRect( 5, 5, 5, 400, 2, 2 ); //(x, y, w, l, curvature of rounds x2) LEFT WALL
    		g.fillRoundRect( 165, 5, 5, 400, 2, 2 ); // RIGHT WALL
    		g.fillRoundRect( 3, 552, 168, 5, 2, 2 ); // BOTTOM FLOOR
     
    		//draw ramps
    		g.setColor( Color.CYAN );
    		g.drawLine( 18, 40, 118, 55 ); //first 2 integers are 1st point, second 2 are 2nd point
    		g.drawLine( 60, 110, 157, 95 );
    		g.drawLine( 18, 155, 118, 175 );
    		g.drawLine( 60, 235, 157, 220 );
    		g.drawLine( 18, 285, 118, 300 );
    		g.drawLine( 60, 360, 157, 345 );
     
    		//start 1st white egg's for loop here - MOVES 1st ramp's eggs each iteration according to the math in the loop. giving it a less than 10 definition: means it will repeat until i has been made 9 times.
    			for ( i=0; i<10; i++ ){
     
    			g.setColor( Color.WHITE );
    			g.fillOval( 20 + i*10, 16, 18, 23 ); //x, y, w, h - FIRST egg position, this MAKES eggs (i*??)
    			//g.fillOval( 110, 33, 23, 18 ); // - TENTH egg's position; right now this line is not doing anything, it's just here to show about where the 1st egg should stop
     
    			//1st BLACK eggs - these hide the white ones to create the illusion of 1 egg moving
    			g.setColor( Color.BLACK );
    			g.fillOval( 23 + i*10, 16, 23, 18 );
    			//g.fillOval( 110, 33, 23, 18 ); // - final 1st egg's supposed position		
    		} //end 1st ramp's eggs for loop
     
    			//2nd eggs' for loop here
    			for ( i=10; i>0; i-- ){
     
    			g.setColor( Color.WHITE );
    			g.fillOval( 59 + i*8, 76, 23, 18 ); 
     
    			//2nd BLACK eggs
    			g.setColor( Color.BLACK );
    			g.fillOval( 61 + i*4, 76, 18, 23 ); //NOTE: this egg isn't in the right spot, I will have to change it later to start at the right end of the ramp instead of the left.
    		} //end 2nd ramp's eggs' for loop
     
    			//3rd white egg's for loop here.
    			for ( i=0; i<10; i++ ){
     
    			g.setColor( Color.WHITE );
    			g.fillOval( 16 + i*10, 131, 18, 23 ); 
     
    			//3rd BLACK eggs
    			g.setColor( Color.BLACK );
    			g.fillOval( 19 + i*10, 131, 23, 18 );
    		} //end 3rd ramp's eggs' for loop
     
    			//4th eggs' for loop here
    			for ( i=10; i>0; i-- ){
     
    			g.setColor( Color.WHITE );
    			g.fillOval( 59 + i*8, 201, 23, 18 ); 
     
    			//4th BLACK eggs
    			g.setColor( Color.BLACK );
    			g.fillOval( 61 + i*4, 201, 18, 23 );
    		} //end 4th ramp's eggs' for loop
     
    			//5th white egg's for loop here.
    			for ( i=0; i<10; i++ ){
     
    			g.setColor( Color.WHITE );
    			g.fillOval( 16 + i*10, 262, 18, 23 ); 
     
    			//5th BLACK eggs
    			g.setColor( Color.BLACK );
    			g.fillOval( 19 + i*10, 262, 23, 18);
    		} //end 5th ramp's eggs' for loop
     
    			//6th eggs' for loop here
    			for ( i=10; i>0; i-- ){
     
    			g.setColor( Color.WHITE );
    			g.fillOval( 59 + i*8, 327, 23, 18 ); 
     
    			//6th BLACK eggs
    			g.setColor( Color.BLACK );
    			g.fillOval( 61 + i*4, 327, 18, 23 );
    		} //end 6th ramp's eggs' for loop
     
    		//"~~~" find a way to have all the 'busted egg' bits below not appear until the last "whole" black eggs are done moving down the final (5th) ramp
     
    		//busted egg
    		g.setColor( Color.WHITE );
    		g.fillOval( 59, 527, 18, 23 ); //main chunk
    		g.fillRect( 42, 478, 5, 3 ); //x, y, w, h - little bits start
    		g.fillRect( 80, 491, 3, 4 );
    		g.fillOval( 130, 500, 4, 3 );
    		g.fillOval( 140, 470, 3, 4 );
    		g.fillOval( 54, 482, 4, 4 );
     
    		//busted egg yolk
                    g.setColor( Color.YELLOW );
    		g.fillRoundRect( 20, 500, 3, 3, 1, 1 ); //(x, y, w, l, curvature of rounds x2)
    		g.fillRect( 50, 484, 3, 3 ); //x, y, w, h
    		g.fillRect( 85, 492, 3, 4 );
    		g.fillOval( 100, 505, 3, 3 );
    		g.fillOval( 130, 460, 3, 4 );
    		g.fillOval( 54, 472, 4, 4 );
     
    		//busted egg shaping (hiding bits of colored shapes previously made)
    		g.setColor( Color.BLACK );
    		g.fillRect( 62, 525, 13, 16 ); //x, y, w, l
    		g.fillRect( 60, 535, 3, 4 );
    		g.fillRect( 74, 535, 3, 4 );
    		g.fillRect( 57, 529, 23, 8 );
    		g.setColor( Color.YELLOW );
    		g.fillRoundRect( 63, 540, 10, 3, 1, 1 );
     
       } // end method main
    } // end class finalB

    --- Update ---

    Here is the sister file that actually runs the finalB file.
     
    //creation of frame and execution of program - the run file 
     
    import java.awt.Color;
    import javax.swing.JFrame;
     
    public class finalA
    {
       // execute application - run this one
    	// This is like the HTML to your CSS file. It sets up your applet window.
       public static void main( String[] args )
       {
          //create frame for LinesRectsOvalsJPanel
    		JFrame frame =
    			new JFrame( "Rolling Egg" );
    			frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
     
    			finalB finBInsta = 
    				new finalB();
    			finBInsta.setBackground( Color.BLACK );
    			frame.add( finBInsta ); //add panel to frame
    			frame.setSize( 191, 600 ); // set frame size w, h
    			frame.setVisible( true ); //display frame
     
    		   } // end main
    } // end class finalA

    Also, integers x1, y1, and del are all being unused currently. I added them to remind myself I might be able to use them to create a delay (del), and to move the eggs downward as they are made as well (x1, y1).
    Last edited by 3r1c4; April 30th, 2013 at 11:37 PM. Reason: I changed the code.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: For Loop Delay Help, Visibility Manipulation Assistance

    The compiler expects a numeric value, followed by a ,
    133 i*10 is not a valid value. there needs to be an operator after the 133 that connects the two parts to make a valid numeric value
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Apr 2013
    Location
    Utah
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: For Loop Delay Help, Visibility Manipulation Assistance

    Thank you! That problem was just me not noticing I'd forgotten a "+" sign between the x value and the i*10! Now I need help delaying the movement of the eggs.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: For Loop Delay Help, Visibility Manipulation Assistance

    help delaying the movement of the eggs.
    What does "delaying the movement" mean? Can you describe how the eggs are moving and what it would mean to "delay" the movement?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Apr 2013
    Location
    Utah
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Wink Re: For Loop Delay Help, Visibility Manipulation Assistance

    Quote Originally Posted by Norm View Post
    What does "delaying the movement" mean? Can you describe how the eggs are moving and what it would mean to "delay" the movement?
    Delaying the movement means slowing down how quickly the for loops create and or place each egg. Their "movement" is how they appear to move across each ramp (or at least I am trying to get it to look like they're moving-- right now the program just makes them all so fast it looks like a plain old row of eggs).

    for ( i=0; i<10; i++ ){
    	g.setColor( Color.WHITE );
    	g.fillOval( 20 + i*10, 16, 18, 23 );
    }

    Thank you for being patient with me!

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: For Loop Delay Help, Visibility Manipulation Assistance

    To change the location slowly over time, the code will need to use a Timer. The Timer's method will compute the next position of the egg and call the repaint() method which will cause the paintComponent() method to be called where the egg will be displayed at the new position.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Question - Delay
    By Vincent G in forum Java Theory & Questions
    Replies: 1
    Last Post: February 9th, 2013, 10:42 AM
  2. [SOLVED] for loop assistance
    By Kseidel in forum Loops & Control Statements
    Replies: 3
    Last Post: September 17th, 2012, 08:10 PM
  3. 1 packet delay with inputstream
    By tobi06 in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: October 27th, 2011, 04:53 AM
  4. Delay.
    By Tjstretch in forum Java Theory & Questions
    Replies: 1
    Last Post: October 21st, 2010, 11:18 AM
  5. String manipulation help
    By Duff in forum Loops & Control Statements
    Replies: 7
    Last Post: March 19th, 2010, 04:02 AM

Tags for this Thread