/************************************************************ * * * PROJECT 3: Java Application: ShowGraphic * * * * Author: Lingling Den * * * * File Name: ShowGraphic * * * * Date of submission: November 7, 2001 * * * * Purpose: This program is a java applet * * that is used to show people how * * java can be used to draw different * * kinds Geometry.It presents the user * * with 9 different shapes and a panel * * where the user selects his shape * * appropriate object is shown on the * * panel wherever the user clicks. It * * refreshes after each drawing so the * * user can see the different shapes * * * ************************************************************/ //import the specific libraries needed for this program //specifically swing for applet components and the abstract //widowing toolkit(AWT)to make GUIs cross different platforms import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.applet.Applet; import java.awt.Graphics; import java.awt.Dimension; import java.awt.Color; //class definition line derived from the JApplet public class ShowGraphic extends JApplet { //variable declarations private JButton choices[]; //shape names in an array format private String names[] = {"SimpleLine", "Sin","FineSin","Rectangle", "FilledRect", "BlinkRect","Mondrian", "Bullseye","FilledCircle"}; //use Panels to separate the applet window private JPanel buttonPanel; private DrawPanel drawingArea; private int width =300, height =300; public void init() //predefined applet method { //create new panel where shapes will be displayed drawingArea = new DrawPanel(width, height); //create buttons for all the different shapes and use //the names from the array given above choices = new JButton[names.length]; buttonPanel = new JPanel(); //format the buttons in its own panel buttonPanel.setLayout(new GridLayout(3, choices.length)); ButtonHandler handler = new ButtonHandler(); //add the button names and place them on the panel //add a buttonhandler to listen when the buttons are pressed for (int i=0; i=0 ? w : 300); height = (h>=0 ? h : 300); } //end DrawPanel function definition /******************************************************************** * The paintComponent function is used everytime the shape needs to * * be drawn. In other words, every time repaint() is called, the * * drawingPanel is cleared and the new shapes are drawn. It uses a * * graphic object, g. * ********************************************************************/ public void paintComponent (Graphics g) { super.paintComponent(g); //Switch statement based on which button was pressed //and determines which shape will be drawn on the panel //The xcord and ycord variables are used here to determine //where to draw the chosen shape. repaint(); switch(currentChoice) { //An applet that draws a simple lines. case 0: setBackground(Color.lightGray); g.setColor(Color.red); g.drawLine(0, 0, size().width, size().height); g.setColor(Color.green); g.drawLine(0, size().height, size().width, 0 ); break; //An applet that draws a sine wave case 1: { int x0 = 0; int xN = size().width-1; int y0=0; int yN=size().height-1; g.setColor(Color.red); for (int x = x0; x < xN; x++) { g.drawLine(x,(int) (yN*Math.sin(x)),x+1, (int)(yN*Math.sin(x+1))); } } break; //An applet that draws a nice sine wave case 2: { int i, j1, j2; double xmin = -10.0; double xmax = 10.0; double ymin = -1.0; double ymax = 1.0; double x, y; int jvalue; int ivalue = 0; x = (ivalue * (xmax - xmin)/(size().width - 1)) + xmin; // Take the sine of that x y = Math.sin(x); // Scale y into window coordinates jvalue = (int) ((y - ymin)*(size().height - 1)/(ymax - ymin)); /* Switch jvalue from Cartesian coordinates to computer graphics coordinates */ jvalue = size().height - jvalue; j1 = jvalue; g.setColor(Color.red); for (i = 0; i < size().width; i++) { ivalue = i+1; x = (ivalue * (xmax - xmin)/(size().width - 1)) + xmin; // Take the sine of that x y = Math.sin(x); // Scale y into window coordinates jvalue = (int) ((y - ymin)*(size().height - 1)/(ymax - ymin)); /* Switch jvalue from Cartesian coordinates to computer graphics coordinates */ j2 = size().height - jvalue; g.drawLine(i, j1 ,i+1, j2); j1 = j2; } } break; //An applet that draw a rectangle around the sides of an applet. //Drawing rectangles is simple. Start with a Graphics object g //and call its fillRect(int left, int top, int width, int height) method. //As the variable names suggest, the first int is the left hand side of // the rectangle, the second is the top of the rectangle, the third // is the width and the fourth is the height. This is in contrast to some //API's where the four sides of the rectangle are given. case 3: { g.setColor(Color.red); g.drawRect(1, 1, size().width/2, size().height/2); } break; //An applet that draws a filled square in the center of the applet. //This requires you to separate the applet width // and height from the rectangle width and height. case 4: { int AppletHeight = size().height; int AppletWidth = size().width; int RectHeight = AppletHeight/3; int RectWidth = AppletWidth/3; int RectTop = (AppletHeight - RectHeight)/2; int RectLeft= (AppletWidth - RectWidth)/2; g.setColor(Color.red); g.fillRect(RectTop, RectLeft, RectWidth-1, RectHeight-1); } break; //An applet that uses clearRect to blink a rectangle on the screen. // It is also possible to clear a rectangle that you've drawn. The //syntax is exactly what you'd expect, g.clearRect(int left, int top, int width, int height). case 5: { int AppletHeight = size().height; int AppletWidth = size().width; int RectHeight = AppletHeight/3; int RectWidth = AppletWidth/3; int RectTop = (AppletHeight - RectHeight)/2; int RectLeft= (AppletWidth - RectWidth)/2; for (int i=0; i < 1000; i++) { g.fillRect(RectTop, RectLeft, RectWidth-1, RectHeight-1); g.clearRect(RectTop, RectLeft, RectWidth-1, RectHeight-1); } } break; // Randomly selects a series of rectangles to be painted on the screen. //Each has a random position, width, height and color. case 6: { int numberRectangles = 10; int RectHeight, RectWidth, RectTop, RectLeft; int AppletHeight = size().height; int AppletWidth = size().width; Color RectColor; for (int i=0; i < numberRectangles; i++) { RectColor = new Color(Randomize(255), Randomize(255),Randomize(255)); g.setColor(RectColor); RectTop = Randomize(AppletHeight); RectLeft= Randomize(AppletWidth); RectHeight = Randomize(AppletHeight - RectTop); RectWidth = Randomize(AppletWidth - RectLeft); g.fillRect(RectTop, RectLeft, RectWidth-1, RectHeight-1); } } break; //Bullseye is a simple applet which draws a series of filled, //concentric circles alternating red and white, in other words a bullseye. case 7: { int RectLeft, RectTop, RectHeight, RectWidth; int AppletHeight = size().height; int AppletWidth = size().width; for (int i=8; i >= 0; i--) { if ((i % 2) == 0) g.setColor(Color.red); else g.setColor(Color.white); RectHeight = AppletHeight*i/8; RectWidth = AppletWidth*i/8; RectLeft = AppletWidth/2 - i*AppletWidth/16; RectTop = AppletHeight/2 - i*AppletHeight/16; g.fillOval(RectLeft, RectTop, RectWidth, RectHeight); } } break; //An applet that draws several filled circles case 8: { g.setColor(Color.red); g.fillOval(size().height/5, size().width/5, 30, 30); g.setColor(Color.blue); g.fillOval(size().height/3, size().width/3, 50, 50); g.setColor(Color.red); g.fillOval(size().height/2, size().width/2, 30, 30); } break; }//end switch }//end paintComponent function /*NOTE: The following function is called in handlers defined above, buttonhandler, */ //sets the currentChoice variable based on which button was clicked public void setCurrentChoice(int c) { currentChoice = c; } public int Randomize(int range) { double rawResult; rawResult = Math.random(); return (int) (rawResult * range); } } //end class DrawPanel