colorblend2.java

来自「java 完全探索的随书源码」· Java 代码 · 共 158 行

JAVA
158
字号
// ColorBlend2.java

import java.awt.*;
import java.awt.image.*;
import java.applet.Applet;

public class ColorBlend2 extends Applet implements Runnable
{
   final static int UP = 0;
   final static int DOWN = 1;

   Image im;
   Thread t;

   public void start ()
   {
      if (t == null)
      {
          t = new Thread (this);
          t.start ();
      }
   }

   public void run ()
   {
      int directionB = UP;
      int directionG = UP;
      int directionR = UP;

      int width = getSize ().width;
      int height = getSize ().height;

      int [] pixels = new int [width * height];

      MemoryImageSource mis = new MemoryImageSource (width, height,
                                                     pixels, 0, width);
      mis.setAnimated (true);
      mis.setFullBufferUpdates (false);

      im = createImage (mis);

      Thread cur = Thread.currentThread ();

      while (t == cur)
      {
         // Create new image by modifying colors.

         int index = 0;

         for (int y = 0; y < height; y++)
         {
              int numerator = y * 255;

              int b = numerator / height;

              if (directionB == UP)
              {
                  b += 5;
                  if (b > 255)
                  {
                      b = 255;
                      directionB = DOWN;
                  }
              }
              else
              {
                  b -= 5;
                  if (b < 0)
                  {
                      b = 0;
                      directionB = UP;
                  }
              }

              int r = 255 - numerator / height;

              if (directionR == UP)
              {
                  r += 5;
                  if (r > 255)
                  {
                      r = 255;
                      directionR = DOWN;
                  }
              }
              else
              {
                  r -= 5;
                  if (r < 0)
                  {
                      r = 0;
                      directionR = UP;
                  }
              }

              for (int x = 0; x < width; x++)
              {
                   int g = x * 255 / width;

                   if (directionG == UP)
                   {
                       g += 5;
                       if (g > 255)
                       {
                           g = 255;
                           directionG = DOWN;
                       }
                   }
                   else
                   {
                       g -= 5;
                       if (g < 0)
                       {
                           g = 0;
                           directionG = UP;
                       }
                   }

                   pixels [index++] = (255 << 24) | (r << 16) | (g << 8)
                                      | b;
              }
         }

         // Deliver pixels.

         mis.newPixels ();

         // Draw image.

         repaint ();

         // Pause.

         try
         {
            Thread.sleep (50);
         }
         catch (InterruptedException e) {}
      }
   }

   public void paint (Graphics g)
   {
      if (im != null)
          g.drawImage (im, 0, 0, this);
   }

   public void update (Graphics g)
   {
      paint (g);
   }

   public void stop ()
   {
      t = null;
   }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?