From 200c791eede30f1d6814e88e110b15531ba195cd Mon Sep 17 00:00:00 2001 From: Li REN Date: Mon, 17 Jun 2013 14:27:54 -0400 Subject: [PATCH] fix a bug causes npe. --- .../mage/client/dialog/PreferencesDialog.java | 6 +- .../mage/plugins/theme/ThemePluginImpl.java | 66 +++++++++---------- 2 files changed, 35 insertions(+), 37 deletions(-) diff --git a/Mage.Client/src/main/java/mage/client/dialog/PreferencesDialog.java b/Mage.Client/src/main/java/mage/client/dialog/PreferencesDialog.java index 3248afeff32..f3452fdb8a3 100644 --- a/Mage.Client/src/main/java/mage/client/dialog/PreferencesDialog.java +++ b/Mage.Client/src/main/java/mage/client/dialog/PreferencesDialog.java @@ -180,8 +180,10 @@ public class PreferencesDialog extends javax.swing.JDialog { @Override public String getDescription() { - if(extend.equals(".png") || extend.equals(".bmp")) - return "Image File(*.png, *.bmp)"; + if(extend.equals(".png")) + return "Image File(*.png)"; + if(extend.equals(".bmp")) + return "Image File(*.bmp)"; if(extend.equals(".jpg") || extend.equals(".jpg")) return "JEGP file(*.jpg,*.jepg)"; return ""; diff --git a/Mage.Client/src/main/java/org/mage/plugins/theme/ThemePluginImpl.java b/Mage.Client/src/main/java/org/mage/plugins/theme/ThemePluginImpl.java index f2a10884a10..46e8ab67637 100644 --- a/Mage.Client/src/main/java/org/mage/plugins/theme/ThemePluginImpl.java +++ b/Mage.Client/src/main/java/org/mage/plugins/theme/ThemePluginImpl.java @@ -78,25 +78,12 @@ public class ThemePluginImpl implements ThemePlugin { background = loadbuffer_default(); } - - - /* - if(loadimages()){ - int it = (int)Math.abs(Math.random()*(flist.getItemCount())); - filename = BackgroundDir + flist.getItem(it); - background = ImageIO.read(new File(filename)); - }else{ - filename = "/dragon.png"; - InputStream is = this.getClass().getResourceAsStream(filename); - if (is == null) - throw new FileNotFoundException("Couldn't find " + filename + " in resources."); - background = ImageIO.read(is); + if (background == null) { + background = loadbuffer_default(); + } + if (background == null){ + throw new FileNotFoundException("Couldn't find in resources."); } - */ - if (background == null) { - throw new FileNotFoundException("Couldn't find background file in resources."); - } - if (ui.containsKey("gamePanel") && ui.containsKey("jLayeredPane")) { ImagePanel bgPanel = new ImagePanel(background, ImagePanel.TILED); @@ -143,9 +130,13 @@ public class ThemePluginImpl implements ThemePlugin { BufferedImage res; String path = PreferencesDialog.getCachedValue(PreferencesDialog. KEY_BATTLEFIELD_IMAGE, ""); - if(path != null){ - res = ImageIO.read(new File(path)); - return res; + if(path != null && !path.equals("")){ + try{ + res = ImageIO.read(new File(path)); + return res; + }catch(Exception e){ + res = null; + } } return null; } @@ -176,23 +167,28 @@ public class ThemePluginImpl implements ThemePlugin { if (is == null) throw new FileNotFoundException("Couldn't find " + filename + " in resources."); background = ImageIO.read(is); - }else if(PreferencesDialog.getCachedValue(PreferencesDialog. - KEY_BACKGROUND_IMAGE, "") != null){ + }else{ String path = PreferencesDialog.getCachedValue(PreferencesDialog. - KEY_BATTLEFIELD_IMAGE, ""); - if(path != null){ - background = ImageIO.read(new File(path)); - }else{ - InputStream is = this.getClass().getResourceAsStream(filename); - if (is == null) - throw new FileNotFoundException("Couldn't find " + filename + " in resources."); - background = ImageIO.read(is); - } + KEY_BACKGROUND_IMAGE, ""); + if(path != null && !path.equals("")){ + try{ + File f = new File(path); + if(f != null) + background = ImageIO.read(f); + }catch(Exception e){ + background = null; + } + } } - - if (background == null) + if (background == null){ + InputStream is = this.getClass().getResourceAsStream(filename); + if (is == null) throw new FileNotFoundException("Couldn't find " + filename + " in resources."); - } catch (Exception e) { + background = ImageIO.read(is); + } + if(background == null) throw new + FileNotFoundException("Couldn't find " + filename + " in resources."); + }catch (Exception e) { log.error(e.getMessage(), e); return null; }