SplashScreen java更改alpha

SplashScreen java更改alpha,java,animation,png,splash-screen,Java,Animation,Png,Splash Screen,我正在循环使用一些png来为java splashscreen创建动画 我用这个开始动画 java -splash:images/anim.png SplashDemo 并在类内使用PNG。你可以在这里找到班级- 我唯一的问题是,我选择使用anim.png启动动画的任何alpha都是最终的,并且将在以后覆盖所有png 我试过AlphaComposite。清晰,Src,SrcOver,但没有任何效果。如果我以不透明度为0的方式加载png,则整个动画将消失。有谁能告诉我如何摆脱这个问题吗?因此,您

我正在循环使用一些png来为java splashscreen创建动画

我用这个开始动画

java -splash:images/anim.png SplashDemo
并在类内使用PNG。你可以在这里找到班级-

我唯一的问题是,我选择使用anim.png启动动画的任何alpha都是最终的,并且将在以后覆盖所有png


我试过AlphaComposite。清晰,Src,SrcOver,但没有任何效果。如果我以不透明度为0的方式加载png,则整个动画将消失。有谁能告诉我如何摆脱这个问题吗?

因此,您面临的问题与您正在绘制的
图形
上下文在更新之间从未真正“清理”或“静止”有关。这是一种痛苦,我知道,但确实如此

在绘制下一幅图像之前,您唯一的选择是在每个循环中实际重置输出

幸运的是,
SplashScreen
实际上为背景图像提供了
URL
。这允许我们自己加载图像并根据需要重新绘制到曲面上

您还应该尽最大努力将
图形
上下文恢复到您找到它的状态(当然,除了您在其上绘制的内容)。这可以通过在绘制之前复制图形状态来完成

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

它还将所有图像转换为“设备兼容”图像,这意味着它们应该渲染得更快,因为它们的彩色托盘不需要动态转换

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


使用这个例子,我得到了一个稳定的更新,没有问题,使用相同的图像与
SplashScreen
,这是非常可怕的…

链接的代码是你正在使用的唯一代码还是你改变了它?@MadProgrammer我修改了一点对不起,我更新了帖子这不会弄糟帧率吗??我明天会试试,当我在图像上循环时,我得到的帧率有时会下降。任何提高帧速率的建议。我想你解决了阿尔法问题,非常感谢你。你现在根本没有帧速率控制。我加了一点延迟,因为它只在不到一秒钟的时间内快速浏览了我的图像:P。尝试使用
线程。sleep
来调整更新之间的时间(40毫秒是25fps)。实际上,由于alpha问题,我无法使用gif。。我有1280x720的png,一旦我加载了其中10个,帧速率下降到2 fpsI,说得太快了,即使你的代码,我仍然有同样的问题。你使用了gif,这就是为什么它对你有用。只需使用极低不透明度的png,动画就会消失
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();
    }

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