2013-10-10 74 views
0

我正在循環幾個PNG來創建一個java splashscreen的動畫。SplashScreen java change alpha

我開始使用這個

java -splash:images/anim.png SplashDemo 

動畫和使用類裏面的PNG圖像。您可以在這裏 - 類http://pastebin.com/UWm25QfY

我唯一的問題是什麼阿爾法我選擇使用anim.png是最後啓動動畫和被覆蓋所有後來

我試過AlphaComposite.Clear的PNG圖像, Src,SrcOver但沒有任何工作。如果我用0不透明度加載png,則整個動畫消失。有誰能告訴我如何擺脫這個?

+0

是鏈接的代碼,你正在使用或已經改變了你的唯一的代碼? – MadProgrammer

+0

@MadProgrammer http://pastebin.com/UWm25QfY我修改了一下對不起,我更新了帖子 –

回答

1

因此,您所面臨的問題與您正在繪製的Graphics上下文事實上在更新之間從未實際「清除」或「休息」有關。我知道,這是一種痛苦,但它就是這樣。

關於您唯一的選擇是在繪製下一個圖像之前,在每個循環中實際重置輸出。

幸運使用,SplashScreen實際上提供URL到背景圖片。這允許我們自己加載圖像並根據需要重新繪製到表面上。

您還應該盡一切努力將Graphics上下文恢復到您找到的狀態(當然除了您曾經畫過的任何東西)。這可以通過圖形狀態的副本eaisly做你畫到它之前...

Graphics2D g2d = (Graphics2D)g.create(); 
// Do you're painting here... 
// Release the state when you're done. 
g2d.dispose(); 

enter image description here

import java.awt.AlphaComposite; 
import java.awt.Dimension; 
import java.awt.Frame; 
import java.awt.Graphics2D; 
import java.awt.GraphicsConfiguration; 
import java.awt.GraphicsDevice; 
import java.awt.GraphicsEnvironment; 
import java.awt.Image; 
import java.awt.SplashScreen; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.event.WindowAdapter; 
import java.awt.event.WindowEvent; 
import java.awt.event.WindowListener; 
import java.awt.image.BufferedImage; 
import java.io.File; 
import java.io.IOException; 
import java.util.ArrayList; 
import java.util.logging.Level; 
import java.util.logging.Logger; 

import javax.imageio.ImageIO; 

public class SplashScreen100 extends Frame implements ActionListener { 

    static ArrayList<Image> imgs; 

    private static final long serialVersionUID = 1L; 
    private BufferedImage background; 

    protected void renderSplashFrame(Graphics2D g, Image bg) { 

     // Get the splash screen size... 
     Dimension size = SplashScreen.getSplashScreen().getSize(); 
     int width = size.width; 
     int height = size.height; 

     // Center the image within the splash screen 
     int x = (width - bg.getWidth(null))/2; 
     int y = (height - bg.getHeight(null))/2; 
     Graphics2D g2d = (Graphics2D) g.create(); 

     // Draw the background 
     g2d.drawImage(background, 0, 0, null); 
     // Apply alpha composite 
     g2d.setComposite(AlphaComposite.SrcOver.derive(0.5f)); 
     // Draw the image... 
     g2d.drawImage(bg, x, y, null); 
     g2d.dispose(); 
    } 

    public SplashScreen100() { 
     super("SplashScreen demo"); 
     final SplashScreen splash = SplashScreen.getSplashScreen(); 
     if (splash == null) { 
      System.out.println("SplashScreen.getSplashScreen() returned null"); 
      return; 
     } 
     Graphics2D g = splash.createGraphics(); 
     if (g == null) { 
      System.out.println("g is null"); 
      return; 
     } 

     try { 
      background = ImageIO.read(splash.getImageURL()); 

      for (Image img : imgs) { 
       renderSplashFrame(g, img); 
       splash.update(); 
       // I put this in to slow the updates down... 
       try { 
        Thread.sleep(250); 
       } catch (InterruptedException ex) { 
        Logger.getLogger(SplashScreen100.class.getName()).log(Level.SEVERE, null, ex); 
       } 
      } 
     } catch (IOException exp) { 
      exp.printStackTrace(); 
     } 
     splash.close(); 
    } 

    public void actionPerformed(ActionEvent ae) { 
     System.exit(0); 
    } 

