// Lawrence Schiller // CPS 406 - Assignment 3 // mImage class - child class of mRectangle // // This class extends the mRectangle class by using an image // instead of a solid colored shape. This class will stretch and shrink // the image if desired. /////////////////////////////////////////////////////////////////// import java.awt.*; import java.net.URL; import java.applet.Applet; public class mImage extends mRectangle { // Instance variables: private int tree,leaf,count=0; private Image img; private Applet dad; public int sizeUse = 0; // these arrays hold the values that will change the bouncing effect of // the images private int size1[] = {0,4,8,12,16,20,24,28,32,36,40,36,32,28,24,20,16,12,8,4}; private int size2[] = {-20,-18,-16,-14,-12,-10,-8,-6,-4,-2,0,-2,-4,-6,-8,-10,-12,-14,-16,-18}; /////////////////////////////////////////////////////////////////// /*The constructor mImage calls a mRectanle constructor, sets a link to the applet and codebase gets, the width and height of the images and takes the rate of stretching each image*/ /////////////////////////////////////////////////////////////////// public mImage(int new_x, int new_y, String fname, int size, Applet parapp) { super(new_x,new_y,0,0); // call mRectangle constructor //Sets a link to the applet and codebase dad = parapp; URL base = dad.getCodeBase(); img = dad.getImage(base,fname); //Gets the Image Width and Length w = img.getWidth(dad); h = img.getHeight(dad); sizeUse = size; } public void setSize(int new_w, int new_h) { w = new_w; h = new_h; } //Returns the width of the image public int getWidth() { return w; } //Returns the height of the image public int getHeight() { return h; } // Implements mPoint.paint( Graphics ): ////////////////////////////////////////////////////////////////////////// /*This part of the code call upon implememting mPoint.paint( Graphics ): If the image isn't loaded then get its width and height. Set w to tree and h to leaf, then run a switch case on the to decide the size of the image for images 1 and 2 or drawImage. Switch case first will draw an image then if switch equals 1 set the height and width of the image and draw that image and the same goes for switch equals 2. If the count of thee array is 19, the end of the arry then reset count to the 1st element of the array else progress though the array. */ //////////////////////////////////////////////////////////////////////////// public void paint(Graphics g) { if(h < 1) { //set the width and height w = img.getWidth(dad); h = img.getHeight(dad); tree = w; leaf = h; } switch(sizeUse) { case 0:g.drawImage(img,x,y,w,h,dad); break; case 1:setSize(tree - size1[count],leaf - size1[count]); g.drawImage(img,x,y,w,h,dad); break; case 2:setSize(tree - size2[count],leaf - size2[count]); g.drawImage(img,x,y,w,h,dad); default:break; } if(count==19) count = 0; else count++; } }