Good day to all,
I am having a little trouble with placing an image into my application, my application is a media player, and i simply would like an equalizer image placed in the center to give it a tiny bit more life. I searched for the methods and it seems straight forward, but i can't actually implement it into my code. Below is my code for the program (working)
CODE
package mediaplayer3;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.io.File.*;
import java.applet.AudioClip.*;
import javax.media.*;
public class MediaPlayer extends JFrame{
//Declaring my variables
static JFrame frame1;
static Container pane;
static JButton pause, stop, play;
static Insets insets;
static Player player;
static File file;
public static void main(String[] args) {
//Create the look and feel
try {UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());}
catch (ClassNotFoundException e){}
catch (InstantiationException e){}
catch (IllegalAccessException e){}
catch (UnsupportedLookAndFeelException e){}
//Create the frame
frame1 = new JFrame ("JPlayer");
//Set the App's Size
frame1.setSize(300,200);
//Prepare the panel
pane = frame1.getContentPane();
insets = pane.getInsets();
//Apply the null layout
pane.setLayout (null);
//Create controls
play = new JButton("Play");
play.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
player.start();
}
});
pause = new JButton("Pause");
pause.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
player.stop();
}
});
stop = new JButton("Stop");
stop.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
player.stop();
}
});
//Add the controls to the panel
pane.add (pause);
pane.add (stop);
pane.add (play);
//Positioning of the buttons
play.setBounds(insets.left + 60, insets.top + 117,
play.getPreferredSize().width,
play.getPreferredSize().height);
pause.setBounds(insets.left + 112, insets.top + 117,
pause.getPreferredSize().width,
pause.getPreferredSize().height);
stop.setBounds(insets.left + 172, insets.top +117,
stop.getPreferredSize().width,
stop.getPreferredSize().height);
//Create menu bar across top
JMenuBar menuBar = new JMenuBar();
JMenu file1 = new JMenu("File");
menuBar.add(file1);
JMenuItem open = new JMenuItem("Open File");
open.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
openFile();
createPlayer();
}
private void openFile(){
JFileChooser jfc = new JFileChooser();
jfc.setFileSelectionMode(
JFileChooser.FILES_ONLY);
int result = jfc.showOpenDialog(pane);
if(result == JFileChooser.CANCEL_OPTION)
file = null;
else
file = jfc.getSelectedFile();
}
private void createPlayer(){
if(file ==null)
return;
try{
player = Manager.createPlayer(file.toURL());
player.start();
}
catch(Exception e){
}
}
});
JMenuItem exit = new JMenuItem("Exit Program");
exit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.exit(0);
}
});
file1.add(open);
file1.add(exit);
frame1.setJMenuBar(menuBar);
frame1.setVisible(true);
}
}//END OF PROGRAM!
now i dont know where to place this code (if it is the right code) to make my image appear, where ever i seem to place it comes up with some errors.
CODE
Image img = Toolkit.getDefaultToolkit().getImage("C:/Users/Jason/Pictures/equalizer.png");
g.drawImage(img,100,40,this);
Any help in placing a simple image into my JFrame would be lovely,
Thanks in advance
Jason