Person Applet (Version 2 with Slider that adjusts height)


/* 
  Version 2 of the person applet that demonstrates the use of sliders
  as an interactive GUI widget.
   
   We will write a method, called drawPerson, that given the x and y
   coordinates of an anchor point, and a height in pixels, it draws
   a stick figure anchored at that point.
   
   The x and y coordinates specify the anchor point as the point between
   the legs at the bottom.
   
*/

import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;

public class personApplet extends Applet implements AdjustmentListener {

	Scrollbar slider;	// the GUI scrollbar object
	int sliderValue;	// the current value of the scrollbar
	int height = 20;	
	int horiz=100;
	
	public void init() {
		
		// create the new scroll bar object, add it to the applet
		// and register this applet as its listener
		slider = new Scrollbar(Scrollbar.HORIZONTAL, 20, 1, 20, 350);
		add(slider);
		slider.addAdjustmentListener(this);
		
		sliderValue = 20;
	} // end of init
	
	public void paint(Graphics g) {
	
		height = sliderValue;	// use the slider value to size the person(s)
		
		// draw some people!
		g.setColor(Color.red);
		drawPerson(g, horiz, 380, height);
		g.setColor(Color.cyan);
		drawPerson(g, horiz+50, 380, height);
		g.setColor(Color.blue);
		drawPerson(g, horiz+100, 380, (int) (height*1.5));
		
		
	} // end of paint
	
	private void drawPerson(Graphics g, int x, int y, int h) {
	

		// Draw head (about a 1/4 of the height in proportion)
			g.fillOval(x-h/8, y-h, h/4, h/4);
			
		// Draw body
			g.drawLine(x, y-3*h/4, x, y - h/4);
			
		// Draw legs
			g.drawLine(x, y-h/4, x-h/8, y);
			g.drawLine(x, y-h/4, x+h/8, y);
		
		// Draw arms
			g.drawLine(x, y-5*h/8, x-h/8, y-3*h/4);
			g.drawLine(x, y-5*h/8, x+h/8, y-3*h/4);
		
	} // end of drawPerson
	
	public void adjustmentValueChanged(AdjustmentEvent e) {
	
		sliderValue = slider.getValue();
		repaint();
	} // end of adjustmentValueChanged

} // end of personApplet