Sign in to follow this  

[java] image to application..

This topic is 4096 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hei! Im a newbie with swing, heres my question.. my code, an copy paste from modified "google 'java almanac'" code is displaying an shape instead of my image.gif, why.. Yours : JariTapio HomePage : www.EuroJari.net EMail : JariTapio@EuroJari.net
import java.awt.*;
import javax.swing.*;

import java.net.*;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.applet.*;
import java.awt.event.*;


public class FirstFrameApp {
        Image image;
        public static void main(String[] args) {
            new FirstFrameApp();
            
            boolean gamemode = true;
            while ( gamemode == true ) 
            {
                gamemode = true;
                // Main game loop..
                // repaint (); for rendering..
            };            
        }
        FirstFrameApp() {
            image = Toolkit.getDefaultToolkit().getImage("image.gif");// this image.gif is on an './build/classes/' directory, why it is not displayed..
            
            //try { Thread.sleep(5000); } catch ( InterruptedException e ) { };
            
            // Creates a frame            
            JFrame frame = new JFrame("my very first frame application!! hmmm... application is not displaying image.gif as it should do..");
            
            // Add a component with a custom paint method
            frame.getContentPane().add(new MyComponent());    
            // Display the frame
            Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();            
            int frameWidth = dim.width;
            int frameHeight = dim.height;
            frame.setSize(frameWidth,frameHeight);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);            
            frame.setVisible(true);           
        }
    
        class MyComponent extends JComponent {
            // This method is called whenever the contents needs to be painted
            public void paint(Graphics g) {
                // Retrieve the graphics context; this object is used to paint shapes
                Graphics2D g2d = (Graphics2D)g;
    
                // Draw an oval that fills the window
                int x = 0;
                int y = 0;
                int width = getSize().width-1;
                int height = getSize().height-1;
                g2d.drawOval(x, y, width, height);                      
                
                g2d.drawImage(image,0,0,this); // this line is as good as an //NULL; but why..
            }
        }
    }

Share this post


Link to post
Share on other sites
Hi,

Try to load your image using:

Image img = ImageIO.read( getClass().getResource("image.gif") );


Also, I would suggest putting all of your images and other resources under a common directory (e.g. "rsrc"), and then use an ant target to build a .jar with them and use them in the classpath of your application. If ant is a mistery to you, then you can easily create a jar with the command:


jar -cf rsrc.jar rsrc


Which will create a file called "rsrc.jar", with all the contents of the "rsrc" folder.

Son Of Cain

Share this post


Link to post
Share on other sites
---Hi,
---Try to load your image using:
---image = ImageIO.read( getClass().getResource("image.gif") );

Hei!

hmm.. im getting a red downline..
i inserted the line "import java.io.*;" to my code but the line you gave me is still redlined..
as this is one of my firsts applications im confused..
why the line is still red even if there is a line :: import java.io.*;

Yours : JariTapio
HomePage : www.EuroJari.net
EMail : JariTapio@EuroJari.net

Share this post


Link to post
Share on other sites
Because this class does not belong to the java.io.package.
You are using an IDE, I presume. So, look for a "fix imports" option, to reorganize and give you the right paths to the classes.

By the way, ImageIO is on "javax.image.io", if I'm not mistaken.

Share this post


Link to post
Share on other sites
---You are using an IDE, I presume. So, look for a "fix imports" option, to
---reorganize and give you the right paths to the classes.
---By the way, ImageIO is on "javax.image.io", if I'm not mistaken

Hei!

Yes, im using netbeans IDE and the ImageIO is "javax.image.io", thanks, still im not getting that redline away from the following code line..

the "fix imports" was allthough a very great option, i dont have to write thouse import lines anymore..

[error:]
:27: unreported exception java.io.IOException; must be caught or declared to be thrown
bimage = ImageIO.read ( getClass().getResource("image.gif") );


bimage = ImageIO.read ( getClass().getResource("image.gif") ); // line is redlined, why??

//Could someone please give me my needed code.. i have never thrown or caught anything else than "try thread.sleep() catch ()"



Yours : JariTapio
HomePage : www.EuroJari.net
EMail : JariTapio@EuroJari.net

Share this post


Link to post
Share on other sites
You'll have to catch the specific exception that can be thrown on the try{} body, or you can catch the Exception type, which is super class of all possible exceptions in the Java language.

Catching specific types is better, though. Because you can response to specific errors, giving more detailed messages. Instead of "an exception ocurred!", you can detail the message to "error triying to load file" + information returned by the java.io.IOException object.

I can see you're quite green on Java, so I recommend you take a few tutorials on the great Java Tutorial, hosted by Sun. It should give you the basics to solve these simple problems by yourself, and you help you much more than us here :D

Share this post


Link to post
Share on other sites
Hei!

Man im totally new with java applications..
i have learned the applet init() start() run() stop() destroy() well but applications are new..

but i get it working, thank you for helping directions..

here is the code mission completed..


try
{
image = ImageIO.read ( getClass().getResource("image.gif") );
}
catch (java.io.IOException e)
{
System.err.println("Unable to locate or read .gif file");
}



Yours : JariTapio
HomePage : www.EuroJari.net
EMail : JariTapio@EuroJari.net

Share this post


Link to post
Share on other sites

This topic is 4096 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this