// Scott DeRuiter 3/28/2003 // GridWithImages.java // Using a grid layout, click and print some cards. import java.awt.*; import java.awt.event.*; import javax.swing.*; public class GridWithImages extends JApplet implements MouseListener { private int xpos, ypos, width, height, max; private Image cards; private DrawingArea [] draw; public GridWithImages() { xpos = ypos = width = height = 40; max = 5; draw = new DrawingArea [ max ]; } public void init ( ) { setBackground ( Color.black ); Container contentPane = getContentPane(); contentPane.setLayout ( new GridLayout ( 0, max ) ); for ( int i = 0; i < draw.length; i++ ) { draw[i] = new DrawingArea ( ); draw[i].setBackground( new Color ( 130, 150, 140 ) ); contentPane.add ( draw[i] ); draw[i].addMouseListener(this); } cards = getImage ( getDocumentBase ( ), "smallcards.gif" ); WaitForImage ( this, cards ); } public void WaitForImage ( JApplet component, Image image ) { MediaTracker tracker = new MediaTracker ( component ); try { tracker.addImage ( image, 0 ); tracker.waitForID ( 0 ); } catch ( InterruptedException e ) { e.printStackTrace ( ); } } public Insets getInsets() { return new Insets ( 3, 3, 3, 3 ); } class DrawingArea extends JPanel { public void paintComponent ( Graphics g ) { super.paintComponent ( g ); int wid = this.getWidth ( ); int hei = this.getHeight ( ); int x = (int)( Math.random ( ) * 13 ); int y = (int)( Math.random ( ) * 4 ); g.drawImage( cards, wid / 2 - 50, hei / 2 - 75, wid / 2 + 50, hei / 2 + 75, x * 40, y * 60, ( x + 1 ) * 40, ( y + 1 ) * 60, this ); } } public void mousePressed ( MouseEvent e ) { for ( int i = 0; i < draw.length; i++ ) draw[i].repaint ( ); } public void mouseReleased ( MouseEvent e ) {} public void mouseClicked ( MouseEvent e ) {} public void mouseEntered ( MouseEvent e ) {} public void mouseExited ( MouseEvent e ) {} }