0

I wrote a game in Java and I want to put it on a HTML page. I know how, but the problem is that it gives me a AcessControlException (java.io.FilePermission "/SomePath/SomeFile.SomeExtension" "read").

Here is the Game class (loads the files at initGame method and init the 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();
    }
}

EDIT: Here is the Image2d class(holds ImageIO.read command)

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;
    }
}

And here is html code (it is inside a jar file called Blocks.jar):

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

I saw people getting the same error but, no answer gave me a solution that worked. So what shoud I do for load the images without erros?

PS: The JApplet is running fine in eclipse, but at the html page throws me this error.

Thanks!

A Cat
  • 629
  • 7
  • 20
  • 2
    First I did discourage you for using null layouts, you don't control many of the rendering differences between different systems that can change the size of individual components. You could try writing your content out as HTML or implement your own markup language – MadProgrammer May 22 '14 at 21:42
  • 2
    *"I'm stuck on saving/loading"* ..do you have a question, and if so, what is it? – Andrew Thompson May 23 '14 at 00:34
  • 1) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete and Verifiable Example). 2) Please learn common [Java naming conventions](http://java.sun.com/docs/books/jls/second_edition/html/names.doc.html#73307) (specifically the case used for the names) for class, method & attribute names & use them consistently. .. (continued) – Andrew Thompson May 23 '14 at 00:36
  • .. (continued) 3) Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556), along with layout padding & borders for [white space](http://stackoverflow.com/q/17874717/418556). – Andrew Thompson May 23 '14 at 00:39
  • What's the question/problem, bud? You've stated what you want to do and showed us some code, but this is not a show and tell site. Please ask a real question about the problem you are facing – Paul Samsotha May 23 '14 at 01:41
  • Sorry about the bad question, I changed it right now. – A Cat May 23 '14 at 16:56
  • What exactly is an `Image2d`? If you had posted an MCVE as I advised yesterday, I feel this would have progressed further.. – Andrew Thompson May 24 '14 at 07:36
  • Just a class that holds ImageIO.read command. It is now in the question. – A Cat May 24 '14 at 13:16

0 Answers0