Source of DrawingPad.java


  1: 
  2: package draw3; 
  3: 
  4: import java.awt.*;
  5: import java.awt.event.*;
  6: import java.io.*;
  7: import javax.swing.*; 
  8: import draw2.*;
  9: import scribble3.*;
 10: 
 11: public class DrawingPad extends draw2.DrawingPad { 
 12: 
 13:   public DrawingPad(String title) {
 14:     super(title); 
 15:     JMenu optionMenu = menuBar.getMenu(2); 
 16:     addFontOptions(optionMenu); 
 17:   }
 18: 
 19:   // factory method 
 20:   protected ScribbleCanvas makeCanvas() {
 21:     return (drawingCanvas = keyboardDrawingCanvas = new KeyboardDrawingCanvas()); 
 22:   }
 23: 
 24:   protected void initTools() { 
 25:     super.initTools();
 26:     toolkit.addTool(new TextTool(canvas, "Text")); 
 27:   }  
 28: 
 29:   protected void addFontOptions(JMenu optionMenu) { 
 30:     String[] fontFamilyNames = {
 31:       "Serif", 
 32:       "Sans-serif",
 33:       "Monospaced",
 34:       "Dialog",
 35:       "DialogInput"
 36:     };
 37: 
 38:     int[] fontSizes = {
 39:       8, 10, 12, 16, 20, 24, 28, 32, 40, 48, 64
 40:     };
 41: 
 42:     String[] fontStyleNames = {
 43:       "plain", 
 44:       "bold", 
 45:       "italic", 
 46:       "bold italic" 
 47:     };
 48: 
 49:     int i; 
 50:     ActionListener fontFamilyAction = new ActionListener() { 
 51:         public void actionPerformed(ActionEvent event) { 
 52:           Object source = event.getSource(); 
 53:           if (source instanceof JCheckBoxMenuItem) { 
 54:             JCheckBoxMenuItem mi = (JCheckBoxMenuItem) source; 
 55:             String name = mi.getText(); 
 56:             keyboardDrawingCanvas.setFontFamily(name);             
 57:           }
 58:         }
 59:       };
 60:     JMenu fontFamilyMenu = new JMenu("Font family"); 
 61:     ButtonGroup group = new ButtonGroup(); 
 62:     for (i = 0; i < fontFamilyNames.length; i++) { 
 63:       JCheckBoxMenuItem mi = new JCheckBoxMenuItem(fontFamilyNames[i]); 
 64:       fontFamilyMenu.add(mi); 
 65:       mi.addActionListener(fontFamilyAction);
 66:       group.add(mi); 
 67:     }
 68:     optionMenu.add(fontFamilyMenu); 
 69: 
 70:     ActionListener fontSizeAction = new ActionListener() { 
 71:         public void actionPerformed(ActionEvent event) { 
 72:           Object source = event.getSource(); 
 73:           if (source instanceof JCheckBoxMenuItem) { 
 74:             JCheckBoxMenuItem mi = (JCheckBoxMenuItem) source; 
 75:             String size = mi.getText(); 
 76:             try { 
 77:               keyboardDrawingCanvas.setFontSize(Integer.parseInt(size));             
 78:             } catch (NumberFormatException e) {} 
 79:           }
 80:         }
 81:       };
 82:     JMenu fontSizeMenu = new JMenu("Font size"); 
 83:     group = new ButtonGroup(); 
 84:     for (i = 0; i < fontSizes.length; i++) { 
 85:       JCheckBoxMenuItem mi = new JCheckBoxMenuItem(Integer.toString(fontSizes[i])); 
 86:       fontSizeMenu.add(mi); 
 87:       mi.addActionListener(fontSizeAction);
 88:       group.add(mi); 
 89:     }
 90:     optionMenu.add(fontSizeMenu); 
 91:     
 92:     ActionListener fontStyleAction = new ActionListener() { 
 93:         public void actionPerformed(ActionEvent event) { 
 94:           Object source = event.getSource(); 
 95:           if (source instanceof JCheckBoxMenuItem) { 
 96:             JCheckBoxMenuItem mi = (JCheckBoxMenuItem) source; 
 97:             String styleName = mi.getText(); 
 98:             int style = Font.PLAIN;
 99:             if (styleName.equals("bold")) { 
100:               style = Font.BOLD; 
101:             } else if (styleName.equals("italic")) { 
102:               style = Font.ITALIC;
103:             } else if (styleName.equals("bold italic")) { 
104:               style = Font.BOLD | Font.ITALIC; 
105:             }
106:             keyboardDrawingCanvas.setFontStyle(style);             
107:           }
108:         }
109:       };
110:     JMenu fontStyleMenu = new JMenu("Font style"); 
111:     group = new ButtonGroup(); 
112:     for (i = 0; i < fontStyleNames.length; i++) { 
113:       JCheckBoxMenuItem mi = new JCheckBoxMenuItem(fontStyleNames[i]); 
114:       fontStyleMenu.add(mi); 
115:       mi.addActionListener(fontStyleAction);
116:       group.add(mi); 
117:     }
118:     optionMenu.add(fontStyleMenu); 
119:   }
120: 
121:   protected KeyboardDrawingCanvas keyboardDrawingCanvas; 
122: 
123:   public static void main(String[] args) {
124:     JFrame frame = new draw3.DrawingPad("Drawing Pad");
125:     frame.setSize(width, height);
126:     Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
127:     frame.setLocation(screenSize.width/2 - width/2,
128:                       screenSize.height/2 - height/2);
129:     frame.show();
130:   }
131: 
132: }