这是我在网上找到的一个颜色由深入浅渐变效果的方法,我想改成有浅入深的效果,自己没改明白,请高手帮忙改一下!小弟在此先谢谢了!/**
* 绘制渐变色选择条
*
* @param g
* @param color
* @param x
* @param y
* @param width
* @param height
*/
public final static void drawRGBRect(Graphics g, int color, int x, int y,
int width, int height) {
int[] rgb = getRGBColor(color, width);
for (int by = y; by < y + height; by += 4) { int nTemp = y + height - (by - y); nTemp = nTemp > 4 ? 4 : nTemp;
g.drawRGB(rgb, 0, width, x, by, width, nTemp, true);
} } /**
* 获取颜色渐变RGB数组,
*
* @param width
* @return
*/
public final static int[] getRGBColor(int color, int h) {
int[] rgb; int RGB_L = h;
int nRgbData = RGB_L * 4; rgb = new int[nRgbData]; int alpha = -127;
for (int i = 0; i < RGB_L; i++) {
alpha = -127 + i; int col = color | (128 - alpha << 24);
rgb[i] = col;
rgb[i + RGB_L] = col;
rgb[i + RGB_L * 2] = col;
rgb[i + RGB_L * 3] = col; } return rgb;
}
* 绘制渐变色选择条
*
* @param g
* @param color
* @param x
* @param y
* @param width
* @param height
*/
public final static void drawRGBRect(Graphics g, int color, int x, int y,
int width, int height) {
int[] rgb = getRGBColor(color, width);
for (int by = y; by < y + height; by += 4) { int nTemp = y + height - (by - y); nTemp = nTemp > 4 ? 4 : nTemp;
g.drawRGB(rgb, 0, width, x, by, width, nTemp, true);
} } /**
* 获取颜色渐变RGB数组,
*
* @param width
* @return
*/
public final static int[] getRGBColor(int color, int h) {
int[] rgb; int RGB_L = h;
int nRgbData = RGB_L * 4; rgb = new int[nRgbData]; int alpha = -127;
for (int i = 0; i < RGB_L; i++) {
alpha = -127 + i; int col = color | (128 - alpha << 24);
rgb[i] = col;
rgb[i + RGB_L] = col;
rgb[i + RGB_L * 2] = col;
rgb[i + RGB_L * 3] = col; } return rgb;
}
解决方案 »
- java String的方法使用问题
- 急问,怎么实现这种界面的跳转啊?
- JAVA如何处理连续分隔符?请高人指点
- 怎样实现Java键盘控制图像缩放
- 正则表达式?
- java中将一维数组a[]中不相同的数按从小到大的序重新存于一组数组a[]中,在线等,
- 字符串加密解密,用VB写的加密,要用java解密(附VB解密源代码)。会VB与java的请进!
- JAVA中的数组最大是多大呀???????????????????????????
- 怎样将一个能int值转变为四个byte?(big endian)请提供代码实例,谢谢!
- awt中的高级事件(语意事件)和低级事件的区别
- Properties类的使用问题
- 大家帮我看看我的代码有什么问题啊(急急!!!!!!)
alpha = 127 - i;
import java.awt.*;
import javax.swing.*;
import java.awt.geom.*;public class DrawRGBRect extends JPanel{
public void paintComponent(Graphics g){
Graphics2D g2=(Graphics2D)g;
int x=50,y=50,lastx=200,lasty=100;
GradientPaint r2b = new GradientPaint(x, y, Color.red, lastx,lasty,Color.blue);
g2.setPaint(r2b);
int width=150,height=50,arcw=10,arch=10;
g2.fill(new RoundRectangle2D.Double(x,y,width,height,arcw, arch));
} public static void main(String args[]){
JFrame jf=new JFrame("画渐变填充矩形");
jf.getContentPane().add(new DrawRGBRect());
jf.setSize(300,200);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}