Swing (三原色)调色板
立即下载
资源介绍:
功能:
1、点击调色板区域,获取随机颜色
2、输入框(手动输入、滚动修改)输入,获取指定颜色
3、指定方法,自动滚动式获取颜色
(所属分类:后端/Java是随便填的)
package com.view;
import com.util.CompUtils;
import com.util.MathUtils;
import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import java.awt.*;
import java.awt.event.*;
import java.net.URL;
import java.util.Objects;
import java.util.Timer;
import java.util.TimerTask;
/**
* 第二代调色板
* 2024.08.15~2024.08.19
* 功能:
* 1、点击调色板区域,获取随机颜色
* 2、输入框(手动输入、滚动修改)输入,获取指定颜色
* 3、指定方法,自动滚动式获取颜色
* */
public class Home {
private static int RED = 0, GREEN = 0, BLUE = 0;// 三原色值
private static String[] startsTypes = new String[]{"R>G>B","R>B>G","G>R>B","G>B>R","B>R>G","B>G>R","RG>B","RB>G","GB>R","R>GB","G>RB","B>RG","RGB"};// 滚动方法
private static boolean isStop = true;// 默认暂停状态
private static String inputTime = "1";
//
private static Timer timer = new Timer();// 定时器
//
private static JPanel pColor;// 三原色调色板
private static JTextField redJTF, greenJTF, blueJTF, timeJTF;// 三原色设置、定时周期设置
private static JComboBox rgbType = new JComboBox<>(startsTypes);// 滚动方式设置
private static JButton startBtn, stopBtn;// 滚动模式启动/暂停按钮
public Home(){
init();
}
// 界面初始化
private void init(){
//窗体
JFrame f = new JFrame("三原色调试器");
f.setSize(570,600);
f.setLocationRelativeTo(null);// 默认居中展示
f.setLayout(new FlowLayout(FlowLayout.CENTER));
URL url = getClass().getClassLoader().getResource("com/icon/RGB.png");// 图片存放位置
assert url != null;
f.setIconImage(new ImageIcon(url).getImage());// 设置ico图标(logo图标)
f.setResizable(false);// false不允许放大缩小
// 基础面板
JPanel base = new JPanel();
base.setPreferredSize(new Dimension(555,555));
base.setLayout(new FlowLayout(FlowLayout.CENTER,0,0));
// 展示区面板
JPanel show = new JPanel();
show.setPreferredSize(new Dimension(555,300));
show.setLayout(new FlowLayout(FlowLayout.CENTER,0,100));
show.setBorder(BorderFactory.createMatteBorder(2,2,1,2,Color.black));
show.setBackground(Color.WHITE);
// 输入区面板
JPanel input = new JPanel();
input.setPreferredSize(new Dimension(555,75));
input.setLayout(new FlowLayout(FlowLayout.CENTER,50,30));
input.setBorder(BorderFactory.createMatteBorder(1,2,0,2,Color.black));
input.setBackground(Color.WHITE);
// (输入区辅助)标签面板
JPanel tips = new JPanel();
tips.setPreferredSize(new Dimension(555,60));
tips.setLayout(new FlowLayout(FlowLayout.CENTER,50,0));
tips.setBorder(BorderFactory.createMatteBorder(0,2,1,2,Color.black));
tips.setBackground(Color.WHITE);
// 滚动类型选择及其定时任务面板
JPanel select = new JPanel();
select.setPreferredSize(new Dimension(555,120));
select.setBorder(BorderFactory.createMatteBorder(1,2,2,2,Color.BLACK));
select.setLayout(new FlowLayout(FlowLayout.CENTER,10,40));
select.setBackground(Color.WHITE);
// 调色板
pColor = new JPanel();
pColor.setPreferredSize(new Dimension(100,100));
pColor.setBackground(new Color(RED,GREEN,BLUE));
pColor.setBorder(BorderFactory.createMatteBorder(1,1,1,1,Color.BLACK));
// 颜值输入及展示
redJTF = CompUtils.input(RED);
redJTF.setBackground(new Color(RED,0,0));
greenJTF = CompUtils.input(GREEN);
greenJTF.setBackground(new Color(0,GREEN,0));
blueJTF = CompUtils.input(BLUE);
blueJTF.setBackground(new Color(0,0,BLUE));
// 标签
JLabel redJL = CompUtils.label("red", Color.red);
JLabel greenJL = CompUtils.label("green", Color.green);
JLabel blueJL = CompUtils.label("blue", Color.blue);
// 滚动方式选择下拉框
rgbType.setPreferredSize(new Dimension(120,40));
rgbType.setFont(new Font("宋体",Font.BOLD,20));
// 定时周期设置
timeJTF = CompUtils.input(1);
timeJTF.setPreferredSize(new Dimension(120,40));
timeJTF.setForeground(Color.BLACK);
// 清零启动按钮
startBtn = new JButton("从零开始");
startBtn.setPreferredSize(new Dimension(120,40));
startBtn.setFont(new Font("宋体",Font.BOLD,20));
startBtn.setFocusPainted(false);
// 启动及暂停按钮
stopBtn = new JButton("启动");
stopBtn.setPreferredSize(new Dimension(120,40));
stopBtn.setFont(new Font("宋体",Font.BOLD,20));
stopBtn.setFocusPainted(false);
// 组件“组装”
show.add(pColor);
CompUtils.add(input,redJTF,greenJTF,blueJTF);
CompUtils.add(tips, redJL, greenJL, blueJL);
CompUtils.add(select,rgbType,timeJTF,startBtn,stopBtn);
CompUtils.add(base,show,input,tips,select);
f.add(base);
// 监听事件
listen();
// 窗体
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 关闭即退出程序
f.setVisible(true);// 显示窗体
}
/**
* 从零开始执行定时任务
* */
private void startTimer(){
rgbZeroClearing();// 清零,重新计数
continueTimer();
}
/**
* 停止计时器
* */
private void stopTimer(){
timer.cancel();// 停用旧计时器(不停用的话,可能会造成多个计时器同时存在)
isStop = true;// 正在暂停状态,标记状态
stopBtn.setText("继续");
}
/**
* 继续计时器
* */
private void continueTimer(){
String startType = Objects.requireNonNull(rgbType.getSelectedItem()).toString();// 任务形式
int period = Integer.parseInt(timeJTF.getText());// 定时周期
timer.cancel();// 停用旧计时器(不停用的话,可能会造成多个计时器同时存在)
timer = new Timer();// 新建计时器
TimerTask task = startChangeRGB(startType);// 更新计时器任务
timer.scheduleAtFixedRate(task,0,period);// 启动计时器
isStop = false;// 正在非暂停状态,标记状态
stopBtn.setText("暂停");
}
/**
* 定时任务
* */
private TimerTask startChangeRGB(String startType){
//"R>G>B","R>B>G","G>R>B","G>B>R","B>R>G","B>G>R","RG>B","RB>G","GB>R","R>GB","G>RB","B>RG","RGB"
return new TimerTask() {
@Override
public void run() {
// 改变“颜”值
switch (startType){
case "R>G>B":
rgbAutoIncrement(0,1,2);
break;
case "R>B>G":
rgbAutoIncrement(0,2,1);
break;
case "G>R>B":
rgbAutoIncrement(1,0,2);
break;
case "G>B>R":
rgbAutoIncrement(1,2,0);
break;
case "B>R>G":
rgbAutoIncrement(2,0,1);
break;
case "B>G>R":
rgbAutoIncrement(2,1,0);
break;
case "RG>B":
rgbAutoIncrement(0,1);
break;
case "RB>G":
rgbAutoIncrement(0,2);
break;
case "GB>R"