Skip to content

Instantly share code, notes, and snippets.

@Chaitanya-Pratap-Singh
Last active November 1, 2021 07:46
Show Gist options
  • Save Chaitanya-Pratap-Singh/8216525b3d59e7df2185e787dfa99a8b to your computer and use it in GitHub Desktop.
Save Chaitanya-Pratap-Singh/8216525b3d59e7df2185e787dfa99a8b to your computer and use it in GitHub Desktop.
This is a simple java program for buzzer quizzes
package com.company;
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.sound.sampled.*;
import javax.sound.sampled.spi.*;
class Audio
// Download the audio file at : https://icedrive.net/0/f795m469Cd
{
public static void red()
{
try {
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File("sound.wav").getAbsoluteFile());
Clip clip = AudioSystem.getClip();
clip.open(audioInputStream);
clip.start();
} catch(Exception ex)
{
System.out.println("Error with playing sound.");
System.out.println(ex);
}
}
public static void green()
{
try {
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File("sound.wav").getAbsoluteFile());
Clip clip = AudioSystem.getClip();
clip.open(audioInputStream);
clip.start();
} catch(Exception ex)
{
System.out.println("Error with playing sound.");
System.out.println(ex);
}
}
public static void blue()
{
try {
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File("sound.wav").getAbsoluteFile());
Clip clip = AudioSystem.getClip();
clip.open(audioInputStream);
clip.start();
} catch(Exception ex)
{
System.out.println("Error with playing sound.");
System.out.println(ex);
}
}
public static void yellow()
{
try {
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File("sound.wav").getAbsoluteFile());
Clip clip = AudioSystem.getClip();
clip.open(audioInputStream);
clip.start();
} catch(Exception ex)
{
System.out.println("Error with playing sound.");
System.out.println(ex);
}
}
}
package com.company;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class Buzzer extends JFrame implements KeyListener
{
JLabel label;
boolean canPress;
public Buzzer()
{
setVisible(true);
//frame.setResizable(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setExtendedState(getExtendedState() | JFrame.MAXIMIZED_BOTH);
setLayout(null);
Font f=new Font("ARIEL",Font.BOLD,250);
label=new JLabel("..........");
label.setBounds(100,100,1200,500);
label.setFont(f);
canPress=true;
add(label);
addKeyListener(this);
//frame.addKeyListener(this);
}
public void keyPressed(KeyEvent e)
{
char c=e.getKeyChar();
if(c>='0' && c<='7')
{
if(canPress)
{
if(c=='0')
{
label.setText("Red");
Audio.red();
}
else if(c=='1')
{
label.setText("Green");
Audio.green();
}
else if(c=='2')
{
label.setText("Blue");
Audio.blue();
}
else if(c=='3')
{
label.setText("Yellow");
Audio.yellow();
}
canPress=false;
}
}
else
{
label.setText("..........");
canPress=true;
}
}
public void keyTyped(KeyEvent e)
{
}
public void keyReleased(KeyEvent e)
{
}
public static void main(String args[])throws Exception
{
Buzzer ob=new Buzzer();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment