001 /* 002 * @(#)ColorSwatch.java 1.0 2008-03-01 003 * 004 * Copyright (c) 2008 Jeremy Wood 005 * E-mail: mickleness@gmail.com 006 * All rights reserved. 007 * 008 * The copyright of this software is owned by Jeremy Wood. 009 * You may not use, copy or modify this software, except in 010 * accordance with the license agreement you entered into with 011 * Jeremy Wood. For details see accompanying license terms. 012 */ 013 014 package com.colorpicker.swing; 015 016 import java.awt.event.*; 017 import java.awt.*; 018 import javax.swing.*; 019 import com.colorpicker.awt.*; 020 import java.awt.datatransfer.*; 021 import java.io.*; 022 import java.awt.image.*; 023 024 /** This is a square, opaque panel used to indicate 025 * a certain color. 026 * <P>The color is assigned with the <code>setForeground()</code> method. 027 * <P>Also the user can right-click this panel and select 'Copy' to send 028 * a 100x100 image of this color to the clipboard. (This feature was 029 * added at the request of a friend who paints; she wanted to select a 030 * color and then quickly print it off, and then mix her paints to match 031 * that shade.) 032 * 033 * @version 1.0 034 * @author Jeremy Wood 035 */ 036 public class ColorSwatch extends JPanel { 037 private static final long serialVersionUID = 1L; 038 039 040 JPopupMenu menu; 041 JMenuItem copyItem; 042 MouseListener mouseListener = new MouseAdapter() { 043 public void mousePressed(MouseEvent e) { 044 if(e.isPopupTrigger()) { 045 if(menu==null) { 046 menu = new JPopupMenu(); 047 copyItem = new JMenuItem(ColorPicker.strings.getObject("Copy").toString()); 048 menu.add(copyItem); 049 copyItem.addActionListener(actionListener); 050 } 051 menu.show(ColorSwatch.this,e.getX(),e.getY()); 052 } 053 } 054 }; 055 ActionListener actionListener = new ActionListener() { 056 public void actionPerformed(ActionEvent e) { 057 Object src = e.getSource(); 058 if(src==copyItem) { 059 BufferedImage image = new BufferedImage(100,100,BufferedImage.TYPE_INT_RGB); 060 Graphics2D g = image.createGraphics(); 061 g.setColor(getBackground()); 062 g.fillRect(0, 0, image.getWidth(), image.getHeight()); 063 g.dispose(); 064 Transferable contents = new ImageTransferable(image); 065 Toolkit.getDefaultToolkit().getSystemClipboard().setContents(contents, null); 066 } 067 } 068 }; 069 int w; 070 public ColorSwatch(int width) { 071 w = width; 072 setPreferredSize(new Dimension(width,width)); 073 setMinimumSize(new Dimension(width,width)); 074 addMouseListener(mouseListener); 075 } 076 077 private static TexturePaint checkerPaint = null; 078 private static TexturePaint getCheckerPaint() { 079 if(checkerPaint==null) { 080 int t = 8; 081 BufferedImage bi = new BufferedImage(t*2,t*2,BufferedImage.TYPE_INT_RGB); 082 Graphics g = bi.createGraphics(); 083 g.setColor(Color.white); 084 g.fillRect(0,0,2*t,2*t); 085 g.setColor(Color.lightGray); 086 g.fillRect(0,0,t,t); 087 g.fillRect(t,t,t,t); 088 checkerPaint = new TexturePaint(bi,new Rectangle(0,0,bi.getWidth(),bi.getHeight())); 089 } 090 return checkerPaint; 091 } 092 093 public void paint(Graphics g0) { 094 super.paint(g0); //may be necessary for some look-and-feels? 095 096 Graphics2D g = (Graphics2D)g0; 097 098 Color c = getForeground(); 099 int w2 = Math.min(getWidth(), w); 100 int h2 = Math.min(getHeight(), w); 101 Rectangle r = new Rectangle(getWidth()/2-w2/2,getHeight()/2-h2/2, w2, h2); 102 103 if(c.getAlpha()<255) { 104 TexturePaint checkers = getCheckerPaint(); 105 g.setPaint(checkers); 106 g.fillRect(r.x, r.y, r.width, r.height); 107 } 108 g.setColor(c); 109 g.fillRect(r.x, r.y, r.width, r.height); 110 PaintUtils.drawBevel(g, r); 111 } 112 } 113