tt

 

package com.saic.mscatalog.plugin.item;

import java.awt.AlphaComposite;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.awt.image.ConvolveOp;
import java.awt.image.Kernel;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.ListCellRenderer;

public class PluginPanel extends JPanel implements ListCellRenderer {

	private PluginInfo pluginInfo = null;

	private Image image = null;

	private JPanel imagePanel = null;

	private JButton imageBtn = null;

	private static Dimension itemSize = new Dimension(255, 255);

	public PluginPanel(PluginInfo pluginInfo) {
		this.pluginInfo = pluginInfo;
		this.image = new ImageIcon(pluginInfo.getImagePath()).getImage();
		initDisplay();
	}

	private void initDisplay() {
		this.setSize(itemSize);
		this.setBackground(new Color(51, 51, 51));

		GridBagLayout gridbag = new GridBagLayout();
		GridBagConstraints c = new GridBagConstraints();
		this.setLayout(gridbag);

		c.gridx = 0;
		c.gridy = 0;
		c.anchor = GridBagConstraints.CENTER;
		JLabel nameLabel = new JLabel();
		nameLabel.setFont(new Font("Arial", Font.PLAIN, 19));
		nameLabel.setForeground(Color.WHITE);
		nameLabel.setText(pluginInfo.getName());
		gridbag.setConstraints(nameLabel, c);
		this.add(nameLabel);

		c.gridx = 0;
		c.gridy = 1;
		imagePanel = new ImagePanel();
		gridbag.setConstraints(imagePanel, c);
		this.add(imagePanel);

		imagePanel.setLayout(new BoxLayout(imagePanel, BoxLayout.Y_AXIS));
		imagePanel.add(Box.createVerticalStrut(70));
		imageBtn = new JButton("加载"); 
		imageBtn.setPreferredSize(new Dimension(65, 25));
		imageBtn.setAlignmentX(Component.CENTER_ALIGNMENT);
		imagePanel.add(imageBtn);

		this.addMouseListener(new MouseAdapter() {
			public void mouseExited(MouseEvent e) {
				if (e.getX() > PluginPanel.this.getWidth() || e.getY() > PluginPanel.this.getHeight() || e.getX() < 0 || e.getY() < 0) {
					imageBtn.setVisible(false);
					PluginPanel.this.setBackground(new Color(51, 51, 51));
					// PluginPanel.this.setBorder(border);
				}
			}

			@Override
			public void mouseEntered(MouseEvent e) {
				imageBtn.setVisible(true);
				PluginPanel.this.setBackground(new Color(38, 38, 38));
				// PluginPanel.this.setBorder(border);
			}
		});

	}
	
	private  JButton getImageButton(){
	 	JButton btn = new JButton();
	 	btn.setSize(image .getWidth(null), image.getHeight(null));
	 	btn.setIcon(null);
	 	btn.setMargin(new Insets(0, 0, 0, 0));
	 	btn.setIconTextGap(0);
	 	btn.setBorderPainted(false);
	 	btn.setBorder(null);
	 	btn.setText(null);

	 	btn.setPressedIcon(new ImageIcon("images/button-down.png"));
//	 	btn.setRolloverIcon(new ImageIcon("images/button-over.png"));
//	 	btn.setSelectedIcon(new ImageIcon("images/button-sel.png"));
//	 	btn.setRolloverSelectedIcon(new ImageIcon("images/button-sel-over.png"));
//	 	btn.setDisabledIcon(new ImageIcon("images/button-disabled.png"));
//	 	btn.setDisabledSelectedIcon(new ImageIcon("images/button-disabled-selected.png"));
		return null;
	}

	@Override
	public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
		// TODO Auto-generated method stub
		return null;
	}

	private class ImagePanel extends JPanel {
		public ImagePanel() {
			this.setSize(image.getWidth(null), image.getHeight(null) * 2);
			this.setPreferredSize(new Dimension(image.getWidth(null), image.getHeight(null) * 2));
		}

		public Insets getInsets() {
			return new Insets(0, 0, 0, 0);
		}

		protected void paintComponent(Graphics g) {
			Graphics2D g2 = (Graphics2D) g;
			g2.drawImage(createReflection(image), 0, 0, null);
		}
	}

	private BufferedImage createReflection(Image image) {
		int width = image.getWidth(null);
		int height = image.getHeight(null);

		BufferedImage result = new BufferedImage(width, height * 2, BufferedImage.TYPE_INT_ARGB);
		Graphics2D g2 = result.createGraphics();

		// 画原始图片。
		g2.drawImage(image, 0, 0, null);
		// 画黑影。

		GradientPaint shadow = new GradientPaint(0, 0, Color.BLACK, 0, 5, Color.DARK_GRAY, true);
		g2.setPaint(shadow);
		g2.fillRect(0, height, width, height + 5);

		// 画对称图片。
		g2.scale(1.0, -1.0);
		g2.drawImage(image, 0, -2 * height - 10, null);
		g2.scale(1.0, -1.0);

		// 对称图片设置渐变。
		g2.translate(0, height);
		Color c1 = new Color(255, 255, 255, 150);
		Color c2 = new Color(0, 0, 0, 255);

		GradientPaint mask = new GradientPaint(0, 0, c1, 0, height, c2);
		g2.setPaint(mask);
		g2.setComposite(AlphaComposite.DstOut);
		g2.fillRect(0, 0, width, height);
		g2.dispose();
		return result;

	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		JFrame jf = new JFrame();
		jf.setSize(itemSize);
		jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		jf.setLocationRelativeTo(null);

		jf.getContentPane().add(new PluginPanel(PluginInfoGen.getPluginInfos().get(0)));
		jf.setVisible(true);

	} 
}

  

posted @ 2012-03-19 08:03  jinc  阅读(502)  评论(0编辑  收藏  举报