/*********************************************************** * Tejal Desai * * November 13, 2000 * * CIS 525.001 * * Presented To: Dr. Bruce Maxim * * Assignment #3 - Java Applet * * This program is a java applet that is used to help tutor * * people on their shapes and colors. It presents the user * * with 6 different shapes & 6 different colors and a panel * * where the user selects his shape and color and the * * appropriate object is drawn on the panel wherever the * * user clicks. It refreshes after each drawing so the user * * can try and imagine what will be drawn and then check * * his answer by clicking on the drawing area. * ***********************************************************/ //import the specific libraries needed for this program //specifically swing for applet components and the abstract //widowing toolkit to make GUIs cross different platforms import java.awt.*; import java.awt.event.*; import javax.swing.*; //class definition line derived from the JApplet public class LittleTutor extends JApplet { //variable declarations private JButton choices[]; //shape names in an array format private String names[] = {"Square", "Circle", "Triange", "Rectangle","Oval", "Polygon"}; //use Panels to separate the applet window private JPanel buttonPanel; private DrawPanel drawingArea; private JPanel radioPanel; //color names private JRadioButton red, blue, green, yellow, orange, magenta; private ButtonGroup ColorsGroup; private int width = 300, height =200; 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(2, 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 : 100); height = (h>=0 ? h : 100); } //end 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 to determine what color to set //the graphic object will be -- cases determined from //the pre-determined values chosen by programmer. See //RadioButtonHandler class comments for more details. switch(currentColor){ case 5: g.setColor(Color.red); break; case 10: g.setColor(Color.blue); break; case 15: g.setColor(Color.green); break; case 20: g.setColor(Color.yellow); break; case 25: g.setColor(Color.orange); break; case 30: g.setColor(Color.magenta); break; } //end switch statement //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. switch(currentChoice) { case 0: g.fillRect(xcord-25, ycord-25, 100, 100); break; case 1: g.fillOval(xcord-25, ycord-25,100,100); break; case 2: Polygon ftriangle = new Polygon(); ftriangle.addPoint(xcord-25,ycord-25); ftriangle.addPoint(xcord + 75,ycord); ftriangle.addPoint(xcord + 25,ycord + 62); g.fillPolygon(ftriangle); break; case 3: g.fillRect(xcord-25, ycord-25,100,50); break; case 4: g.fillOval(xcord-25, ycord-25,55,105); break; case 5: Polygon poly = new Polygon(); poly.addPoint(xcord,ycord); poly.addPoint(xcord+55,ycord+25); poly.addPoint(xcord+80,ycord); poly.addPoint(xcord+65,ycord-60); poly.addPoint(xcord+15,ycord-60); g.fillPolygon(poly); break; }//end switch }//end paintComponent function /*NOTE: All of the following functions are called in handlers defined above...ie, radionbuttonhandler, buttonhandler, and mouseclickhandler*/ //sets the currentChoice variable based on which button was clicked public void setCurrentChoice(int c) { currentChoice = c; } //sets the currentColor variable based on which radio button was selected public void setCurrentColor(int c) { currentColor = c; } //sets the x and y coordinates based on where the mouse was cliced public void setCurrentCoords(int x, int y) { xcord=x; ycord=y; repaint(); //once the user clicks, call repaint to draw the shape } } //end class DrawPanel