-1

I am trying to add an image to my applet. I have googled this but I have not found a decent example that I understand. Does anyone know where I can find a good example of adding an image to and applet?

I got this online but when I run the applet it doesn't display my image.

   public class Lab5 extends JApplet {
        //Lab5(){}


        Image img;

        public void init() {
                img = getImage(getDocumentBase(), "img\flag0.gif");                 
        }


        public void paintComponent(Graphics g) {
            g.drawImage(img, 50, 50, this);
        }
}

Below is my HTML file

<!DOCTYPE html>
<html>
    <head>
        <title></title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    </head>
    <body>
    <applet code="Lab5.class" width= 250 height = 50></applet>
    </body>
</html>
mKorbel
  • 109,525
  • 20
  • 134
  • 319
Robert
  • 4,306
  • 11
  • 45
  • 95
  • 1
    There are many, many examples available as I'm sure you've seen. I'm not sure how we can point you to a "better" example or give you helpful advice unless you tell us which sites you've studied and what specifically about the current examples you don't understand, or what code isn't working for you and what errors you may be seeing. Consider putting more effort into this question if it is to have a hope of getting a decent answer. – Hovercraft Full Of Eels Feb 21 '12 at 00:46
  • 1
    First you'll want to read the tutorials on how to create applets. Consider putting the image into an ImageIcon and that into a JLabel. You can then give the JLabel a decent layout manager such as a BorderLayout, make it opaque and make it the contentPane for the applet. Alternatively you can draw the image in the `paintComponent()` method of a JPanel and make it the contentPane for the applet. – Hovercraft Full Of Eels Feb 21 '12 at 00:49
  • Okay I will look at it again. I copied that almost verbatum out of my class book – Robert Feb 21 '12 at 00:50
  • Edited question: swing and image tags added. Let's try to attract some Swing experts here. – Hovercraft Full Of Eels Feb 21 '12 at 00:54
  • [Loading and Drawing an Image in an Applet](http://www.exampledepot.com/egs/java.applet/LoadImageApplet.html) – tenorsax Feb 21 '12 at 01:01
  • @Max that is pretty much what I have above why doesn't my code show the image. Am I adding it to the HTML properly – Robert Feb 21 '12 at 01:03
  • @user512915: you're trying to draw it in a paintComponent method but applet doesn't even have a paintComponent method. Again, do the drawing in a JLabel or a JPanel and again add it to the applet's contentPane. Please look at my suggestions [here](http://stackoverflow.com/a/9342358/522444). – Hovercraft Full Of Eels Feb 21 '12 at 01:05
  • @user512915: Change paintComponent() to paint() – tenorsax Feb 21 '12 at 01:09
  • Also check out Andrew Thompson's recommendations [here](http://stackoverflow.com/a/5744763/522444). – Hovercraft Full Of Eels Feb 21 '12 at 01:09
  • @Max: no, don't ever draw directly in a top-level window. Please don't give this advice. – Hovercraft Full Of Eels Feb 21 '12 at 01:10
  • I don't think I fully grasp how to use and Applet yet. I am doing more research on how they function. – Robert Feb 21 '12 at 01:12
  • @user512915 you may also want to consider using tags instead of , as it's been depreciated. – Hawken Feb 21 '12 at 01:48
  • @Hawken Thanks our book uses – Robert Feb 21 '12 at 01:50

1 Answers1

5

Here's a simple example that shows an image from a URL from the internet. You'd probably use a resource in the internet url's place, such as an image held in a directory of the application's jar:

Class SimpleAppletImage.java

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.*;

@SuppressWarnings("serial")
public class SimpleAppletImage extends JApplet {
   @Override
   public void init() {
      try {
         SwingUtilities.invokeAndWait(new Runnable() {
            public void run() {
               try {
                  // you might want to use a file in place of a URL here
                  URL url = new URL("http://duke.kenai.com/gun/Gun.jpg");
                  BufferedImage img = ImageIO.read(url);
                  MyPanel myPanel = new MyPanel(img );
                  getContentPane().add(myPanel);
               } catch (MalformedURLException e) {
                  e.printStackTrace();
               } catch (IOException e) {
                  e.printStackTrace();
               }
            }
         });
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
}

class MyPanel.java

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;

import javax.swing.JPanel;

@SuppressWarnings("serial")
class MyPanel extends JPanel {
   private BufferedImage img;

   public MyPanel(BufferedImage img) {
      this.img = img;
      setPreferredSize(new Dimension(img.getWidth(), img.getHeight()));
   }

   @Override
   protected void paintComponent(Graphics g) {
      super.paintComponent(g);
      if (img != null) {
         g.drawImage(img, 0, 0, this); // corrected
      }
   }
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • We haven't gotten to exception handling yet gonna take me a few mins to figure out this code. – Robert Feb 21 '12 at 01:34