/* Rainbowc.java A more complex drawing applet by Michael Dubson ENVD 3252 WeekFive assignment, Feb.16, 1999 */ import java.awt.*; import java.applet.Applet; import java.awt.event.*; public class Rainbowc extends Applet implements MouseListener, MouseMotionListener { int redValue, greenValue, blueValue; int w, h; // screen width, height int xStart, yStart; // starting x,y position of shape drag int xLast, yLast; // used in Draw int xPrevious, yPrevious; int x, y; // x,y position on screen int rs = 10; // size of pallette symbols (never changes) int r; // variable size of drawing objects int shapetype; //1=filled Oval, 2 = filled square, etc int redX[], greenX[], blueX[]; //arrays for mapping x-position to RGB values Image painting1; //Shortcut image public void init() // Initialize variables { addMouseListener( this ); addMouseMotionListener( this); int w = getSize().width; int h = getSize().height; xStart = 0; yStart = 0; xLast = 0; yLast = 0; redValue = 0; greenValue = 0; blueValue = 0; shapetype=1; r=10; //radius of spray paint painting1 = getImage(getDocumentBase(), "danse.jpg"); } // end of init method public void paint( Graphics g ) //Paint the background, pallette, tool bars, etc. { w = getSize().width; h = getSize().height; redX = new int[w]; greenX = new int[w]; blueX = new int[w]; //System.out.println(w); // Draw the toolbars g.setColor( Color.yellow ); g.fillRect( 0, 0, w, h ); g.setColor( Color.black ); g.fillRect(w/10,h/19,8*w/10,19*h/20); g.fillRect(w/10,h/20, 3, 19*h/20); g.fillRect(9*w/10, h/20, 3, 19*h/20); g.fillRect(0,h/10,w/10,2); g.fillRect(0,2*h/10,w/10,2); g.fillRect(0,3*h/10,w/10,2); g.fillRect(0,4*h/10,w/10,2); g.fillRect(0,5*h/10,w/10,2); g.fillRect(0,6*h/10,w/10,2); g.fillRect(0,7*h/10,w/10,2); g.fillRect(0,8*h/10,w/10,2); g.fillRect(0,9*h/10,w/10,2); g.setColor( Color.blue ); int[] xTri = {19*w/20, 39*w/40, 38*w/40 }; int[] yTri = { h/20, 39*h/40, 39*h/40 }; g.fillPolygon(xTri, yTri, 3); g.setFont(new Font( "Times", Font.BOLD, 12)); g.fillOval(w/20-rs, 15*h/100-rs, 2*rs, 2*rs); g.fillRect(w/20-rs, 25*h/100-rs, 2*rs, 2*rs); g.drawOval(w/20-rs, 35*h/100-rs, 2*rs, 2*rs); g.drawRect(w/20-rs, 45*h/100-rs, 2*rs, 2*rs); g.drawString( "Line", w/80, 57*h/100 ); g.drawString( "Draw", w/80, 67*h/100 ); g.drawString( "Spray", w/80, 77*h/100 ); g.drawString( "Short", w/80, 85*h/100); g.drawString( "cut", w/80, 89*h/100); g.drawString( "Erase", w/80, 39*h/40 ); for( int x = 0; x < w; x++ ) //Load colors into RGB arrays and draw color bar { if (x < w/4){ redValue=255; greenValue = 4*255*x/w; blueValue=0; } else if (x >= w/4 && x < w/2){ redValue = 255 - 4*255*(x-w/4)/w; greenValue = 255; blueValue=0; } else if (x >= w/2 && x < 3*w/4){ redValue=0; greenValue = 255; blueValue=4*255*(x-w/2)/w; } else if (x >= 3*w/4){ redValue=0; greenValue = 255 - 4*255*(x-3*w/4)/w; blueValue=255; } g.setColor(new Color( redValue, greenValue, blueValue)); g.fillRect(x,0,1,h/20); //System.out.println("X-value is " + x + " RGB is "+redValue+" "+greenValue+" "+blueValue ); redX[x]=redValue; greenX[x]=greenValue; blueX[x]=blueValue; } } // end of paint method public void mousePressed( MouseEvent e ) { x = e.getX(); y = e.getY(); //System.out.println("Width w is " + w); Graphics g = getGraphics(); if (y < h/20){ //set color redValue = redX[x]; greenValue = greenX[x]; blueValue = blueX[x]; /* System.out.println("Red Value is " + redValue + ". Green value is " + greenValue + ". Blue value is " + blueValue + "."); */ g.setColor(Color.black); g.fillRect(w/9,37*h/40, 15*w/40, h/8); g.setColor(Color.white); g.setFont(new Font("Times", Font.PLAIN, w/30)); //g.drawString("Text", 4*w/5, h/2); g.drawString("R = " + redValue + ", G = " + greenValue + ", B = " + blueValue + ".", w/9, h - h/40); } else if (x > 9*w/10 && y >= h/20){ //set spray radius r = (y - h/20)/5; //System.out.println("Radius is " + r); g.setColor(Color.black); g.fillRect(w/2,37*h/40, w/4,h/8); g.setColor(Color.white); g.setFont(new Font("Times", Font.PLAIN, w/30)); g.drawString("Radius = "+r+".", w/2, 39*h/40); } else if ((x < w/10 && y>h/10)&& y<2*h/10){ //set shapetype = Solid Oval(1) shapetype = 1; System.out.println("Shapetype is " + shapetype); } else if ((x < w/10 && y>2*h/10)&& y<3*h/10){ //set shapetype = Solid Square(2) shapetype = 2; System.out.println("Shapetype is " + shapetype); } else if ((x < w/10 && y>3*h/10)&& y<4*h/10){ //set shapetype = Open Oval(3) shapetype = 3; System.out.println("Shapetype is " + shapetype); } else if ((x < w/10 && y>4*h/10)&& y<5*h/10){ //set shapetype = Open Square (4) shapetype = 4; System.out.println("Shapetype is " + shapetype); } else if ((x < w/10 && y>5*h/10)&& y<6*h/10){ //set shapetype = Line (5) shapetype = 5; System.out.println("Shapetype is " + shapetype); } else if ((x < w/10 && y>6*h/10)&& y<7*h/10){ //set shapetype = ? (6) shapetype = 6; System.out.println("Shapetype is " + shapetype); } else if ((x < w/10 && y>7*h/10)&& y<8*h/10){ //set shapetype = Spray (7) shapetype = 7; System.out.println("Shapetype is " + shapetype); } else if ((x < w/10 && y>8*h/10)&& y<9*h/10){ //set shapetype = Shortcut (8) shapetype = 8; System.out.println("Shapetype is " + shapetype); g.drawImage(painting1, w/10, h/20, 82*w/100, 19*h/20, this); } else if ((x < w/10 && y>9*h/10)&& y h/20 && x > w/10) && x < 9*w/10){ //if in drawing region xStart=x; // starting x,y postions for dragging yStart=y; xLast = x; yLast = y; xPrevious = x; // intial values needed for dragging yPrevious = y; } } // end of mousePressed method public void mouseDragged( MouseEvent e) { Graphics g = getGraphics(); int x = e.getX(); int y = e.getY(); int xBegin, xEnd, yBegin, yEnd; /*if (x < xStart){ xBegin =x; xEnd = xStart; } else { xBegin = xStart; xEnd = x; } if (y < yStart){ yBegin =y; yEnd = yStart; } else { yBegin = yStart; yEnd = y; }*/ xBegin=xStart; yBegin=yStart; xEnd = x; yEnd = y; if ( (shapetype == 1) && (y > h/20 && x > w/10) ) //if shape is solid oval and within drawing region { int radiusX = x - xStart; int radiusY = y - yStart; int oldRadX = xPrevious - xStart; int oldRadY = yPrevious - yStart; g.setColor(Color.black); //draw over old oval g.fillOval(xStart - oldRadX, yStart - oldRadY, 2*oldRadX, 2*oldRadY); //draw oval g.setColor(new Color( redValue, greenValue, blueValue)); g.fillOval(xStart - radiusX, yStart - radiusY, 2*radiusX, 2*radiusY); //draw oval xPrevious = x; yPrevious = y; } if ( (shapetype == 2) && (y > h/20 && x > w/10) ) //if shape is solid square and within drawing region { g.setColor(Color.black); //draw over old oval g.fillRect(xBegin,yBegin,(xPrevious-xBegin),(yPrevious-yBegin)); g.setColor(new Color( redValue, greenValue, blueValue)); g.fillRect(xBegin,yBegin,(xEnd-xBegin),(yEnd-yBegin)); //draw oval xPrevious = xEnd; yPrevious = yEnd; } if ( (shapetype == 3) && (y > h/20 && x > w/10) ) //if shape is open circle and within drawing region { int radiusX = x - xStart; int radiusY = y - yStart; int oldRadX = xPrevious - xStart; int oldRadY = yPrevious - yStart; g.setColor(Color.black); //draw over old oval g.drawOval(xStart - oldRadX, yStart - oldRadY, 2*oldRadX, 2*oldRadY); //draw oval g.setColor(new Color( redValue, greenValue, blueValue)); g.drawOval(xStart - radiusX, yStart - radiusY, 2*radiusX, 2*radiusY); //draw oval xPrevious = x; yPrevious = y; } if ( (shapetype == 4) && (y > h/20 && x > w/10) ) //if shape is open square and within drawing region { g.setColor(Color.black); //draw over old oval g.drawRect(xBegin,yBegin,(xPrevious-xBegin),(yPrevious-yBegin)); g.setColor(new Color( redValue, greenValue, blueValue)); g.drawRect(xBegin,yBegin,(xEnd-xBegin),(yEnd-yBegin)); //draw oval xPrevious = xEnd; yPrevious = yEnd; } if ( (shapetype == 5) && (y > h/20 && x > w/10) ) //if shape is Line and within drawing region { g.setColor(Color.black); g.drawLine(xStart, yStart, xPrevious, yPrevious); //Draw over old line g.setColor(new Color( redValue, greenValue, blueValue)); g.drawLine(xStart, yStart, x, y); xPrevious = x; yPrevious = y; } if ( (shapetype == 6) && (y > h/20 && x > w/10) ) //if shape is Draw and within drawing region { g.setColor(new Color( redValue, greenValue, blueValue)); g.drawLine(xLast, yLast, x, y); xLast = x; yLast = y; } if ( (shapetype == 7) && (y > h/20 && x > w/10) ) { int xRnd, yRnd; //random x-, y-coords of spray paint g.setColor(new Color( redValue, greenValue, blueValue)); for (int c=0; c<20; c++) { xRnd = (int)((Math.random()*r)-r/2); yRnd = (int)(Math.random()*r-r/2); g.fillOval(x+xRnd,y+yRnd,2,2); } } } //end of mouseDragged method public void mouseMoved( MouseEvent e ) {;} public void mouseReleased( MouseEvent e ) {;} public void mouseClicked( MouseEvent e ) {;} public void mouseEntered( MouseEvent e ) {;} public void mouseExited( MouseEvent e ) {;} }