📄 drawapplication.java
字号:
/* * @(#)DrawApplication.java 5.1 * */package CH.ifa.draw.application;import java.awt.*;import java.awt.event.*;import java.util.*;import java.io.*;import CH.ifa.draw.framework.*;import CH.ifa.draw.standard.*;import CH.ifa.draw.figures.*;import CH.ifa.draw.util.*;/** * DrawApplication defines a standard presentation for * standalone drawing editors. The presentation is * customized in subclasses. * The application is started as follows: * <pre> * public static void main(String[] args) { * MayDrawApp window = new MyDrawApp(); * window.open(); * } * </pre> */public class DrawApplication extends Frame implements DrawingEditor, PaletteListener { private Drawing fDrawing; private Tool fTool; private Iconkit fIconkit; private TextField fStatusLine; private StandardDrawingView fView; private ToolButton fDefaultToolButton; private ToolButton fSelectedToolButton; private String fDrawingFilename; static String fgUntitled = "untitled"; // the image resource path private static final String fgDrawPath = "/CH/ifa/draw/"; public static final String IMAGES = fgDrawPath+"images/"; /** * The index of the file menu in the menu bar. */ public static final int FILE_MENU = 0; /** * The index of the edit menu in the menu bar. */ public static final int EDIT_MENU = 1; /** * The index of the alignment menu in the menu bar. */ public static final int ALIGNMENT_MENU = 2; /** * The index of the attributes menu in the menu bar. */ public static final int ATTRIBUTES_MENU = 3; /** * Constructs a drawing window with a default title. */ public DrawApplication() { super("JHotDraw"); } /** * Constructs a drawing window with the given title. */ public DrawApplication(String title) { super(title); } /** * Opens the window and initializes its contents. * Clients usually only call but don't override it. */ public void open() { fIconkit = new Iconkit(this); setLayout(new BorderLayout()); fView = createDrawingView(); Component contents = createContents(fView); add("Center", contents); //add("Center", fView); Panel tools = createToolPalette(); createTools(tools); add("West", tools); fStatusLine = createStatusLine(); add("South", fStatusLine); MenuBar mb = new MenuBar(); createMenus(mb); setMenuBar(mb); initDrawing(); Dimension d = defaultSize(); setSize(d.width, d.height); addListeners(); show(); } /** * Registers the listeners for this window */ protected void addListeners() { addWindowListener( new WindowAdapter() { public void windowClosing(WindowEvent event) { exit(); } } ); } private void initDrawing() { fDrawing = createDrawing(); fDrawingFilename = fgUntitled; fView.setDrawing(fDrawing); toolDone(); } /** * Creates the standard menus. Clients override this * method to add additional menus. */ protected void createMenus(MenuBar mb) { mb.add(createFileMenu()); mb.add(createEditMenu()); mb.add(createAlignmentMenu()); mb.add(createAttributesMenu()); mb.add(createDebugMenu()); } /** * Creates the file menu. Clients override this * method to add additional menu items. */ protected Menu createFileMenu() { Menu menu = new Menu("File"); MenuItem mi = new MenuItem("New", new MenuShortcut('n')); mi.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent event) { promptNew(); } } ); menu.add(mi); mi = new MenuItem("Open...", new MenuShortcut('o')); mi.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent event) { promptOpen(); } } ); menu.add(mi); mi = new MenuItem("Save As...", new MenuShortcut('s')); mi.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent event) { promptSaveAs(); } } ); menu.add(mi); mi = new MenuItem("Save As Serialized..."); mi.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent event) { promptSaveAsSerialized(); } } ); menu.add(mi); menu.addSeparator(); mi = new MenuItem("Print...", new MenuShortcut('p')); mi.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent event) { print(); } } ); menu.add(mi); menu.addSeparator(); mi = new MenuItem("Exit"); mi.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent event) { exit(); } } ); menu.add(mi); return menu; } /** * Creates the edit menu. Clients override this * method to add additional menu items. */ protected Menu createEditMenu() { CommandMenu menu = new CommandMenu("Edit"); menu.add(new CutCommand("Cut", fView), new MenuShortcut('x')); menu.add(new CopyCommand("Copy", fView), new MenuShortcut('c')); menu.add(new PasteCommand("Paste", fView), new MenuShortcut('v')); menu.addSeparator(); menu.add(new DuplicateCommand("Duplicate", fView), new MenuShortcut('d')); menu.add(new DeleteCommand("Delete", fView)); menu.addSeparator(); menu.add(new GroupCommand("Group", fView)); menu.add(new UngroupCommand("Ungroup", fView)); menu.addSeparator(); menu.add(new SendToBackCommand("Send to Back", fView)); menu.add(new BringToFrontCommand("Bring to Front", fView)); return menu; } /** * Creates the alignment menu. Clients override this * method to add additional menu items. */ protected Menu createAlignmentMenu() { CommandMenu menu = new CommandMenu("Align"); menu.add(new ToggleGridCommand("Toggle Snap to Grid", fView, new Point(4,4))); menu.addSeparator(); menu.add(new AlignCommand("Lefts", fView, AlignCommand.LEFTS)); menu.add(new AlignCommand("Centers", fView, AlignCommand.CENTERS)); menu.add(new AlignCommand("Rights", fView, AlignCommand.RIGHTS)); menu.addSeparator(); menu.add(new AlignCommand("Tops", fView, AlignCommand.TOPS)); menu.add(new AlignCommand("Middles", fView, AlignCommand.MIDDLES)); menu.add(new AlignCommand("Bottoms", fView, AlignCommand.BOTTOMS)); return menu; } /** * Creates the debug menu. Clients override this * method to add additional menu items. */ protected Menu createDebugMenu() { Menu menu = new Menu("Debug"); MenuItem mi = new MenuItem("Simple Update"); mi.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent event) { fView.setDisplayUpdate(new SimpleUpdateStrategy()); } } ); menu.add(mi); mi = new MenuItem("Buffered Update"); mi.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent event) { fView.setDisplayUpdate(new BufferedUpdateStrategy()); } } ); menu.add(mi); return menu; } /** * Creates the attributes menu and its submenus. Clients override this * method to add additional menu items. */ protected Menu createAttributesMenu() { Menu menu = new Menu("Attributes"); menu.add(createColorMenu("Fill Color", "FillColor")); menu.add(createColorMenu("Pen Color", "FrameColor")); menu.add(createArrowMenu()); menu.addSeparator(); menu.add(createFontMenu()); menu.add(createFontSizeMenu()); menu.add(createFontStyleMenu()); menu.add(createColorMenu("Text Color", "TextColor")); return menu; } /** * Creates the color menu. */ protected Menu createColorMenu(String title, String attribute) { CommandMenu menu = new CommandMenu(title); for (int i=0; i<ColorMap.size(); i++) menu.add( new ChangeAttributeCommand( ColorMap.name(i), attribute, ColorMap.color(i), fView ) ); return menu; } /** * Creates the arrows menu. */ protected Menu createArrowMenu() { CommandMenu menu = new CommandMenu("Arrow"); menu.add(new ChangeAttributeCommand("none", "ArrowMode", new Integer(PolyLineFigure.ARROW_TIP_NONE), fView)); menu.add(new ChangeAttributeCommand("at Start", "ArrowMode", new Integer(PolyLineFigure.ARROW_TIP_START), fView)); menu.add(new ChangeAttributeCommand("at End", "ArrowMode", new Integer(PolyLineFigure.ARROW_TIP_END), fView)); menu.add(new ChangeAttributeCommand("at Both", "ArrowMode", new Integer(PolyLineFigure.ARROW_TIP_BOTH), fView)); return menu; } /** * Creates the fonts menus. It installs all available fonts * supported by the toolkit implementation. */ protected Menu createFontMenu() { CommandMenu menu = new CommandMenu("Font"); String fonts[] = Toolkit.getDefaultToolkit().getFontList(); for (int i = 0; i < fonts.length; i++) menu.add(new ChangeAttributeCommand(fonts[i], "FontName", fonts[i], fView)); return menu; } /** * Creates the font style menu with entries (Plain, Italic, Bold). */ protected Menu createFontStyleMenu() { CommandMenu menu = new CommandMenu("Font Style"); menu.add(new ChangeAttributeCommand("Plain", "FontStyle", new Integer(Font.PLAIN), fView)); menu.add(new ChangeAttributeCommand("Italic","FontStyle", new Integer(Font.ITALIC),fView)); menu.add(new ChangeAttributeCommand("Bold", "FontStyle", new Integer(Font.BOLD), fView)); return menu; } /** * Creates the font size menu. */ protected Menu createFontSizeMenu() { CommandMenu menu = new CommandMenu("Font Size"); int sizes[] = { 9, 10, 12, 14, 18, 24, 36, 48, 72 }; for (int i = 0; i < sizes.length; i++) { menu.add( new ChangeAttributeCommand( Integer.toString(sizes[i]), "FontSize", new Integer(sizes[i]), fView) ); } return menu; } /** * Creates the tool palette. */ protected Panel createToolPalette() { Panel palette = new Panel(); palette.setBackground(Color.lightGray);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -