为什么不画这幅画? [英] Why won't this draw the image?

查看:73
本文介绍了为什么不画这幅画?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试做的是当我运行我的应用程序时,它启动线程并且图像显示3秒(3000ms),然后线程停止运行。

What I'm trying to do is make it so when I run my application, it starts the thread and the image is shown for 3 seconds (3000ms), then the thread stops running.

图像路径正确,图像文件存在,线程本身运行;但是,图像似乎没有显示出来。可能有什么不对?这是我的代码:

The path for the image is correct, the image file exists, and the thread itself runs; however, the image doesn't seem to show. What could be wrong? Here is my code:

package org.main;

import java.awt.Graphics;
import java.awt.Image;

import javax.swing.ImageIcon;
import javax.swing.JPanel;

public class Splasher extends JPanel implements Runnable {
    private static final long serialVersionUID = 1L;
    Image image;
    ImageIcon splash = new ImageIcon("res/splash.png");
    public static Thread DrawSplash = new Thread(new Splasher());

    public Splasher() {
        setFocusable(true);
        image = splash.getImage();  
        repaint();
    }
    boolean hasRan = false;
    public void run() {
        try {
            System.out.println("Drawing Splash Screen");
            repaint();
            Thread.sleep(3000);
            System.out.println("Repainted");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

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

    public Image getImage() {
        return image;
    }
}


推荐答案

这里有不足以回答你的问题。

There's not enough to go from you question.

你没有显示你如何使用启动画面,它是否附加到任何东西或如何开始/使用线程

You don't show how you use the splash screen, if it's attached to anything or how you start/use the Thread.

所以问题可能是什么......

So the problem could be anything...

除了VishalK已经指出的其他一切,我还要添加 public static Thread DrawSplash = new Thread(new Splasher())是一个坏主意。你不应该使用 static 线程 s线程是不可重入的,也就是说,你可以运行相同的线程两次。

In addition to everything else VishalK has already pointed out, I would add public static Thread DrawSplash = new Thread(new Splasher()) is a bad idea. You shouldn't use static Threads are threads are non-reentrant, that is, you can run the same thread twice.

这是一个展示渐弱启动画面的小例子,使用了许多Swing 计时器 s

This is a little example demonstrating a "fading" splash screen, using a number of Swing Timers

这假设您使用的是Java 7,它还可以使它适用于Java 6,我没有发布该代码。

This assumes you're using Java 7, there is away to make it work for Java 6, I've not posted that code.

public class TestSplashScreen01 {

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

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

                SplashScreen splash = new SplashScreen();
                splash.start();

            }
        });
    }

    public class SplashScreen extends JWindow {

        private SplashPane splash;

        public SplashScreen() {
            setBackground(new Color(0, 0, 0, 0));
            splash = new SplashPane();
            add(splash);
            pack();
            setLocationRelativeTo(null);
        }

        public void start() {
            splash.start();
        }

        public class SplashPane extends JPanel {

            private BufferedImage splash;
            private Timer timer;
            private float alpha = 0f;
            private int duration = 1000;
            private long startTime = -1;

            public SplashPane() {
                try {
                    splash = ImageIO.read(getClass().getResource("/res/SokahsScreen.png"));
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
                timer = new Timer(3000, new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        fadeOut();
                    }
                });
                timer.setRepeats(false);
            }

            protected void fadeOut() {
                Timer fadeInTimer = new Timer(40, new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        long now = System.currentTimeMillis();
                        long runTime = now - startTime;
                        alpha = 1f - ((float) runTime / (float) duration);
                        if (alpha <= 0.01f) {
                            alpha = 0f;
                            ((Timer) (e.getSource())).stop();
                            SwingUtilities.invokeLater(new Runnable() {
                                @Override
                                public void run() {
                                    dispose();
                                }
                            });
                        }
                        repaint();
                    }
                });
                startTime = System.currentTimeMillis();
                fadeInTimer.setRepeats(true);
                fadeInTimer.setCoalesce(true);
                fadeInTimer.start();
            }

            protected void fadeIn() {
                Timer fadeInTimer = new Timer(40, new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        long now = System.currentTimeMillis();
                        long runTime = now - startTime;
                        alpha = (float) runTime / (float) duration;
                        if (alpha >= 1f) {
                            alpha = 1f;
                            ((Timer) (e.getSource())).stop();
                            timer.start();
                        }
                        repaint();
                    }
                });
                startTime = System.currentTimeMillis();
                fadeInTimer.setRepeats(true);
                fadeInTimer.setCoalesce(true);
                fadeInTimer.start();
            }

            public void start() {
                if (!SplashScreen.this.isVisible()) {
                    alpha = 0f;
                    SplashScreen.this.setVisible(true);
                    SwingUtilities.invokeLater(new Runnable() {
                        @Override
                        public void run() {
                            fadeIn();
                        }
                    });
                }
            }

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

            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                if (splash != null) {
                    Graphics2D g2d = (Graphics2D) g.create();
                    g2d.setComposite(AlphaComposite.SrcOver.derive(alpha));
                    int x = (getWidth() - splash.getWidth()) / 2;
                    int y = (getHeight() - splash.getHeight()) / 2;
                    g2d.drawImage(splash, x, y, this);
                    g2d.dispose();
                }
            }
        }
    }
}

NB:

这个例子的问题是一旦你调用start,程序将继续执行,这将需要某种倾听者来告诉感兴趣的各方启动画面完成时。

The problem with this example is once you call start, the program will continue to execute, this will require some kind of listener to tell interested parties when the splash screen has completed.

或者,你可以使用未修饰的模态 JDialog

Alternatively, you could use a undecorated modal JDialog

这篇关于为什么不画这幅画?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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