Results 1 to 8 of 8
Like Tree3Likes
  • 1 Post By Fubarable
  • 1 Post By Fubarable
  • 1 Post By camickr

Thread: How to load an image using Toolkit

  1. #1
    pappyj77 is offline Member
    Join Date
    Jul 2011
    Location
    kent, UK
    Posts
    47
    Rep Power
    0

    Default How to load an image using Toolkit

    Hi anyone, please i am very new to Java and this forum too, i need help trying to create a banner for my GUI but i am trying to use the toolkit to load it. i have tried everything i can to load the image but i cant, no error occurred but the image did not load too...

    please take a look at this code and help me correct what i am doing wrong. i can post the rest of the code if require it. Thank you very much.

    toolkit = Toolkit.getDefaultToolkit();
    if(toolkit.getScreenSize().getWidth() > 600)
    setSize(600, 575);
    else
    setSize((int)toolkit.getScreenSize().getWidth(),(i nt)toolkit.getScreenSize().getHeight() - 20);
    setResizable(false);
    Dimension dimension = getSize();
    setLayout(new FlowLayout());

    Image image = getToolkit().getImage("images/defaultbanner.gif");

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Your Toolkit may be looking in the wrong place for the image. Note that it will look by default into the user directory which you can identify using:

    Java Code:
    System.out.println(System.getProperty("user.dir"));
    One solution is to use the full path to the image, and another is to use a URL to the image, and this solution will look for the image in a path relative to the class paths for this application. You can create this URL via the class#getResource(...) method.
    pappyj77 likes this.

  3. #3
    pappyj77 is offline Member
    Join Date
    Jul 2011
    Location
    kent, UK
    Posts
    47
    Rep Power
    0

    Default

    thank you for your quick response. i have tried that too but still no result, i even copied the image to the same directory to make things easier, but still nothing...

  4. #4
    pappyj77 is offline Member
    Join Date
    Jul 2011
    Location
    kent, UK
    Posts
    47
    Rep Power
    0

    Default

    and i dont know how to do the second option, "and another is to use a URL to the image"

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    You may need to show more of your code and give more information on just where this image is located. If you post code, please don't forget to use code tags (please see my signature below). Also, have you run the little snippet of code in my post above to see just where Java is looking to find your image?
    pappyj77 likes this.

  6. #6
    pappyj77 is offline Member
    Join Date
    Jul 2011
    Location
    kent, UK
    Posts
    47
    Rep Power
    0

    Default

    Here is the entire code, trying to create a chat system, but am stuck at the GUI. and its very rough too. yes i have run the snippet thank you.
    the image is located in C:\Users\PMO\Desktop\threadchat\images.
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    class  MainView extends JFrame	{
    	private JLabel lbl;
    	private JButton sendBtn;
    	private JTextArea genMsg, frndLst;
    	private JTextField msgF;
    	private JMenuBar menubar;
    	private JMenu loginmenu, aboutmenu;
    	private JMenuItem loginitem, disconnectitem, seperatoritem, quititem, aboutitem;
    	private Toolkit toolkit; 
    	
    	public MainView()	{
    		
    		toolkit = Toolkit.getDefaultToolkit();		
    		if(toolkit.getScreenSize().getWidth() > 600)
    		setSize(600, 575);
    		else
    		setSize((int)toolkit.getScreenSize().getWidth(),(int)toolkit.getScreenSize().getHeight() - 20);			
    		setResizable(false);
    		Dimension dimension = getSize();	
    		setLayout(new FlowLayout());	
    
    		Image image = getToolkit().getImage("images/defaultbanner.gif");
    		System.out.println(System.getProperty("user.dir"));
    
    
    		setTitle("FRESHER MARKETING COMPANY");		
    		addWindowListener(new WindowAdapter() {
    		public void windowClosing(WindowEvent evt) { System.exit(0);}});
    	
    
    		menubar = new JMenuBar();
    		loginmenu = new JMenu("Login");		
    		loginitem = new JMenuItem("Login");
    		
    		disconnectitem = new JMenuItem("Disconnect");
    		seperatoritem = new JMenuItem("---------------");
    		quititem = new JMenuItem("Quit");
    
    		loginmenu.add(loginitem);
    		loginmenu.add(disconnectitem);
    		loginmenu.add(seperatoritem);
    		loginmenu.add(quititem);
    		
    		aboutmenu = new JMenu("Help ");
    		aboutitem = new JMenuItem("About ");
    
    		aboutmenu.add(aboutitem);
    		
    		menubar.add(loginmenu);
    		menubar.add(aboutmenu);
    		setJMenuBar(menubar);
    
    		Container container = getContentPane();
    		container.setLayout(new FlowLayout());
    
    		// create General Message Screen
    		genMsg = new JTextArea(30,45);
            genMsg.setEditable(false);
            genMsg.setFont(new java.awt.Font("Times New Roman", 0, 12)); // NOI18N
    		genMsg.setLineWrap(true);
    		container.add( new JScrollPane( genMsg ));
    
    		// create Friend List View
    		frndLst = new JTextArea(30, 15);
    		frndLst.setFont(new java.awt.Font("Times New Roman", 0, 12)); // NOI18N
    		container.add( new JScrollPane( frndLst));
    		frndLst.setEditable(false);
    		frndLst.setLineWrap(true);
    
    		lbl = new JLabel ("Message:");
    		container.add(lbl);
    
    		// create Message Field
    		msgF = new JTextField(38);
    		msgF.setEnabled( true );
    		msgF.setText("");
            msgF.requestFocus();
    		msgF.addActionListener(
    
    		new ActionListener() 
    		{
    			// send message to client
    			public void actionPerformed( ActionEvent event )
    			{
    			//	sendData( event.getActionCommand() );
    			}
    		} // end anonymous inner class
    		); // end call to addActionListener
    		container.add(msgF);
    		
    			// create Send Button
    		sendBtn = new JButton ("Send");
    		container.add(sendBtn);
    
    
    		setVisible( true );
    	
    		
    	}
    	
    	public static void main(String[] args) 
    	{
    	MainView application = new MainView();
    	
    	application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	}
    }

  7. #7
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    You read the Image but you don't do anything with it. Create an ImageIcon, using the image and add the icon to a JLabel. Also, you should try using ImageIO to read the image. Or you can also look at How to Use Icons (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components) for working examples.
    pappyj77 likes this.

  8. #8
    pappyj77 is offline Member
    Join Date
    Jul 2011
    Location
    kent, UK
    Posts
    47
    Rep Power
    0

    Default

    thank you very much Camickr, the image has loaded at last, may not be the best or tidy way but it did the job . here is what i added below;
    Java Code:
    		// create an ImageIcon
    		ImageIcon banner =new ImageIcon("images\\defaultbanner.gif");  
    		bannerLabel = new JLabel(banner);
    		container.add(bannerLabel);
    not so hard afterall.

Similar Threads

  1. Load percentage of an image
    By trishtren in forum Java 2D
    Replies: 0
    Last Post: 04-15-2011, 03:02 PM
  2. Load image to jPanel
    By DJIT in forum AWT / Swing
    Replies: 7
    Last Post: 12-27-2010, 01:48 PM
  3. load image in the jsp page
    By maneuk in forum Eclipse
    Replies: 0
    Last Post: 04-09-2010, 10:23 PM
  4. [SOLVED] Load image into a JSP
    By jazz2k8 in forum New To Java
    Replies: 0
    Last Post: 05-08-2008, 12:33 PM
  5. Help with load image
    By trill in forum New To Java
    Replies: 1
    Last Post: 08-01-2007, 08:16 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •