⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 timerdemo2.java

📁 java 完全探索的随书源码
💻 JAVA
字号:
// TimerDemo2.java

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;

class TimerDemo2 extends JFrame
{
   final static int WIDTH = 200;
   final static int HEIGHT = 200;

   final static int DIRECTCOLORBACK = 0;
   final static int INDEXCOLORBACK = 1;

   int backType = INDEXCOLORBACK;

   Timer timer;
   int frameNumber = -1;

   TimerDemo2 (String title)
   {
      super (title);

      addWindowListener (new WindowAdapter ()
                         {
                             public void windowClosing (WindowEvent e)
                             {
                                System.exit (0);
                             }
                         });

      int [] pixels = new int [WIDTH * HEIGHT];

      Toolkit tk = Toolkit.getDefaultToolkit ();
      Image imBack;

      if (backType == DIRECTCOLORBACK)
      {
          MkDirectColorBackground (pixels, WIDTH, HEIGHT);
          imBack = tk.createImage (new MemoryImageSource (WIDTH,
                                                          HEIGHT,
                                                          pixels,
                                                          0,
                                                          WIDTH));
      }
      else
      {
          IndexColorModel icm;
          icm = MkIndexColorBackground (pixels, WIDTH, HEIGHT);
          imBack = tk.createImage (new MemoryImageSource (WIDTH,
                                                          HEIGHT,
                                                          icm,
                                                          pixels,
                                                          0,
                                                          WIDTH));
      }

      Image imFront = tk.getImage ("bullet.gif");

      final AnimPane ap = new AnimPane (imBack, imFront);

      setContentPane (ap);

      ActionListener a = new ActionListener ()
                         {
                             public void actionPerformed (ActionEvent e)
                             {
                                frameNumber++;

                                ap.repaint ();
                             }
                         };

      timer = new Timer (300, a);

      timer.start ();

      setSize (WIDTH, HEIGHT);
      setVisible (true);
   }

   void MkDirectColorBackground (int [] pixels, int w, int h)
   {
      int index = 0;

      for (int y = 0; y < h; y++)
      {
           int numerator = y * 255;
           int b = numerator / h;
           int r = 255 - numerator / h;

           for (int x = 0; x < w; x++)
           {
                int g = x * 255 / w;
                pixels [index++] = (255 << 24) | (r << 16) | (g << 8)
                                   | b;
           }
      }
   }

   IndexColorModel MkIndexColorBackground (int [] pixels, int w, int h)
   {
      Color [] colors = { Color.magenta, Color.green, Color.blue };

      byte [] reds = new byte [colors.length];
      byte [] greens = new byte [colors.length];
      byte [] blues = new byte [colors.length];

      for (int i = 0; i < colors.length; i++)
      {
           reds [i] = (byte) colors [i].getRed ();
           greens [i] = (byte) colors [i].getGreen ();
           blues [i] = (byte) colors [i].getBlue ();
      }

      int stripeSize = w / colors.length;

      int colorIndex;
      int index = 0;

      for (int y = 0; y < h; y++)
           for (int x = 0; x < w; x++)
           {
                if (x < stripeSize)
                    colorIndex = 0; 
                else
                if (x < stripeSize * 2)
                    colorIndex = 1;
                else
                    colorIndex = 2;

                pixels [index++] = colorIndex;
           }

      IndexColorModel icm;
      icm = new IndexColorModel (8, colors.length, reds, greens, blues);
      return icm;
   }

   class AnimPane extends JPanel
   {
      Image back, front;
    
      AnimPane (Image back, Image front)
      {
         this.back = back;
         this.front = front;
      }
    
      //Draw the current frame of animation.

      public void paintComponent (Graphics g)
      {
         super.paintComponent (g);  // Paint space not covered
                                    // by background image.

         int compWidth = getWidth ();
         int compHeight = getHeight ();
         int imgWidth, imgHeight;
    
         // If we have a valid width and height for the 
         // background image, draw this image - centered
         // horizontally and vertically.

         imgWidth = back.getWidth (this);
         imgHeight = back.getHeight (this);

         if (imgWidth > 0 && imgHeight > 0)
             g.drawImage (back, 
                          (compWidth - imgWidth) / 2,
                          (compHeight - imgHeight) / 2, this);
    
         // If we have a valid width and height for the 
         // front image, draw it.

         imgWidth = front.getWidth (this);
         imgHeight = front.getHeight (this);

         if (imgWidth > 0 && imgHeight > 0)
         {
             // Compute new horizontal position to fall in component
             // bounds.  The larger the multiplier (such as 10), the
             // greater the horizontal distance that's travelled.

             int x = (frameNumber * 10) % (imgWidth + compWidth)
                     - imgWidth;

             // Center front image vertically.

             int y = (compHeight - imgHeight) / 2;

             // Draw front image.

             g.drawImage (front, x, y, this);
         }
      }
   }

   public static void main (String [] args)
   {
      new TimerDemo2 ("Timer Demo2");
   }
}


⌨️ 快捷键说明

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