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

Thread: Raindrop animation, almost done but the program form any of the raindrops I have programmed.

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Raindrop animation, almost done but the program form any of the raindrops I have programmed.

    Hey everyone here is an assignment that creates raindrops every 500ms, grows them 2p every 10ms, and removes them after they have exceeded 200p. I am almost done with the program, but I can't get the drops to form. I cannot tell what is wrong with my code. Any help would be appreciated.

    // Jon Katz
    // Calls on drop class and draws the decal
     
     
    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;
    	private int delay10ms = 10;
    	private int delay500ms = 500;
     
    	// Create timer and Arraylist for raindrops
    	private Timer timer1;
    	private Timer timer2;
    	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>();
     
    		Timer timer1 = new Timer(delay10ms, this);
    		timer1.start();
     
    		Timer timer2 = new Timer(delay500ms, this);
    		timer2.start();
     
    	}
     
    	// 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) {
     
    		// Differentiates ticks, Grow, Remove, and Add drops			
    		if (e.getSource() == timer1) {
    			// Grow all drops
     
    			for (int i=0; i<raindrops.size(); i++) {
    				raindrops.get(i).grow();
     
    				// Remove large drops
    				ArrayList<Drop> bigraindrops = new ArrayList<Drop>();
    				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) {
     
    		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);
    	}
     
     
    }


    --- Update ---

    // Jon Katz
    // Drop class
     
    import java.awt.*;
     
     
    public class Drop {
     
     
    	// 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);
    	}
    }


  2. #2
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default Re: Raindrop animation, almost done but the program form any of the raindrops I have programmed.

    There are two problems that i see right off, then you'll have to work at it and come back when you hit your next road block.

    1. timer1 and timer2 are declared twice at two different scopes. The actionPerformed function is referencing the global timer1 and timer2 while the ones that called them are redeclared in the RainPanel constructor. These are now two different object. To fix that just remove the declaration before timer1 and timer 2 in the constructor so they reference the global variables.

    2. You are never calling draw() for the drops. You have to draw them to get them on the screen.
    Writing code is your job, helping you fix and understand it is mine.

    <-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!

Similar Threads

  1. been many years since I programmed in java
    By chicken_council in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 11th, 2011, 06:29 AM
  2. Is there a way I could create/get a program that outputs a form in BBC?
    By Elite Cow in forum Java Theory & Questions
    Replies: 5
    Last Post: October 11th, 2011, 01:01 PM
  3. Inserting pre programmed UI elements
    By luisp88 in forum AWT / Java Swing
    Replies: 2
    Last Post: September 20th, 2011, 12:56 PM
  4. Blender file with animation, how to import OBJ(w/ animation) into Java?
    By cr80expert5 in forum Object Oriented Programming
    Replies: 0
    Last Post: May 12th, 2011, 03:11 AM
  5. Java Swing :Back Button from one form to another form
    By srinivasan_253642 in forum AWT / Java Swing
    Replies: 1
    Last Post: December 26th, 2009, 09:51 AM