Results 1 to 6 of 6
  1. #1
    jikatz09 is offline Member
    Join Date
    Mar 2013
    Posts
    9
    Rep Power
    0

    Default Help with program that creates random raindrops that grow in size and dissapear

    Hey everyone. I am working on an assignment where we had to design a program that creates random raindrops. The drops are added every 500ms, grow 2 p every 10ms and cannot exceed 200p. There are two classes in this assignment, a Drop class, and RainPanel class that calls on methods in Drop. I am pretty much done with the assignment, but cannot figure out why the drops are not appearing. If yall could take a quick look I would really appreciate it. -Jon

  2. #2
    jikatz09 is offline Member
    Join Date
    Mar 2013
    Posts
    9
    Rep Power
    0

    Default Re: Help with program that creates random raindrops that grow in size and dissapear

    Java Code:
    // Jon Katz
    // Calls on drop class
    
    
    import java.awt.event.*;
    import java.awt.*;
    import java.util.Random;
    import java.util.ArrayList;
    import javax.swing.*;
    
    public class RainPanel extends JPanel implements ActionListener{
    
    	// Panel dimensions
    	private final int width = 500;
    	private final int height = 500;
    
    	// Create timer and Arraylist for raindrops
    	private Timer timer;
    	private ArrayList<Drop> raindrops;
    
    	// Constructor sets up size and background
    	public RainPanel() {
    		this.setPreferredSize(new Dimension(width, height));
    		this.setBackground(Color.blue);
    
    		raindrops = new ArrayList<Drop>();
    
    	}
    		
    	// This method will get called whenever the timer ticks
    	// Its job is to move the circle and repaint the panel
    	
    	public void actionPerformed(ActionEvent e) {
    		
    		Timer timer1 = new Timer(10, this);
    		timer.start();
    		
    		Timer timer2 = new Timer(500, this);
    		timer.start();
    		
    		// Differentiates ticks, Grow, Remove, and Add drops
    	
    			
    		if (e.getSource() == timer1) {
    			// Grow all drops
    			ArrayList<Drop> bigraindrops = new ArrayList<Drop>();
    		
    			for (int i=0; i<raindrops.size(); i++) {
    				raindrops.get(i).grow();
    				
    				// Remove large drops
    				
    				if (raindrops.get(i).isTooBig() == true) {
    					bigraindrops.add(raindrops.get(i));
    			
    			
    			}
    			raindrops.removeAll(bigraindrops);
    		}
    			repaint();
    	}	
    		else if (e.getSource() == timer2) {
    			// Add a drop
    			Random random = new Random();
    			Drop drop = new Drop(random.nextInt(500), random.nextInt(500));
    			raindrops.add(drop);
    		}
    
    		super.repaint(); // To make the changes visible
    	}
    
    
    		Random random = new Random();
    		
    		int rand = random.nextInt(100);
    
    	
    	// Display the panel inside a frame
    	public static void main(String[] args) {
    		
    		Random random = new Random();
    		
    		JFrame frame = new JFrame("Raindrops");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		
    		RainPanel panel1 = new RainPanel();
    		
    		JPanel panel = new JPanel();
    		panel.add(panel1);
    		frame.getContentPane().add(panel);
    		
    		frame.pack();
    		frame.setVisible(true);
    	}
    
    
    }

  3. #3
    jikatz09 is offline Member
    Join Date
    Mar 2013
    Posts
    9
    Rep Power
    0

    Default Re: Help with program that creates random raindrops that grow in size and dissapear

    Java Code:
    // Jon Katz
    // Drop class
    
    import java.awt.*;
    import java.util.Random;
    
    public class Drop {
    
    	Random random = new Random();
    	
    	// Creating variables
    	private int centerX;
    	private int centerY;
    	private int diameter = 1;
    	private static Color color = Color.white;
    	private static int maxDiameter = 200;
    	
    	// Creates raindrops
    	public Drop(int x, int y) {
    		centerX = x;
    		centerY = y;
    	}
    	
    	// Makes circles larger
    	public void grow() {
    		diameter = diameter + 2;
    	}
    	
    	// Decides whether or not raindrop is too large
    	public boolean isTooBig() {
    		if (diameter > maxDiameter)
    			return true;
    		else
    			return false;
    	}
    	
    	// Draw on the page
    	public void draw(Graphics page) {
    		page.setColor(color);
    		page.drawOval(centerX-(diameter/2), centerY-(diameter/2), diameter, diameter);
    	}
    }

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: Help with program that creates random raindrops that grow in size and dissapear

    It appears the bulk of your code is inside the actionPerformed method. What event fires this method?

  5. #5
    jikatz09 is offline Member
    Join Date
    Mar 2013
    Posts
    9
    Rep Power
    0

    Default Re: Help with program that creates random raindrops that grow in size and dissapear

    The timers are supposed to fire the method. Should they come before the actionListen method?

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: Help with program that creates random raindrops that grow in size and dissapear

    You create the Timer objects inside the actionPerformed method. They will never be created if you never enter the actionPerformed method.

Similar Threads

  1. Random size of JComponent
    By Karl-von-bahnhof in forum AWT / Swing
    Replies: 0
    Last Post: 04-13-2010, 08:14 PM
  2. Need help with program size.
    By endtojunc in forum New To Java
    Replies: 1
    Last Post: 01-11-2010, 03:33 AM
  3. How to make my snake grow?
    By AndersBjörnör in forum AWT / Swing
    Replies: 20
    Last Post: 11-15-2009, 02:47 PM
  4. How to grow an array
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-14-2008, 08:49 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
  •