import java.awt.*; import java.awt.event.*; import javax.swing.*; public class GridandCard extends JApplet { private Image [] image; private int index, max; IntroPanel firstpanel; MainPanel primary; ExitPanel byebye; private CardLayout cards; private Container c; public GridandCard ( ) { index = 0; max = 5; image = new Image[max]; } public void init ( ) { c = this.getContentPane(); for ( int i = 0; i < image.length; i++ ) { image[i] = getImage ( getDocumentBase ( ), "pic" + i + ".jpg" ); WaitForImage ( this, image[i] ); } firstpanel = new IntroPanel ( image[2] ); primary = new MainPanel(); byebye = new ExitPanel ( image[4] ); cards = new CardLayout ( ); c.setLayout ( cards ); c.add ( firstpanel, "First" ); c.add ( primary, "Second" ); c.add ( byebye, "Third" ); getContentPane().setBackground( Color.blue ); } 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 ( ); } } class IntroPanel extends JPanel implements MouseListener { private Image pict; public IntroPanel ( Image im ) { pict = im; addMouseListener(this); } public void paintComponent(Graphics g) { super.paintComponent ( g ); g.drawImage ( pict, 0, 0, this ); } public void mousePressed(MouseEvent evt) { cards.next ( c ); } public void mouseEntered(MouseEvent evt) { } public void mouseExited(MouseEvent evt) { } public void mouseClicked(MouseEvent evt) { } public void mouseReleased(MouseEvent evt) { } } class MainPanel extends JPanel { private ImagePanel panel1; private BackPanel panel2; private ClickPanel panel3; private DrawPanel panel4; public MainPanel ( ) { this.setLayout ( new GridLayout ( 2, 2, 5, 5 ) ); panel1 = new ImagePanel ( ); panel1.setBackground( Color.blue ); this.add ( panel1 ); panel2 = new BackPanel ( ); panel2.setBackground( Color.red ); this.add ( panel2 ); panel3 = new ClickPanel ( ); panel3.setBackground( Color.darkGray ); this.add ( panel3 ); panel4 = new DrawPanel ( ); panel4.setBackground( Color.lightGray ); this.add ( panel4 ); } class ImagePanel extends JPanel implements MouseListener { private Image [] image; private int value; public ImagePanel ( ) { value = 0; image = new Image[5]; for ( int i = 0; i < image.length; i++ ) { image[i] = getImage ( getCodeBase ( ), "pic" + i + ".jpg" ); WaitForImage ( this, image[i] ); } addMouseListener(this); } public void WaitForImage ( JPanel component, Image image ) { MediaTracker tracker = new MediaTracker ( component ); try { tracker.addImage ( image, 0 ); tracker.waitForID ( 0 ); } catch ( InterruptedException e ) { e.printStackTrace ( ); } } public void paintComponent ( Graphics g ) { super.paintComponent ( g ); g.drawImage ( image[value], 0, 0, getSize().width, getSize().height, this ); } public void mousePressed ( MouseEvent e ) { value++; if ( value == 5 ) value = 0; repaint ( ); } public void mouseReleased ( MouseEvent e ) {} public void mouseClicked ( MouseEvent e ) {} public void mouseEntered ( MouseEvent e ) {} public void mouseExited ( MouseEvent e ) {} } class BackPanel extends JPanel implements MouseListener { public BackPanel ( ) { addMouseListener(this); } public void paintComponent ( Graphics g ) { super.paintComponent ( g ); } public void mousePressed ( MouseEvent e ) { cards.next ( c ); } public void mouseReleased ( MouseEvent e ) {} public void mouseClicked ( MouseEvent e ) {} public void mouseEntered ( MouseEvent e ) {} public void mouseExited ( MouseEvent e ) {} } class ClickPanel extends JPanel implements MouseListener { private int x, y; public ClickPanel ( ) { x = y = 0; addMouseListener(this); } public void paintComponent ( Graphics g ) { super.paintComponent ( g ); } public void PlaceCircle ( Graphics g ) { g.setColor ( Color.red ); g.fillOval ( x - 15, y - 15, 30, 30 ); g.setColor ( Color.black ); g.drawOval ( x - 15, y - 15, 30, 30 ); } public void mousePressed ( MouseEvent e ) { x = e.getX ( ); y = e.getY ( ); Graphics gr = getGraphics ( ); PlaceCircle ( gr ); } public void mouseReleased ( MouseEvent e ) {} public void mouseClicked ( MouseEvent e ) {} public void mouseEntered ( MouseEvent e ) {} public void mouseExited ( MouseEvent e ) {} } class DrawPanel extends JPanel implements MouseListener, MouseMotionListener { private int x, y, oldx, oldy; public DrawPanel ( ) { x = y = oldx = oldy = 0; addMouseListener(this); addMouseMotionListener(this); } public void paintComponent ( Graphics g ) { super.paintComponent ( g ); } public void DrawIt ( Graphics g ) { g.setColor ( Color.black ); g.drawLine ( x, y, oldx, oldy ); oldx = x; oldy = y; } public void mousePressed ( MouseEvent e ) { x = oldx = e.getX ( ); y = oldy = e.getY ( ); Graphics gr = getGraphics ( ); DrawIt ( gr ); } public void mouseReleased ( MouseEvent e ) {} public void mouseClicked ( MouseEvent e ) {} public void mouseEntered ( MouseEvent e ) {} public void mouseExited ( MouseEvent e ) {} public void mouseMoved ( MouseEvent e ) {} public void mouseDragged ( MouseEvent e ) { x = e.getX ( ); y = e.getY ( ); Graphics gr = getGraphics ( ); DrawIt ( gr ); } } } class ExitPanel extends JPanel implements ActionListener { private Image pict; public ExitPanel ( Image im ) { pict = im; setBackground ( Color.red ); JButton reset = new JButton ( "RESET" ); reset.addActionListener ( this ); this.add ( reset ); } public void paintComponent(Graphics g) { super.paintComponent ( g ); g.drawImage ( pict, 0, 0, this ); } public void actionPerformed ( ActionEvent evt ) { String command = evt.getActionCommand(); if ( command.equals ( "RESET" ) ) cards.first ( c ); } } }