


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



private void initColorChooser() { // Create combo box with color choices pickColor = new JComboBox(); pickColor.addItem("Black"); pickColor.addItem("Blue"); pickColor.addItem("Green"); pickColor.addItem("Red"); // Don't allow user to type in a color pickColor.setEditable(false); // Set initial color selection pickColor.setSelectedItem("Black"); // Create label (with separating spaces) for combo box add(new JLabel(" Color:"), SOUTH ); // Add combo box to control bar add(pickColor, SOUTH ); } // Returns diameter size corresponding to radio button choice private double getDiamSize() { double size = 0; if (smallRB.isSelected()) { size = SMALL_DIAM ; } else if (medRB.isSelected()) { size = MED_DIAM ; } else if (largeRB.isSelected()) { size = LARGE_DIAM ; } return size; } // Returns Color object corresponding to combo box choice private Color getCurrentColor() { String name = (String) pickColor.getSelectedItem(); if (name.equals("Blue")) { return Color. BLUE ; } else if (name.equals("Green")) { return Color. GREEN ; } else if (name.equals("Red")) { return Color. RED ; } else return Color. BLACK ; } // Called every time user clicks mouse public void mouseClicked(MouseEvent e) { GObject obj; double diam = getDiamSize(); if (checkbox.isSelected()) { obj = new GFace(diam, diam); } else { obj = new GOval(diam, diam); } obj.setColor(getCurrentColor()); add(obj, e.getX(), e.getY());
public GFace(double width, double height) { head = new GOval(width, height); leftEye = new GOval( EYE_WIDTH * width, EYE_HEIGHT * height); rightEye = new GOval( EYE_WIDTH * width, EYE_HEIGHT * height); nose = createNose( NOSE_WIDTH * width, NOSE_HEIGHT * height); mouth = new GRect( MOUTH_WIDTH * width, MOUTH_HEIGHT * height); add(head, 0, 0); add(leftEye, (0.25 * width) - ( EYE_WIDTH * width) / 2, (0.25 * height) - ( EYE_HEIGHT * height) / 2); add(rightEye, (0.75 * width) - ( EYE_WIDTH * width) / 2, (0.25 * height) - ( EYE_HEIGHT * height) / 2); add(nose, 0.50 * width, 0.50 * height); add(mouth, (0.50 * width) - ( MOUTH_WIDTH * width) / 2, (0.75 * height) - ( MOUTH_HEIGHT * height) / 2); } /* Creates a triangle for the nose */ private GPolygon createNose(double width, double height) { GPolygon poly = new GPolygon(); poly.addVertex(0, -height / 2); poly.addVertex(width / 2, height / 2); poly.addVertex(-width / 2, height / 2); return poly; } }
/* File: TextFieldExample.java