Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: How do you put files and images where your applet can get them?

  1. #1
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Smile How do you put files and images where your applet can get them?

    I've found that apparently applets aren't allowed to access files and images on your computer. (I tried and got some kind of security exception.)

    How do you put the files and stuff into the html page? I suppose an

    <img source="./image.gif">

    could work to add an image to the html document, but how would the applet get to it?

    Also, if you had a file called text.txt, how would you get your applet to find it? I'm not even sure how to embed a text file in an html document, let alone get an applet to access it. I've no clue how to pull this off, so no applet code at the moment. (So no asking for an SSCCE, or you'll get an from me!)

    If you really want a JApplet, I can make one up.

     
    import javax.swing.JApplet;
    import java.io.File;
    import javax.swing.ImageIcon;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import java.awt.Color;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;
    import javax.swing.JPanel;
    import java.util.Scanner;
    import javax.swing.JMenuBar;
    import javax.swing.JMenu;
    import javax.swing.JMenuItem;
    import javax.swing.JLabel;
    import javax.swing.Image;
    import javax.swing.Icon;
    import java.awt.GridLayout;
     
    public class TestApplet extends JApplet
    {
     
    private JPanel contentPane;
    private JScrollPane contentScroll;
    private File textFile;
    private Scanner fileReader;
    private JMenuBar mBar;
    private JMenu file, edit, view, help, save;
    private JMenuItem exit, save2, load, saveAs, print, help2, copy, paste, cut, selectAll;
    private JTextArea textArea;
    private JScrollPane textScroll;
    private JLabel image;
    private ImageIcon icon;
     
    public void init()
    {
    contentPane = new JPanel();
    contentScroll = new JScrollPane(contentPane, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    setContentPane(contentScroll);
     
    mBar = new JMenuBar();
    file = new JMenu("File");
    mBar.add(file);
    edit = new JMenu("Edit");
    mBar.add(edit);
    view = new JMenu("View");
    mBar.add(view);
    help = new JMenu("Help");
    mBar.add(help);
    help2 = new JMenuItem("Help");
    help.add(help2);
    help.addSeparator();
    save = new JMenu("Save");
    file.add(save);
    file.addSeparator();
    save2 = new JMenuItem("Save");
    save.add(save2);
    save.addSeparator();
    saveAs = new JMenuItem("Save As");
    save.add(saveAs);
    save.addSeparator();
    load = new JMenuItem("Load");
    file.add(load);
    file.addSeparator();
    exit = new JMenuItem("Exit");
    file.add(exit);
    file.addSeparator();
    copy = new JMenuItem("Copy");
    edit.add(copy);
    edit.addSeparator();
     
    textArea = new JTextArea(200,200);
    setJMenuBar(mBar);
    textScroll = new JScrollPane(textArea, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    contentPane.setLayout(new GridLayout(2,1));
    contentPane.add(textScroll);
     
    try
    {
    textReader = new Scanner( new File("./text.txt")); // how do I get it to find this file?
    }
     
    catch(java.io.FileNotFoundException fnfe)
    {
    System.out.println("File not found.");
    }
     
     
    icon = new ImageIcon("./image.gif"); // how do I get it to find this image?
     
    image = new JLabel("I am an Image in a JApplet!", icon, JLabel.LEFT);
    contentPane.add(image);
     
     
     
     
     
    }
     
    public void start()
    {
     
    setBackground(Color.GREEN);
     
    }
     
    public void run()
    {
    start();
    }
     
    }

    Not sure if all the imports will compile and don't quite recall how to set up an applet that much either. But I can look that part up pretty quickly in the API. I already have some samples of JApplets, somewhere. However, how to access images from an html from with the applet and how to get text files to go into a spot that the files can be found.


  2. #2
    Junior Member
    Join Date
    Feb 2012
    Posts
    9
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: How do you put files and images where your applet can get them?

    Here is a way to do images. I will need to rewrite some of my code for a text file and I don't have the time to do it atm. How I have it set up you upload the file from a location on your webhost. Really you could do this from any website that has an image. You just load it using the URL. I have the imageCache in there incase you are planning on reusing the same image over and over no need to reload it every time, however you will want to do some management on that. It could get really large quickly if you plan on using a bunch of images in your program, especially if they are high res images.

    	private static HashMap<String, BufferedImage> imageCache = new HashMap<String, BufferedImage>();
     
    	public static BufferedImage getImage(String imageURL)
    	{
    		BufferedImage picture = null;
    		URL url = null;
    		try {
    			url = new URL("http://imageName");
    		} catch (MalformedURLException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		if(!imageCache.containsKey(url))
    		{	
    			try {
    				picture = ImageIO.read(url);
    				imageCache.put(imageURL, picture);
    			} 
    			catch (IOException e) {
    				e.printStackTrace();
    			}
    		}
    		else
    		{
    			picture = imageCache.get(url);
    		}
    		return picture;
    	}

  3. The Following User Says Thank You to mesatrin1 For This Useful Post:

    javapenguin (February 25th, 2012)

  4. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: How do you put files and images where your applet can get them?

    Your applet could
    either get them from the jar file the class files are in (treat them as resources)
    or it could send an HTTP GET request to its server and get them from there.

  5. The Following User Says Thank You to Norm For This Useful Post:

    javapenguin (February 25th, 2012)

Similar Threads

  1. [SOLVED] files size for files inside the JAR file
    By Natherul in forum Java Theory & Questions
    Replies: 3
    Last Post: February 9th, 2012, 03:17 PM
  2. How to create or edit .ser files in Jar files
    By xbill in forum Java IDEs
    Replies: 1
    Last Post: May 18th, 2011, 05:15 AM
  3. Seraching through files in a folder for a pattern match inside the files.
    By dazzabiggs in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 2nd, 2011, 08:35 AM
  4. Replies: 1
    Last Post: March 22nd, 2011, 06:59 PM
  5. What are the best way of placing images in GUI?
    By Ciwan in forum AWT / Java Swing
    Replies: 5
    Last Post: February 26th, 2009, 05:19 PM