// Scott DeRuiter 4/7/03
// MoreCards.java
// Simple example of a CardLayout
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MoreCards extends JApplet implements MouseListener
{
private Image [] image;
private int index, max;
private JPanel [] panel;
private CardLayout cards;
private Container c;
private Font font;
public MoreCards ( )
{
index = 0;
max = 30;
font = new Font ( "Serif", Font.BOLD, 26 );
}
public void init ( )
{
c = this.getContentPane();
image = new Image[max];
panel = new JPanel[max];
cards = new CardLayout ( );
c.setLayout ( cards );
for ( int i = 0; i < image.length; i++ )
{
image[i] = getImage ( getDocumentBase ( ), "pic" + i + ".jpg" );
WaitForImage ( this, image[i] );
panel[i] = new MyPanel ( image[i], i + 1 );
panel[i].addMouseListener(this);
c.add ( panel[i], "Panel" + (i+1) );
}
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 MyPanel extends JPanel
{
private Image pict;
private int number;
public MyPanel ( Image im, int i )
{
pict = im;
number = i;
}
public void paintComponent(Graphics g)
{
super.paintComponent ( g );
g.setFont ( font );
g.drawImage ( pict, 0, 0, this );
g.setColor ( Color.black );
g.fillRect ( 6, 364, 188, 30 );
g.setColor ( Color.white );
g.fillRect ( 8, 366, 184, 26 );
g.setColor ( Color.red );
g.drawString ( "PICTURE " + number, 16, 388 );
}
}
public void mousePressed(MouseEvent evt)
{
if ( evt.isShiftDown() )
cards.first ( c );
else if ( evt.isMetaDown() )
cards.previous ( c );
else
cards.next ( c );
}
public void mouseEntered(MouseEvent evt) { }
public void mouseExited(MouseEvent evt) { }
public void mouseClicked(MouseEvent evt) { }
public void mouseReleased(MouseEvent evt) { }
}
Back to Lesson 29 Examples
Back to Java Main Page