import java.awt.*; import java.awt.image.*; public class Graphics6 { static public void pause(int n) throws Exception { Thread.sleep( 100 * n ); } static public void main(String[] args) throws Exception { final int YELLOW = 0xFFFF00; final int BLACK = 0x000000; /* ------------------------------------------------ Preparing to draw picture... ------------------------------------------------ */ Canvas pic = new MyCanvas(); // Make a canvas (2 dim array) Frame f = new Frame( "My image" ); // Create a window f.add("Center", pic); // Put the canvas in the window f.setSize(400,300); // Set size of the window f.setVisible(true); // Make window visible /* ----------------------------------------------------------- Repeatedly draw a 10x10 box moving over a horizontal line ----------------------------------------------------------- */ int x = 0, y = 100; while ( true ) // Repeat FOREVER !!! { /* ------------------------------------------------ Paint the box at position (x,y) ------------------------------------------------ */ for (int i = 0; i < 10; i++ ) for (int j = 0; j < 10; j++ ) MyCanvas.Image.setRGB(x+i, y+j, YELLOW); pic.repaint(); // Repaint the picture pause(5); /* ------------------------------------------------ ERASE the box (= paint it BLACK !) ------------------------------------------------ */ for (int i = 0; i < 10; i++ ) for (int j = 0; j < 10; j++ ) MyCanvas.Image.setRGB(x+i, y+j, BLACK); pic.repaint(); // Repaint the picture /* ------------------------------------------------ Move box 10 pixels to the right ------------------------------------------------ */ x = x + 10; /* ---------------------------------------- Check if the box has run off the canvas ---------------------------------------- */ if ( (x + 10) > 400 ) x = 0; // If did, repeat from the left again } } }