package kubach.gui;
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.swing.JEditorPane;
import javax.swing.JPanel;
import kubach.ConfigManager;
/**
* Panel with logo
* @author Cr0s
*/
public class LogoPanel extends JPanel {
private BufferedImage bg, logo;
private ChangelogPanel cp;
/**
* Creates new form LogoPanel
*/
public LogoPanel(Component parent) {
initComponents();
this.setLayout(null);
this.setBackground(new Color(0f,0f,0f));
this.setSize(parent.getSize());
this.setPreferredSize(parent.getPreferredSize());
try {
File imgDir = new File(ConfigManager.getInstance().pathToJar + File.separatorChar + "pics");
if (imgDir.exists() && imgDir.isDirectory()) {
File[] files = imgDir.listFiles();
if (files.length > 0) {
File imgFile = files[(new Random().nextInt(files.length))];
bg = ImageIO.read(imgFile);
}
}
} catch (IOException e) { }
try {
logo = ImageIO.read(LogoPanel.class.getClassLoader().getResource("logo.png"));
} catch (IOException e) {
e.printStackTrace();
}
logo = resize(logo, this.getSize().width, this.getSize().height);
bg = resize(bg, this.getSize().width, this.getSize().height);
this.cp = new ChangelogPanel(this);
// Set location inside parent
Insets i = getInsets();
this.add(cp);
int h = cp.getPreferredSize().height;
cp.setBounds(2 + i.left, this.getPreferredSize().height - h + i.top, this.getPreferredSize().width - (this.getPreferredSize().width / 4), h);
}
public static int getContentHeight(String content) {
JEditorPane dummyEditorPane=new JEditorPane();
dummyEditorPane.setSize(100,Short.MAX_VALUE);
dummyEditorPane.setText(content);
return dummyEditorPane.getPreferredSize().height;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (bg != null) {
g.drawImage(bg,0,0,null);
}
g.drawImage(logo,0,0,null);
//cp.paintComponent(g);
}
public static BufferedImage resize(BufferedImage image, int width, int height) {
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TRANSLUCENT);
Graphics2D g2d = (Graphics2D) bi.createGraphics();
g2d.addRenderingHints(new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY));
g2d.drawImage(image, 0, 0, width, height, null);
g2d.dispose();
return bi;
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);
}// </editor-fold>//GEN-END:initComponents
// Variables declaration - do not modify//GEN-BEGIN:variables
// End of variables declaration//GEN-END:variables
}