好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Java实现颜色渐变效果

本文实例为大家分享了java实现 颜色渐变 效果的具体代码,供大家参考,具体内容如下

rgb色彩,在自然界中肉眼所能看到的任何色彩都可以由红(r)、绿(g)、蓝(b)这三种色彩混合叠加而成,因此我们只要递增递减的修改其特定值就能得到相应的渐变效果。

运行效果:(图1)

运行5秒后:(图2)

java源代码:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

import java.awt.color;

import java.awt.dimension;

import java.awt.graphics;

import java.awt.image.bufferedimage;

 

  import javax.swing.jframe;

  import javax.swing.jpanel;

 

  /**

  * 图片显示面板类<br>

  * 颜色渐变效果测试

  *

  * @author wql

  *

  */

  public class imagepanel extends jpanel {

   private static final long serialversionuid = 1l;

   private int height = 350 ; // 高度

   private int width = 600 ; // 宽度

   private bufferedimage bufimg = null ; // 在该bufferedimage对象中绘制颜色

 

   /**

    * 构造方法

    */

   public imagepanel() {

     this .setpreferredsize( new dimension(width, height)); // 设置首选大小

   }

 

   /**

    * 初始化颜色

    */

    private void initcolor() {

      bufimg = new bufferedimage(width, height, bufferedimage.type_4byte_abgr); // 实例化bufferedimage

      graphics g = bufimg.getgraphics(); // 获取图片的graphics

      int w = width / 6 ; // 分成六个部分进行绘制

      for ( int i = 0 ; i < w; i++) {

        int x = 0 ;

        int d = ( int ) (i * ( 255.0 / w)); // 使d从0递增到255,实际可能只是接近255

        // 画第一部分颜色---红色到黄色

        g.setcolor( new color( 255 , d, 0 )); // 设置颜色

        g.drawline(i + w * x, 0 , i + w * x++, height); // 画直线---一条单色竖线

        // 画第二部分颜色---黄色到绿色

        g.setcolor( new color( 255 - d, 255 , 0 ));

        g.drawline(i + w * x, 0 , i + w * x++, height);

        // 画第三部分颜色---绿色到青色

        g.setcolor( new color( 0 , 255 , d));

        g.drawline(i + w * x, 0 , i + w * x++, height);

        // 画第四部分颜色---青色到蓝色

        g.setcolor( new color( 0 , 255 - d, 255 ));

        g.drawline(i + w * x, 0 , i + w * x++, height);

        // 画第五部分颜色---蓝色到洋红色

        g.setcolor( new color(d, 0 , 255 ));

        g.drawline(i + w * x, 0 , i + w * x++, height);

        // 画第六部分颜色---洋红色到红色

        g.setcolor( new color( 255 , 1 , 255 - d));

        g.drawline(i + w * x, 0 , i + w * x++, height);

      }

      repaint(); // 重绘

      try {

        system.out.println( "5秒后绘制黑色分隔线.." );

        thread.sleep( 5000 ); // 线程休息

      } catch (interruptedexception e) {

        e.printstacktrace();

      }

      // 绘制黑线来隔开六个部分

      system.out.println( "开始绘制黑色分隔线..." );

      g.setcolor(color.black); // 设置黑色

      for ( int i = 1 ; i < w; i++) {

        g.drawline(i * w, 0 , i * w, height); // 画直线

      }

      repaint(); // 重绘

    }

 

    /**

    * 绘制图片

    */

    public void paint(graphics g) {

      g.drawimage(bufimg, 0 , 0 , null ); // 画图片

    }

 

    /**

    * 主方法

    */

    public static void main(string[] args) {

      jframe f = new jframe( "颜色渐变效果" ); // 实例化一个窗体

      f.setdefaultcloseoperation(jframe.exit_on_close); // 设置窗体关闭时退出程序

      imagepanel imgpanel = new imagepanel(); // 实例化图片显示面板

      f.getcontentpane().add(imgpanel); // 添加到窗体

      f.pack(); // 根据窗体子组件的首选大小进行调整

      f.setlocationrelativeto( null ); // 设置窗体在屏幕中居中显示

      f.setvisible( true ); // 显示窗体

      imgpanel.initcolor(); // 绘制颜色

    }

  }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

原文链接:https://HdhCmsTestcnblogs测试数据/wuqianling/p/5340395.html

查看更多关于Java实现颜色渐变效果的详细内容...

  阅读:14次