Mazeryt
Mazeryt

Reputation: 915

Java read and write picture using MediaTracker

recently I wrote a code to get image (using jfileChooser) and then save it as new picture on hard Drive, and that worked. But when I try to use this code on other computers, or system it didn't work. The MediaTracker always contain a error but I can't display readable information about what going wrong, and I dont have idea how to fix this issue (but I don't won't to read again this source).

Thanks a lot for any ideas what can do wrong.

    JFileChooser jfc = new JFileChooser();
    jfc.setCurrentDirectory(new File("C:\\"));
    jfc.showOpenDialog(null);
    File sf = jfc.getSelectedFile();
    if( sf==null )
        return false;

    String iconName = sf.getAbsolutePath();
    URL imgUrl = null;
    try
    {
        imgUrl = new URL("file:\\"+iconName);
    }
    catch(MalformedURLException murle){
    //plujemy!
        System.out.println(murle);
    }
    imageA = getToolkit().getImage(imgUrl);
    MediaTracker mt = new MediaTracker(this);
    try
    {
        mt.addImage(imageA,0);
        mt.waitForAll();
    }
    catch (InterruptedException ie)
    {
        ie.printStackTrace(new PrintStream(System.out));
        return false;
    }
    if(mt.isErrorAny()){
        return false;
    }else{
        return true;
    }

Upvotes: 0

Views: 308

Answers (1)

trashgod
trashgod

Reputation: 205885

At a guess, absent a complete example, your call to waitForAll() is blocking the event dispatch thread. Note how the MediaTracker API example waits in a background thread. As an alternative, use SwingWorker to load the image(s), as shown here.

Upvotes: 1

Related Questions