So guys yap....third blog post i wanna this post special as 3...well is one of my favorite numbers, so well in my last post i mentioned that i might be posting source codes for java programming.....well here is a short code to implement a custom button alongside sound in java...images included..
Firstly images you will need to create...
Here is the whole source code....this is Netbeans auto compiled code by the way so ya u could it out or just use Netbeans ..Jframes and stuff...so u simply got to create a subfolder in the package's main for images and
Firstly images you will need to create...
![]() |
| This is the custom_button kind of 3d..cool right |
![]() |
| button_hover..as in when mouse is over this |
![]() |
| Mouse clicked.. |
for sound....and include them in the paths when calling them...pardon the lengthy codes i just want u guys to see it all
For example
Package Main
folder Sound
sound file
folder Images
all images stored here
I hope is helpful
package App_package;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.swing.ImageIcon;
/**
*
* @author DAVID
*/
public class Main_menu extends javax.swing.JFrame {
File sound = new File("(This should be the sound file location on your system..eg"C:\\Users\\David\\ring.wav")");
public Main_menu() throws LineUnavailableException {
initComponents();
Clip clip = AudioSystem.getClip();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel2 = new javax.swing.JLabel();
jLabel1 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setPreferredSize(new java.awt.Dimension(300, 200));
setResizable(false);
getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/custom_button.png"))); // NOI18N
jLabel2.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jLabel2MouseClicked(evt);
}
public void mouseEntered(java.awt.event.MouseEvent evt) {
jLabel2MouseEntered(evt);
}
public void mouseExited(java.awt.event.MouseEvent evt) {
jLabel2MouseExited(evt);
}
public void mousePressed(java.awt.event.MouseEvent evt) {
jLabel2MousePressed(evt);
}
public void mouseReleased(java.awt.event.MouseEvent evt) {
jLabel2MouseReleased(evt);
}
});
getContentPane().add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 60, -1, 40));
getContentPane().add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(60, 20, 160, 110));
pack();
}// </editor-fold>
void playSound(File sound)
{
try{
clip.open(AudioSystem.getAudioInputStream(sound));
clip.start();
Thread.sleep(clip.getMicrosecondLength()/1000);
}catch(LineUnavailableException | UnsupportedAudioFileException | IOException | InterruptedException e)
{
System.err.println(e);
}
}
static void stopSound(File sound) throws UnsupportedAudioFileException, LineUnavailableException, IOException
{
clip.stop();
}
private void jLabel2MouseEntered(java.awt.event.MouseEvent evt) {
ImageIcon ii = new ImageIcon(getClass().getResource("/images/button_hover.png"));
jLabel2.setIcon(ii);
}
private void jLabel2MouseExited(java.awt.event.MouseEvent evt) {
ImageIcon ii = new ImageIcon(getClass().getResource("/images/custom_button.png"));
jLabel2.setIcon(ii);
}
private void jLabel2MousePressed(java.awt.event.MouseEvent evt) {
ImageIcon ii = new ImageIcon(getClass().getResource("/images/mouse_clicked.png"));
jLabel2.setIcon(ii);
playSound(Alarm);
}
private void jLabel2MouseReleased(java.awt.event.MouseEvent evt) {
ImageIcon ii = new ImageIcon(getClass().getResource("/images/button_hover.png"));
jLabel2.setIcon(ii);
System.out.println("You clicked the button");
try {
stopSound(sound);
} catch (UnsupportedAudioFileException ex) {
Logger.getLogger(Main_menu.class.getName()).log(Level.SEVERE, null, ex);
} catch (LineUnavailableException ex) {
Logger.getLogger(Main_menu.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Main_menu.class.getName()).log(Level.SEVERE, null, ex);
}
// playSound(Alarm);
}
private void jLabel2MouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Main_menu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Main_menu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Main_menu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Main_menu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Main_menu().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
// End of variables declaration
}
So hopefully in future posts for source codes they will be much more concise and succinct, ok guys i hope this is helpful, try to pay most attention to the methods and the fields.
Become a developer



No comments:
Post a Comment