drawapplet.java
来自「开源(Open Source)项目JHotDraw的文档和源程序」· Java 代码 · 共 550 行 · 第 1/2 页
JAVA
550 行
/*
* @(#)DrawApplet.java 5.2
*
*/
package CH.ifa.draw.applet;
import javax.swing.*;
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
import java.net.*;
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
* a drawing editor that is run as an applet. The presentation is
* customized in subclasses.<p>
* Supported applet parameters: <br>
* <i>DRAWINGS</i>: a blank separated list of drawing names that is
* shown in the drawings choice.
*/
public class DrawApplet
extends JApplet
implements DrawingEditor, PaletteListener {
transient private Drawing fDrawing;
transient private Tool fTool;
transient private StandardDrawingView fView;
transient private ToolButton fDefaultToolButton;
transient private ToolButton fSelectedToolButton;
transient private boolean fSimpleUpdate;
transient private JButton fUpdateButton;
transient private JComboBox fFrameColor;
transient private JComboBox fFillColor;
transient private JComboBox fTextColor;
transient private JComboBox fArrowChoice;
transient private JComboBox fFontChoice;
transient private Thread fSleeper;
private Iconkit fIconkit;
static String fgUntitled = "untitled";
private static final String fgDrawPath = "/CH/ifa/draw/";
public static final String IMAGES = fgDrawPath+"images/";
/**
* Initializes the applet and creates its contents.
*/
public void init() {
fIconkit = new Iconkit(this);
getContentPane().setLayout(new BorderLayout());
fView = createDrawingView();
JPanel attributes = createAttributesPanel();
createAttributeChoices(attributes);
getContentPane().add("North", attributes);
JPanel toolPanel = createToolPalette();
createTools(toolPanel);
getContentPane().add("West", toolPanel);
getContentPane().add("Center", fView);
JPanel buttonPalette = createButtonPanel();
createButtons(buttonPalette);
getContentPane().add("South", buttonPalette);
initDrawing();
// JFC should have its own internal double buffering...
//setBufferedDisplayUpdate();
setupAttributes();
}
/*
* Gets the iconkit to be used in the applet.
*/
/**** not sure whether this will still be needed on 1.1 enabled browsers
protected Iconkit iconkit() {
if (fIconkit == null) {
startSleeper();
loadAllImages(this); // blocks until images loaded
stopSleeper();
}
return fIconkit;
} */
/**
* Creates the attributes panel.
*/
protected JPanel createAttributesPanel() {
JPanel panel = new JPanel();
panel.setLayout(new PaletteLayout(2, new Point(2,2), false));
return panel;
}
/**
* Creates the attribute choices. Override to add additional
* choices.
*/
protected void createAttributeChoices(JPanel panel) {
panel.add(new JLabel("Fill"));
fFillColor = createColorChoice("FillColor");
panel.add(fFillColor);
panel.add(new JLabel("Text"));
fTextColor = createColorChoice("TextColor");
panel.add(fTextColor);
panel.add(new JLabel("Pen"));
fFrameColor = createColorChoice("FrameColor");
panel.add(fFrameColor);
panel.add(new JLabel("Arrow"));
CommandChoice choice = new CommandChoice();
fArrowChoice = choice;
choice.addItem(new ChangeAttributeCommand("none", "ArrowMode", new Integer(PolyLineFigure.ARROW_TIP_NONE), fView));
choice.addItem(new ChangeAttributeCommand("at Start", "ArrowMode", new Integer(PolyLineFigure.ARROW_TIP_START), fView));
choice.addItem(new ChangeAttributeCommand("at End", "ArrowMode", new Integer(PolyLineFigure.ARROW_TIP_END), fView));
choice.addItem(new ChangeAttributeCommand("at Both", "ArrowMode", new Integer(PolyLineFigure.ARROW_TIP_BOTH), fView));
panel.add(fArrowChoice);
panel.add(new JLabel("Font"));
fFontChoice = createFontChoice();
panel.add(fFontChoice);
}
/**
* Creates the color choice for the given attribute.
*/
protected JComboBox createColorChoice(String attribute) {
CommandChoice choice = new CommandChoice();
for (int i=0; i<ColorMap.size(); i++)
choice.addItem(
new ChangeAttributeCommand(
ColorMap.name(i),
attribute,
ColorMap.color(i),
fView
)
);
return choice;
}
/**
* Creates the font choice. The choice is filled with
* all the fonts supported by the toolkit.
*/
protected JComboBox createFontChoice() {
CommandChoice choice = new CommandChoice();
String fonts[] = Toolkit.getDefaultToolkit().getFontList();
for (int i = 0; i < fonts.length; i++)
choice.addItem(new ChangeAttributeCommand(fonts[i], "FontName", fonts[i], fView));
return choice;
}
/**
* Creates the buttons panel.
*/
protected JPanel createButtonPanel() {
JPanel panel = new JPanel();
panel.setLayout(new PaletteLayout(2, new Point(2,2), false));
return panel;
}
/**
* Creates the buttons shown in the buttons panel. Override to
* add additional buttons.
* @param panel the buttons panel.
*/
protected void createButtons(JPanel panel) {
panel.add(new Filler(24,20));
JComboBox drawingChoice = new JComboBox();
drawingChoice.addItem(fgUntitled);
String param = getParameter("DRAWINGS");
if (param == null)
param = "";
StringTokenizer st = new StringTokenizer(param);
while (st.hasMoreTokens())
drawingChoice.addItem(st.nextToken());
// offer choice only if more than one
if (drawingChoice.getItemCount() > 1)
panel.add(drawingChoice);
else
panel.add(new JLabel(fgUntitled));
drawingChoice.addItemListener(
new ItemListener() {
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
loadDrawing((String)e.getItem());
}
}
}
);
panel.add(new Filler(6,20));
JButton button;
button = new CommandButton(new DeleteCommand("Delete", fView));
panel.add(button);
button = new CommandButton(new DuplicateCommand("Duplicate", fView));
panel.add(button);
button = new CommandButton(new GroupCommand("Group", fView));
panel.add(button);
button = new CommandButton(new UngroupCommand("Ungroup", fView));
panel.add(button);
button = new JButton("Help");
button.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent event) {
showHelp();
}
}
);
panel.add(button);
fUpdateButton = new JButton("Simple Update");
fUpdateButton.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent event) {
if (fSimpleUpdate)
setBufferedDisplayUpdate();
else
setSimpleDisplayUpdate();
}
}
);
// panel.add(fUpdateButton); // not shown currently
}
/**
* Creates the tools palette.
*/
protected JPanel createToolPalette() {
JPanel palette = new JPanel();
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(JPanel palette) {
Tool tool = createSelectionTool();
fDefaultToolButton = createToolButton(IMAGES+"SEL", "Selection Tool", tool);
palette.add(fDefaultToolButton);
}
/**
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?