import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class FunkyGame extends JApplet
{
private CardLayout maincards, smallercards;
private Container cmain, csmall;
private FirstPanel intro;
private MainPanel thegame;
private LastPanel last;
public void init()
{
cmain = this.getContentPane();
maincards = new CardLayout ( );
cmain.setLayout ( maincards );
intro = new FirstPanel ( );
thegame = new MainPanel ( );
last = new LastPanel ( );
cmain.add ( intro, "First" );
cmain.add ( thegame, "Second" );
cmain.add ( last, "Third" );
getContentPane().setBackground( Color.darkGray );
}
class FirstPanel extends JPanel implements MouseListener
{
public FirstPanel()
{
addMouseListener( this );
setBackground ( Color.blue );
}
public void paintComponent( Graphics g )
{
super.paintComponent( g );
Font direct = new Font ( "Arial", Font.BOLD, 34 );
g.setFont ( direct );
g.drawString ( "This is the intro panel,", 50, 180 );
g.drawString ( "click anywhere", 50, 260 );
g.drawString ( "to continue.", 50, 340 );
}
public void mousePressed(MouseEvent evt)
{
maincards.next ( cmain );
}
public void mouseEntered(MouseEvent evt) { }
public void mouseExited(MouseEvent evt) { }
public void mouseClicked(MouseEvent evt) { }
public void mouseReleased(MouseEvent evt) { }
}
class MainPanel extends JPanel
{
public MainPanel()
{
setBackground ( Color.black );
this.setLayout(null);
ControlSmallCards lpanel = new ControlSmallCards ( );
lpanel.setLocation ( 410, 125 );
lpanel.setSize ( 85, 370 );
lpanel.setBackground ( Color.blue );
this.add ( lpanel );
ControlLargeCards bpanel = new ControlLargeCards ( );
bpanel.setLocation ( 5, 5 );
bpanel.setSize ( 490, 115 );
bpanel.setBackground ( Color.gray );
this.add ( bpanel );
JPanel flip = new CardPanel ( );
flip.setLocation ( 5, 125 );
flip.setSize ( 400, 370 );
flip.setBackground ( Color.green );
this.add ( flip );
}
class ControlSmallCards extends JPanel implements ActionListener
{
JButton one, two, three, four;
public ControlSmallCards ( )
{
setLayout(null);
ImageIcon icon1 = getIcon ("dog.jpg");
ImageIcon icon2 = getIcon ("kitten.jpg");
ImageIcon icon3 = getIcon ("mouse.jpg");
ImageIcon icon4 = getIcon ("ladybug.jpg");
Font buttonfont = new Font ( "Serif", Font.BOLD, 50 );
one = new JButton ( icon1 );
one.addActionListener ( this );
one.setLocation ( 5, 5 );
one.setSize ( 75, 75 );
this.add ( one );
two = new JButton ( icon2 );
two.addActionListener ( this );
two.setLocation ( 5, 90 );
two.setSize ( 75, 75 );
two.setFont ( buttonfont );
this.add ( two );
three = new JButton ( icon3 );
three.addActionListener ( this );
three.setLocation ( 5, 175 );
three.setSize ( 75, 75 );
three.setFont ( buttonfont );
this.add ( three );
four = new JButton ( icon4 );
four.addActionListener ( this );
four.setLocation ( 5, 260 );
four.setSize ( 75, 75 );
four.setFont ( buttonfont );
this.add ( four );
}
ImageIcon getIcon (String icon_name)
{
ImageIcon icon;
try
{
icon = new ImageIcon (new java.net.URL ( getCodeBase (), icon_name) );
}
catch (java.net.MalformedURLException mue)
{
return null;
}
return icon;
} // getIcon
public void actionPerformed ( ActionEvent evt )
{
String command = evt.getActionCommand();
Object source = evt.getSource();
if ( source == one )
smallercards.show ( csmall, "First" );
else if ( source == two )
smallercards.show ( csmall, "Second" );
else if ( source == three )
smallercards.show ( csmall, "Third" );
else if ( source == four )
smallercards.show ( csmall, "Fourth" );
}
}
class ControlLargeCards extends JPanel implements ActionListener
{
public ControlLargeCards ( )
{
setLayout(null);
Font buttonfont = new Font ( "Serif", Font.BOLD, 18 );
JButton gotofirst = new JButton ( "RETURN TO START" );
gotofirst.addActionListener ( this );
gotofirst.setLocation ( 25, 20 );
gotofirst.setSize ( 220, 80 );
gotofirst.setFont ( buttonfont );
this.add ( gotofirst );
JButton gotoend = new JButton ( "GO TO END" );
gotoend.addActionListener ( this );
gotoend.setLocation ( 280, 20 );
gotoend.setSize ( 180, 80 );
gotoend.setFont ( buttonfont );
this.add ( gotoend );
}
public void actionPerformed ( ActionEvent evt )
{
String command = evt.getActionCommand();
if ( command.equals ( "RETURN TO START" ) )
maincards.first ( cmain );
else if ( command.equals ( "GO TO END" ) )
maincards.last ( cmain );
}
}
class CardPanel extends JPanel
{
SimplePanel startpanel, onepanel, pads, end;
public CardPanel()
{
csmall = this;
smallercards = new CardLayout();
csmall.setLayout( smallercards );
startpanel = new SimplePanel( "Dog Panel" );
startpanel.setBackground ( Color.yellow );
onepanel = new SimplePanel( "Cat Panel" );
onepanel.setBackground ( Color.lightGray );
pads = new SimplePanel( "Mouse Panel" );
pads.setBackground ( Color.red );
end = new SimplePanel( "Ladybug Panel" );
end.setBackground ( Color.green );
csmall.add( startpanel, "First" );
csmall.add( onepanel, "Second" );
csmall.add( pads, "Third" );
csmall.add( end, "Fourth" );
}
class SimplePanel extends JPanel
{
private String words;
public SimplePanel ( String s )
{
words = s;
}
public void paintComponent( Graphics g )
{
super.paintComponent( g );
Font direct = new Font ( "Arial", Font.BOLD, 34 );
g.setFont ( direct );
g.drawString ( words, 40, 200 );
}
}
}
public void paintComponent( Graphics g )
{
super.paintComponent( g );
}
}
class LastPanel extends JPanel implements MouseListener
{
public LastPanel()
{
addMouseListener( this );
setBackground ( Color.red );
}
public void paintComponent( Graphics g )
{
super.paintComponent( g );
Font direct = new Font ( "Arial", Font.BOLD, 34 );
g.setFont ( direct );
g.drawString ( "This is the final panel,", 50, 180 );
g.drawString ( "click anywhere to go", 50, 260 );
g.drawString ( "back to the first panel.", 50, 340 );
}
public void mousePressed(MouseEvent evt)
{
maincards.first ( cmain );
}
public void mouseEntered(MouseEvent evt) { }
public void mouseExited(MouseEvent evt) { }
public void mouseClicked(MouseEvent evt) { }
public void mouseReleased(MouseEvent evt) { }
}
}
Back to Lesson 30 Examples
Back to Java Main Page