package org.wrap;

import java.awt.*;
import java.io.*;
import javax.swing.*;

public class SplashScreen {
	private JFrame f = null;
	JWindow splashScreen = null;
	JLabel splashLabel = null;
	
	public SplashScreen() {
		GraphicsConfiguration gc =
	    	GraphicsEnvironment.getLocalGraphicsEnvironment().
				getDefaultScreenDevice().getDefaultConfiguration();
		f = new JFrame(gc);
		try {
			InputStream is = org.cmdline.sudoku.SolvingSudokuBoard.class.getResourceAsStream("images/splashscreen.png");
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			BufferedOutputStream bos = new BufferedOutputStream(baos);
			BufferedInputStream bis = new BufferedInputStream(is);
			byte [] buf = new byte[1024];
			int cnt = bis.read(buf,0,buf.length);
			while (cnt > -1) {
				bos.write(buf,0,cnt);
				cnt = bis.read(buf,0,buf.length);
			}
			bis.close();
			bos.flush();
			bos.close();
			byte[] img = baos.toByteArray();
			ImageIcon imgico = new ImageIcon(img,"splash screen");
			splashLabel = new JLabel(imgico);
		}
		catch(IOException ioex) { ioex.printStackTrace(); }
		splashScreen = new JWindow(f);
		splashScreen.getContentPane().add(splashLabel);
		splashScreen.pack();
		Rectangle screenRect = f.getGraphicsConfiguration().getBounds();
		splashScreen.setLocation(
			screenRect.x + screenRect.width/2 - splashScreen.getSize().width/2,
			screenRect.y + screenRect.height/2 - splashScreen.getSize().height/2);
	}
	
	public void setVisible(boolean visible) {
		if (visible) splashScreen.setVisible(true);
		else {
			splashScreen.setVisible(false);
			splashScreen = null;
			splashLabel = null;
		}
	}
}