访问被拒绝java.lang.runtimepermission exitvm.0

时间:2014-04-13 20:22:53

标签: java swing security applet jframe

我是applet的新手,我有一个问题访问被拒绝java.lang.runtimepermission exitvm.0它在Safari上显示我这个错误,Firefox没有向我显示任何内容,我不知道为什么会这样,它在我的Eclipse上完美运行,但它不能在Web浏览器上运行任何帮助?感谢。

    package test;



import javax.imageio.ImageIO;
import javax.swing.*;

import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
import java.awt.image.FilteredImageSource;
import java.awt.image.ImageFilter;
import java.awt.image.ImageProducer;
import java.awt.image.RGBImageFilter;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.net.URL;
import java.lang.reflect.Method;
public class app extends JApplet{
    URL url;
    String methodName;
    String f1="http://localhost/webpage/upload/suit.jpeg";
    String f2="http://localhost/webpage/customers/download.jpeg";
    boolean setR=false;
    Image image=null;
    int x=0;
    int y=0;
    int endX;
    int endY;
    int center1;
    int center2;
    int imageWidth;
    int imageHeight;
    boolean clicked=false;
    int initialX=0;
    int initialY=0;
    public void init(){
        load();
    }
    public void start(){
        load();
    }
    public void load(){
        //String methodName = method.getName();

     //   try{
            Image img= getImage(getDocumentBase(), f1);
            makeColorTransparent(img,Color.white);
            initialX=img.getWidth(null);
            initialY=img.getHeight(null);
      //  }//catch (InvocationTargetException ex) {
            //ex.getTargetException();

       // }
    }
      public Image makeColorTransparent
        (Image im, final Color color) {
        ImageFilter filter = new RGBImageFilter() {
          // the color we are looking for... Alpha bits are set to opaque
          public int markerRGB = color.getRGB() | 0xFF000000;

          public final int filterRGB(int x, int y, int rgb) {
            if ( ( rgb | 0xFF000000 ) == markerRGB ) {
              // Mark the alpha bits as zero - transparent
              return 0x00FFFFFF & rgb;
              }
            else {
              // nothing to do
              return rgb;
              }
            }
          }; 

        ImageProducer ip = new FilteredImageSource(im.getSource(), filter);
        image=Toolkit.getDefaultToolkit().createImage(ip);      
        drawing();
        return Toolkit.getDefaultToolkit().createImage(ip);
        }
        public void drawing(){
            Image img=null;
            //File f=new File("./src/trans1/download.jpeg");
            draw2();
            //buff=ImageIO.read(img);
            //try {
              //  img = ImageIO.read(new File("download.jpeg"));
        //  } catch (IOException e) {
        //  }
            BufferedImage buff=new BufferedImage(100, 100, BufferedImage.TYPE_INT_ARGB);
            //buff=img;
            ImageIcon pic1=null;
            //try {
            //  pic1 = new ImageIcon(ImageIO.read(new File("./src/trans1/download.jpeg")));
                System.out.println("I am working");
        //  } catch (IOException e) {
                // TODO Auto-generated catch block
            //  e.printStackTrace();
            //}
            JFrame frame=new JFrame();
            Graphics2D bGr = buff.createGraphics();
            bGr.drawImage(img, 0, 0, null);
            bGr.dispose();
            buff.getGraphics().drawImage(img, 100, 100, null);

            //JLabel label1=new JLabel(pic1);
            frame.setSize(300,300);
        //  frame.add(label1);
        //  this.add(label1);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add(this);
            this.addMouseListener(new MouseL());
            this.addMouseMotionListener(new MouseList());
            frame.setVisible(true);
        }
         public void draw2(){
             BufferedImage buff;
             int type;
             int width;
             int height;
             try {
                   //  buff=new BufferedImage(100, 100, BufferedImage.TYPE_INT_ARGB);
                     url = new URL (getCodeBase(), f2 );
                   buff=ImageIO.read(url);
                   Image img=ImageIO.read(url);
                   type=buff.getType();
                   width=buff.getWidth();
                   height=buff.getHeight();
                   Graphics g=buff.getGraphics();
                   g.drawImage(img, width, height, null);

           } catch (IOException e) {
                   // TODO Auto-generated catch block
                   e.printStackTrace();
           }

     }
          public void draw3(Graphics g, Image img){
              g.drawImage(img, 0, 0, null);
             // g.drawImage(img, img.getWidth(null), img.getWidth(null), null);
          }
      public void paint(Graphics g){
            Image img=getImage(getDocumentBase(), f2);
            draw3(g,img);
            draw2();
             if(image == null) return;
             if(clicked==false){
                imageWidth = image.getWidth(this);
                imageHeight = image.getHeight(this);}        
                endX=x+imageWidth;
                endY=y+imageHeight;
                g.drawImage(image, x, y, imageWidth, imageHeight, this );
                if(setR){
                    g.drawRect(x, y, imageWidth, imageHeight);
                    center1=(x+endX)/2;
                    center2=(y+endY)/2;
                    g.drawRect(center1, y, 3, 3);
                    g.drawRect(center1,endY, 3,3);
                    g.drawRect(x, center2, 3, 3);
                    g.drawRect(endX, center2, 3, 3);
                }
      }
        int inX;
        int inY;
        int subX;
        int subY;
        int diffX;
        int diffY;
        long ratio;
        boolean isSet=false;
        class MouseList implements MouseMotionListener{
            @Override
            public void mouseDragged(MouseEvent arg0) {
                setR=true;    
                if(isSet==true){
                        subX=arg0.getX();
                        subY=arg0.getY();
                        diffX=subX-inX;
                        diffY=subY-inY;
                        x=x+diffX;
                        y=y+diffY;
                        repaint();
                    inX=arg0.getX();
                    inY=arg0.getY();
                    //setR=false;
                    repaint();
                }else{
                if(clicked==true){
                            subY=arg0.getY();
                            diffY=subY-inY;
                            if(ratio==0){
                                ratio=1;
                            }
                            diffX=(int) (diffY/ratio);
                            y=y-diffY;
                            x=x-diffX;
                            imageHeight=imageHeight+diffY;
                            imageWidth=imageWidth+diffX;
                            //size of the screen
                            Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

                            //height of the task bar
                            Insets scnMax = Toolkit.getDefaultToolkit().getScreenInsets(getGraphicsConfiguration());
                            int taskBarSize = scnMax.bottom;

                            //available size of the screen 
                            //setLocation(screenSize.width - getWidth(), screenSize.height - taskBarSize - getHeight());
                            if(imageHeight<0 || imageWidth<0 || imageWidth>=screenSize.width ||imageHeight>=screenSize.height){
                                imageHeight=initialY;
                                x=0;
                                y=0;
                                imageWidth=initialX;
                            }
                            setR=false;
                            repaint();          
                }   
            }
        }
            @Override
            public void mouseMoved(MouseEvent arg0) {
                // TODO Auto-generated method stub

            }

        }
        class MouseL implements MouseListener{

