/* assignment4.java Date: 12/13/00 Title: assignment4 - final project Author: Ruth West Description: Applet displays an image that is an interactive painting. You can change the colors of the painting when you move the mouse */ import java.applet.*; import java.awt.*; import java.awt.image.*; public class assignment4 extends Applet { int mouseX = 200; //original x value for mouse position, used to calculate mouse movement differences int mouseY = 200; //original y value for mouse position, used to calculate mouse movement differences int mouseDiffx; //original mouse x minus new mouse x --- done in mouse method int mouseDiffy; //original mouse y minus new mouse y --- done in mouse method int width; //width of my image int height; //height of my image int[] pixels; //array to hold the pixels of my image int[] pixelscopy; //array to save the original pixels Image myImage; //the image to work with //get my image and display it on the applet "screen" public void init(){ myImage = getImage(getDocumentBase(), "img04lg.jpg"); getThePixels(myImage); myImage = showImage(); }// end init //get the pixels of the image so that they can be processed public void getThePixels(Image picture){ waitToOpenImage(picture); width = picture.getWidth(this); height = picture.getHeight(this); pixels = new int[width*height]; PixelGrabber pg = new PixelGrabber(picture,0,0,width,height,pixels,0,width); try { pg.grabPixels(); } catch (InterruptedException e){ System.out.println("Cannot open Pixels"); }//end exception //make an array to copy the original pixels into - without this array to save the original pixels into the image gets progressively destroyed as I change the color of the pixels pixelscopy = new int[width*height]; for(int index=0; index>16; int g=(c&0xff00)>>8; int b=(c&0xff); r = (int)(r * Math.abs(mouseDiffx)/100.0);//shift the r pixel color //g = (int)(g * Math.abs(mouseDiffy)/50.0);//shift the g pixel color - when I use this it goes to black to quickly //b = (int)(b * Math.abs(mouseDiffy)/100.0);//looks more interesting with r as the source for this change than when I used b b = (int)(r * Math.abs(mouseDiffy)/100.0);//looks better this way! pixels[index] = (c&0xff000000) + ((r&0xff0000)<<16) + ((c&0xff00)<<8) + (b&0xff); }//end shift loop }//end shift method //allow new/modified image to be displayed onto screen public Image showImage(){ return createImage(new MemoryImageSource(width,height,pixels,0,width)); }//end show image //paint the image on the applet public void paint(Graphics g){ g.drawImage(myImage, 0, 0, this); System.out.println("paint"); }//end paint //override paint to reduce flicker public void update(Graphics g){ paint(g); } //use mouse move to generate values for x and y public boolean mouseMove(Event e, int x, int y){ mouseDiffx = x - mouseX;//difference of current mouse x to original mouse x --- to be used in modifying image colors mouseDiffy = y - mouseY;//difference of current mouse y to original mouse y --- to be used in modifying image colors shift();// shift the colors myImage = showImage();//display the changed image on the screen repaint(); return true; }//end mouseMove }//end applet