Java中如何将RGB三个颜色的值存放到数组中

2024-12-12 11:33:09
推荐回答(4个)
回答1:

先定义一个类:其中三个属性,R、G、B
public class ColorVo{
private int r;
private int g;
private int b;

getter & setter

}

然后创建一个 ColorVo 数组

ColorVo[] array = new ColorVo[n];
n表示数组长度。
然后
ColorVo cv = new ColorVo();
cv.setR(1);
cv.setG(2);
cv.setB(3);
array[i] = cv
i表示数组的下表
此时就已经将ColorVo 放入了数组中

回答2:

package util;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;

import javax.imageio.ImageIO;

public class valIamge {

public String getvalIamge(OutputStream os)
{
BufferedImage bf=new BufferedImage(60,20,BufferedImage.TYPE_INT_RGB);
Graphics g=bf.getGraphics();
g.setColor(Color.WHITE);
g.fillRect(0,0,60,20);

Random r=new Random();

g.setColor(new Color(r.nextInt(256),r.nextInt(256),r.nextInt(256)));
int k1=r.nextInt(10);
int k2=r.nextInt(10);
int k3=r.nextInt(10);
g.drawLine(k1,k1,60,k1);
g.setColor(new Color(r.nextInt(256),r.nextInt(256),r.nextInt(256)));
g.drawLine(k2,k2,60,k2);
g.setColor(new Color(r.nextInt(256),r.nextInt(256),r.nextInt(256)));
g.drawLine(k3,k3,60,k3);
String res="";
g.setFont(new Font("MS Gothic",15,15));
for(int i=0;i<4;i++)
{
g.setColor(new Color(r.nextInt(100),r.nextInt(100),r.nextInt(100)));
System.out.println();

if(r.nextInt(4)<=2)
{
int so=65+r.nextInt(25);
char ss=(char)so;
String s=String.valueOf(ss);
res+=s;
g.drawString(s,15*i+2,15);
}
else
{
int k=r.nextInt(10);
String s=String.valueOf(k);
res+=s;
g.drawString(s,15*i+2,15);
}

}
try {

ImageIO.write(bf,"jpeg",os);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return res;

}

}

//jsp

<%@ page language="java" contentType="image/JPEG;"import="java.util.*" pageEncoding="gb2312"%>
<%@ page import="util.valIamge"%>







<%
valIamge vo=new valIamge();
String a=vo.getvalIamge(response.getOutputStream());
session.setAttribute("yz",a);
%>

回答3:

用ArrayList保存数组啊

回答4:

.这不是那个叫什么了???

hmtl里貌似就有..