            @Override
            public void mouseClicked(MouseEvent arg0) {
                // TODO Auto-generated method stub
                if((arg0.getX()<x &&arg0.getY()<y)||(arg0.getX()<x && arg0.getY()>endY)||(arg0.getX()>endX && arg0.getY()<y)||(arg0.getX()>endX && arg0.getY()>endY)){
                    System.out.println(" 1 I am working");
                    setR=false;
                    repaint();
                }

            }

            @Override
            public void mouseEntered(MouseEvent arg0) {
            /*  if(arg0.getX()>x && arg0.getX()<endX && arg0.getY()>y && arg0.getY()<endY){
                    if(isSet==false){
                        inX=arg0.getX();
                        inY=arg0.getY();
                        isSet=true;
                    }
                }*/
                if(arg0.getX()>x && arg0.getX()<endX && arg0.getY()>y && arg0.getY()<endY){
                    System.out.println("Yes i am here");
                    Cursor c=new Cursor(Cursor.HAND_CURSOR);
                    setCursor(c);
                }else if((arg0.getX()>=center1 && arg0.getX()<=center1+3)&&((arg0.getY()>=y && arg0.getY()<=y+3)||(arg0.getY()>=endY&& arg0.getY()<=endY+3))||(arg0.getY()>=center2 && arg0.getY()<=center2+3)&&((arg0.getX()>=x && arg0.getX()<=x+3)||(arg0.getX()>=endX && arg0.getX()<=endX+3))){
                    System.out.println("No i am here");
                    Cursor c=new Cursor(Cursor.N_RESIZE_CURSOR);
                    setCursor(c);
                }/*else if(arg0.getX()<x || arg0.getX()>endX && arg0.getY()<y || arg0.getY()>endY){
                    System.out.println(" 1 I am working");
                    setR=false;
                    repaint();
                }*/

            }

            @Override
            public void mouseExited(MouseEvent arg0) {
                // TODO Auto-generated method stub

            }

            @Override
            public void mousePressed(MouseEvent arg0) {
                System.out.println(x+" "+y+" "+endX+" "+endY+" "+arg0.getX()+" "+arg0.getY());
                if(arg0.getX()>x && arg0.getX()<endX && arg0.getY()>y && arg0.getY()<endY){
                    if(isSet==false){
                        inX=arg0.getX();
                        inY=arg0.getY();
                        isSet=true;
                    }
                    //repaint();
                    System.out.println("2");
                }
                else if((arg0.getX()>=center1 && arg0.getX()<=center1+3)&&((arg0.getY()>=y && arg0.getY()<=y+3)||(arg0.getY()>=endY&& arg0.getY()<=endY+3))||(arg0.getY()>=center2 && arg0.getY()<=center2+3)&&((arg0.getX()>=x && arg0.getX()<=x+3)||(arg0.getX()>=endX && arg0.getX()<=endX+3))){
                    if((arg0.getX()>=center1 && arg0.getX()<=center1+3)&&((arg0.getY()>=y && arg0.getY()<=y+3)||(arg0.getY()>=endY&& arg0.getY()<=endY+3))){
                            ratio=imageHeight/imageWidth;
                            inY=arg0.getY();
                            clicked=true;
                            }
                    //repaint();
                    System.out.println("3");
                    }else if((arg0.getX()<x || arg0.getY()<y)||(arg0.getX()<x || arg0.getY()>endY)||(arg0.getX()>endX || arg0.getY()<y)||(arg0.getX()>endX || arg0.getY()>endY)){
                        System.out.println(" 1 I am working");
                        setR=false;
                        repaint();
                        System.out.println("4");
                    }
                //repaint();
                /*else if((arg0.getX()<x || arg0.getX()>endX)|| (arg0.getY()<y || arg0.getY()>endY)){
                        System.out.println(" 1 I am working");
                        setR=false;
                        repaint();
                    }*/



            }

            @Override
            public void mouseReleased(MouseEvent arg0) {
                // TODO Auto-generated method stub
                isSet=false;
            //  clicked=false;
            //  setR=false;
            //  repaint();

            }

        }


    }

1 个答案:

答案 0 :(得分:2)

问题在于这一行:

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

设置EXIT_ON_CLOSE时,我们有效地尝试让JVM在帧关闭后立即关闭。 applet安全管理器(即使applet受信任)也会阻止它。

应该是:

frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

这将解决当前的问题,但更好的解决方案是废弃applet并将所有必要的控件放在一个框架中。完成后,使用Java Web Start启动框架。