Java 为什么赢了';这不就是图像吗?
我试图做到的是,当我运行我的应用程序时,它启动线程,图像显示3秒(3000毫秒),然后线程停止运行 映像路径正确,映像文件存在,线程本身运行;然而,图像似乎没有显示出来。有什么不对劲吗?这是我的密码:Java 为什么赢了';这不就是图像吗?,java,swing,Java,Swing,我试图做到的是,当我运行我的应用程序时,它启动线程,图像显示3秒(3000毫秒),然后线程停止运行 映像路径正确,映像文件存在,线程本身运行;然而,图像似乎没有显示出来。有什么不对劲吗?这是我的密码: package org.main; import java.awt.Graphics; import java.awt.Image; import javax.swing.ImageIcon; import javax.swing.JPanel; public class Splasher
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;
}
}
你提出的问题还不够 您不会显示如何使用启动屏幕(如果它连接到任何东西),也不会显示如何启动/使用
线程
所以问题可能是任何东西
除了VishalK已经指出的所有其他内容之外,我还要添加publicstaticthreaddrawsplash=newthread(newsplasher())
这是个坏主意。如果线程是不可重入的,则不应使用静态线程,也就是说,可以运行同一线程两次
这是一个演示“渐弱”启动屏幕的小示例,使用了大量Swing计时器
这假设您使用的是Java7,要使它在Java6上工作还有很多困难,我还没有发布这些代码
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();
}
}
}
}
}
公共类TestSplashScreen01{
公共静态void main(字符串[]args){
新的TestSplashScreen01();
}
公共测试屏幕01(){
invokeLater(新的Runnable(){
@凌驾
公开募捐{
试一试{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}catch(ClassNotFoundException |实例化Exception | IllegalacessException |不支持ookandfeelException ex){
}
SplashScreen splash=新SplashScreen();
splash.start();
}
});
}
公共类SplashScreen扩展JWindow{
私人喷溅;
公共屏幕(){
后退(新颜色(0,0,0,0));
飞溅=新的飞溅窗格();
添加(飞溅);
包装();
setLocationRelativeTo(空);
}
公开作废开始(){
splash.start();
}
公共类SplashPane扩展了JPanel{
私有缓冲区图像飞溅;
私人定时器;
专用浮点alpha=0f;
专用int持续时间=1000;
私有长startTime=-1;
公共窗格(){
试一试{
splash=ImageIO.read(getClass().getResource(“/res/sokahscreen.png”);
}捕获(IOEX异常){
例如printStackTrace();
}
计时器=新计时器(3000,新ActionListener(){
@凌驾
已执行的公共无效操作(操作事件e){
淡出();
}
});
timer.setRepeats(假);
}
受保护的无效衰减(){
Timer fadeInTimer=新计时器(40,新ActionListener(){
@凌驾
已执行的公共无效操作(操作事件e){
long now=System.currentTimeMillis();
long runTime=now-startTime;
alpha=1f-((浮动)运行时间/(浮动)持续时间);
如果(α=1f){
α=1f;
((计时器)(e.getSource()).stop();
timer.start();
}
重新油漆();
}
});
startTime=System.currentTimeMillis();
fadeInTimer.setRepeats(真);
fadeInTimer.setCoalesce(真);
fadeInTimer.start();
}
公开作废开始(){
如果(!SplashScreen.this.isVisible()){
α=0f;
SplashScreen.this.setVisible(true);
SwingUtilities.invokeLater(新的Runnable(){
@凌驾
公开募捐{
fadeIn();
}
});
}
}
@凌驾
公共维度getPreferredSize(){
return splash==null?super.getPreferredSize():新维度(splash.getWidth(),splash.getHeight());
}
@凌驾
受保护组件(图形g){
超级组件(g);
如果(飞溅!=null){
Graphics2D g2d=(Graphics2D)g.create();
g2d.setComposite(AlphaComposite.SrcOver.derive(alpha));
intx=(getWidth()-splash.getWidth())/2;
int y=(getHeight()-splash.getHeight())/2;
g2d.drawImage(splash,x,y,this);
g2d.dispose();
}
}
}
}
}
注意:
这个例子的问题是,一旦调用start,程序将继续执行,这将需要某种侦听器在启动屏幕完成时告诉相关方
或者,您可以使用未修饰的模态JDialog
您的问题没有足够的内容
您不会显示如何使用启动屏幕(如果它连接到任何东西),也不会显示如何启动/使用线程
所以问题可能是任何东西
除了VishalK已经指出的所有其他内容之外,我还要添加publicstaticthreaddrawsplash=newthread(newsplasher())
这是个坏主意。如果线程是不可重入的,则不应使用静态线程,也就是说,可以运行同一线程两次
这是一个演示“渐弱”启动屏幕的小示例,使用了大量Swing计时器
这个假设
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentAdapter;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.Timer;
import javax.swing.SwingUtilities;
public class Splasher extends JPanel {
private static final long serialVersionUID = 1L;
Image image;
ImageIcon splash = new ImageIcon("apple.png");
MyComponentListener componentListener ;
Timer timer ;
public Splasher()
{
componentListener = new MyComponentListener();
setFocusable(true);
image = splash.getImage();
timer = new Timer(3000, new LoadAction());
addComponentListener(componentListener);
}
boolean hasRan = false;
@Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
if (image == null && timer !=null )
{
g.clearRect(0, 0, getWidth(), getHeight()) ;
timer.stop();
removeComponentListener(componentListener);
}
else
{
g.drawImage(image, 0, 0, null);
}
}
public Image getImage()
{
return image;
}
private class MyComponentListener extends ComponentAdapter
{
@Override
public void componentResized(ComponentEvent evt)
{
System.out.println("Resized..");
timer.start();
}
}
private class LoadAction implements ActionListener
{
public void actionPerformed(ActionEvent evt)
{
System.out.println("Drawing Splash Screen");
repaint();
image = null;
repaint();
System.out.println("Repainted");
}
}
public static void main(String st[])
{
SwingUtilities.invokeLater ( new Runnable()
{
@Override
public void run()
{
JFrame frame = new JFrame("Splash:");
frame.getContentPane().add(new Splasher());
frame.setSize(300,500);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
});
}
}