/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package de.nplusc.izc.tools.UiToolz;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JPanel;

/**
 *
 * @author LH
 */
public class UiTools
{
    /**
     * Fragt die Bildschirmgröße ab
     * @return höhe*breite in nem 2langen array
     */    
    public static int[] getScreenRes()
    {
        int[] ret = new int[2];
        ret[0] = (int) Toolkit.getDefaultToolkit().getScreenSize().getWidth();
        ret[1] = (int) Toolkit.getDefaultToolkit().getScreenSize().getHeight();
        return ret;
    }
    
    public static void ScaleImageIntoFrame(String imagePath, JPanel target)
    {
        int ph=target.getHeight();
        int pw=target.getWidth();
        Graphics2D g = (Graphics2D) target.getGraphics();
        ScaleImageIntoFrame(imagePath, g, ph, pw);
    }
    
    @SuppressWarnings("empty-statement")
    public static void ScaleImageIntoFrame(String imagePath,Graphics2D da,int ph, int pw)
    {
        try
        {
           
            BufferedImage tmp = ImageIO.read(new File(imagePath));
             /*
              * int hgh = tmp.getHeight();
            int wdh = tmp.getWidth();
            float sf = (float)hgh/(float)ph;
            if((wdh*sf)>pw)
                sf = (float)wdh/(float)pw;
            hgh /=sf;//<<<<
            wdh /=sf;//<<<<Kurzdivisionen */
            int hgh=ph, wdh=pw;
            while(tmp.getHeight()==-1);//HACK. wartet darauf dass das bild geladen ist
            Image tmp2 = tmp.getScaledInstance(wdh, hgh,BufferedImage.SCALE_FAST);
            da.setColor(Color.black);
            da.drawRect(0, 0, pw, ph);
            da.fillRect(0, 0, pw, ph);
            int ctrw=pw/2,ctrh=ph/2;
            /*boolean t=*/da.drawImage(tmp2,0,0,null);//,ctrw-wdh/2, ctrh-hgh/2,
        }
        catch (IOException ex)
        {
            ex.printStackTrace();
        }
    }
    
    
    
    public static String convertToSizeString(long bytesCount)
    {
        //EInheiten durchleiern
        if(bytesCount<1024)
            return bytesCount+" Bytes";
        bytesCount=bytesCount/1024;
        if(bytesCount<1024)
            return bytesCount+" KB";
        bytesCount=bytesCount/1024;
        if(bytesCount<1024)
            return bytesCount+" MB";
        bytesCount=bytesCount/1024;
        return bytesCount+" GB";
    }
}
