drawapplet.java
来自「开源(Open Source)项目JHotDraw的文档和源程序」· Java 代码 · 共 550 行 · 第 1/2 页
JAVA
550 行
* 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 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 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() {
return new StandardDrawingView(this, 410, 370);
}
/**
* 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) {
if (inside)
showStatus(((ToolButton) button).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() {
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) {
setupAttributes();
}
private void initDrawing() {
fDrawing = createDrawing();
fView.setDrawing(fDrawing);
toolDone();
}
private void setTool(Tool t, String name) {
if (fTool != null)
fTool.deactivate();
fTool = t;
if (fTool != null) {
showStatus(name);
fTool.activate();
}
}
private void setSelected(ToolButton button) {
if (fSelectedToolButton != null)
fSelectedToolButton.reset();
fSelectedToolButton = button;
if (fSelectedToolButton != null)
fSelectedToolButton.select();
}
protected void loadDrawing(String param) {
if (param == fgUntitled) {
fDrawing.release();
initDrawing();
return;
}
String filename = getParameter(param);
if (filename != null)
readDrawing(filename);
}
private void readDrawing(String filename) {
toolDone();
String type = guessType(filename);
if (type.equals("storable"))
readFromStorableInput(filename);
else if (type.equals("serialized"))
readFromObjectInput(filename);
else
showStatus("Unknown file type");
}
private void readFromStorableInput(String filename) {
try {
URL url = new URL(getCodeBase(), filename);
InputStream stream = url.openStream();
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 filename) {
try {
URL url = new URL(getCodeBase(), filename);
InputStream stream = url.openStream();
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";
}
private void setupAttributes() {
Color frameColor = (Color) AttributeFigure.getDefaultAttribute("FrameColor");
Color fillColor = (Color) AttributeFigure.getDefaultAttribute("FillColor");
Color textColor = (Color) AttributeFigure.getDefaultAttribute("TextColor");
Integer arrowMode = (Integer) AttributeFigure.getDefaultAttribute("ArrowMode");
String fontName = (String) AttributeFigure.getDefaultAttribute("FontName");
FigureEnumeration k = fView.selectionElements();
while (k.hasMoreElements()) {
Figure f = k.nextFigure();
frameColor = (Color) f.getAttribute("FrameColor");
fillColor = (Color) f.getAttribute("FillColor");
textColor = (Color) f.getAttribute("TextColor");
arrowMode = (Integer) f.getAttribute("ArrowMode");
fontName = (String) f.getAttribute("FontName");
}
fFrameColor.setSelectedIndex(ColorMap.colorIndex(frameColor));
fFillColor.setSelectedIndex(ColorMap.colorIndex(fillColor));
//fTextColor.select(ColorMap.colorIndex(textColor));
if (arrowMode != null)
fArrowChoice.setSelectedIndex(arrowMode.intValue());
if (fontName != null)
fFontChoice.setSelectedItem(fontName);
}
protected void setSimpleDisplayUpdate() {
fView.setDisplayUpdate(new SimpleUpdateStrategy());
fUpdateButton.setText("Simple Update");
fSimpleUpdate = true;
}
protected void setBufferedDisplayUpdate() {
fView.setDisplayUpdate(new BufferedUpdateStrategy());
fUpdateButton.setText("Buffered Update");
fSimpleUpdate = false;
}
/**
* Shows a help page for the applet. The URL of the help
* page is derived as follows: codeBase+appletClassname+Help.html"
*/
protected void showHelp() {
try {
String appletPath = getClass().getName().replace('.', '/');
URL url = new URL(getCodeBase(), appletPath+"Help.html");
getAppletContext().showDocument(url, "Help");
} catch (IOException e) {
showStatus("Help file not found");
}
}
/**
* *** netscape browser work around ***
*/
private void startSleeper() {
if (fSleeper == null)
fSleeper = new SleeperThread(this);
fSleeper.start();
}
private void stopSleeper() {
if (fSleeper != null)
fSleeper.stop();
}
}
class SleeperThread extends Thread {
JApplet fApplet;
SleeperThread(JApplet applet) {
fApplet = applet;
}
public void run() {
try {
for (;;) {
fApplet.showStatus("loading icons...");
sleep(50);
}
} catch (InterruptedException e) {
return;
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?