Results 1 to 8 of 8
- 07-25-2011, 04:53 AM #1
Member
- Join Date
- Jul 2011
- Location
- kent, UK
- Posts
- 47
- Rep Power
- 0
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");
-
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:
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.Java Code:System.out.println(System.getProperty("user.dir"));
- 07-25-2011, 05:10 AM #3
Member
- Join Date
- Jul 2011
- Location
- kent, UK
- Posts
- 47
- Rep Power
- 0
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...
- 07-25-2011, 05:12 AM #4
Member
- Join Date
- Jul 2011
- Location
- kent, UK
- Posts
- 47
- Rep Power
- 0
and i dont know how to do the second option, "and another is to use a URL to the image"
-
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?
- 07-25-2011, 05:26 AM #6
Member
- Join Date
- Jul 2011
- Location
- kent, UK
- Posts
- 47
- Rep Power
- 0
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); } }
- 07-25-2011, 07:10 AM #7
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,158
- Rep Power
- 5
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.
- 07-25-2011, 01:16 PM #8
Member
- Join Date
- Jul 2011
- Location
- kent, UK
- Posts
- 47
- Rep Power
- 0
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;
not so hard afterall.Java Code:// create an ImageIcon ImageIcon banner =new ImageIcon("images\\defaultbanner.gif"); bannerLabel = new JLabel(banner); container.add(bannerLabel);
Similar Threads
-
Load percentage of an image
By trishtren in forum Java 2DReplies: 0Last Post: 04-15-2011, 02:02 PM -
Load image to jPanel
By DJIT in forum AWT / SwingReplies: 7Last Post: 12-27-2010, 12:48 PM -
load image in the jsp page
By maneuk in forum EclipseReplies: 0Last Post: 04-09-2010, 09:23 PM -
[SOLVED] Load image into a JSP
By jazz2k8 in forum New To JavaReplies: 0Last Post: 05-08-2008, 11:33 AM -
Help with load image
By trill in forum New To JavaReplies: 1Last Post: 08-01-2007, 07:16 AM


3Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks