The Artima Developer Community
Sponsored Link

Java Answers Forum
How do we put images in an applet

8 replies on 1 page. Most recent reply: Sep 10, 2003 8:34 PM by Joe Parks

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 8 replies on 1 page
Ram

Posts: 19
Nickname: srkothuru
Registered: Jul, 2003

How do we put images in an applet Posted: Sep 2, 2003 1:55 PM
Reply to this message Reply
Advertisement
Hi i have an applet which has a frame and several panelsthat are loaded on the frame. I need to put an image which acts as a header and the panels should be loaded below the header image. How can i do this. Can anyone post a sample code here.

Thank you
Ram


mausam

Posts: 243
Nickname: mausam
Registered: Sep, 2003

Re: How do we put images in an applet Posted: Sep 2, 2003 11:59 PM
Reply to this message Reply
Image photo = getImage(getCodeBase(), imageName);
in update(Graphics g)
draw this image where ever u like

Arun Agarwal

Posts: 5
Nickname: arunrecw
Registered: May, 2003

Re: How do we put images in an applet Posted: Sep 3, 2003 5:11 AM
Reply to this message Reply
Hi.

You can also put this code as:

Image myimg ;
in init() function :

myimg = Toolkit.getDefaultToolkit().getImage(<absolutepath> );

and in paint or repaint functions you can use the same
top level reference of Image class as follows:

g.drawImage( myimg, <x>, <y>, this );

here, you need to import classes appropriately.
Toolkit comes from java.awt.Toolkit here.
and remember one thing absolute path would not contain
backward slashes, they will be forward only.
for example in d drive suppose you have first.gif then
you have to give "d:/first.gif" .

-Arun.

mausam

Posts: 243
Nickname: mausam
Registered: Sep, 2003

Re: How do we put images in an applet Posted: Sep 3, 2003 5:17 AM
Reply to this message Reply
Will using it from toolkit give SecurityException

Ram

Posts: 19
Nickname: srkothuru
Registered: Jul, 2003

Re: How do we put images in an applet Posted: Sep 4, 2003 1:29 PM
Reply to this message Reply
HI, i tried toolkit, it gave me the following errors

pine 382% javac WaltzPanel.java
WaltzPanel.java:1531: Incompatible type for method. Can't convert java.lang.String to byte[].
     logo = toolkit.createImage(nanocadHome + "\\logo.jpg");
                                            ^
WaltzPanel.java:1536: Incompatible type for method. Can't convert java.awt.Image to java.awt.Component.
        PageLogoPanel.add(logo);
                          ^
Note: WaltzPanel.java uses a deprecated API.  Recompile with "-deprecation" for details.
2 errors, 1 warning

Mohamed Riyaz

Posts: 7
Nickname: riyaz
Registered: Jul, 2003

Re: How do we put images in an applet Posted: Sep 7, 2003 3:15 AM
Reply to this message Reply
Since security issues limit you to use external files.You can use URL class.It will give best result.

Code:
import java.net.*;
import javax.swing.*;
-
---
URL url=getClass().getResource("india.gif");
Icon ic=new ImageIcon(url);

JLabel label=new JLabel(ic);
(or)
label.setIcon(ic);

---
Riyaz,Dubai

Ram

Posts: 19
Nickname: srkothuru
Registered: Jul, 2003

Re: How do we put images in an applet Posted: Sep 9, 2003 8:28 AM
Reply to this message Reply
hi, i can not use swings. I am using jdk 1.1.7 which does not support swings
So is there any other alternative?

Joe Parks

Posts: 107
Nickname: joeparks
Registered: Aug, 2003

Re: How do we put images in an applet Posted: Sep 10, 2003 8:31 PM
Reply to this message Reply

Joe Parks

Posts: 107
Nickname: joeparks
Registered: Aug, 2003

Re: How do we put images in an applet Posted: Sep 10, 2003 8:34 PM
Reply to this message Reply
import java.awt.Image; 
import java.awt.Graphics; 
import java.applet.Applet; 
public class TheBigPicture extends Applet {
     private Image image;
 
 
     public void init() {
         image = getImage(getCodeBase(), "thebigpicture.gif");
     }
 
 
     public void paint(Graphics g) {
         g.drawImage(image, 0, 0, this);
     }
}

Flat View: This topic has 8 replies on 1 page
Topic: Reading the last cell in a JTable Previous Topic   Next Topic Topic: =NullPointerException  with Compartor, help

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use