📄 asciidrawframe.java
字号:
package joeh.asciidraw;
import java.awt.*;
import java.applet.*;
import java.util.*;
import java.io.*;
/////////
//////////
//////////
/////////
public class AsciiDrawFrame extends Frame {
MenuBar mbar;
public MenuItem undo; //public so that outsiders can disable/enable it
AsciiDrawPanel adp;
String fileSTR = "Save to a file...";
String printSTR = "Preview ascii output";
String clearSTR = "Clear screen";
String quitSTR = "Close";
String undoSTR = "Undo '" + clearSTR + "'";
public boolean nextUndo = false; //a flag set to true just after they clear the screen
//this flag enables the undo menu and gives the user a chance
//to restore the screen. If they don't excise that chance by
//choosing undo, then the screen remains cleared and the
//data itself is deleted.
//A lot of how this actually is implemented is very hacked.
//It relies on setting values of public class variables (e.g. "MenuItem undo")
//ugly code, I'm sorry but I needed to finish this in a hurry
StatusLabel statusLabel;
paletteItem currentColorPaletteSelection;
toolItem currentToolSelection;
paletteItem colorItem1; //publicly avaliable for resetting and such
toolItem tool1; //publicly avaliable for resetting and such
public void initializeUI() {
tool1.select();
setToolSelection(tool1);
colorItem1.select();
setColorPaletteSelection(colorItem1);
}
public AsciiDrawFrame(int xw, int yh, int sqrsize) {
super("AsciiDraw");
this.resize(xw, yh);
adp = new AsciiDrawPanel(this, sqrsize);
// this.setLayout(new BorderLayout());
// this.add("Center", adp);
this.setLayout(new GridBagLayout());
// paletteItems and the ColorPalette
palettePanel ColorPalette = new palettePanel("Shades", adp);
colorItem1 = new paletteItem(Color.black, "%", adp);
paletteItem pi2 = new paletteItem(Color.darkGray, "+", adp);
paletteItem pi3 = new paletteItem(Color.gray, "-", adp);
paletteItem pi4 = new paletteItem(Color.lightGray, ".", adp);
paletteItem pi5 = new paletteItem(Color.white, " ", adp);
paletteItem pi6 = new paletteItem(Color.green, "g", adp);
paletteItem pi7 = new paletteItem(Color.blue, "b", adp);
paletteItem pi8 = new paletteItem(Color.magenta, "m", adp);
paletteItem pi9 = new paletteItem(Color.orange, "O", adp);
paletteItem pi10 = new paletteItem(Color.pink, "p", adp);
paletteItem pi11 = new paletteItem(Color.red, "r", adp);
ColorPalette.add(colorItem1);
ColorPalette.add(pi2);
ColorPalette.add(pi3);
ColorPalette.add(pi4);
ColorPalette.add(pi5);
// ColorPalette.add(pi6);
// ColorPalette.add(pi7);
// ColorPalette.add(pi8);
// ColorPalette.add(pi9);
// ColorPalette.add(pi10);
// ColorPalette.add(pi11);
currentColorPaletteSelection = pi2;
//Load a vector with colors so I can loop through systematically with darker and lighter...
Vector cv = new Vector();
cv.addElement(Color.blue);
cv.addElement(Color.red);
cv.addElement(Color.green);
for (int i = 0; i < cv.size(); i++) {
ColorPalette.add(new paletteItem(((Color)cv.elementAt(i)).darker().darker(),
"#", adp));
ColorPalette.add(new paletteItem(((Color)cv.elementAt(i)).darker(),
"+", adp));
ColorPalette.add(new paletteItem((Color)cv.elementAt(i),
".", adp));
}
// toolItems and the ToolPalette
palettePanel ToolPalette = new palettePanel("Tools", adp);
tool1 = new toolItem(new ICON(ToolType.PENCIL), ToolType.PENCIL, adp);
toolItem tool2 = new toolItem(new ICON(ToolType.FILL), ToolType.FILL, adp);
toolItem tool3 = new toolItem(new ICON(ToolType.BRUSH), ToolType.BRUSH, adp);
ToolPalette.add(tool1);
ToolPalette.add(tool2);
ToolPalette.add(tool3);
currentToolSelection = tool2;
adp.requestFocus(); //Because of this, the applet will start and typing into it will be
//alright even if the user doesn't first click inside the applet. (you might have
//seen applets that say "click inside first to activate" -- that's bs. It is
//because the programmer didn't know this trick. Now you do!)
//-------------
statusLabel = new StatusLabel("Welcome to AsciiDraw. Choose a color and draw.", adp);
statusLabel.setBackground(Color.lightGray);
statusLabel.setForeground(Color.blue);
mbar = new MenuBar();
Menu F = new Menu("File");
Menu E = new Menu("Edit");
F.add(new MenuItem(printSTR));
F.add(new MenuItem(fileSTR));
F.addSeparator();
F.add(new MenuItem(quitSTR));
undo = new MenuItem(undoSTR);
undo.disable(); //will be enabled if they clear the screen
E.add(undo);
E.addSeparator();
E.add(new MenuItem(clearSTR));
mbar.add(F);
mbar.add(E);
setMenuBar(mbar);
// this.add("North", ToolPalette);
GridBagHelper.constrain(this, ToolPalette, 0, 0, 1, 2,
GridBagConstraints.HORIZONTAL,
GridBagConstraints.WEST,
1.0, 0.0, 0, 0, 0, 0);
// this.add("North", ColorPalette);
GridBagHelper.constrain(this, ColorPalette, 0, 2, 1, 2,
GridBagConstraints.HORIZONTAL,
GridBagConstraints.WEST,
1.0, 0.0, 1, 0, 0, 0);
// this.add("Center", adp);
GridBagHelper.constrain(this, adp, 0, 4, 1, 1,
GridBagConstraints.BOTH,
GridBagConstraints.CENTER,
1.0, 1.0, 2, 0, 0, 0);
// this.add("South", statusLabel);
GridBagHelper.constrain(this, statusLabel, 0, 5, 1, 1,
GridBagConstraints.HORIZONTAL,
GridBagConstraints.CENTER,
1.0, 0.0, 0, 0, 0, 0);
}
public void setColorPaletteSelection(paletteItem newPaletteSelection) {
if (currentColorPaletteSelection != newPaletteSelection)
{
currentColorPaletteSelection.deselect();
currentColorPaletteSelection = newPaletteSelection;
}
}
public void setToolSelection(toolItem newToolSelection) {
if (currentToolSelection != newToolSelection) {
currentToolSelection.deselect();
currentToolSelection = newToolSelection;
int k = currentToolSelection.getKind();
if (k == ToolType.PENCIL)
setStatusLabel("The pencil tool is selected. Use it to draw one space at a time.");
if (k == ToolType.FILL)
setStatusLabel("The fill tool is selected, click inside bounded regions to fill.");
if (k == ToolType.BRUSH)
setStatusLabel("The brush tool is selected. It is similar to the pencil, but thicker.");
}
}
public void setStatusLabel(String s) {
statusLabel.setText(s);
}
public boolean handleEvent(Event evt)
{
//if the nextUndo flag was set, the have a chance to undo the clear with thier next action
//Really, we should only lose this chance if the screen changes after a clear screen.
//until then it should be okay to undo. Hmm
//So here we check for undo
//and we check for missed-undo-chance everywhere the screen changes
/// ---> inside the panel event handler!
if ((undoSTR.equals(evt.arg)) && (nextUndo) )
{
// System.out.println("UNDO CLEAR");
//adp.setDirty();
if (adp.SIZEDCHANGED) //sized changed while user had a chance to undo clear
nextUndo = false; //so do this to trigger a deep repaint looking at data.
adp.paint(adp.getGraphics()); //give them the image back
undo.disable(); //okay, they only need to undo it once.
nextUndo = false;
adp.requestFocus(); //give the focus back to the panel
return true; //their event was choosing undo
}
switch(evt.id)
{
case Event.ACTION_EVENT:
{
if (evt.target instanceof MenuItem) {
if (printSTR.equals(evt.arg)) {
if (!(nextUndo)) {
adp.doSave(false, 1); //1 == ascii normal
//they won't need to "preview" the Java version
}
return true;
}
if (fileSTR.equals(evt.arg)) {
if (!(nextUndo)) {
AsciiFileOutFrame fileFrame = new AsciiFileOutFrame(400,200,adp);
fileFrame.show();
}
return true;
}
if (clearSTR.equals(evt.arg)) {
adp.clearGraphics();
nextUndo = true; //give them a chance to undo the clear with thier next action
undo.enable();
return true;
}
if (quitSTR.equals(evt.arg)) {
dispose();
return true;
}
}
}
case Event.WINDOW_DESTROY: //they close the window!
{
dispose();
return true;
}
}
adp.requestFocus(); //give the focus back to the panel
return true;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -