Class Arkanoid

All Implemented Interfaces:
ActionListener, ImageObserver, MenuContainer, Serializable, EventListener, Accessible

public class Arkanoid extends JPanel implements ActionListener
An ultimate Barça-themed Arkanoid (Breakout) clone implemented as a Swing game.

This implementation features a professional physics engine for ball-paddle collisions, responsive keyboard and mouse controls, MIDI sound synthesis for 8-bit retro audio, particle systems, and multiple power-up items (multiball, wider paddle, and fireball).

It integrates a HierarchyListener to automatically clean up resources (stopping timers, closing synthesizers) when the panel is closed or removed from the screen.

Author:
anahata
See Also:
  • Field Details

    • width

      private final int width
      The width of the game arena in pixels.
      See Also:
    • height

      private final int height
      The height of the game arena in pixels.
      See Also:
    • paddleY

      private final double paddleY
      The fixed Y-coordinate position of the paddle.
      See Also:
    • paddleHeight

      private final double paddleHeight
      The height of the paddle in pixels.
      See Also:
    • stateMenu

      private final int stateMenu
      Game state: Main menu screen.
      See Also:
    • statePlaying

      private final int statePlaying
      Game state: Currently playing.
      See Also:
    • statePaused

      private final int statePaused
      Game state: Paused.
      See Also:
    • stateGameover

      private final int stateGameover
      Game state: Game over / lost.
      See Also:
    • stateVictory

      private final int stateVictory
      Game state: Victory / level cleared.
      See Also:
    • state

      private int state
      The active game state.
    • score

      private int score
      The player's current score (Gols).
    • lives

      private int lives
      The player's remaining lives.
    • level

      private int level
      The current game difficulty level.
    • paddleX

      private double paddleX
      The actual X-coordinate of the paddle's left edge.
    • targetPaddleX

      private double targetPaddleX
      The target X-coordinate of the paddle's left edge for smooth interpolation.
    • paddleWidth

      private double paddleWidth
      The current width of the paddle, which changes with power-ups.
    • balls

      private final ArrayList<Ball> balls
      Active balls in the game field.
    • bricks

      private final ArrayList<Brick> bricks
      Active bricks in the level.
    • powerUps

      private final ArrayList<PowerUp> powerUps
      Active dropping power-up items.
    • particles

      private final ArrayList<Particle> particles
      Visual particle effects.
    • widePaddleTimer

      private int widePaddleTimer
      Timer for wide-paddle power-up duration.
    • fireballTimer

      private int fireballTimer
      Timer for fireball power-up duration.
    • fireballActive

      private boolean fireballActive
      Flag indicating if the fireball power-up is active.
    • synth

      private Synthesizer synth
      MIDI Synthesizer used for retro sound generation.
    • channel

      private MidiChannel channel
      MIDI channel dedicated to game sound effects.
    • synthInitialized

      private boolean synthInitialized
      Flag indicating if MIDI system has been initialized.
    • timer

      private Timer timer
      The high-frequency game loop timer.
  • Constructor Details

    • Arkanoid

      public Arkanoid()
      Constructs a new Arkanoid game panel and registers listeners.
  • Method Details

    • initSynth

      private void initSynth()
      Initializes the MIDI sound system asynchronously.

      Synthesizer is acquired from MidiSystem and loaded with lead Square Lead program. Runs on an autonomous thread to avoid blocking the Event Dispatch Thread during startup.

    • playSound

      private void playSound(int note, int duration)
      Plays an 8-bit sound note on a separate thread.

      Runs asynchronously using an individual short-lived thread to preserve fluid game physics execution and UI performance.

      Parameters:
      note - The MIDI note to play (0-127).
      duration - The duration in milliseconds.
    • playMelody

      private void playMelody(int[] notes, int[] delays, int[] durations)
      Plays a sequence melody asynchronously.

      Plays multiple MIDI note events sequentially separated by duration delays.

      Parameters:
      notes - Array of MIDI notes.
      delays - Array of delays between notes.
      durations - Array of note play durations.
    • initLevel

      private void initLevel()
      Initializes the brick and ball layouts for the current level.

      Arranges the game bricks to represent a beautiful FCB layout: - The "F" and "B" letters are colored in deep Grana (Type 2), requiring 2 hits. - The "C" letter is colored in rich Azul/Blue (Type 3), requiring 3 hits. - The empty background space is filled with warm Light Yellow bricks (Type 1), requiring 1 hit.

    • resetFullGame

      private void resetFullGame()
      Resets score, lives, levels, and initializes the first stage.
    • cleanup

      public void cleanup()
      Cleans up running loops, timers, and MIDI synthesizer resources to prevent memory leaks.
    • handleKeyPress

      private void handleKeyPress(KeyEvent e)
      Handles key press inputs based on the current game state.
      Parameters:
      e - The key event.
    • launchBalls

      private void launchBalls()
      Releases resting balls from the paddle.
    • activatePowerUp

      private void activatePowerUp(int type)
      Activates a captured power-up.
      Parameters:
      type - The power-up type.
    • playVictoryFanfare

      private void playVictoryFanfare()
      Plays victory fanfare and generates celebration particles.
    • actionPerformed

      public void actionPerformed(ActionEvent e)

      Runs the high-frequency animation tick, updating positions, handling physics, checking wall, brick, and paddle collisions, and evaluating win/loss conditions.

      Specified by:
      actionPerformed in interface ActionListener
    • paintComponent

      protected void paintComponent(Graphics g)

      Draws the background arena, scoreboard, paddle, football, bricks, and overlays.

      Overrides:
      paintComponent in class JComponent
    • drawBackground

      private void drawBackground(Graphics2D g2, int w, int h)
      Draws the stadium background.
      Parameters:
      g2 - The graphics context.
      w - The viewport width.
      h - The viewport height.
    • drawPaddle

      private void drawPaddle(Graphics2D g2, double x, double y, double w, double h)
      Draws the striped Barça player paddle.
      Parameters:
      g2 - The graphics context.
      x - The paddle X position.
      y - The paddle Y position.
      w - The width of the paddle.
      h - The height of the paddle.
    • drawFootball

      private void drawFootball(Graphics2D g2, double x, double y, double r)
      Draws a football vector graphics ball.
      Parameters:
      g2 - The graphics context.
      x - The ball's center X coordinate.
      y - The ball's center Y coordinate.
      r - The radius of the ball.
    • drawBrick

      private void drawBrick(Graphics2D g2, Brick brick)
      Draws a Barça flag themed brick with proper highlight shine.

      Renders cracks on damaged bricks based on remaining hit points: - Azul/Blue bricks (Type 3) require 3 hits and show cracks on the 1st and 2nd hits. - Grana bricks (Type 2) require 2 hits and show cracks on the 1st hit. - Light Yellow background bricks (Type 1) require 1 hit and break instantly.

      Parameters:
      g2 - The graphics context.
      brick - The brick to render.
    • drawPowerUp

      private void drawPowerUp(Graphics2D g2, PowerUp pu)
      Draws floating power-up symbols.
      Parameters:
      g2 - The graphics context.
      pu - The power-up object to draw.
    • drawSplashOverlay

      private void drawSplashOverlay(Graphics2D g2, String title, String sub, String action)
      Renders overlays for splash, victory, or gameover states.
      Parameters:
      g2 - The graphics context.
      title - The primary overlay title.
      sub - The secondary subtitle text.
      action - The call-to-action prompt.
    • main

      public static void main(String[] args)
      Standalone main launcher for the Arkanoid game.
      Parameters:
      args - Command line arguments.