📄 drawapplication.java
字号:
mi.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent event) {
newLookAndFeel(lnfClassName);
}
}
);
menu.add(mi);
}
return menu;
}
/**
* Creates the tool palette.
*/
protected JToolBar createToolPalette() {
JToolBar palette = new JToolBar();
palette.setBackground(Color.lightGray);
// use standard FlowLayout for JToolBar
// 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(JToolBar 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(800, 800);
}
/**
* 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 JScrollPane.
*/
protected JComponent createContents(StandardDrawingView view) {
JScrollPane sp = new JScrollPane(view);
sp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
sp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
return sp;
}
/**
* Factory method to create a StorageFormatManager for supported storage formats.
* Different applications might want to use different storage formats and can return
* their own format manager by overriding this method.
*/
public StorageFormatManager createStorageFormatManager() {
StorageFormatManager storageFormatManager = new StorageFormatManager();
storageFormatManager.setDefaultStorageFormat(new StandardStorageFormat());
storageFormatManager.addStorageFormat(storageFormatManager.getDefaultStorageFormat());
storageFormatManager.addStorageFormat(new SerializationStorageFormat());
return storageFormatManager;
}
/**
* Set the StorageFormatManager. The StorageFormatManager is used when storing and
* restoring Drawing from the file system.
*/
private void setStorageFormatManager(StorageFormatManager storageFormatManager) {
fStorageFormatManager = storageFormatManager;
}
/**
* Return the StorageFormatManager for this application.The StorageFormatManager is
* used when storing and restoring Drawing from the file system.
*/
public StorageFormatManager getStorageFormatManager() {
return fStorageFormatManager;
}
/**
* Sets the drawing to be edited.
*/
public void setDrawing(Drawing drawing) {
view().setDrawing(drawing);
fDrawing = drawing;
}
/**
* Gets the default size of the window.
*/
protected Dimension defaultSize() {
return new Dimension(600,450);
}
/**
* Creates the status line.
*/
protected JTextField createStatusLine() {
JTextField field = new JTextField("No Tool", 40);
field.setBackground(Color.white);
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) {
JMenuBar mb = getJMenuBar();
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 (tool() != null)
tool().deactivate();
fTool = t;
if (tool() != null) {
fStatusLine.setText(name);
tool().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 JFrame
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() {
toolDone();
JFileChooser openDialog = createOpenFileChooser();
getStorageFormatManager().registerFileFilters(openDialog);
if (openDialog.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
StorageFormat foundFormat = getStorageFormatManager().findStorageFormat(openDialog.getFileFilter());
if (foundFormat != null) {
loadDrawing(foundFormat, openDialog.getSelectedFile().getAbsolutePath());
}
else {
showStatus("Not a valid file format: " + openDialog.getFileFilter().getDescription());
}
}
}
/**
* Shows a file dialog and saves drawing.
*/
public void promptSaveAs() {
toolDone();
JFileChooser saveDialog = createSaveFileChooser();
getStorageFormatManager().registerFileFilters(saveDialog);
if (saveDialog.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
StorageFormat foundFormat = getStorageFormatManager().findStorageFormat(saveDialog.getFileFilter());
if (foundFormat != null) {
saveDrawing(foundFormat, saveDialog.getSelectedFile().getAbsolutePath());
}
else {
showStatus("Not a valid file format: " + saveDialog.getFileFilter().getDescription());
}
}
}
/**
* Create a file chooser for the open file dialog. Subclasses may override this
* method in order to customize the open file dialog.
*/
protected JFileChooser createOpenFileChooser() {
JFileChooser openDialog = new JFileChooser();
openDialog.setDialogTitle("Open File...");
return openDialog;
}
/**
* Create a file chooser for the save file dialog. Subclasses may override this
* method in order to customize the save file dialog.
*/
protected JFileChooser createSaveFileChooser() {
JFileChooser saveDialog = new JFileChooser();
saveDialog.setDialogTitle("Save File...");
return saveDialog;
}
/**
* Prints the drawing.
*/
public void print() {
tool().deactivate();
PrintJob printJob = getToolkit().getPrintJob(this, "Print Drawing", null);
if (printJob != null) {
Graphics pg = printJob.getGraphics();
if (pg != null) {
((StandardDrawingView)view()).printAll(pg);
pg.dispose(); // flush page
}
printJob.end();
}
tool().activate();
}
/**
* Save a Drawing in a file
*/
protected void saveDrawing(StorageFormat storeFormat, String file) {
try {
setDrawingTitle(storeFormat.store(file, drawing()));
}
catch (IOException e) {
showStatus(e.toString());
}
}
/**
* Load a Drawing from a file
*/
protected void loadDrawing(StorageFormat restoreFormat, String file) {
try {
Drawing restoredDrawing = restoreFormat.restore(file);
if (restoredDrawing != null) {
newWindow();
setDrawing(restoredDrawing);
setDrawingTitle(file);
}
else {
showStatus("Unknown file type: could not open file '" + file + "'");
}
} catch (IOException e) {
showStatus("Error: " + e);
}
}
/**
* Switch to a new Look&Feel
*/
private void newLookAndFeel(String landf) {
try {
UIManager.setLookAndFeel(landf);
SwingUtilities.updateComponentTreeUI(this);
}
catch (Exception e) {
System.err.println(e);
}
}
/**
* Set the title of the currently selected drawing
*/
protected void setDrawingTitle(String drawingTitle) {
fDrawingFilename = drawingTitle;
if (fgUntitled.equals(drawingTitle)) {
setTitle(getApplicationName());
}
else {
setTitle(getApplicationName() + " - " + drawingTitle);
}
}
/**
* Return the title of the currently selected drawing
*/
protected String getDrawingTitle() {
return fDrawingFilename;
}
/**
* Set the name of the application build from this skeleton application
*/
public void setApplicationName(String applicationName) {
fApplicationName = applicationName;
}
/**
* Return the name of the application build from this skeleton application
*/
public String getApplicationName() {
return fApplicationName;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -