Fireworks in swing

firework panel

firework panel

Want to show firework in swing panel, here it is a way to show something like above picture in java swing. It is done by extending JPanel class and overriding its paintComponent method.  To know what it exactly looks like, give it a try and run once. Let me know your feedback. 🙂

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Stroke;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import javax.swing.JPanel;
import javax.swing.Timer;

 * Panel in which you can see fireworks.
public class FireworkPanel extends JPanel {

private static final long serialVersionUID = 5053650234847311814L;
 private Timer timer;
 private static final int DELAY = 10, DIVIDER = 180, MULTIPLY_FACTOR = 36, LINE_LENGTH = 2, FIREWORK_RADIUS = 75;
 private static final int ARRAY_LENGTH = 5;
 private static int X_CENTER[] = new int[ARRAY_LENGTH], Y_CENTER[] = new int[ARRAY_LENGTH];
 private static Color colors[] = new Color[ARRAY_LENGTH];
 private static final double PI = 3.14159;
 int x[] = new int[10],y[] = new int[10];
 private int x1, moveX, index, color_index;
 List<Integer> xx = new ArrayList<Integer>();
 List<Integer> yy = new ArrayList<Integer>();

public FireworkPanel() {
      x1 = index = color_index = 0;
      moveX = 3;
      timer = new Timer(DELAY, new MyChangeListener());





      colors[0] = Color.ORANGE;
      colors[1] = Color.BLUE;
      colors[2] = Color.CYAN;
      colors[3] = Color.RED;
      colors[4] = Color.PINK;


protected void paintComponent(Graphics g) {
     Graphics2D graphics2d = (Graphics2D) g;
     Stroke stroke = new BasicStroke(3, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, 5, new float[]{9},  0);
     for (int i = 0; i < xx.size(); i++) {

class MyChangeListener implements ActionListener {
public void actionPerformed(ActionEvent arg0) {
     x1 += moveX;
     if (x1 == 0 || x1 >= FIREWORK_RADIUS) {
          Random random = new Random();
          index = random.nextInt(ARRAY_LENGTH);
          color_index = random.nextInt(ARRAY_LENGTH);
     for (int i = 0; i < 10; i++) {
         xx.add((int) (X_CENTER[index] + x1 * Math.cos((MULTIPLY_FACTOR * i * PI) / DIVIDER)));
         yy.add((int) (Y_CENTER[index] + x1 * Math.sin((MULTIPLY_FACTOR * i * PI) / DIVIDER)));

And here is how to test it:

 JFrame frame = new JFrame("Fireworks");
 frame.setSize(400, 425);
 frame.setLayout(new BorderLayout());
 JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER));
 JLabel jLabel = new JLabel("Fireworks By Harry Joy");
 jLabel.setFont(new Font("Tahoma", Font.PLAIN, 14));
 frame.getContentPane().add(panel, BorderLayout.NORTH);
 frame.getContentPane().add(new FireworkPanel(), BorderLayout.CENTER);

Hope you like it. 😀

About harryjoy
A passionate self-taught software developer. Currently working as Lead Software Engineer in an IT company.

One Response to Fireworks in swing

  1. Pingback: JavaPins

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: