hey can anyone help me...........

the below code is for getting a jpeg image called rabbit which resides within the bin folder......

Image img= getImage("rabit.jpg");

instead of placing the image in the bin folder.........can we call a image by giving a path like ("D\:Images\rabbit.jpg")

is there any method with which we can call any image by specifying the path like the above mention..............

Recommended Answers

All 19 Replies

Yes. You need to use ordinary file i/o to establish the path to the image (this can be a hard-coded path, or entered by the user, or selected in a file browser, so you have plenty of options here). Then you create a BufferedImage object and try to read it using the ImageIO class.
The tutorial here will help.

still not working..............

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
//<applet code=newt.class width=1200 height =1200></applet>
public class newt extends Applet //implements ActionListener
{
    BufferedImage img = null;
public void init()
{   
       try {
    img = ImageIO.read(new File("D:\Winter.jpg"));
} catch (IOException e) {}

    }
    public void paint(Graphics g)
     {
        g.drawImage(img, 10, 10, this);
      }
}
catch (IOException e) {}

That is a big mistake.
If there is any problem locating or reading the file, you have opted for Java to do nothing and tell you nothing. How can you debug that?

catch (IOException e) {e.printStackTrace()}

will ensure that you have a proper full explanation of problems that arise.

commented: Damn straight +2
public class newt extends Applet

Applets don't have access to the file system. They can only access files they bring with them in the jar.
If you look at the stack trace from the exception you're currently swallowing, I wouldn't be surprised to see a security exception is what's holding you up.

i want that to be done in applet..............

can anyone please help me.........................

You could ask James Gosling very nicely if he'd revise the security policy so you can write applets that go rummaging around in people's computers without asking permission. Or you could look into running it as a trusted applet, a bit of research on that might help you out. I've never jumped through those hoops, so I can't help you there, but it might get you there.
But a non-trusted applet can't access the local file system, period.

still not working..............

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
//<applet code=newt.class width=1200 height =1200></applet>
public class newt extends Applet //implements ActionListener
{
    BufferedImage img = null;
public void init()
{   
       try {
    img = ImageIO.read(new File("D:\Winter.jpg"));
} catch (IOException e) {}

    }
    public void paint(Graphics g)
     {
        g.drawImage(img, 10, 10, this);
      }
}

First of all import java.io.*;
Secondly use "d:/Winter.jpg" in filename isntead of "d:\Winter.jpg"

d:\\Winter.jpg

import java.io.*;
    import javax.imageio.*;
    import java.awt.*;
    import java.awt.image.*;
    import java.applet.*;
    
    //<applet code=newt.class width=1200 height =1200></applet>
    
    public class A extends Applet //implements ActionListener
    {
    	File input;
    
    	BufferedImage img = null;
    
    public void init()
    {
    	input = new File( "D:\\b.jpg");
    	try {
    		img = ImageIO.read( input );
    	}
    	catch (IOException e) {}
    }
    
    public void paint(Graphics g)
    {
    	g.drawImage(img, 10, 10, this);
    }
}

// you have to import classes

And Use "D:\\" instead of "D:\" to read the backslash...:-)

still not working.........

import java.io.*;
    import javax.imageio.*;
    import java.awt.*;
    import java.awt.image.*;
    import java.applet.*;
    
    //<applet code=newt.class width=1200 height =1200></applet>
    
    public class newt extends Applet //implements ActionListener
    {
    	File input;
    
    	BufferedImage img = null;
    
    public void init()
    {
    	input = new File( "D:\\kk.jpg");
    	try {
    		img = ImageIO.read( input );
    	}
    	catch (IOException e) {}
    }
    
    public void paint(Graphics g)
    {
    	g.drawImage(img, 10, 10, this);
    }
}

the output is as shown in the picture below

What is the error you're getting?

Here's one major problem with your code:
Replace this

catch (IOException e) {}

with this

catch (IOException e) {e.printStackTrace()}

this is the command details......

C:\Program Files\Java\jdk1.6\bin>javac newt.java

C:\Program Files\Java\jdk1.6\bin>Appletviewer newt.java
java.security.AccessControlException: access denied (java.io.FilePermission D:\k
k.jpg read)
        at java.security.AccessControlContext.checkPermission(AccessControlConte
xt.java:323)
        at java.security.AccessController.checkPermission(AccessController.java:
546)
        at java.lang.SecurityManager.checkPermission(SecurityManager.java:532)
        at java.lang.SecurityManager.checkRead(SecurityManager.java:871)
        at java.io.File.canRead(File.java:689)
        at javax.imageio.ImageIO.read(ImageIO.java:1274)
        at newt.init(newt.java:19)
        at sun.applet.AppletPanel.run(AppletPanel.java:424)
        at java.lang.Thread.run(Thread.java:619)

C:\Program Files\Java\jdk1.6\bin>

and the output window(applet) is as shown in the attached output.bmp (ABOVE)

Hey i want to do that in applet................that's why

CAN WE CALL IMAGES IN APPLET FROM EXTERNAL FOLDERS

commented: This was already answered on the first page. -3

OK, very well, that your deci***, welcome on road to the hell

<FW> then look around, maybe google hepls you with that
no Applet (upTo unsuported Java1.4) but JApplet<FW>

What r u doing...???

appletviewer newt.java

It's not the way to run an applet

u need to create a HTML document.

e.g, RunApplet.html -->> where u should the following code

<html>
	<head>
	</head>
	<body bgcolor="000000">
		<center>
			<applet
				code	= "newt.class"
				width	= "500"
				height	= "300"
				>
			</applet>
		</center>
	</body>
</html>

now run -->> appletviewer RunApplet.html

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.