Welcome to Dream.In.Code
Become a Java Expert!

Join 150,406 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 933 people online right now. Registration is fast and FREE... Join Now!




Adding a.png to a JFrame

 
Reply to this topicStart new topic

Adding a.png to a JFrame, getting an image to display!

Chopster
3 Apr, 2008 - 05:59 AM
Post #1

New D.I.C Head
*

Joined: 29 Mar, 2008
Posts: 35


My Contributions
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
User is offlineProfile CardPM
+Quote Post

1lacca
RE: Adding A.png To A JFrame
3 Apr, 2008 - 06:08 AM
Post #2

code.rascal
Group Icon

Joined: 11 Aug, 2005
Posts: 3,822



Thanked: 12 times
My Contributions
If you simply want to display a picture, then the easiest way is to go with a JLabel, see this tutorial how to do it.

(and add it to your frame)
User is offlineProfile CardPM
+Quote Post

Chopster
RE: Adding A.png To A JFrame
3 Apr, 2008 - 06:36 AM
Post #3

New D.I.C Head
*

Joined: 29 Mar, 2008
Posts: 35


My Contributions
Really not sure how i missed that tutorial, thanks again 1Lacca

was a breeze, cheers
User is offlineProfile CardPM
+Quote Post

1lacca
RE: Adding A.png To A JFrame
3 Apr, 2008 - 06:38 AM
Post #4

code.rascal
Group Icon

Joined: 11 Aug, 2005
Posts: 3,822



Thanked: 12 times
My Contributions
No problem.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 07:24PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month