bufferedimagetest.java

来自「java learn PPT java learn PPT java learn」· Java 代码 · 共 79 行

JAVA
79
字号
     import java.applet.*;
     import java.awt.*;
     import java.awt.image.*;
     import java.awt.geom.*; 
          
     public class BufferedImageTest extends Applet 
     {  
          // 一个 BufferedImage, 还有它的宽和高
          private BufferedImage image;
          private final int BI_WIDTH  = 100;
          private final int BI_HEIGHT = 100;

          // 用来生成随机颜色和屏幕位置
          private java.util.Random random;

          public void init()
          {
               random = new java.util.Random();

               // 创建一个(BI_WIDTH x BI_HEIGHT)的缓冲图像 
               image = new BufferedImage(BI_WIDTH, BI_HEIGHT, BufferedImage.TYPE_INT_RGB);

               // 为BufferedImage创建一个 Graphics2D容器. 记住,它和Applet的Graphics2D没有什么关系	
               Graphics2D g2d = image.createGraphics();

               // 我们将在BufferedImage上绘制一些条纹
               // 条纹的宽和高分别是图像宽和高的十分之一 
               final int stripWidth  = BI_WIDTH  / 10;
               final int stripHeight = BI_HEIGHT / 10;

               // 用随机颜色填充图像
               g2d.setPaint(new Color(random.nextInt()));
               g2d.fill(new Rectangle(0, 0, BI_WIDTH, BI_HEIGHT));

               // 使用随机颜色绘制垂直的条纹
               g2d.setPaint(new Color(random.nextInt()));
               int x = stripWidth / 2;
               while(x < BI_WIDTH)
               {
                    g2d.fill(new Rectangle(x, 0, stripWidth, BI_HEIGHT));                       
                    x += 2 * stripWidth;
               }
              
               // 给条纹设置一个透明通道
               g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f));

               // 使用随机颜色绘制水平的条纹
               g2d.setPaint(new Color(random.nextInt()));
               int y = stripHeight / 2;
               while(y < BI_HEIGHT)
               {
                    g2d.fill(new Rectangle(0, y, BI_WIDTH, stripHeight));                       
                    y += 2 * stripHeight;
               }

               // render a dark opaque outline around the perimeter of the image
               g2d.setStroke(new BasicStroke(2.0f));
               g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
               g2d.setPaint(Color.BLACK);
               g2d.draw(new Rectangle(0, 0, BI_WIDTH, BI_HEIGHT));
          }
          
          public void paint(Graphics g)
          {
               // cast the sent Graphics context to get a usable Graphics2D object
               Graphics2D g2d = (Graphics2D)g;

               // draw a bunch of images at random locations               
               for(int i = 0; i < 20; i++)
               {
                    g2d.drawImage(image,
                                  AffineTransform.getTranslateInstance(
                                       random.nextInt()%getSize().getWidth(), 
                                       random.nextInt()%getSize().getHeight()),
                                  this);               
               }
          }

     }    // BufferedImageTest

⌨️ 快捷键说明

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