开发者

SWT-App Loading .swf files from a runnable .jar or external folder

开发者 https://www.devze.com 2023-04-01 08:49 出处:网络
I\'m programming using the SWT Widget Library for Java in eclipse, and I\'m designing a runnable Java application. I\'ve got the application down, I just don\'t know how to load external .swf files fr

I'm programming using the SWT Widget Library for Java in eclipse, and I'm designing a runnable Java application. I've got the application down, I just don't know how to load external .swf files from a folder on "ALL" computers. I can load Images from any computer, because I use the getResourceAsStream line of code. But the "import com.docuverse.swt.flash.FlashPlayer" "loadMovie(arg, arg)" only takes a String.

So I did ClassName.class.getResource("blah.swf").getPath(); which gives you a string, I set it up, and running i开发者_JS百科t on eclipse it can perfectly find the file in the package. When I export it, the runnable Jar I made cannot find the "blah.swf" inside of the .jar file.

So there is my problem, how do I load my .swf files from within the .jar or from an external folder so clients can download along side the .jar executable application, so it can point to those swf files.

Thankyou.


Here is the way I talked about in comments (creating temp file and save flash animation from jar to it)

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

import com.docuverse.swt.flash.FlashPlayer;

public class SWTFlashPlayer {
    private FlashPlayer player = null;

    private final String FLASH_FILE_PATH = "/EdnCateDance.swf";
    private final String TMP_FILE_PREFFIX = "tmp_";
    private final String TMP_FILE_SUFFIX = ".swf";

    private File swfFile = null;

    public SWTFlashPlayer() {
        final Display display = new Display();
        final Shell shell = new Shell(display);
        shell.setLayout(new FillLayout());

        try {
            swfFile = copyFileFromJar(FLASH_FILE_PATH, TMP_FILE_PREFFIX, TMP_FILE_SUFFIX);
        } catch (IOException e) {
            e.printStackTrace();
            return;
        }

        player = new FlashPlayer(shell, SWT.NONE);
        player.loadMovie(0, swfFile.getAbsolutePath());
        player.setSize(150, 150);
        player.activate();

        shell.pack();
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch())
                display.sleep();
        }
        display.dispose();
    }

    /**
     * Copy file packed inside current jar to temp file
     * @param jarPath String - path inside jar
     * @param filePreffix String - temp file preffix
     * @param fileSuffix - temp file suffix
     * @throws IOException - temp file cannot be created or writing somehow fails
     */
    private File copyFileFromJar(String jarPath, String filePreffix, String fileSuffix) throws IOException {
        File toFile = File.createTempFile(filePreffix, fileSuffix);
        // delete file after application finishes
        toFile.deleteOnExit();

        if(!toFile.canWrite()) throw new IOException("File (" + toFile.getPath() + ") not exists or is not writable!");

        FileOutputStream fos = new FileOutputStream(toFile);
        InputStream is = this.getClass().getResourceAsStream(jarPath);

        if(is == null) throw new IOException("File on jar path could not be located or loaded!");

        int read = 0;
        byte bytes[] = new byte[1024];
        while ((read = is.read(bytes)) != -1) {
            fos.write(bytes, 0, read);
        }

        fos.flush();
        fos.close();

        return toFile;
    }

    public static void main(String[] args) {
        new SWTFlashPlayer();
    }
}

I used the EdnCateDance.swf flash file which is one of the examples in SWT Flash library.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号