📄 asciidrawpanel.java
字号:
if (!(spaceInRange(cooX, cooY)))
return;
if (check4Fill(g, cooX, cooY, oc, nc)) {
areaFill(g, oc, nc, cooX-1, cooY);
areaFill(g, oc, nc, cooX+1, cooY);
areaFill(g, oc, nc, cooX, cooY-1);
areaFill(g, oc, nc, cooX, cooY+1);
}
}
private boolean spaceInRange(int cooX, int cooY) {
return ((cooX < columns) && (cooY < rows) && (cooY >= 0) && (cooX >= 0));
}
//Figure out whichcoordinate they clicked, and draw in that one square if appropriate.
//Also here, what we do depends on what tool we are using
public void processMouse(int x, int y, int mouseBehavior) {
Dimension d = this.size();
int oneW = d.width/columns;
int oneH = d.height/rows;
int cooX, cooY;
cooX = x/oneW;
cooY = y/oneH;
//System.out.println("clicked " + cooX +","+ cooY);
//they may drag mouse out of frame, but event still registers
if (spaceInRange(cooX, cooY)) {
//They have clicked or dragged to draw on a valid location
if (parent.nextUndo) //they had a chance to undo a clear
{
//user did clear screen and next didn't do UNDO clear screen
//so take out the data too
doClear();
parent.undo.disable(); //They had their chance and now its too late.
parent.nextUndo = false;
}
int arrayNum = (cooX*rows + cooY);
Graphics g = this.getGraphics(); //the graphics of our actual panel
Switch sw = ((Switch)positions.get(keys.elementAt(arrayNum))); //the switch at the space
//Now we konw our measurements and have our space.
//We make a big if then else coosing by tool type down here.
//Each one decides how to behave based on what kind of tool it is and
//on what the mouse state was at the space (mouse-up-down-drag...)
if (mode == ToolType.BRUSH) {
//A brush is simpler than a pencil. It just draws no matter what.
//We just draw a 2X2 square making sure not to go out of bounds.
if ( (mouseBehavior==MouseType.MOUSE_DOWN) ||
(mouseBehavior==MouseType.MOUSE_DRAG) ) { //mouse down or drag
//our convention is to draw "me" and the
//three squares: south, east, south-east
int tempArrayNum =0;
Switch tempSw;
int nX, nY;
//screen draw it all at once... is easy b/c it is
//okay to go out of bounds without checking in graphic objects
g.setColor(THEcolor);
g.fillRect(cooX*oneW,cooY*oneH, oneW*2, oneH*2);
im2g.setColor(THEcolor);
im2g.fillRect(cooX*oneW,cooY*oneH, oneW*2, oneH*2);
//Now change the underlying space data
//south: cooX, cooY + oneH
nX = cooX;
nY = cooY + 1;
if (spaceInRange(nX, nY)) {
tempArrayNum = (nX*rows + nY);
tempSw = ((Switch)positions.get(keys.elementAt(tempArrayNum)));
tempSw.setChar(THEchar, false);
tempSw.setColor(THEcolor);
}
//east: cooX + oneW, cooY
nX = cooX + 1;
nY = cooY;
if (spaceInRange(nX, nY)) {
tempArrayNum = (nX*rows + nY);
tempSw = ((Switch)positions.get(keys.elementAt(tempArrayNum)));
tempSw.setChar(THEchar, false);
tempSw.setColor(THEcolor);
}
//s-e: cooX + oneW, cooY + oneH
nX = cooX + 1;
nY = cooY + 1;
if (spaceInRange(nX, nY)) {
tempArrayNum = (nX*rows + nY);
tempSw = ((Switch)positions.get(keys.elementAt(tempArrayNum)));
tempSw.setChar(THEchar, false);
tempSw.setColor(THEcolor);
}
//"ME"
sw.setChar(THEchar, false);
sw.setColor(THEcolor);
}
}
else
if (mode == ToolType.FILL) {
if (mouseBehavior==MouseType.MOUSE_DOWN) {
/////setDrawing();
Color originalC = sw.getColor();
if (!(originalC.equals(THEcolor))) //dont fill if same (infinite loop)
areaFill(g, originalC, THEcolor, cooX, cooY);
}
}
else
if (mode == ToolType.PENCIL) {
//behavior is like a regular paint program pencil tool;
//if they click a square of the same color as the selected color,
//then we begin erasing from there and all subsequent space dragged over
//If it is a different color, we begin drawing!
if (mouseBehavior==MouseType.MOUSE_DOWN) {
if ( THEcolor.equals(sw.getColor()) ) {
////.. g.setColor(Color.white);
sw.setColor(Color.white); //white turns it off!
setErasing();
}
else { //it is a different color!
////.. g.setColor(THEcolor);
setDrawing();
}
}
if ( (mouseBehavior==MouseType.MOUSE_DOWN) ||
(mouseBehavior==MouseType.MOUSE_DRAG) ) { //mouse down or drag
if (isDrawing()) {
sw.setChar(THEchar, false);
sw.setColor(THEcolor);
g.setColor(THEcolor);
im2g.setColor(THEcolor);
}
else {
g.setColor(Color.white);
im2g.setColor(Color.white);
sw.setColor(Color.white); //setting white turns it off
}
//finally, draw it (mouse down, drag)
g.fillRect(cooX*oneW,cooY*oneH, oneW, oneH);
im2g.fillRect(cooX*oneW,cooY*oneH, oneW, oneH);
}
}
else
System.out.println("UNKNOWN TOOL ERROR");
//Code here applies to all tools the same.
if (mouseBehavior==MouseType.MOUSE_UP) {
theCursor.setLoc(cooX,cooY);
//the mouse up sets the cursor
}
}
}
//Fill at cursor with current color
public void fillNewOne() {
//put in color it there
Dimension d = this.size();
int oneW = d.width/columns;
int oneH = d.height/rows;
int cooX, cooY;
Coordinate cursorCoords = theCursor.getLoc();
cooX = cursorCoords.getX();
cooY = cursorCoords.getY();
//uh-oh protection...if bad cursor
if ((cooX < columns) && (cooY < rows) && (cooY >= 0) && (cooX >= 0))
{
int arrayNum = (cooX*rows + cooY);
Graphics g = this.getGraphics();
setDrawing();
Switch sw = ((Switch)positions.get(keys.elementAt(arrayNum)));
sw.setChar(THEchar, false);
sw.setColor(THEcolor); //the background color of a user-typed letter
//the letter itself will be black.
//actually draw it
g.setColor(THEcolor);
g.fillRect(cooX*oneW, cooY*oneH, oneW, oneH);
im2g.setColor(THEcolor);
im2g.fillRect(cooX*oneW, cooY*oneH, oneW, oneH);
}
else System.out.println("JOE - FIX ME!");
}
//stick a char at cursor
public void type(String what) {
//Type it there
//System.out.println("type");
Dimension d = this.size();
int oneW = d.width/columns;
int oneH = d.height/rows;
int cooX, cooY;
Coordinate cursorCoords = theCursor.getLoc();
cooX = cursorCoords.getX(); //the actual pixel location to draw
cooY = cursorCoords.getY();
//uh-oh protection...if bad cursor
if ((cooX < columns) && (cooY < rows) && (cooY >= 0) && (cooX >= 0))
{
int arrayNum = (cooX*rows + cooY);
Graphics g = this.getGraphics();
setDrawing();
Switch sw = ((Switch)positions.get(keys.elementAt(arrayNum)));
sw.setColor(Color.white); //background of space is white
sw.setChar(what, true);
//actually draw it
g.setColor(Color.white);
g.fillRect(cooX*oneW, cooY*oneH, oneW, oneH);
g.setColor(Color.black);
drawStringWell(g, what, cooX*oneW, cooY*oneH);
im2g.setColor(Color.white);
im2g.fillRect(cooX*oneW, cooY*oneH, oneW, oneH);
im2g.setColor(Color.black);
drawStringWell(im2g, what, cooX*oneW, cooY*oneH);
// g.drawString(what, cooX*oneW, cooY*oneH);
}
else System.out.println("JOE - FIX ME!");
}
public void drawStringWell(Graphics gg, String what, int x, int y) {
Dimension d = this.size();
int oneW = d.width/columns;
int oneH = d.height/rows;
Font ff = new Font("Courier",Font.PLAIN,10); //small
gg.setFont(ff);
FontMetrics fm = gg.getFontMetrics();
int adjY = ((fm.getHeight()-fm.getDescent())/2) + (oneH/2); //the amount to shift down.
int adjX = (oneW/2) - (fm.stringWidth(what)/2); //the amount to shift over.
gg.drawString(what, x + adjX, y + adjY);
}
//Calling all programmers: Does someone have code so that the user
//can select the directory they want to save in that I (or you) can stick in here?
public void writeStrToFile(String data) {
if (destFile.exists())
{
// System.out.println("File exists.");
if (destFile.isFile()) {
// System.out.println("File is a file.");
if (destFile.canWrite()) {
// System.out.println("File can write. We will overwrite!");
try {
destination = new FileOutputStream(destFile); //catch
PrintStream doit = new PrintStream(destination);
System.out.println("Do it!");
doit.println(data);
}
catch (IOException e)
{
String str = "Sorry, your file couldn't be written.";
str += "---That file may already exist.";
str += "---Please try a different file name.";
PreviewFrame pf = new PreviewFrame(400,100, str);
pf.show();
}
}
else
{
String str = "Sorry, your file couldn't be written.";
str += "---The disk may be locked.";
PreviewFrame pf = new PreviewFrame(400,100, str);
pf.show();
}
}
else
{
String str = "Sorry, your file couldn't be written.";
str += "---Invalid file name.";
str += "---Please try a different file name.";
PreviewFrame pf = new PreviewFrame(400,100, str);
pf.show();
}
}
else
{
//System.out.println("Making a new file for you...");
File parentdir = parent(destFile);
if (!parentdir.exists())
{
String str = "Sorry, your file couldn't be written.";
str += "---Couldn't locate directory.";
PreviewFrame pf = new PreviewFrame(400,100, str);
pf.show();
}
else if (!parentdir.canWrite())
{
String str = "Sorry, your file couldn't be written.";
str += "---Locked volume.";
PreviewFrame pf = new PreviewFrame(400,100, str);
pf.show();
}
else {
// System.out.println("Now actually writing your disk file!...");
try {
destination = new FileOutputStream(destFile); //catch
PrintStream doit = new PrintStream(destination);
// System.out.println("Do it!");
doit.println(data);
}
catch (IOException e) { //something else went wrong and I don't know why
System.out.println("Something terribly wrong has happened which prevented the file from being written.");
}
}
}
}
private static File parent(File f) {
String dirname = f.getParent();
if (dirname == null) {
if (f.isAbsolute()) return new File(File.separator);
else {
//System.out.println("CURRENT DIR:" + System.getProperty("user.dir"));
return new File(System.getProperty("user.dir"));
}
}
return new File(dirname);
}
//We catch events. But we are inside a frame.
//The frame needs to process whatever events change the appearance of the screen
// (for the undo clear screen thing)
//So we pass those events along by returning false
public boolean handleEvent(Event evt)
{
switch(evt.id) {
case 403: { //type arrow keys
setStatusLabel("Try holding down the shift key while pressing the arrow keys.");
// if they hold down the shift while using arrow key, draw the current
// box before moving cursor
int m = evt.modifiers;
int EKEY = evt.key;
boolean fill = false;
if ((m & Event.SHIFT_MASK) != 0) //User is drawing with shift key
fill = true;
if (fill) { //this has to do with filling one space on case of shift-arrow key.
if (parent.nextUndo) { //they had a chance to undo a clear
//user did clear screen and next didn't do UNDO clear screen
//so take out the data too
doClear();
parent.undo.disable(); //They had their chance and now its too late.
parent.nextUndo = false;
}
fillNewOne(); //fill the one space we are on and the next one
}
//just move cursor if arrow keys
if (EKEY == Event.UP) {
theCursor.hardUp();
if (fill)
fillNewOne(); //the next one
return true;
}
else if (EKEY == Event.DOWN) {
theCursor.hardDown();
if (fill)
fillNewOne(); //the next one
return true;
}
else if (EKEY == Event.LEFT) {
theCursor.hardLeft();
if (fill)
fillNewOne(); //the next one
return true;
}
else if (EKEY == Event.RIGHT) {
theCursor.hardRight();
if (fill)
fillNewOne(); //the next one
return true;
}
}
case Event.KEY_PRESS: {
setStatusLabel("Arrow keys position the yellow cursor for typing.");
char keychar = (char)evt.key;
Character charPushed = new Character(keychar);
//some special cases
if (keychar == '\010') //backspace
{
if (parent.nextUndo) //they had a chance to undo a clear
{
//user did clear screen and next didn't do UNDO clear screen
//so take out the data too
doClear();
parent.undo.disable(); //They had their chance and now its too late.
parent.nextUndo = false;
}
type(" ");
theCursor.moveLeft(true); //delete and go back
return true;
}
else if (keychar == '\t') {
theCursor.moveRight();
theCursor.moveRight();
theCursor.moveRight();
theCursor.moveRight();
theCursor.moveRight();
return true; //on tab go forward a few
}
else if (keychar == '\033')
{
return true; //on escape do nothing
}
else if (keychar == '\n')
{
// type(" "); //on return, go to new line
theCursor.moveNewLineUser();
return true;
}
else {
//else type and move cursor
if (parent.nextUndo) //they had a chance to undo a clear
{
//user did clear screen and next didn't do UNDO clear screen
//so take out the data too
doClear();
parent.undo.disable(); //They had their chance and now its too late.
parent.nextUndo = false;
}
//on space we can check about moving line!
if (charPushed.toString().equals(" ")) {
type(charPushed.toString());
theCursor.moveRightCheck();
}
else {
type(charPushed.toString());
theCursor.moveRight();
}
return true;
}
}
case Event.MOUSE_DOWN:{
//if it is a shift down, we don't draw because user is just setting the cursor
if ( !((evt.modifiers & Event.SHIFT_MASK) != 0) ){
setStatusLabel("Type to add text. The yellow box is the cursor.");
processMouse(evt.x,evt.y,MouseType.MOUSE_DOWN);
}
return true;
}
case Event.MOUSE_DRAG:
{
//System.out.println("MOUSE_DRAG");
//do nothing on shift drag
if (!((evt.modifiers & Event.SHIFT_MASK) != 0))
processMouse(evt.x,evt.y,MouseType.MOUSE_DRAG);
return true;
}
case Event.MOUSE_UP: {
int oneW = this.size().width/columns;
int cooX;
cooX = evt.x/oneW;
//click sets start of a line
//Shift-click sets end of a line
//Note that click also resets the end of the line to the end of the doc
//we set the cursor on a mouse up
//but we don't if the shift key is down
//because that case is a margin set (see mouse_down code)
if ((evt.modifiers & Event.SHIFT_MASK) != 0)
theCursor.setEnd(cooX);
else {
theCursor.setStart(cooX);
processMouse(evt.x,evt.y,MouseType.MOUSE_UP);
}
return true;
}
}
return true;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -