📄 colorpalette.java
字号:
//colorPalette.java//
//2007年4月28日//
//*********************//
import javax.swing.*;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class colorPalette extends JApplet implements ActionListener
{ String S_RGB;
int n=5; //0至255颜色码,分为(0-5)六个色板级差
JPanel p1,p2; //新增颜色和当前颜色面板
JLabel L_CP; //“调色板”
JLabel L_RGB; //显示选中颜色码
JLabel L_XS,L_DS; //新增颜色和当前颜色标签
JButton b[][][]=new JButton[n+1][n+1][n+1]; //(n+1)^3=216个颜色按钮
JButton B_ok; //确定新增颜色按钮
Color C_color=Color.yellow; //color.cyan,color.white
public void init()
{ setLayout(null);
p1=new JPanel(); //显示新增颜色面板
p2=new JPanel(); //显示当前颜色面板
p1.setBackground(Color.cyan);
p2.setBackground(C_color);
add(p1);
add(p2);
p1.setBounds(175,70,80,70);
p2.setBounds(175,140,80,70);
L_RGB=new JLabel("RGB(255,255,255)"); //显示选中的颜色码
add(L_RGB);
L_RGB.setBounds(165,30,130,40);
L_XS=new JLabel("新增颜色"); //新增颜色标签
L_DS=new JLabel("当前颜色"); //当前颜色标签
add(L_XS);
add(L_DS);
L_XS.setBounds(190,55,95,15);
L_DS.setBounds(190,211,95,15);
L_CP=new JLabel("调色板"); //调色板标签
L_CP.setBounds(10,5,50,15);
add(L_CP);
B_ok=new JButton("确定"); //确定新增颜色按钮
add(B_ok);
B_ok.setBounds(185,5,60,20);
B_ok.addActionListener(this);
setColor(n,this,p1,b);
setBounds(10,10,300,250);
setVisible(true);
}
public void setColor(int n,JApplet APL,JPanel p1,JButton b[][][])
{ int re=0,gr=0,bl=0; //三颜色码
int y=25; //按钮纵向起始指或位置
int x=0; //按钮横向位置
int size=10; //按钮尺寸
int sc=255/n; //色差为255/n=51,6个级别(n+1)
int i=0,i1=0,i2=0,i3=0;
for(i=0;i<=n;i++) //red色差
{re=i*sc;
for(i1=0;i1<=n;i1++) //green色差
{gr=i1*sc;
for(i2=0;i2<=n;i2++) //blue色差
{bl=i2*sc;
if(i3==2*n+2) //i3恒等于12的倍数
{i3=0; //一行布置12个按钮,X轴位置从新开始
//一行布置12列按钮后,开始下一行按钮
y=y+(size+1); //按钮Y轴位置,起始值y=25,增量size+1,按钮Y轴间距1。
}
x=i3*(size+1)+size; //按钮X轴位置,起始值size=10,增量size+1,按钮X轴间距1。
b[i][i1][i2]=new JButton();
Color color=new Color(re,gr,bl);
b[i][i1][i2].setBackground(color); //每个按钮的背景颜色设置
APL.add(b[i][i1][i2]);
b[i][i1][i2].setBounds(x,y,size,size); //每个按钮的位置和大小设置
b[i][i1][i2].addActionListener(this); //每个按钮注册监听器
++i3;
}
}
}
}
public void actionPerformed(ActionEvent e)
{ if(e.getSource()==B_ok)
p2.setBackground(C_color);
else{
for(int i=0;i<n+1;i++)
{for(int i1=0;i1<n+1;i1++)
{for(int i2=0;i2<n+1;i2++)
{if(e.getSource()==b[i][i1][i2])
{C_color=b[i][i1][i2].getBackground();
p1.setBackground(C_color);
S_RGB="RGB("+Integer.toString(C_color.getRed())+","+Integer.toString(C_color.getGreen())+","+Integer.toString(C_color.getBlue())+")";
L_RGB.setText(S_RGB);
}
}
} } }
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -