AccessControlException html页面中的Applet

时间:2014-05-22 21:37:31

标签: java html eclipse jar japplet

我用Java编写了一个游戏,我希望将它放在HTML页面上。我知道怎么做,但问题是它给了我一个 AcessControlException(java.io.FilePermission“/SomePath/SomeFile.SomeExtension”“read”)

这是Game类(在initGame方法中加载文件并初始化JApplet):

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;

import javax.swing.JApplet;

public class Game extends JApplet implements Runnable {
    private static final long serialVersionUID = 1L;

    private Thread thread;

    public static BufferedImage level;
    public static BufferedImage background;
    public static Image2d tileset;

    public static Sprite red;
    public static Sprite green;
    public static Sprite player;

    public static boolean mouse_left, mouse_right;
    public static boolean jumping;
    public static boolean play;
    private boolean running = false;

    private Image screen;

    public static RandomLevel map;
    public static Listener listener;
    public static Gui gui;

    public static int FALL_SPEED = 3;
    public static int JUMP_SPEED = 3;
    public static int JUMP_HEIGHT = 20;
    public static int jump_amount = 0;
    public static float START_OFFSET_SPEED = 3.0f;
    public static float OFFSET_SPEED = 2.0f;
    public static final int sxOffset = 0, syOffset = 0;
    public static int xOffset = 0, yOffset = 0;
    public static final int CURR_COLOR_SCALE = 32;

    public static Sprite[] tiles = new Sprite[2];
    public static int selected_color = 0;
    public static int SCORE = 0;

    public static final int TILE_SIZE = 16;
    public static final int SCALE = TILE_SIZE * 2;

    public static boolean left, right;
    public static int currTime = 0;

    public static final Dimension size = new Dimension(800, 600);
    public static final Dimension pixel = new Dimension(size.width - SCALE, size.height - SCALE);

    public void initGame() {
        try {
            URL levelUrl = new URL(getCodeBase(), "res/map.png");
            level = new Image2d(levelUrl).getImage();

            URL backgroundUrl = new URL(getCodeBase(), "res/background.png");
            background = new Image2d(backgroundUrl).getImage();

            URL tilesetUrl = new URL(getCodeBase(), "res/tileset.png");
            tileset = new Image2d(tilesetUrl);
        } catch (IOException e) {
        }

        red = new Sprite(new Image2d(tileset.crop(0, 0, Game.TILE_SIZE, Game.TILE_SIZE)));
        green = new Sprite(new Image2d(tileset.crop(1, 0, Game.TILE_SIZE, Game.TILE_SIZE)));
        player = new Sprite(new Image2d(tileset.crop(2, 0, Game.TILE_SIZE, Game.TILE_SIZE)));

        tiles[0] = red;
        tiles[1] = green;

        gui = new Gui();
        listener = new Listener();

        player.setX((size.width - player.getImage().getWidth(null)) / 2);
        player.setY((size.height - player.getImage().getHeight(null)) / 2 + 100);

        map = new RandomLevel(10000, 20, TILE_SIZE, SCALE, size, xOffset, yOffset);

        map.addCollsion(tiles[0]);

        addMouseListener(listener);
        addMouseMotionListener(new Mouse());
    }

    public static void switchCollision(Sprite tile) {
        map.collision.add(0, tile);

        for (int i = 0; i < map.collision.size(); i++) {
            if (i != 0)
                map.collision.remove(i);
        }
    }

    public void resetGame() {
        xOffset = sxOffset;
        yOffset = syOffset;
        OFFSET_SPEED = START_OFFSET_SPEED;
        Game.SCORE = 0;
        play = false;
        map.generateLevel();
    }

    public void startGame() {
        if (running)
            return;

        initGame();
        running = true;
        thread = new Thread(this);
        thread.start();
    }

