CS 160

March 8, 2005

The Beauty Pageant for Spring 2005



You have an Outlab due Tuesday, March 22nd that is drawing a head (get as fancy as you want). Below is an example of a head I did Monday afternoon.

If you have looked at the applet Appendix in the Textbook, they have examples for drawing different shapes. Ignore the part of the appendix that starts on page 705, just pay attention to the first part of the appendix.
ALT="Your browser understands the <APPLET> tag but isn't running the applet, for some reason." Your browser is ignoring the <APPLET> tag! This applet to the right uses Graphics2D, it's a better way to do your drawing. Graphics2D includes everything Graphics includes, but it handles it better. I included the paint method below.
  	public void paint(Graphics g)
	{
	Graphics2D g2 = (Graphics2D)g;
        g.drawLine(20, 20, 100, 50);
        g.setColor(Color.green);
        g.setColor(Color.red);
        g.drawLine(20, 100, 100, 50);
        g.drawRect(0, 0, 20, 20);
        g.setColor(Color.blue);
        g.drawRect(20, 100, 50, 60);
	}

This applet is the same program but it's just using the regular Graphics class.
   
	public void paint(Graphics g)
	{
		
        g.drawLine(20, 20, 100, 50);
        g.setColor(Color.green);
        g.setColor(Color.red);
        g.drawLine(20, 100, 100, 50);
        g.drawRect(0, 0, 20, 20);
        g.setColor(Color.blue);
        g.drawRect(20, 100, 50, 60);
	}
This an example of a head that I did using Rectangles, ovals, and lines. Your outlab next Tuesday is to create a head of your own using the ovals, rects, and anything else in the Graphics class that you wish to use. You will have a beauty pageant in the lab Tuesday.
I wish each of you the best of luck, be creative.

Can you create the ultimate in Applet Beauty?
I expect you to do much better than the one above.
Advanced topics
Things you might want to try to add to make your beauty even more interesting.
  • Hair
  • Accessorize, hats, jewelry, ties, etc. etc.
Even more advanced
  • Use a Image instance to put in a background image. Look up Image in the API.
  • Use a while loop to create animation, maybe make the eyes blink, or the mouth talk.

    Very advanced :

  • Put in a MouseListener to start an animation when the mouse is clicked in your Applet. You need to do four things to implement a simple MouseListener:
    1. First import the awt event package:
                 import java.awt.event.*;  
              
    2. Next implement the MouseListener, add it to your class definition:
      public class MyApplet extends JApplet implements MouseListener
    3. Next in your init method start the MouseListeners listening to your applet for a mouse command.
              addMouseListener(this);
           
    4. Last include the MouseListener methods in your class. You can put them below your paint method. Each must be included. On this example I put a repaint call in the mouseClicked method, the rest of the methods do nothing, but you have to include all of them if you are implementing the MouseListener interface (you can also use any of them if you want). If the mouse is clicked in the Applet it calls repaint() which will then call paint. You can set an int flag that changes everytime the mouse is clicked to create animation. See example below.
          public void mousePressed(MouseEvent e) {
             repaint();
          }
      
          public void mouseReleased(MouseEvent e) {
             
          }
      
          public void mouseEntered(MouseEvent e) {
             
          }
      
          public void mouseExited(MouseEvent e) {
             
          }
      
          public void mouseClicked(MouseEvent e) {
            
          }
      
  • Here is an example of a animation with a mouse click I just change the X and Y values call repaint() and then draw my rect with the new x and y paramters.
ALT="Your browser understands the <APPLET> tag but isn't running the applet, for some reason." Your browser is ignoring the <APPLET> tag!