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

📄 staticgenerator.java

📁 learning java的源代码。书中每个实例都有相关的代码example。
💻 JAVA
字号:
//file: StaticGenerator.javaimport java.awt.*;import java.awt.event.*;import java.awt.image.*;import java.util.Random;import javax.swing.*;public class StaticGenerator extends JComponent implements Runnable {  byte[] data;  BufferedImage image;  Random random;  public void initialize(  ) {    int w = getSize().width, h = getSize(  ).height;    int length = ((w + 7) * h) / 8;    data = new byte[length];    DataBuffer db = new DataBufferByte(data, length);    WritableRaster wr = Raster.createPackedRaster(db, w, h, 1, null);    ColorModel cm = new IndexColorModel(1, 2,        new byte[] { (byte)0, (byte)255 },        new byte[] { (byte)0, (byte)255 },        new byte[] { (byte)0, (byte)255 });    image = new BufferedImage(cm, wr, false, null);    random = new Random(  );    new Thread(this).start(  );  }  public void run(  ) {    while (true) {      random.nextBytes(data);      repaint(  );      try { Thread.sleep(1000 / 24); }      catch( InterruptedException e ) { /* die */ }    }  }  public void paint(Graphics g) {    if (image == null) initialize(  );    g.drawImage(image, 0, 0, this);  }  public static void main(String[] args) {    JFrame f = new JFrame("StaticGenerator");    f.getContentPane().add(new StaticGenerator(  ));    f.setSize(300, 300);    f.setLocation(100, 100);    f.addWindowListener(new WindowAdapter(  ) {      public void windowClosing(WindowEvent e) {        System.exit(0);      }    });    f.setVisible(true);  }}

⌨️ 快捷键说明

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