0

I am trying to create an applet that responses to a mouse click and then draws square on the screen and stores the mouse co-ordinates in string.Finally if I press 'S' key then it saves the string in a file named "Level1.txt". But when I am running this applet and pressing S nothing gets saved in file.I don't the reason.Please help. Here is my code:-

 import java.io.*;
import java.awt.event.*;
import java.applet.*;
import java.awt.*;
public class levelEditor extends Applet{
int x,y;
boolean clicked;
FileWriter fw;
PrintWriter pw;
BufferedWriter bw;
String pix;
    public void init() {
        setSize(500,500);
        x=0;
        y=0;
        pix="";

        clicked=false;
        addMouseListener(new MouseAdapter() {
            public void mouseReleased(MouseEvent e){
                clicked=true;
                x=e.getX();
                y=e.getY();
                pix=pix+x+","+y+" ";
                repaint();
            }
        } );
        addKeyListener(new KeyAdapter() {
            public void keyPressed(KeyEvent e){
                try{
                    if(e.getKeyCode()==KeyEvent.VK_S){
                        fw=new FileWriter("Level1.txt");
                        bw=new BufferedWriter(fw);
                        pw=new PrintWriter(bw);
                        pw.println(pix);
                        pw.close();
                        System.exit(0);
                    }
                    }
                    catch(Exception exp)
                    {

                    }
            }
        });

    }
    public void paint(Graphics g){
        update(g);
    }
public void update(Graphics g){
    if(clicked){
    g.setColor(Color.GREEN);
    g.fillRect(x-5, y-5,10,10);

    }
}
}
Gunjan
  • 81
  • 9
  • Have you googled things like "write file from java applet"? Also: http://stackoverflow.com/questions/17388523/how-to-write-to-a-file-in-applets-in-java?rq=1 – reto Feb 13 '15 at 06:47
  • yes but I haven't got any proper answer – Gunjan Feb 13 '15 at 07:06
  • 1) Why code an applet? If it is due to the teacher specifying it, please refer them to [Why CS teachers should **stop** teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) Why use AWT? See [this answer](http://stackoverflow.com/questions/6255106/java-gui-listeners-without-awt/6255978#6255978) for many good reasons to abandon AWT using components in favor of Swing. 3) Use a logical and consistent form of indenting code lines and blocks. The indentation is intended to make the flow of the code easier to follow! – Andrew Thompson Feb 14 '15 at 01:47
  • `catch(Exception exp) { }` should be `catch(Exception exp) { e.printStackTrace(); }`.. Then be sure the [Java Console](http://www.java.com/en/download/help/javaconsole.xml) is configured to show. If there is no output at the default level, raise the level and try it again. As an aside, the code will need to be digitally signed and request `all-permissions` in the manifest and JNLP launch file (if used) in order to get the right to store a local file. It can be done with less permissions if using the JNLP API to store/retrieve the info. – Andrew Thompson Feb 14 '15 at 01:50
  • I have no teacher to teach me how to use and create applet.I learned it from internet and Herbert Schilt's book.Over there it was mentioned that before moving on to swing java.awt package must be learnt because swing is built on top of it.If you can mention a good book to learn so,then I will be extremely grateful to you. – Gunjan Feb 14 '15 at 16:59

1 Answers1

0

You need a signed applet to access the local file system. See the Oracle docs: What Applets Can and Cannot do.

BetaRide
  • 16,207
  • 29
  • 99
  • 177
  • *"See the Oracle docs: What Applets Can and Cannot do."* From that document.. *"Applets that are not signed are restricted to the security sandbox, and run only if the user accepts the applet."* Well, not really. These days, an unsigned applet will not make it to screen. That makes the rest of the document largely redundant, in that once code **is digitally signed,** it is just a matter of specifying the correct security levels in the manifest and JNLP and the applet can do whatever it needs to do. – Andrew Thompson Feb 14 '15 at 01:54