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

Thread: array of drawn objects

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Location
    Netherlands, zuid-holland
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default array of drawn objects

    heey people,

    i'm trying to write myself a program which simulates snow falling.

    i already created a decent random moving generator and made the snowflake respawn if it gets out of screen.

    however i can't seem to find a way to generate more as 1 snowflake to spawn at the same time without have to write code for 50 separate flakes.

    is there some way i could use an array or a similar method to recreate a drawn object for a preset or random amount of times?

    thanks in advance, l.oomen.

    if necessary, here is the original code:

    Snowflake.pdf


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

    Default Re: array of drawn objects

    Hi l.oomen and welcome to the forum.
    Please paste the code to the forum rather than an attachment.




    is there some way
    Sure there is. Do you have a plan yet? What have you tried? Where are you stuck?

  3. #3
    Junior Member
    Join Date
    Oct 2012
    Location
    Netherlands, zuid-holland
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: array of drawn objects

    yeah sorry, i saw how to input code after i posted my problem.

    here is the code again, this time in correct position:

     
    	public ValConfPaneel()
     
    	{
     
    		Timer clock = new Timer( 100 , this );
    		clock.start();
     
    	}
     
            public void paintComponent( Graphics g )
     
    	{
     
    		super.paintComponent( g );
     
                    g.fillRect( 0 , 0 , getWidth() , getHeight() );
    		g.setColor( new Color( 255 , 255 , 255 ) );
    		g.fillOval( x + movement , 0 + y , 20 , 20 );
     
     
    	}
     
    	public void bereken()
     
    	{
     
    		movement += randomMovement.nextInt( 21 ) - 11;
    		y += 20;
     
    		if ( y > getHeight() )
     
    		{
     
    			y = 0;
    			x = randomStart.nextInt( getWidth() );
     
     
    		}
     
    		if ( x > getWidth() )
     
    		{
     
    			x = 0;
     
    		}
     
    	}
     
    	@Override
    	public void actionPerformed( ActionEvent e )
    	{
     
    		bereken();
    		repaint();
     
    	}

    this time with more detailed instructions on what i'm trying to achieve and the attempts already made.

    the goal of my application is that it creates a multiple amount of objects that move downwards at a previous set speed, with randomised horizontal movement (snowflakes).

    the graphics of the object are of later concern, first i have to tackle my problem with generating multiple objects.

    i tried using for/while loops in the paintComponent, which mostly ended in exceptions or creating objects that had the same horizontal position and/or the same randomised movement.

    i tried making arrays for different x-coördinates and different random movements, which 9/10 times ended in exceptions or problems with the declarations and reading of the arrays.

    i attempted every method, algorithm and combinations i learned so far but i'm really stuck on this one.

    if you have some hints or tips on this matter, please let me know.

    with kind regards, l.oomen

Similar Threads

  1. Array of objects
    By spark in forum Object Oriented Programming
    Replies: 2
    Last Post: August 1st, 2012, 03:06 PM
  2. Initializing an Array of Objects
    By Destro in forum Object Oriented Programming
    Replies: 1
    Last Post: April 20th, 2012, 06:25 AM
  3. Loop through a 2d array of objects
    By ssjg0ten5 in forum Loops & Control Statements
    Replies: 1
    Last Post: March 28th, 2012, 09:53 PM
  4. all objects in array the same?
    By abrohm in forum What's Wrong With My Code?
    Replies: 6
    Last Post: June 17th, 2011, 11:21 AM
  5. [SOLVED] Creation of objects of Array in Java
    By sadi_shihab in forum Collections and Generics
    Replies: 4
    Last Post: July 9th, 2009, 01:38 PM