Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Jan 2008
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How is a JApplet uploaded to a web browser

    Hi All,

    I'm new to Java and have been struggling to upload my JApplet to a webpage HTML document. It seems that I am missing some code in my HTML document because when I test my JApplet with eclipse appletviewer, it works 100%

    Please have a look at my Applet code and HTML code below. Any help will be appreciated.

    Best Regards
    Sibusiso


    Java File

    package applet;

    import javax.swing.JApplet;
    import javax.swing.JPanel;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.io.IOException;
    import javax.imageio.ImageIO;
    import java.io.File;

    /**
    * @author Sibusiso
    * This class represents a JApplet
    * that displays a picture and an
    * error message if the picture is
    * not loaded properly.
    */
    public class MyApplet extends JApplet
    {
    /**
    * This method initiates the
    * JApplet
    */
    public void init()
    {
    setSize(300,300);

    /**
    * inner class to override JPanel
    * paintComponent() to paint an
    * image
    */
    JPanel panel = new JPanel()
    {

    public void paintComponent(Graphics g)
    {
    Image picture = getImage();

    if(picture == null)
    g.drawString("Image File " +
    "could not be found",50,50);
    else
    g.drawImage(picture,0,0,300,300,
    null);
    }//end paintComponent()

    };// end inner class

    setContentPane(panel);

    }//end init()


    /**
    * Reads the image from a file and displays
    * an error message if image file is not
    * found
    * @return an Image object if the image file
    * is found. A null value otherwise
    */
    private Image getImage()
    {
    Image img = null;

    try
    {
    img = ImageIO.read(new File("img1.jpeg"));
    }
    catch(IOException e)
    {
    System.out.println("\nImage not found");
    //e.printStackTrace();
    }

    return img;

    }// end getImage()



    }//end class


    HTML code

    <html>

    <head>
    <title> MyApplet </title>
    </head>

    <body>
    <p class = normal align = "center">
    <applet code = "MyApplet.class" width = 300 height = 300>

    </applet>
    </p>
    </body>
    </html>

  • #2
    New to the CF scene
    Join Date
    Nov 2007
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    u have to use "codebase" attribute in applet tag in .html file, in that codebase attribute u have to give the full path of that applet.

    cheers
    BhargavKumar.B


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •