Swing 取色器

本文介绍如何在Swing应用程序中实现一个颜色选择器功能,通过提供的代码和截图展示了其工作原理和效果。

Swing 取色器


效果图:



代码:

package linkeo.tools.applications;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.HashMap;

import javax.swing.ButtonGroup;
import javax.swing.JLabel;
import javax.swing.JRadioButton;
import javax.swing.JSlider;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import linkeo.tools.TestPanel;

public class ColorPicker extends TestPanel implements ChangeListener,MouseMotionListener,MouseListener{
	Boolean LOCKED = false;
	Color mask = new Color(255,255,255,127);
	Color invisible = new Color(0,0,0,0);
	Color currentColor = mask;
	int MIN = 0, MAX = 255, VAR = 255;
	HashMap<Integer,Cell> cells = new HashMap<Integer,Cell>();
	JSlider slider = new JSlider(MIN, MAX, VAR);
	JLabel lMin = new JLabel(String.valueOf(MIN));
	JLabel lMax = new JLabel(String.valueOf(MAX));
	JLabel lVar = new JLabel(String.valueOf(VAR));
	JLabel lLock = new JLabel(LOCKED?"LOCKED":"UNLOCKED");
	JTextField lColor = new JTextField();
	JTextField lColorH = new JTextField();
	JRadioButton rbRed = new JRadioButton("Red",false);
	JRadioButton rbGreen = new JRadioButton("Green",true);
	JRadioButton rbBlue = new JRadioButton("Blue",false);
	ButtonGroup bg = new ButtonGroup();
	/**
	 * 
	 */
	private static final long serialVersionUID = 8754344010881914616L;
	public ColorPicker(int width, int height, int flag) {
		super(width, height, flag);
	}
	@Override
	public void init() {
		this.setLayout(null);
		this.addMouseListener(this);
		this.addMouseMotionListener(this);
		this.add(slider);
		slider.setBounds(10, 30, PANEL_WIDTH-530, 20);
		slider.addChangeListener(this);
		rbRed.addChangeListener(this);
		rbGreen.addChangeListener(this);
		rbBlue.addChangeListener(this);
		this.add(lMin);	this.add(lMax);	this.add(lVar);
		lMin.setBounds(10, 45, 20, 20);
		lMax.setBounds(PANEL_WIDTH-535, 45, 20, 20);
		lVar.setBounds((PANEL_WIDTH-525)/2, 45, 20, 20);
		this.add(rbRed);	this.add(rbGreen);	this.add(rbBlue);
		bg.add(rbRed);	bg.add(rbGreen);	bg.add(rbBlue);
		rbRed.setBounds(10, 70, 60, 20);
		rbGreen.setBounds((PANEL_WIDTH-570)/2, 70, 60, 20);
		rbBlue.setBounds(PANEL_WIDTH-580, 70, 60, 20);
		slider.setBackground(invisible);
		rbRed.setBackground(invisible);
		rbGreen.setBackground(invisible);
		rbBlue.setBackground(invisible);

		this.add(lLock);
		lLock.setBounds(10, PANEL_HEIGHT-90, 60, 20);
		this.add(lColor);
		lColor.setBounds(10, PANEL_HEIGHT-60, 100, 20);
		lColor.setEditable(false);
		this.add(lColorH);
		lColorH.setBounds(10, PANEL_HEIGHT-30, 100, 20);
		lColorH.setEditable(false);
		
		
		double dx = 2;
		double dy = 2;
		for(int i = PANEL_WIDTH-510, ii = 0; i<dx*255+PANEL_WIDTH-510;i+=dx, ii++)
			for(int j = 0, jj = 0; j<dy*255;j+=dy,jj++)
				cells.put(ii*255+jj,new Cell(i,j,dx,dy,(i-PANEL_WIDTH+510)/dx,j/dy));
		stateChanged(null);
	}
	@Override
	public void run() {
		repaint();
	}
	@Override
	public void paint(Graphics g) {
		g.setColor(currentColor);
		g.fillRect(0,0,PANEL_WIDTH,PANEL_HEIGHT);
		g.setColor(mask);
		g.fillRect(0,0,PANEL_WIDTH-510,120);
		for(Cell c : cells.values())
			c.paint(g);
		this.paintComponents(g);
	}
	public static void main(String[] args) {
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (ClassNotFoundException | InstantiationException
				| IllegalAccessException | UnsupportedLookAndFeelException e) {
			e.printStackTrace();
		}
		new ColorPicker(800, 510, 20).startTest();
	}
	@Override
	public void stateChanged(ChangeEvent e) {
		VAR = slider.getValue();
		lVar.setText(String.valueOf(VAR));
		if(rbGreen.isSelected())
			for(Cell c : cells.values())
				c.setColor(c.v1, VAR, c.v2);
		if(rbRed.isSelected())
			for(Cell c : cells.values())
				c.setColor(VAR, c.v1, c.v2);
		if(rbBlue.isSelected())
			for(Cell c : cells.values())
				c.setColor(c.v1, c.v2, VAR);
	}
	@Override
	public void mouseMoved(MouseEvent e) {
		if(LOCKED)return;
		int index = 0;
		Point mPos = this.getMousePosition();
		if(mPos!=null)
			if(mPos.x>PANEL_WIDTH-510)	{
				index = (mPos.x-PANEL_WIDTH+510)/2*255+(mPos.y)/2;
				currentColor = new Color(((Cell)cells.get(index)).color.getRGB());
				lColor.setText(String.format("%d,%d,%d",currentColor.getRed(),currentColor.getGreen(),currentColor.getBlue()));
				lColorH.setText(String.format("#%02X%02X%02X",currentColor.getRed(),currentColor.getGreen(),currentColor.getBlue()));
			}
	}
	@Override
	public void mouseDragged(MouseEvent e) {}
	@Override
	public void mouseClicked(MouseEvent e) {
		LOCKED = LOCKED?false:true;
		lLock.setText(LOCKED?"LOCKED":"UNLOCKED");
	}
	@Override
	public void mousePressed(MouseEvent e) {}
	@Override
	public void mouseReleased(MouseEvent e) {}
	@Override
	public void mouseEntered(MouseEvent e) {}
	@Override
	public void mouseExited(MouseEvent e) {}
}

class Cell {
	double x,y,w,h,v1,v2;
	Color color;
	public Cell(double x, double y, double w, double h, double v1, double v2){
		this.x = x;
		this.y = y;
		this.w = w;
		this.h = h;
		this.v1 = v1;
		this.v2 = v2;
		this.color = Color.white;
	}
	public void setColor(double r, double g, double b){
		this.color = new Color((int)r,(int)g,(int)b);
	}
	public void paint(Graphics g){
		g.setColor(color);
		g.fillRect((int)x, (int)y, (int)w, (int)h);
	}
	public boolean inside(Point p){
		if(p.x>=(int)x && p.x<(int)(x+w) && p.y>=(int)y && p.y<(int)(y+h))
			return true;
		else
			return false;
	}
}



评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值