2012-05-09 19 views
4

我正在爲我的應用程序啓動啓動畫面。它只是由Graphics2D繪製的靜態BufferedImage,在JFrame內部沒有裝飾。我的問題是:有時窗口繪製不正確,這意味着它並不總是包含我的圖像,有時只是灰色。我嘗試在第二個線程中創建啓動畫面,但它沒有幫助。我可以打電話給splashScreen.repaint()每一行,但它是胡說八道......這裏是我的代碼:灰色啓動畫面由於沒有重新粉刷

package SeriousSteve.MapEditor; 

import java.awt.Graphics2D; 
import java.awt.image.BufferedImage; 
import java.io.IOException; 
import java.net.URL; 
import java.util.logging.Level; 
import java.util.logging.Logger; 
import javax.imageio.ImageIO; 
import javax.swing.JFrame; 

/** 
* Simple splash screen, contains only the image. 
* 
* @author m4tx3 
*/ 
public class SplashScreen extends JFrame { 

    private BufferedImage image; 

    /** 
    * Constructor. Creates a window with splash screen, loads an image at the 
    * URL specified in imageURL, and finally displays this image. 
    * 
    * @param imageURL URL to the image that you want to put on the splash 
    *     screen. 
    */ 
    public SplashScreen() { 
     super("Splash screen"); 
    } 

    public void createSplashScreen(final URL imageURL) { 
     try { 
      image = ImageIO.read(imageURL); 
     } catch (IOException ex) { 
      Logger.getLogger(SplashScreen.class.getName()). 
        log(Level.SEVERE, null, ex); 
     } 
     setUndecorated(true); 
     setSize(image.getWidth(), image.getHeight()); 
     setLocationRelativeTo(null); 
     setVisible(true); 

     createGraphics(); 

     repaint(); 
    } 

    /** 
    * Creates a graphics context and draws a splash screen. 
    */ 
    private void createGraphics() { 
     Graphics2D g = (Graphics2D) getGraphics(); 
     g.drawImage(image, 0, 0, null); 
    } 

    /** 
    * Closes the splash screen and frees the memory taken by the image. 
    * 
    * Call this function when the loading is complete. 
    */ 
    public void close() { 
     setVisible(false); 
     image = null; 
     dispose(); 
    } 
} 

和:

SplashScreen splashScreen = new SplashScreen(); 
    splashScreen.createSplashScreen(getClass().getResource(
      "Img/splash.png")); 

順便說一句, - 我創造我自己的閃屏類,因爲我。我已經在1個jar文件中獲得了2個應用程序(遊戲和地圖編輯器)...我只想在地圖編輯器中顯示啓動畫面,所以我無法修改清單文件。

問候

回答

5

改爲使用JLabel。直接在框架上繪畫是一個壞主意。

看到這,它的工作原理每次:

import java.awt.image.BufferedImage; 
import java.io.IOException; 
import java.net.MalformedURLException; 
import java.net.URL; 
import java.util.logging.Level; 
import java.util.logging.Logger; 

import javax.imageio.ImageIO; 
import javax.swing.ImageIcon; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.SwingUtilities; 

/** 
* Simple splash screen, contains only the image. 
* 
* @author m4tx3 
*/ 
public class SplashScreen extends JFrame { 

    /** 
    * Constructor. Creates a window with splash screen, loads an image at the URL specified in imageURL, and finally displays this image. 
    * 
    * @param imageURL 
    *   URL to the image that you want to put on the splash screen. 
    */ 
    public SplashScreen() { 
     super("Splash screen"); 
    } 

    public void createSplashScreen(final URL imageURL) { 

     JLabel splashLabel = new JLabel(new ImageIcon(imageURL)); 
     add(splashLabel); 
      setSize(splashLabel.getPreferredSize()); 
     setUndecorated(true); 
     setLocationRelativeTo(null); 
     setVisible(true); 

     repaint(); 
    } 

    /** 
    * Closes the splash screen and frees the memory taken by the image. 
    * 
    * Call this function when the loading is complete. 
    */ 
    public void close() { 
     setVisible(false); 
     dispose(); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       try { 
        new SplashScreen().createSplashScreen(new URL(
          "http://art.gnome.org/download/themes/splash_screens/1334/Splash-GnomeDarkSplashScreen.png")); 
       } catch (MalformedURLException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
      } 
     }); 
    } 
} 
+0

我不得不添加'drawComponents(getGraphics());',但現在它工作!非常感謝:) – m4tx

0

覆蓋創建自己Graphicspaint方法來代替。

@Override 
public void paint(Graphics g) { 
    g.drawImage(image, 0, 0, null); 
} 

每次調用repaint時間,這又調用此方法。默認情況下,此方法將使用背景色(灰色)填充JFrame

+0

是的,我試過了,但是......它不工作:( – m4tx

+2

@ m4tx:你不應該在JFrame上直接繪製。 Guillaume得到了一個更好的答案,1+給他,但更好的可能是使用[SplashScreen類](http://docs.oracle.com/javase/7/docs/api/java/awt/SplashScreen.html)隨Java一起提供。 –

+0

@ m4tx奇怪,它爲我工作 – Jeffrey

4

1)g.drawImage(image, 0, 0, null);

應該

g2d.drawImage(img, 0, 0, img.getWidth(), img.getHeight(), SplashScreen.this); 

code from

import java.awt.BorderLayout; 
import java.awt.Container; 
import java.awt.Dimension; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.image.BufferedImage; 

import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JProgressBar; 
import javax.swing.JWindow; 

public class SplashScreen extends JWindow { 
    private JProgressBar bar; 
    private JLabel label; 

    public SplashScreen(final BufferedImage img) { 
    JPanel panel = new JPanel() { 
     public void paintComponent(Graphics g) { 
     super.paintComponent(g); 
     Graphics2D g2d = (Graphics2D) g.create(); 

     g2d.drawImage(img, 0, 0, img.getWidth(), img.getHeight(), 
      SplashScreen.this); 
     } 
    }; 
    panel.setPreferredSize(new Dimension(img.getWidth(), img.getHeight())); 

    Container content = getContentPane(); 
    content.setLayout(new BorderLayout()); 
    content.add(panel, BorderLayout.NORTH); 
    content.add(label = new JLabel(), BorderLayout.CENTER); 
    content.add(bar = new JProgressBar(), BorderLayout.SOUTH); 
    pack(); 
    setLocationRelativeTo(null); 
    } 

    public void setMessage(String msg) { 
    label.setText(msg); 
    pack(); 
    } 

    public void setProgress(int prog) { 
    bar.setValue(prog); 
    } 

    public void setIndeterminateProgress(boolean value){ 
    bar.setIndeterminate(value); 
    } 
} 

2)我建議給我們ËJWindowundecorated JDialog而不是JFrame (from tutorial)

+0

是的,它也可以。有關使用JWindow或JDialog的好建議。儘管我對JFrame有一點喜歡:你會在任務欄中看到一個圖標。 –

+0

@Guillaume Polet aaaach我感到「不舒服」,基本教程建議使用JFrame,+1 – mKorbel