This project builds on the Self-protrait applet from Project#2. First, resize your applet so that it is 500x500 pixels. Next, add a slider to it so that its initial value is 100, and its range goes from 50 to 450. Your applet should draw the self-portrait centered in the applet. Its size should vary, as the slider is adjusted. Try the sample program below:
Post your applets in your own web page. Please do all project work on your own. You can discuss the projects with fellow classmates, but not at the level of sharing or discussing details of actual Java code segments. Feel free to discuss any and all details with the TA's and your prefessor.
/* A self-portrait with a slider */
import java.awt.*; import java.applet.Applet; import java.awt.event.*;
public class SelfPortrait extends Applet implements AdjustmentListener { int x, y; // x and y coordinates of top-left corner of portrait int w = 100, h = 100; // width & height of portrait int appletW = 500, appletH = 500; // width and height of the applet // GUI Widgets Scrollbar slider; public void init() { // Make GUI slider = new Scrollbar(Scrollbar.HORIZONTAL, w, 1, 50, 450); add(slider); slider.addAdjustmentListener(this); } // init public void paint(Graphics g) { // center the portrait in the applet x = (appletW - w) / 2; y = (appletH - h) / 2; h = w; drawMe(g, x, y, w, h); } // paint public void drawMe(Graphics g, int x, int y, int w, int h) { g.drawOval(x, y, w, h); // face g.fillOval(x+w/2-w/20, y+h*3/4 - w/40, w/10, w/10); // mouth g.drawLine(x+w/2, y+w/2-h/10, x+w/2, y+w/2+h/10); // nose g.drawLine(x+w/2-w/5, y+w/2+h/10, x+w/2+w/5, y+w/2+h/10); g.drawLine(x+w/2-w/5, y+w/2+h/10, x+w/2, y+w/2-h/10); g.drawLine(x+w/4, y, x+w*3/4, y); g.fillRect(x+w/4-w/10, y+w/4-w/20, w/5, w/10); // left eye g.fillRect(x+w*3/4-w/10, y+w/4-w/20, w/5, w/10); // right eye } // drawMe public void adjustmentValueChanged(AdjustmentEvent e) { w = slider.getValue(); repaint(); } // adjustmentValueChanged } // SelfPortrait
Back to:Course Materials