想要用java声明一个颜色类Color,请问该怎么操作?

一种颜色有红绿蓝三元色组成,称之为RGB值,一个int整数可以表示一种颜色,结构为 最高字节全1,其后3字节分别存储红 绿 蓝 单色值, 单色值的范围是0-255,例如,0xff00ff00表示绿色,RGB值为(0,255,0)

一只名叫tom的猫
浏览 560回答 3
3回答

手掌心

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}}

Cats萌萌

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),0xffff0000Color,RGB(0,255,0),0xff00ff00Color,RGB(0,0,255),0xff0000ffColor,RGB(255,255,255),0xffffffff*/

holdtom

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类型
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JQuery
Java