SplashScreen Java更改Alpha [英] SplashScreen java change alpha

查看:82
本文介绍了SplashScreen Java更改Alpha的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在遍历几个png来为Java启动画面创建动画.

I am looping through a few png's to create an animation for a java splashscreen.

我以此来开始动画

java -splash:images/anim.png SplashDemo

并在类中使用png.您可以在此处找到该类- http://pastebin.com/UWm25QfY

and use the pngs inside the class . You can find the class here- http://pastebin.com/UWm25QfY

我唯一的问题是我选择使用anim.png开始动画的任何Alpha是最终的,以后将被所有PNG覆盖

My only problem is whatever alpha I choose to start the animation using anim.png is final and is being overwritten for all the pngs later

我尝试了AlphaComposite.Clear,Src,SrcOver,但是没有任何效果.如果我最初以0不透明度加载png,则整个动画都会消失.谁能告诉我如何摆脱这种情况?

I tried the AlphaComposite.Clear,Src,SrcOver but nothing worked. If I load a png iniatially with 0 opacity then the entire animation disappears. Could anyone tell me how to get rid of this?

推荐答案

因此,您面临的问题与以下事实有关:您正在绘制的Graphics上下文在它们之间从未真正清理"或休息"更新.我知道这很痛苦,但是确实有.

So, the problem you are facing has to do with the fact that the Graphics context you are painting is never actually "cleaned" or "rest" between updates. Which is a pain, I know, but there it is.

关于唯一的选择,是在绘制下一张图像之前,在每个循环上实际重置输出.

About the only choice you have is to actually reset the output on each cycle, before you paint the next image.

很幸运,SplashScreen实际上提供了URL到背景图像.这样一来,我们就可以自行加载图像,并根据需要在表面上重新绘画.

Lucky for use, SplashScreen actually provides the URL to background image. This allows us to load the image ourselves and repaint onto the surface as we need.

您还应该尽一切努力将Graphics上下文恢复到找到它的状态(当然,除了您在其上绘制的内容之外).可以通过在绘制图形状态之前对其进行复制来轻松完成此操作...

You should also make all best efforts to restore the Graphics context to the state you found it (except for what ever you painted on it of course). This can be eaisly done by making a copy of the graphics state before you paint to it...

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

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();
    }

}

已使用其他方法更新

基本上,随着图像大小的增加,更新速度会降低.取而代之的是,我将自己创建一个,以便更好地控制更新过程.

Basically, as the size of the image increases, the speed of the update decreases. Instead, I would simply create your own so you can better control the update process.

这使用JWindow作为基本窗口,并使用定制的JPanel作为主显示.

This uses an a JWindow as the base window and a customised JPanel as the main display.

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());
    }
}

它还将所有图像转换为与设备兼容的图像,这意味着它们的渲染速度更快,因为不需要实时转换其彩色调色板.

It also converts all the images to "device compatiable" images, meaning they should render faster as their color pallette's don't need to be converted on the fly.

背景图片为1563x1250,面部图片为300x300(具有不同的Alpha级别).

The background image was 1563x1250 and the face images are 300x300 (with varying alpha levels).

使用此示例,我得到了一个稳定的更新,没有问题,使用与SplashScreen相同的图像,真是太恐怖了...

Use this example, I got a steadily update without issue, using the same images with the SplashScreen, it was pretty horrible...

这篇关于SplashScreen Java更改Alpha的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