2012-05-09 11 views
4

私のアプリケーション用のスプラッシュ画面を作っています。 Graphics2Dによって描画された静的なBufferedImageです.JFrameの内部には装飾がありません。私の問題は、ウィンドウが正しく描画されないことがあります。イメージが常にイメージを含むとは限らないことを意味します。私はすでに第2のスレッドでスプラッシュ画面を作成しようとしましたが、それは役に立たなかった。私はすべての行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
独自01​​を作成するのではなく、 paintメソッドをオーバーライドします

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

repaintを呼び出すたびに、このメソッドが呼び出されます。デフォルトでは、このメソッドはJFrameの背景色(グレー)で塗りつぶします。

+0

ええと、私も試しましたが、うまくいきませんでした...それは動作しません:( – m4tx

+2

@ m4tx:JFrameに直接描画しないでください。 Guillaumeさんは彼に1 +以上の答えを与えましたが、[SplashScreen class](http://docs.oracle.com/javase/7/docs/api/java/awt/SplashScreen.html)を使用する方が良いかもしれませんが、それはJavaに付属しています。 –

+0

@ m4tx Strange、それは私のために働いています – 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でなければなりませんe JWindowまたはundecorated JDialogではなく、JFrame (from tutorial)

+0

それもうまく動作します。 JWindowやJDialogの使い方についてのアドバイス。私がJFrameに好きなことは1つありますが、タスクバーにアイコンが表示されます。 –

+0

@Guillaume Polet aaaach私は "不快"だったので、JFrameの基本的なチュートリアルアドバイス、+1 – mKorbel

関連する問題