diff --git a/res/data/data.bin b/res/data/data.bin index 2f0ff67..5984bf6 100644 Binary files a/res/data/data.bin and b/res/data/data.bin differ diff --git a/src/a.java b/src/a.java index fc0bee9..3d81458 100644 --- a/src/a.java +++ b/src/a.java @@ -2,6 +2,8 @@ import java.nio.file.Files; import java.nio.file.Paths; +import utils.Utils; + //import javax.microedition.rms.RecordComparator; //import javax.microedition.rms.RecordFilter; //import javax.microedition.rms.RecordStore; @@ -11,7 +13,7 @@ public final class a { public final void loadLevelNo() { try { - byte[] data = Files.readAllBytes(Paths.get(getClass().getResource("/data/data.bin").toURI())); + byte[] data = Files.readAllBytes(Paths.get(Utils.res_folder+"/data/data.bin")); this.levels_reached = data[1]; } catch (Exception e) { e.printStackTrace(); diff --git a/src/b.java b/src/b.java index a39fc4a..d87c52c 100644 --- a/src/b.java +++ b/src/b.java @@ -25,8 +25,8 @@ import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; -import java.net.URISyntaxException; import java.nio.file.Files; +import java.nio.file.Path; import java.nio.file.Paths; import java.util.Random; @@ -35,6 +35,8 @@ import javax.sound.sampled.Clip; import javax.swing.JFrame; +import utils.Utils; + public final class b implements Runnable, KeyListener { private Random a; private static int scrn_width = 176; @@ -187,8 +189,8 @@ private void a() { InputStream bg_music; InputStream musicDie; try { - bg_music = new FileInputStream(new File(getClass().getResource("music.mid").toURI())); - musicDie = new FileInputStream(new File(getClass().getResource("musicDie.mid").toURI())); + bg_music = Utils.deepCopy(new FileInputStream(Utils.res_folder + "/music.mid")); + musicDie = Utils.deepCopy(new FileInputStream(Utils.res_folder + "/musicDie.mid")); musicBGStream = AudioSystem.getAudioInputStream(new BufferedInputStream(bg_music)); musicDieStream = AudioSystem.getAudioInputStream(new BufferedInputStream(musicDie)); bg_clip = AudioSystem.getClip(); @@ -524,7 +526,7 @@ private void b_void() { try { byte[] data = getBytesFromSave(); data[0] = ak; - FileOutputStream write_data = new FileOutputStream(new File("res/data/data.bin")); + FileOutputStream write_data = new FileOutputStream(new File(Utils.res_folder + "/data/data.bin")); write_data.write(data); write_data.close(); } catch (Exception e) { @@ -545,7 +547,8 @@ private void c() { private static boolean d() { DataInputStream data; try { - data = new DataInputStream(new BufferedInputStream(new FileInputStream(new File("res/data/data.bin")))); + InputStream stream = new FileInputStream(new File(Utils.res_folder + "/data/data.bin")); + data = new DataInputStream(stream); byte b = getBytesFromSave()[0]; if (b == (byte) 0xFF) { return false; @@ -671,11 +674,11 @@ private static void b_void_short_array2d(final short[][] array) { } } } - + public static byte[] getBytesFromSave() { byte[] data = null; try { - data = Files.readAllBytes(Paths.get(b.class.getResource("/data/data.bin").toURI())); + data = Files.readAllBytes(Paths.get(Utils.res_folder+"/data/data.bin")); } catch (Exception e) { e.printStackTrace(); } @@ -1140,21 +1143,26 @@ public final void paint(final Graphics2D graphics) { } public final void run() { - while (b.canRun) { - try { - this.render(); - this.bw = 60L - (System.currentTimeMillis() - this.bv); - if (this.bw <= 0L) { - this.bw = 15L; + try { + while (b.canRun) { + try { + this.render(); + this.bw = 60L - (System.currentTimeMillis() - this.bv); + if (this.bw <= 0L) { + this.bw = 15L; + } + Thread.sleep(this.bw); + this.bv = System.currentTimeMillis(); + } catch (Exception ex) { + ex.printStackTrace(); } - Thread.sleep(this.bw); - this.bv = System.currentTimeMillis(); - } catch (Exception ex) { - ex.printStackTrace(); } - } - if (rocket.a != null) { - rocket.a.destroyApp(true); + if (rocket.a != null) { + rocket.a.destroyApp(true); + System.exit(0); + } + } catch (Exception e) { + e.printStackTrace(); System.exit(0); } } @@ -1422,7 +1430,7 @@ private void g() { private void saveNewScore(final int newScore) { try { - File file = new File("res/data/data.bin"); + File file = new File(Utils.res_folder+"/data/data.bin"); byte[] data = getBytesFromSave(); for (int index = 0; index < 5; index++) { int offset = 2 + 4 * index; @@ -1895,7 +1903,7 @@ private void a(final int n) { if (level_no > a.levels_reached) { a.levels_reached = level_no; try { - File file = new File("res/data/data.bin"); + File file = new File(Utils.res_folder+"/data/data.bin"); byte[] data = getBytesFromSave(); data[1] = (byte) a.levels_reached; FileOutputStream writeFile = new FileOutputStream(file); diff --git a/src/rocket.java b/src/rocket.java index d2151a5..7f76d59 100644 --- a/src/rocket.java +++ b/src/rocket.java @@ -1,5 +1,12 @@ +import java.io.File; +import java.net.URISyntaxException; +import java.nio.file.Path; +import java.nio.file.Paths; + import javax.swing.JFrame; +import utils.Utils; +import static javax.swing.JOptionPane.showMessageDialog; //import javax.microedition.lcdui.Display; //import javax.microedition.lcdui.Displayable; //import javax.microedition.midlet.MIDlet; @@ -26,9 +33,21 @@ public rocket() { d.thread.start(); } - public static void main(String[] args) { + public static void main(String[] args) throws URISyntaxException { + Utils.jar_folder = (getJarDirectory().toString()); + Utils.res_folder = Utils.jar_folder + "/" + "res"; + File directory = new File(Utils.res_folder); + if (directory.exists() == false) { + showMessageDialog(null, "resource folder not found at jar location: "+Utils.jar_folder); + System.exit(0); + } new rocket(); } + private static Path getJarDirectory() throws URISyntaxException { + Path jarPath = Paths.get(rocket.class.getProtectionDomain().getCodeSource().getLocation().toURI()) + .toAbsolutePath(); + return jarPath.getParent(); + } public void startApp() { a = this; diff --git a/src/utils/Image.java b/src/utils/Image.java index f836953..1a2e176 100644 --- a/src/utils/Image.java +++ b/src/utils/Image.java @@ -2,8 +2,7 @@ import java.awt.Graphics; import java.awt.image.BufferedImage; -import java.io.File; -import java.io.IOException; +import java.io.FileInputStream; import javax.imageio.ImageIO; @@ -12,7 +11,7 @@ public class Image { public static java.awt.Image createImage(String string) { BufferedImage img = null; try { - img = ImageIO.read(utils.Image.class.getResource("../"+string)); + img = ImageIO.read(Utils.deepCopy(new FileInputStream(Utils.res_folder+string))); } catch (Exception e) { e.printStackTrace(); } @@ -23,7 +22,6 @@ public static void drawImage(Graphics g, java.awt.Image img, int x, int y, int a if (anchor == 20 || anchor == 0) { g.drawImage(img, x, y, null); } else if (anchor == 17) { - // top | horizontal center g.drawImage(img, x - (img.getWidth(null) / 2), y, null); } else { System.err.println("Anchor: " + anchor); diff --git a/src/utils/Utils.java b/src/utils/Utils.java new file mode 100644 index 0000000..9a9083b --- /dev/null +++ b/src/utils/Utils.java @@ -0,0 +1,24 @@ +package utils; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; + +public class Utils { + + + public static String jar_folder = null; + public static String res_folder = null; + + public static InputStream deepCopy(InputStream inputStream) throws IOException { + long past = System.currentTimeMillis(); + ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); + byte[] buffer = new byte[1024]; + int bytesRead; + while ((bytesRead = inputStream.read(buffer)) != -1) { + byteArrayOutputStream.write(buffer, 0, bytesRead); + } + return new ByteArrayInputStream(byteArrayOutputStream.toByteArray()); + } +}