中易网

用java声明一个颜色类Color

答案:3  悬赏:70  
解决时间 2021-01-13 17:40
  • 提问者网友:活着好累
  • 2021-01-13 14:34
用java声明一个颜色类Color
最佳答案
  • 二级知识专家网友:封刀令
  • 2021-01-13 15:28
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()); //白
}
}
全部回答
  • 1楼网友:怙棘
  • 2021-01-13 17:43
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类型
  • 2楼网友:迟山
  • 2021-01-13 17:01
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

}
}
这是源代码 应该是你想要的
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息!
大家都在看
推荐信息