📄 coordinatecanvas.java
字号:
package ergo.ui;
// $Id: CoordinateCanvas.java,v 1.2 1999/08/13 01:20:07 sigue Exp $
/*
* Copyright (C) 1999 Carl L. Gay and Antranig M. Basman.
* See the file copyright.txt, distributed with this software,
* for further information.
*/
import ergo.*;
import ergo.util.*;
import java.awt.*;
/**************************************************************
* CoordinateCanvas *
* *
* A canvas for displaying the coordinates along the *
* edge of the board. *
**************************************************************/
public class CoordinateCanvas extends ErgoCanvas implements Optionizable {
private static final char[] temp = new char[2];
private GameWindow gwin;
private String position;
private int gameSize;
private int pad = 2;
private String coordinateString = "Coordinate font";
private Font coordinateFont = null;
private Optionizer opser = Ergo.opser;
CoordinateCanvas (String position, GameWindow win) {
try {
opser.expressOwnership(coordinateString, Optionizer.TYPE_FONT,
this, null);
coordinateFont = Ergo.opser.getFontOption(coordinateString);
opser.expressOwnership(GlobalOptions.gwinColorString,
Optionizer.TYPE_COLOR, this,
new ColorSpec(win.gwinDefaultBackground));
setBackground(opser.getColorOption(GlobalOptions.gwinColorString));
}
catch (Exception e) {
Debug.backtrace(e);
}
this.position = position;
gwin = win;
gameSize = win.game.size();
setFont(coordinateFont);
}
// support the Optionizable interface.
public void optionEvent(String keyword, Object value) {
if (opser.isSameKey(keyword, coordinateString)) {
coordinateFont = (Font)value;
setFont(coordinateFont);
// +++ Is this correct, or is refresh() required also?
// validate();
// invalidate();
}
else if (opser.isSameKey(keyword, GlobalOptions.gwinColorString)) {
setBackground(opser.getColorOption(GlobalOptions.gwinColorString));
repaint();
}
}
public void update (Graphics g) { redisplay(g, false); }
public void paint (Graphics g) { redisplay(g, true); }
private void redisplay (Graphics g, boolean forcep) {
if (needsRedisplay
|| forcep
|| getSize().height != height
|| getSize().width != width) {
width = getSize().width;
height = getSize().height;
//g.setFont(gwin.SMALLFONT);
FontMetrics metrics = g.getFontMetrics();
g.setColor(getBackground());
g.fillRect(0, 0, width, height);
g.setColor(getForeground());
int step = (((isVertical() ? height : width) - (2 * gwin.getBevelWidth()))
/ gameSize);
int start = (isVertical()
? ((step - metrics.getHeight()) / 2 + metrics.getAscent())
: (step / 2)) + gwin.getBevelWidth();
if (isHorizontal()) {
int y = position.equalsIgnoreCase("south")
? (metrics.getAscent() + pad)
: (height - metrics.getDescent() - pad);
for (int i = 0; i < gameSize; i++) {
int index = (i < 25) ? i : i - 25;
int it = 'A' + ((index <= 7) ? index : index + 1);
temp[0] = (i < 25) ? (char)it : Character.toLowerCase((char)it);
g.drawChars(temp, 0, 1, start + i * step, y);
}
}
else if (isVertical())
for (int i = gameSize; i > 0; i--) {
temp[0] = (i < 10) ? ' ' : Util.DIGITS[i / 10];
temp[1] = Util.DIGITS[i % 10];
int y = start + (gameSize - i) * step;
if (position.equalsIgnoreCase("east")) {
if (i < 10)
g.drawChars(temp, 1, 1, pad, y);
else
g.drawChars(temp, 0, 2, pad, y);
} else
g.drawChars(temp, 0, 2,
(width - metrics.charsWidth(temp, 0, 2) - pad), y);
}
}
}
private boolean isVertical () {
return (position.equalsIgnoreCase("East")
|| position.equalsIgnoreCase("West"));
}
private boolean isHorizontal () {
return (position.equalsIgnoreCase("South")
|| position.equalsIgnoreCase("North"));
}
private Dimension sizeForFont (Font f) {
Dimension d = new Dimension();
FontMetrics m = getFontMetrics(f);
if (isVertical()) {
d.width = m.stringWidth("19");
d.height = m.getHeight() * gameSize; }
else {
d.width = m.stringWidth("A") * gameSize;
d.height = m.getHeight(); }
d.width += pad * 2;
d.height += pad * 2;
return d;
}
public Dimension getMinimumSize () {
return getPreferredSize();
}
public Dimension getPreferredSize () {
return sizeForFont(getFont());
}
/**
* Shift clicking in a coord canvas hides it.
*/
/*
public boolean mouseUp (Event e, int x, int y) {
if (e.shiftDown()) {
gwin.toggleCoordinates(this);
return true; }
else
return false;
}
*/
} // end of class CoordinateCanvas
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -