Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jul 12th, 2006, 5:52 PM   #1
grimpirate
King of Portal
 
grimpirate's Avatar
 
Join Date: Sep 2005
Posts: 437
Rep Power: 4 grimpirate is on a distinguished road
Send a message via Yahoo to grimpirate
Question Jar File Problem

I have the following line of code in a class named TestGUI:
frame.setIconImage(Toolkit.getDefaultToolkit().createImage("images/grimserve.gif"));
Which basically sets up the icon in JFrame's title bar. However, when I create an executable jar file (with the TestGUI class and the folder images included) and run it, the image grimserve.gif won't display. I was told that I had to use the Class.getResource() method, but I'm unsure how to proceed. Can someone show me how this code should be modified so that the jar file runs properly, or explain how the getResource method is relevant. Thx for the help.
__________________
Lo, there do I see my father. 'Lo, there do I see My mother, and my sisters, and my brothers. 'Lo, there do I see The line of my people... Back to the beginning. 'Lo, they do call to me. They bid me take my place among them. In the halls of Valhalla... Where the brave... May live... ...forever.. GrimBB | Mimesis
grimpirate is offline   Reply With Quote
Old Jul 12th, 2006, 6:34 PM   #2
Kaja Fumei
Hobbyist Programmer
 
Join Date: Oct 2005
Posts: 134
Rep Power: 4 Kaja Fumei is on a distinguished road
If the "images" directory is in the top-level directory of the jar:
InputStream stream = getClass().getResourceAsStream("/images/grimserve.gif");

Then, read the entire input stream into a byte array and pass that array to createImage().

If you remove the beginning "/", it'll treat the path as a relative path from the directory of the current class file. For more info on getResourceAsStream: http://www.javaworld.com/javaworld/j...-property.html
Kaja Fumei is offline   Reply With Quote
Old Jul 12th, 2006, 7:45 PM   #3
Toro
Hobbyist Programmer
 
Toro's Avatar
 
Join Date: Apr 2006
Posts: 136
Rep Power: 0 Toro is an unknown quantity at this point
you must have the folder "images" in the same directory as the Jar file. Try changing the directory by just making the image the same place as the Jar.

Maybe that will help
Toro is offline   Reply With Quote
Old Jul 12th, 2006, 8:47 PM   #4
grimpirate
King of Portal
 
grimpirate's Avatar
 
Join Date: Sep 2005
Posts: 437
Rep Power: 4 grimpirate is on a distinguished road
Send a message via Yahoo to grimpirate
Unhappy I'm Failing to Understand

I apologize, this whole getResource stuff is very cryptic to me and I read the javaworld tutorial/explanation you posted there Kaja Fumei, but had no luck understand it. I get lost in all the technical jargon. So I'm just going to show you what I'm working with.
Here is the full code of the program:
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
import java.awt.geom.*;

public class CBZGUI
{
    private static void createAndShowGUI()
    {
        JFrame frame = new JFrame("GrimServe");
        
//        BufferedImage icon = new BufferedImage(32, 32, BufferedImage.TYPE_INT_ARGB);
//        Graphics2D g2 = icon.createGraphics();
//        g2.setColor(new Color(0, 0, 0));
//        g2.draw(new Line2D.Double(0, 0, 32, 32));

//        frame.setIconImage(icon);
        frame.setIconImage(Toolkit.getDefaultToolkit().createImage("images/grimserve.gif"));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        DisplayPanel dispPanel = new DisplayPanel();
        frame.setContentPane(dispPanel);
        
        frame.pack();
        frame.setVisible(true);
    }
    
    public static void main(String[] args)
    {
        javax.swing.SwingUtilities.invokeLater
        (
            new Runnable()
            {
                public void run()
                {
                    createAndShowGUI();
                }
            }
        );
    }
}

class DisplayPanel extends JPanel implements KeyListener
{
    public DisplayPanel()
    {
        setLayout(new BorderLayout());
        
        textArea.setEditable(false);
        JScrollPane scrollPane = new JScrollPane(textArea);
        
        textArea.setToolTipText("GrimServe console display.");
        textField.setToolTipText("GrimServe command input.");
        
        textField.setFocusTraversalKeysEnabled(false);
        textField.addKeyListener(this);
        
        textArea.setText("-=GrimServe 2.0=-\n");
        add(scrollPane, BorderLayout.CENTER);
        add(textField, BorderLayout.SOUTH);
    }
    
    public void keyPressed(KeyEvent e)
    {
        // Enter key
        if(e.getKeyCode() == 10)
        {
            textArea.append(textField.getText() + "\n");
            textField.setText("");
        }
    }
    public void keyReleased(KeyEvent e){}
    public void keyTyped(KeyEvent e) {}
    
    final JTextArea textArea = new JTextArea(15, 45);
    final JTextField textField = new JTextField();
}
And here is the structure of my jar file which I obtain from typing in my command prompt jar tf grimserve.jar
META-INF/MANIFEST.MF
CBZGUI$1.class
CBZGUI.class
DisplayPanel.class
images/grimserve.gif
So how do I get my code to access that very last file images/grimserve.gif? I hate asking for someone to just like correct my code and show me, but I'm honestly just not grasping what I'm doing wrong. I know that if I put a folder outside with the file in it then the app will work fine, but what I want to do is package all my files together in one jar file and make it work. Thx again for the help.
__________________
Lo, there do I see my father. 'Lo, there do I see My mother, and my sisters, and my brothers. 'Lo, there do I see The line of my people... Back to the beginning. 'Lo, they do call to me. They bid me take my place among them. In the halls of Valhalla... Where the brave... May live... ...forever.. GrimBB | Mimesis
grimpirate is offline   Reply With Quote
Old Jul 13th, 2006, 11:02 AM   #5
Kaja Fumei
Hobbyist Programmer
 
Join Date: Oct 2005
Posts: 134
Rep Power: 4 Kaja Fumei is on a distinguished road
getResourceAsStream is just a method primarily used for reading files stored inside the current jar.

long length = 1024;  // change to the size in bytes of the actual gif
byte[] gif = new byte[length];
InputStream stream = getClass().getResourceAsStream("/images/grimserve.gif");
stream.read(gif);
frame.setIconImage(Toolkit.getDefaultToolkit().createImage(gif));

There may be a simpler way of building an image from the InputStream but I'm not very familiar with the java GUI packages.
Kaja Fumei is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
OnlineTextEditor.Com! Sane Show Off Your Open Source Projects 43 Jun 16th, 2006 9:55 AM
Problem with file and array Marek C 9 Dec 28th, 2005 11:03 AM
Open File Problem :( Silent Visual Basic .NET 17 Dec 5th, 2005 3:30 AM
After execution - Error cannot locate /Skin File? wchar Visual Basic 1 Mar 5th, 2005 10:04 PM
airport Log program using 3D linked List : problem reading from file gemini_shooter C++ 0 Mar 2nd, 2005 5:12 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:56 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC