📄 drawapplication.java
字号:
palette.setLayout(new PaletteLayout(2,new Point(2,2))); return palette; } /** * Creates the tools. By default only the selection tool is added. * Override this method to add additional tools. * Call the inherited method to include the selection tool. * @param palette the palette where the tools are added. */ protected void createTools(Panel palette) { Tool tool = createSelectionTool(); fDefaultToolButton = createToolButton(IMAGES+"SEL", "Selection Tool", tool); palette.add(fDefaultToolButton); } /** * Creates the selection tool used in this editor. Override to use * a custom selection tool. */ protected Tool createSelectionTool() { return new SelectionTool(view()); } /** * Creates a tool button with the given image, tool, and text */ protected ToolButton createToolButton(String iconName, String toolName, Tool tool) { return new ToolButton(this, iconName, toolName, tool); } /** * Creates the drawing view used in this application. * You need to override this method to use a DrawingView * subclass in your application. By default a standard * DrawingView is returned. */ protected StandardDrawingView createDrawingView() { Dimension d = getDrawingViewSize(); return new StandardDrawingView(this, d.width, d.height); } /** * Override to define the dimensions of the drawing view. */ protected Dimension getDrawingViewSize() { return new Dimension(400, 600); } /** * Creates the drawing used in this application. * You need to override this method to use a Drawing * subclass in your application. By default a standard * Drawing is returned. */ protected Drawing createDrawing() { return new StandardDrawing(); } /** * Creates the contents component of the application * frame. By default the DrawingView is returned in * a ScrollPane. */ protected Component createContents(StandardDrawingView view) { ScrollPane sp = new ScrollPane(); Adjustable vadjust = sp.getVAdjustable(); Adjustable hadjust = sp.getHAdjustable(); hadjust.setUnitIncrement(16); vadjust.setUnitIncrement(16); sp.add(view); return sp; } /** * Sets the drawing to be edited. */ public void setDrawing(Drawing drawing) { fView.setDrawing(drawing); fDrawing = drawing; } /** * Gets the default size of the window. */ protected Dimension defaultSize() { return new Dimension(430,406); } /** * Creates the status line. */ protected TextField createStatusLine() { TextField field = new TextField("No Tool", 40); field.setEditable(false); return field; } /** * Handles a user selection in the palette. * @see PaletteListener */ public void paletteUserSelected(PaletteButton button) { ToolButton toolButton = (ToolButton) button; setTool(toolButton.tool(), toolButton.name()); setSelected(toolButton); } /** * Handles when the mouse enters or leaves a palette button. * @see PaletteListener */ public void paletteUserOver(PaletteButton button, boolean inside) { ToolButton toolButton = (ToolButton) button; if (inside) showStatus(toolButton.name()); else showStatus(fSelectedToolButton.name()); } /** * Gets the current drawing. * @see DrawingEditor */ public Drawing drawing() { return fDrawing; } /** * Gets the current tool. * @see DrawingEditor */ public Tool tool() { return fTool; } /** * Gets the current drawing view. * @see DrawingEditor */ public DrawingView view() { return fView; } /** * Sets the default tool of the editor. * @see DrawingEditor */ public void toolDone() { if (fDefaultToolButton != null) { setTool(fDefaultToolButton.tool(), fDefaultToolButton.name()); setSelected(fDefaultToolButton); } } /** * Handles a change of the current selection. Updates all * menu items that are selection sensitive. * @see DrawingEditor */ public void selectionChanged(DrawingView view) { MenuBar mb = getMenuBar(); CommandMenu editMenu = (CommandMenu)mb.getMenu(EDIT_MENU); editMenu.checkEnabled(); CommandMenu alignmentMenu = (CommandMenu)mb.getMenu(ALIGNMENT_MENU); alignmentMenu.checkEnabled(); } /** * Shows a status message. * @see DrawingEditor */ public void showStatus(String string) { fStatusLine.setText(string); } private void setTool(Tool t, String name) { if (fTool != null) fTool.deactivate(); fTool = t; if (fTool != null) { fStatusLine.setText(name); fTool.activate(); } } private void setSelected(ToolButton button) { if (fSelectedToolButton != null) fSelectedToolButton.reset(); fSelectedToolButton = button; if (fSelectedToolButton != null) fSelectedToolButton.select(); } /** * Exits the application. You should never override this method */ public void exit() { destroy(); setVisible(false); // hide the Frame dispose(); // tell windowing system to free resources System.exit(0); } /** * Handles additional clean up operations. Override to destroy * or release drawing editor resources. */ protected void destroy() { } /** * Resets the drawing to a new empty drawing. */ public void promptNew() { initDrawing(); } /** * Shows a file dialog and opens a drawing. */ public void promptOpen() { FileDialog dialog = new FileDialog(this, "Open File...", FileDialog.LOAD); dialog.show(); String filename = dialog.getFile(); if (filename != null) { filename = stripTrailingAsterisks(filename); String dirname = dialog.getDirectory(); loadDrawing(dirname + filename); } dialog.dispose(); } /** * Shows a file dialog and saves drawing. */ public void promptSaveAs() { toolDone(); String path = getSavePath("Save File..."); if (path != null) { if (!path.endsWith(".draw")) path += ".draw"; saveAsStorableOutput(path); } } /** * Shows a file dialog and saves drawing. */ public void promptSaveAsSerialized() { toolDone(); String path = getSavePath("Save File..."); if (path != null) { if (!path.endsWith(".ser")) path += ".ser"; saveAsObjectOutput(path); } } /** * Prints the drawing. */ public void print() { fTool.deactivate(); PrintJob printJob = getToolkit().getPrintJob(this, "Print Drawing", null); if (printJob != null) { Graphics pg = printJob.getGraphics(); if (pg != null) { fView.printAll(pg); pg.dispose(); // flush page } printJob.end(); } fTool.activate(); } private String getSavePath(String title) { String path = null; FileDialog dialog = new FileDialog(this, title, FileDialog.SAVE); dialog.show(); String filename = dialog.getFile(); if (filename != null) { filename = stripTrailingAsterisks(filename); String dirname = dialog.getDirectory(); path = dirname + filename; } dialog.dispose(); return path; } private String stripTrailingAsterisks(String filename) { // workaround for bug on NT if (filename.endsWith("*.*")) return filename.substring(0, filename.length() - 4); else return filename; } private void saveAsStorableOutput(String file) { // TBD: should write a MIME header try { FileOutputStream stream = new FileOutputStream(file); StorableOutput output = new StorableOutput(stream); output.writeStorable(fDrawing); output.close(); } catch (IOException e) { showStatus(e.toString()); } } private void saveAsObjectOutput(String file) { // TBD: should write a MIME header try { FileOutputStream stream = new FileOutputStream(file); ObjectOutput output = new ObjectOutputStream(stream); output.writeObject(fDrawing); output.close(); } catch (IOException e) { showStatus(e.toString()); } } private void loadDrawing(String file) { toolDone(); String type = guessType(file); if (type.equals("storable")) readFromStorableInput(file); else if (type.equals("serialized")) readFromObjectInput(file); else showStatus("Unknown file type"); } private void readFromStorableInput(String file) { try { FileInputStream stream = new FileInputStream(file); StorableInput input = new StorableInput(stream); fDrawing.release(); fDrawing = (Drawing)input.readStorable(); fView.setDrawing(fDrawing); } catch (IOException e) { initDrawing(); showStatus("Error: " + e); } } private void readFromObjectInput(String file) { try { FileInputStream stream = new FileInputStream(file); ObjectInput input = new ObjectInputStream(stream); fDrawing.release(); fDrawing = (Drawing)input.readObject(); fView.setDrawing(fDrawing); } catch (IOException e) { initDrawing(); showStatus("Error: " + e); } catch (ClassNotFoundException e) { initDrawing(); showStatus("Class not found: " + e); } } private String guessType(String file) { if (file.endsWith(".draw")) return "storable"; if (file.endsWith(".ser")) return "serialized"; return "unknown"; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -