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); } }