    public static void main(String args[]) { 
     System.setProperty("sun.java2d.opengl", "True"); 
     GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment(); 
     GraphicsDevice device = env.getDefaultScreenDevice(); 
     GraphicsConfiguration config = device.getDefaultConfiguration(); 

     imgs = new ArrayList<Image>(); 
     for (File file : new File("\path\to\images").listFiles()) { 
      if (file.getName().toLowerCase().endsWith(".png")) { 
       try { 
        Image buffy = ImageIO.read(file); 
        imgs.add(buffy); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
      } 
     } 
     SplashScreen100 test = new SplashScreen100(); 
    } 

} 

用不同的方法更新

基本上,圖像的大小增加,更新速度降低。相反,我會簡單地創建自己的,以便更好地控制更新過程。

這使用一個a JWindow作爲基本窗口和一個自定義JPanel作爲主要顯示。

enter image description here

import java.awt.BorderLayout; 
import java.awt.Dimension; 
import java.awt.EventQueue; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.GraphicsConfiguration; 
import java.awt.GraphicsEnvironment; 
import java.awt.Image; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.event.MouseAdapter; 
import java.awt.event.MouseEvent; 
import java.awt.event.MouseListener; 
import java.awt.image.BufferedImage; 
import java.io.File; 
import java.io.IOException; 
import java.util.ArrayList; 
import java.util.Collections; 
import java.util.List; 
import javax.imageio.ImageIO; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.Timer; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 
import static splashscreen.MySplashScreen.createCompatibleImage; 
import static splashscreen.MySplashScreen.getGraphicsConfiguration; 

public class DifferentSplashScreen { 

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

    public DifferentSplashScreen() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
       } 

       JWindow frame = new JWindow(); 
       frame.setAlwaysOnTop(true); 
       frame.setLayout(new BorderLayout()); 
       frame.add(new SplashPane()); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 
     }); 
    } 

    public class SplashPane extends JPanel { 

     private BufferedImage background; 
     private List<BufferedImage> frames; 
     private int frameIndex; 
     private BufferedImage currentFrame; 

     public SplashPane() { 
      try { 
       background = ImageIO.read(new File("C:\\Users\\shane\\Dropbox\\MegaTokyo\\2005-09-29-3957.jpeg")); 
       frames = new ArrayList<>(40); 
       List<BufferedImage> images = new ArrayList<>(20); 
       for (int index = 0; index < 20; index++) { 
        try { 
         BufferedImage buffy = ImageIO.read(new File(index + ".png")); 
         images.add(createCompatibleImage(buffy)); 
        } catch (IOException e) { 
         e.printStackTrace(); 
        } 
       } 
       frames.addAll(images); 
       Collections.reverse(images); 
       frames.addAll(images); 
      } catch (IOException ex) { 
       ex.printStackTrace(); 
      } 
      final Timer timer = new Timer(40, new ActionListener() { 
       @Override 
       public void actionPerformed(ActionEvent e) { 
        if (frameIndex >= frames.size()) { 
         frameIndex = 0; 
        } 
        currentFrame = frames.get(frameIndex); 
        frameIndex++; 
        repaint(); 
       } 
      }); 
      timer.start(); 
     } 

     @Override 
     public Dimension getPreferredSize() { 
      return background == null ? new Dimension(200, 200) : new Dimension(background.getWidth(), background.getHeight()); 
     } 

     @Override 
     protected void paintComponent(Graphics g) { 
      super.paintComponent(g); 
      if (background != null) { 
       Graphics2D g2d = (Graphics2D) g.create(); 
       int x = (getWidth() - background.getWidth())/2; 
       int y = (getHeight() - background.getHeight())/2; 
       g2d.drawImage(background, x, y, this); 

       if (currentFrame != null) { 

        x = (getWidth() - currentFrame.getWidth())/2; 
        y = (getHeight() - currentFrame.getHeight())/2; 
        g2d.drawImage(currentFrame, x, y, this); 

       } 
       g2d.dispose(); 
      } 
     } 
    } 

    public static GraphicsConfiguration getGraphicsConfiguration() { 
     return GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration(); 
    } 

    public static BufferedImage createCompatibleImage(BufferedImage master) { 
     BufferedImage img = createCompatibleImage(master, master.getWidth(), master.getHeight()); 
     Graphics2D g2d = img.createGraphics(); 
     g2d.drawImage(master, 0, 0, null); 
     g2d.dispose(); 
     return img; 
    } 

    public static BufferedImage createCompatibleImage(BufferedImage image, 
      int width, int height) { 
     return getGraphicsConfiguration().createCompatibleImage(width, height, image.getTransparency()); 
    } 
} 

它也將所有圖像,以「設備compatiable」的圖像,這意味着他們應該呈現更快的顏色調色板的不需要在飛行轉換。

背景圖像是1563x1250,臉部圖像是300x300(具有不同的alpha級別)。

用這個例子,我有一個使用與SplashScreen相同的圖像沒有問題不斷更新,這是非常可怕的......

+0

這是不是會搞砸幀率?我會嘗試tomoro –

+0

什麼幀率? – MadProgrammer

+0

當我遍歷圖像時,我得到的幀速率有時會下降。任何改善幀率的建議。我認爲你解決了阿爾法問題非常感謝你 –