    public void stopGame() {
        if (!running)
            return;

        running = true;
        try {
            thread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public void run() {
        screen = createVolatileImage(pixel.width, pixel.height);
        requestFocus();
        while (running) {
            long lastTime = System.nanoTime();
            long timer = System.currentTimeMillis();
            final double ns = 1000000000.0 / 60.0;
            double delta = 0;
            while (running) {
                long now = System.nanoTime();
                delta += (now - lastTime) / ns;
                lastTime = now;
                while (delta >= 1) {
                    update();
                    delta--;
                }
                render();

                if (System.currentTimeMillis() - timer > 100) {
                    timer += 1000;
                }
            }
            stop();
        }
    }

    public void update() {
        if (!play && mouse_left)
            play = true;

        if (play) {
            switchCollision(tiles[selected_color]);
            if (yOffset > 180)
                resetGame();

            map.update();
            map.setxOffset(xOffset);
            map.setyOffset(yOffset);
            player.update(25);
            gui.update();

            if (!map.getTileDownCollision(player))
                yOffset += FALL_SPEED;

            if (!Game.map.getTileRightCollision(player))
                xOffset += Math.round(OFFSET_SPEED);

            if (SCORE > 20)
                OFFSET_SPEED = 4.0f;
            if (SCORE > 40)
                OFFSET_SPEED = 5.0f;
            if (SCORE > 60)
                OFFSET_SPEED = 6.0f;
            if (SCORE > 80)
                OFFSET_SPEED = 7.0f;
            if (SCORE > 100)
                OFFSET_SPEED = 8.0f;
            if (SCORE > 120)
                OFFSET_SPEED = 9.0f;
            if (SCORE > 140)
                OFFSET_SPEED = 10.0f;

            if (jumping) {
                FALL_SPEED = 0;
                yOffset -= JUMP_SPEED;
                jump_amount++;

                if (jump_amount >= JUMP_HEIGHT) {
                    jumping = false;
                    FALL_SPEED = 3;
                    jump_amount = 0;
                }
            }
        }
    }

    public void render() {
        Graphics g = screen.getGraphics();

        g.setColor(Color.black);
        g.fillRect(0, 0, size.width, size.height);

        map.render(g);
        g.drawImage(player.getImage(), Math.round(player.getX()), Math.round(player.getY()), Game.TILE_SIZE + Game.SCALE, Game.TILE_SIZE + Game.SCALE, null);

        g.setFont(new Font("new Font", Font.PLAIN, 10));
        g.setColor(Color.white);
        g.drawString("CLICK ON THIS SIDE TO JUMP", 200 - xOffset, 240);
        g.drawString("CLICK ON THIS SIDE TO SWITCH COLOR", size.width - 350 - xOffset, 240);

        g.drawImage(background, 0, 0, null);
        g.drawImage(map.collision.get(0).getImage(), (size.width - (Game.TILE_SIZE + CURR_COLOR_SCALE)) / 2, CURR_COLOR_SCALE / 2 - 5, Game.TILE_SIZE + CURR_COLOR_SCALE, Game.TILE_SIZE + CURR_COLOR_SCALE, null);
        gui.render(g);

        g.setFont(new Font("new Font", Font.BOLD, 32));
        g.setColor(Color.yellow);
        g.drawString("SCORE: " + SCORE, 10, 32);

        g = getGraphics();

        g.drawImage(screen, 0, 0, size.width, size.height, 0, 0, pixel.width, pixel.height, null);
        g.dispose();
    }

    public void destroy() {
        System.exit(0);
    }

    public void init() {
        setSize(size);
        setVisible(true);

        startGame();
    }
}

编辑:这是Image2d类(持有ImageIO.read命令)

import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;

public class Image2d{

    private BufferedImage image;

    public Image2d(URL url) {
        try {   
            image = ImageIO.read(url);
        } catch (IOException e) {
            System.err.println("No file found at: " + url);
        }
    }

    public Image2d(String path) {
        try {
            image = ImageIO.read(new File(path));
        } catch (IOException e) {
            System.err.println("No file found at: " + path);
        }
    }

    public static BufferedImage load(URL url) {
        try {
            return ImageIO.read(url);
        } catch (IOException e) {
            System.err.println("No file found at: " + url.getPath());
            return null;
        }
    }

    public static BufferedImage load(String path) {
        try {
            return ImageIO.read(new File(path));
        } catch (IOException e) {
            System.err.println("No file found at: " + path);
            return null;
        }
    }

    public Image2d(BufferedImage image) {
        this.image = image;
    }

    public BufferedImage crop(int x, int y, int w, int h) {
        return image.getSubimage(x * w, y * h, w, h);
    }

    public static BufferedImage insert(Image2d target, Image2d component) {
        BufferedImage tile = new BufferedImage(target.getImage().getWidth(), target.getImage().getHeight(), BufferedImage.TYPE_INT_RGB);
        Graphics g = tile.getGraphics();
        g.drawImage(component.getImage(), 0, 0, component.getImage().getWidth(), component.getImage().getHeight(), null);
        g.drawImage(target.getImage(), 0, 0, target.getImage().getWidth(), target.getImage().getHeight(), null);

        return tile;
    }

    public static BufferedImage switchColor(BufferedImage image, Color color1, Color color2) {
        Graphics g = image.getGraphics();
        for (int y = 0; y < image.getHeight(); y++) {
            for (int x = 0; x < image.getWidth(); x++) {
                Color color = new Color(image.getRGB(x, y));

                if (color.getRed() == color1.getRed() && color.getGreen() == color1.getGreen() && color.getBlue() == color1.getBlue() && color.getAlpha() == color1.getAlpha()) {
                    g.setColor(color2);
                    g.fillRect(x, y, 1, 1);
                }
            }
        }
        return image;
    }

    public static BufferedImage fill(Color color, int width, int height) {
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics g = image.getGraphics();
        g.setColor(color);
        g.fillRect(0, 0, width, height);
        return image;
    }

    public static BufferedImage switchColor(BufferedImage image, Color[] color1, Color[] color2) {
        Graphics g = image.getGraphics();
        for (int y = 0; y < image.getHeight(); y++) {
            for (int x = 0; x < image.getWidth(); x++) {
                Color color = new Color(image.getRGB(x, y));

                for (int i = 0; i < color1.length; i++)
                    if (color.getRed() == color1[i].getRed() && color.getGreen() == color1[i].getGreen() && color.getBlue() == color1[i].getBlue() && color.getAlpha() == color1[i].getAlpha()) {
                        g.setColor(color2[i]);
                        g.fillRect(x, y, 1, 1);
                    }
            }
        }
        return image;
    }

    public static BufferedImage createYDropShadow(int width, int height, int startAlpha, float density, float offset, Color color) {
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        Graphics g = image.getGraphics();
        int alpha = startAlpha;
        for (int y = 0; y < image.getHeight(); y++) {
            if (alpha >= density)
                alpha -= density;
            for (int x = 0; x < image.getWidth(); x++) {
                g.setColor(new Color(color.getRed(), color.getGreen(), color.getBlue(), alpha));
                g.fillRect(x + Math.round(offset), y + Math.round(offset), 1 + Math.round(offset), 1 + Math.round(offset));
            }
        }
        return image;
    }

    public static BufferedImage createXDropShadow(int width, int height, int startAlpha, float density, float offset, Color color) {
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        Graphics g = image.getGraphics();
        int alpha = startAlpha;
        for (int x = 0; x < image.getWidth(); x++) {
            if (alpha >= density)
                alpha -= density;
            for (int y = 0; y < image.getHeight(); y++) {
                g.setColor(new Color(color.getRed(), color.getGreen(), color.getBlue(), alpha));
                g.fillRect(x + Math.round(offset), y + Math.round(offset), 1 + Math.round(offset), 1 + Math.round(offset));
            }
        }
        return image;
    }

    public BufferedImage getImage() {
        return image;
    }
}

这是html代码(它位于名为Blocks.jar的jar文件中):

<!DOCTYPE html>
<html>
 <body>
  <applet code="Game.class" archive="Blocks.jar" width="600" height="600"> </applet>
 </body>
</html>

我看到人们得到同样的错误,但没有回答给了我一个有效的解决方案。那么我在没有错误的情况下加载图像应该怎么做?

PS:JApplet在 eclipse 中正常运行,但是在html页面上抛出了这个错误。

谢谢!

0 个答案:

没有答案