<?php import java.awt.Color;import java.awt.Graphics;import java.awt.Image;import java.awt.Toolkit;import java.net.URL;import java.util.logging.Level;import java.util.logging.Logger;import javax.swing.JTextPane;import javax.swing.text.Style;import javax.swing.text.StyleConstants;/* * To change this template, choose Tools | Templates * and open the template in the editor. *//** * * @author Mentos */public class KPanel extends JTextPane { private StringBuilder contentBuilder = new StringBuilder("<html></html>"); private Image backgroundImage = null; private BACKGROUNDPOSITION position = BACKGROUNDPOSITION.CENTERED; private Color backgroundColor = Color.BLUE; public KPanel() { setBackground(new Color(0,0,0,0)); setContentType("text/html"); } public void setBackgroundImage(String imagePath, BACKGROUNDPOSITION position) { if (imagePath.startsWith("http://")) { try { /*String parameter = java.net.URLEncoder.encode(imagePath.substring(imagePath.lastIndexOf("/") + 1), "UTF-8"); imagePath = String.format("%s/%s", imagePath.substring(0, imagePath.lastIndexOf("/")), parameter);*/ backgroundImage = Toolkit.getDefaultToolkit().getImage(new URL(imagePath)); } catch (Exception x) { Logger.getLogger(getClass().getName()).log(Level.WARNING, String.format("Couldn't load Background-Image: %s\r\n%s", imagePath, x.toString())); } } else { try { backgroundImage = Toolkit.getDefaultToolkit().getImage(imagePath); } catch (Exception x) { Logger.getLogger(getClass().getName()).log(Level.WARNING, String.format("Couldn't load Background-Image: %s\r\n%s", imagePath, x.toString())); } } this.position = position; } public void setBackgroundColor(Color backgroundColor){ this.backgroundColor = backgroundColor; } @Override protected void paintComponent(Graphics g) { g.setColor(backgroundColor); g.fillRect(0, 0, getWidth(), getHeight()); if (backgroundImage != null) { switch(position) { case LEFT: g.drawImage(backgroundImage, 0, 0, this); break; case SCALED: g.drawImage(backgroundImage, 0, 0, getWidth(), getHeight(), this); break; case CENTERED: g.drawImage(backgroundImage, (getWidth() - backgroundImage.getWidth(this))/2, (getHeight() - backgroundImage.getHeight(this))/2, this); break; } } super.paintComponent(g); } public void addMessage(Color nickColor, String senderNick, String message) { contentBuilder.delete(contentBuilder.length() - "</html>".length(), contentBuilder.length()); contentBuilder.append("<b><font color="#").append(Integer.toHexString( nickColor.getRGB() & 0x00ffffff )) .append("">").append(senderNick).append("</font></b>: ").append(message).append("<br>"); contentBuilder.append("</html>"); setText(contentBuilder.toString()); } public static enum BACKGROUNDPOSITION { LEFT, CENTERED, SCALED } //Uncool overrides @Override public void setEditable(boolean b) { super.setEditable(false); } @Override public void setOpaque(boolean isOpaque) { super.setOpaque(false); } } ?>
|