Mini Assignment: Police Sketch
Write
a police sketch program which outputs a customized face. The program prints out the face based on user choices for hair
type, eye style, mouth style, etc. You should have at least three different
parts to a head (e.g., hair, eyes, nose, mouth) and, for each part, at least
three choices.
A
example of a face is:
Notes:
you need white space, comments and tabbing. Make sure that you have the
3 title comments and pre and post conditions on your methods.
Addendum:
Below are some additional faces, in case you run out of artistic steam:
|||||||||||||||| ||||||||||||||||
||||||||||||||||
___\\\|///__
|
|
|
| ||||||||||||||||
/ \
|
|
| -- --
| ||||||||||||||||
/
\
| o
o |
| | | -- |
| | |
| |
___ ___
|
|
|
| |o |/ \|o | | |
o o
| |--|
o |-| o |-|
_|
|_ |
|__| |__| | |
| |
--- ---
|
|_
_| |
| _|
|__
_| ^
|_
| |______|
|
| ///|\\\
| |_
_| |_
_|
|
|
\
/
|
| |
////\\\\ |
|
|
\ o /
|
|______| |
| /|______|\
|
\________/
|
| |||
|||
||| |||
\\\///
Code:
Needs to be saved in the games folder. This code will get you started.
import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.applet.*;
public class Chooser extends Applet implements ActionListener
{
JLabel hallowPic, coinPic;
public void init ()
{
resize (300, 360);
JLabel title = new JLabel (" Choose the picture you want.");
title.setPreferredSize (new Dimension (280, 50));
title.setFont (new Font ("Arial", Font.BOLD, 18));
title.setForeground (Color.red);
JButton skull = new JButton ("Skull");
skull.setActionCommand ("Skull");
skull.addActionListener (this);
JButton web = new JButton ("Web");
web.setActionCommand ("Web");
web.addActionListener (this);
JButton house = new JButton ("House");
house.setActionCommand ("House");
house.addActionListener (this);
Panel p = new Panel (new GridLayout (3, 1));
p.add (skull);
p.add (web);
p.add (house);
JButton heads = new JButton ("Heads");
heads.setActionCommand ("Heads");
heads.addActionListener (this);
JButton tails = new JButton ("Tails");
tails.setActionCommand ("Tails");
tails.addActionListener (this);
Panel p2 = new Panel (new GridLayout (2, 1));
p2.add (heads);
p2.add (tails);
hallowPic = new JLabel (createImageIcon ("hallow1.gif"));
hallowPic.setPreferredSize (new Dimension (150, 122));
coinPic = new JLabel (createImageIcon ("coin1.jpg"));
add (title);
add (p);
add (hallowPic); add (p2); add (coinPic); }
public void actionPerformed (ActionEvent e)
{
if (e.getActionCommand ().equals ("Skull"))
hallowPic.setIcon (createImageIcon ("hallow1.gif"));
else if (e.getActionCommand ().equals ("House"))
hallowPic.setIcon (createImageIcon ("hallow3.gif"));
else if (e.getActionCommand ().equals ("Web"))
hallowPic.setIcon (createImageIcon ("hallow2.gif"));
else if (e.getActionCommand ().equals ("Heads"))
coinPic.setIcon (createImageIcon ("coin1.jpg"));
else if (e.getActionCommand ().equals ("Tails"))
coinPic.setIcon (createImageIcon ("coin2.jpg"));
}
protected static ImageIcon createImageIcon (String path)
{
java.net.URL imgURL = Chooser.class.getResource (path);
if (imgURL != null)
{
return new ImageIcon (imgURL);
}
else
{
System.err.println ("Couldn't find file: " + path);
return null;
}
}
}