loading image frmo client into applet


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: loading image frmo client into applet

  1. #1
    Join Date
    Nov 2003
    Posts
    3

    Exclamation loading image frmo client into applet

    Hi Everyone,

    I'm designing an applet that needs to use a JFileChooser to locate an image file on a client machine and paint that image onto a graphics canvas.

    I've read many articles about how Java doesn't allow applets to write to local disk unless they are signed with a certificate, and are accepted (I don't even know if that's the right terminology) by a user policy. Given that I only want to read a file from a local machine, is there any way I can implement a filechooser (without the process being expensive)?

    Every help is appreciated! Thanks!

  2. #2
    Join Date
    Mar 2003
    Posts
    834
    Unless the applet is signed it can do nothing outside its grey box in a web page - that means no reading, writing etc. on the client.
    ArchAngel.
    O:-)

  3. #3
    Join Date
    Nov 2003
    Posts
    3
    I thought that might be the case... However, I need to explore some alternatives.

    In HTML there's a simple "<input type='file' ... > tag that opens up a file browser, and allows you to locate a file. Is there any way to use javascript in a webpage to read in the file (image data) given the local file path and pass on the data to the applet? Is there an alternative way to read in image data on a client machine?

    I appreciate all the help by the way... You guys (and gals) have super response times. I'm heartily impressed. Thanks!

  4. #4
    Join Date
    Mar 2003
    Posts
    834
    hmmm...I was initially going down the route - you could pass the filename from Javascript to Java, but at some point you still need to get the binary data of the image into the applet somehow.

    What's so wrong with making it a signed applet?
    ArchAngel.
    O:-)

  5. #5
    Join Date
    Nov 2003
    Posts
    3
    Firstly, thank you very much for the replies...

    I would sign the applet, but as I understand it, it's an expensive process (maybe I'm not understanding it right!). I read you needed to buy a certificate from VeriSign or a similar company, which costs more than I'm willing to pay for a school project.

    I tried out the javascript and java applet route, and I can pass variables back and forth. However, I don't suppose there is a way in JavaScript to load an image into a variable? Is JavaScript powerful enough?

    Here's what I've tried doing to test it out:

    1. The webpage

    Code:
    <html>
    <script>
    	function uploadImage () {
    		var image = new Image();
    		image.src = document.myForm.uploadfile;
    		document.myApplet.paintPicture(image);
    	}
    </script>
    <body bgcolor="#A0A0FF">
    <h2><p align="center"><font color="white">Fusion of JavaScript and JApplets</font></p><hr></h2>
    
    <center><form name="myForm">
    <input type="button" value="Upload" name="upload" onclick="uploadImage();">
    &nbsp&nbsp
    <input type="file" name="uploadfile">
    </form></center>
    
    <p align="center">
    <applet code="myApplet.class" name="myApplet" height=310 width=410 MAYSCRIPT>
    </p>
    </body>
    </html>
    2. The Applet

    Code:
    import java.applet.*;
    import java.awt.*;
    import java.awt.image.*;
    import javax.swing.*;
    
    public class myApplet extends Applet {
    
    	public static final int WIDTH=400, HEIGHT=300;
    	public BufferedImage image;
    
    	public void init () {
    		image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_ARGB);
    		Graphics g = image.createGraphics();
    		g.fillRect(0,0,WIDTH,HEIGHT);
    		setBackground(Color.white);
    	}
    
    	public void paintPicture (Image newimage) {
    		Graphics g = image.createGraphics();
    		g.drawImage(newimage, 0, 0, null);
    		repaint();
    	}
    
    	public static void main (String args[]) {
    
    		JFrame myFrame = new JFrame();
    		myApplet myapplet = new myApplet();
    		myapplet.init();
    		myapplet.start();
    		Container myPane = myFrame.getContentPane();
    		myPane.add("Center", myapplet);
    		myFrame.setSize(410, 310);
    		myFrame.setResizable(false);
    		myFrame.setVisible(true);
    
    	}
    
    }
    I'm sad to say... Nothing happens ... Sorry to bombard you with all this code. It's nice to have an expert to consult! Thanks again!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center