// // another example of the Java AWT, showing creation of an Image // an image. // see class definition for details. // // this example is modified from a similar example in chapter 17 of // _Exploring Java_ (Niemeyer and Peck). // import java.awt.* ; import java.awt.event.* ; import java.awt.image.* ; // // a BlackWhiteStatic object is a DrawingFrame that displays a "static" // (random colors) image. the image changes periodically. // if the window is resized, the image is scaled to fit. // class BlackWhiteStatic extends DrawingFrame implements ComponentListener, Runnable { private Image img ; private int imgWidth, imgHeight ; private int pixelData[] ; private MemoryImageSource imgSource ; private int refreshRate ; BlackWhiteStatic(Dimension dims, int refr) { super("BlackWhiteStatic") ; setDrawingSize(dims) ; refreshRate = refr ; initializeImage(dims) ; addComponentListener(this) ; // start thread to change image periodically new Thread(this).start() ; show() ; } // initializes image -- sets up width, height, pixel data // array. private void initializeImage(Dimension dims) { imgWidth = dims.width ; imgHeight = dims.height ; pixelData = new int [imgWidth * imgHeight] ; imgSource = new MemoryImageSource(imgWidth, imgHeight, pixelData, 0, imgWidth) ; imgSource.setAnimated(true) ; img = createImage(imgSource) ; } // generates random static, every refreshRate milliseconds. public void run() { while (true) { try { Thread.sleep(refreshRate) ; } catch (InterruptedException e) { // should not happen } int i=0 ; for (int col = 0 ; col < imgWidth ; col++) { for (int row = 0 ; row < imgHeight ; row++) { boolean rand = (Math.random() > 0.5) ; pixelData[i++] = rand ? Color.black.getRGB() : Color.white.getRGB() ; } } System.out.println("updating image data") ; imgSource.newPixels(0, 0, imgWidth, imgHeight) ; } } // creates onscreen image from pixel data. public void paint(Graphics g) { System.out.println("paint called") ; // position origin to avoid frame borders. translateDrawingArea(g) ; // draw image. g.drawImage(img, 0, 0, this) ; } // updates onscreen image (no need to clear first, just paint). public void update(Graphics g) { System.out.println("update called") ; paint(g) ; } // called when component is resized. public void componentResized(ComponentEvent e) { System.out.println("componentResized called") ; initializeImage(getDrawingSize()) ; repaint() ; } // other methods of ComponentListener interface. public void componentHidden(ComponentEvent e) { } public void componentMoved(ComponentEvent e) { } public void componentShown(ComponentEvent e) { } // // ---- main --------------------------------------------------- // // command-line arguments: initial width, height, // refresh rate in milliseconds (defaults 400, 200, 1000) // public static void main(String[] args) { int w = 400 ; int h = 200 ; int r = 1000 ; if (args.length >= 3) { w = Integer.parseInt(args[0]) ; h = Integer.parseInt(args[1]) ; r = Integer.parseInt(args[2]) ; } BlackWhiteStatic c = new BlackWhiteStatic(new Dimension(w, h), r) ; } }