用java声明一个颜色类Color
发布网友
发布时间:2022-05-10 23:24
我来回答
共3个回答
热心网友
时间:2023-11-13 18:48
import java.awt.*;
import java.awt.event.*;
public class adjustcolor implements AdjustmentListener, WindowListener {
Frame f=new Frame("调整颜色");
Label l1=new Label("调整滚动条,会改变初始颜色",Label.CENTER);
Label l2=new Label("此处显示颜色值",Label.CENTER);
Label l3=new Label("红",Label.CENTER);
Label l4=new Label("绿",Label.CENTER);
Label l5=new Label("蓝",Label.CENTER);
Scrollbar scr1=new Scrollbar(Scrollbar.HORIZONTAL,0,10,0,265);
Scrollbar scr2=new Scrollbar(Scrollbar.HORIZONTAL,0,10,0,265);
Scrollbar scr3=new Scrollbar(Scrollbar.HORIZONTAL,0,10,0,265);
public adjustcolor(){
f.add(l1);
f.add(l2);
f.add(l3);
f.add(l4);
f.add(l5);
f.add(scr1);
f.add(scr2);
f.add(scr3);
f.setSize(400,350);
f.setVisible(true);
f.addWindowListener(this);
f.setResizable(false);
l1.setBackground(Color.GREEN);
scr1.setBounds(35,225,360,25);
scr2.setBounds(35,255,360,25);
scr3.setBounds(35,285,360,25);
l1.setBounds(0,0,400,200);
l2.setBounds(0,310,400,30);
l3.setBounds(0,225,30,30);
l4.setBounds(0,255,30,30);
l5.setBounds(0,285,30,30);
scr1.addAdjustmentListener(this);
scr2.addAdjustmentListener(this);
scr3.addAdjustmentListener(this);
l1.setBackground(Color.GREEN);
scr1.setBackground(Color.RED);
scr2.setBackground(Color.GREEN);
scr3.setBackground(Color.blue);
}
public void adjustmentValueChanged(AdjustmentEvent e){
int a=scr1.getValue();
int b=scr2.getValue();
int c=scr3.getValue();
l1.setBackground(new Color(a,b,c)) ;
l2.setText("红"+" "+"绿"+" "+"蓝"+" "+a+" "+b+" "+c);
l1.setText(null);
}
public static void main(String[] args){
new adjustcolor();
}
public void windowActivated(WindowEvent arg0) {
// TODO Auto-generated method stub
}
public void windowClosed(WindowEvent arg0) {
}
public void windowClosing(WindowEvent arg0) {
System.exit(0);
}
public void windowDeactivated(WindowEvent arg0) {
// TODO Auto-generated method stub
}
public void windowDeiconified(WindowEvent arg0) {
// TODO Auto-generated method stub
}
public void windowIconified(WindowEvent arg0) {
// TODO Auto-generated method stub
}
public void windowOpened(WindowEvent arg0) {
// TODO Auto-generated method stub
}
}
这是源代码 应该是你想要的
热心网友
时间:2023-11-13 18:48
public class Color //颜色类
{
private int value; //颜色值
public Color(int red, int green, int blue) //构造方法
{
value = 0xff000000 | ((red & 0xFF)<<16) | ((green & 0xFF)<<8) | blue & 0xFF;
}
public Color(int rgb)
{
value = 0xff000000 | rgb;
}
public int getRGB()
{
return value;
}
public int getRed()
{
return (getRGB()>>16) & 0xFF;
}
public int getGreen()
{
return (getRGB()>> 8) & 0xFF;
}
public int getBlue()
{
return getRGB() & 0xFF;
}
public String toString()
{
return getClass().getName()+",RGB("+getRed()+","+getGreen()+","+getBlue()+"),0x"
+Integer.toHexString(value);
}
public static void main(String args[])
{
System.out.println(new Color(255,0,0).toString()); //红
System.out.println(new Color(0,255,0).toString()); //绿
System.out.println(new Color(0,0,255).toString()); //蓝
System.out.println(new Color(255,255,255).toString()); //白
}
}
/*
程序运行结果如下:
Color,RGB(255,0,0),0xffff0000
Color,RGB(0,255,0),0xff00ff00
Color,RGB(0,0,255),0xff0000ff
Color,RGB(255,255,255),0xffffffff
*/
热心网友
时间:2023-11-13 18:49
public Class Color{
private String red;
private String blue;
private String green;
//全参构造
Color(String red,String blue,String green){
this.red = red;
this.blue = blue;
this.green = green;
}
//对应的setter和getter
}
//使用时直接通过全参构造实例化
Color col = new Color(0,255,0);
//此时 该col对象就可代表一种颜色
//可以考虑用int类型