📄 sanguomap.java
字号:
package sanguo.ui;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.util.Calendar;
import java.util.Iterator;
import java.util.LinkedList;
import javax.swing.JPanel;
import sanguo.ctrl.action.SanguoAction;
import sanguo.map.Map;
import sanguo.util.Position;
public class SanguoMap extends JPanel {
public static final int MAP_ITEM_NUM = 4;
public static int ITEM_WIDTH = 72;
public static int CURSOR_IMG_NORMAL = 0;
public static int CURSOR_IMG_MOVE = 1;
public static int CURSOR_IMG_ATTACK = 2;
public static int CURSOR_IMG_HEAL = 2;
private static int X_OFFSET = 0;
private static int Y_OFFSET = 0;
private Map map;
private Image mapItems[];
private Position position;
private Position cursorPosition;
private int height, width;
private CharacterPane characterPane;
private CursorPane cursorPane;
private Image cursorImage[];
private int cursorImageIndex = 0;
private Image movableImage;
private LinkedList<SanguoAction> actions = null;
private LinkedList<sanguo.util.Position> movablePositions = null;
private int actionIndex = 0;
private sanguo.character.Character currentCharacter = null;
public SanguoMap(Map map) {
initializeMapItems();
position = new Position(0, 0);
cursorPosition = new Position(3, 3);
this.height = 8;
this.width = 10;
this.map = map;
characterPane = new CharacterPane();
cursorPane = new CursorPane();
characterPane.add(cursorPane);
this.setLayout(new BorderLayout());
this.add(characterPane);
this.setBackground(Color.black);
}
private void initializeMapItems() {
mapItems = new Image[MAP_ITEM_NUM];
for (int i = 0; i < MAP_ITEM_NUM; i++) {
mapItems[i] = Toolkit.getDefaultToolkit().getImage(
"src/resources/pic_" + i + ".png");
}
cursorImage = new Image[4];
for (int i = 0; i < cursorImage.length; i++) {
cursorImage[i] = Toolkit.getDefaultToolkit().getImage(
"src/resources/pic_cursor_" + i + ".png");
}
movableImage = Toolkit.getDefaultToolkit().getImage(
"src/resources/pic_movable.png");
}
public void moveMap(Position pos) {
this.position.setX(pos.getX());
this.position.setY(pos.getY());
update();
}
public sanguo.character.Character getCharacterInPosition() {
return this.currentCharacter;
}
public void moveCursor(int x_offset, int y_offset) {
int x = position.getX();
int y = position.getY();
int cx = cursorPosition.getX();
int cy = cursorPosition.getY();
cx += x_offset;
cy += y_offset;
if (cx < 0)
cx = 0;
if (cy < 0)
cy = 0;
if (cx >= map.getWidth())
cx = map.getWidth() - 1;
if (cy >= map.getHeight())
cy = map.getHeight() - 1;
// check if the cursor is in the visual area.
if (cx < x)
x = cx;
if (cy < y)
y = cy;
if (cx >= width + x) {
x = cx - width + 1;
}
if (cy >= height + y) {
y = cy - height + 1;
}
cursorPosition.setX(cx);
cursorPosition.setY(cy);
moveMap(new Position(x, y));
}
public void moveMenu(int direction) {
if (actions == null)
return;
do {
if (direction < 0) {
actionIndex -= 1;
} else if (direction > 0) {
actionIndex += 1;
}
if (actionIndex < 0) {
actionIndex = actions.size() - 1;
} else if (actionIndex >= actions.size()) {
actionIndex = 0;
}
} while (!((SanguoAction) actions.get(actionIndex)).isEnable());
this.cursorPane.repaint();
}
public void setMessage(String s) {
this.cursorPane.setMessage(" " + s);
}
public void setAction(LinkedList<SanguoAction> actions) {
this.actions = actions;
this.actionIndex = 0;
this.cursorPane.repaint();
}
public void setMovablePositions(LinkedList<sanguo.util.Position> positions) {
this.movablePositions = positions;
}
public SanguoAction getSelectedAction() {
if (actions == null)
return null;
return actions.get(actionIndex);
}
public Map getMap() {
return map;
}
public void setMap(Map map) {
this.map = map;
}
public void update() {
this.repaint();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
// Initialize the parameters.
ITEM_WIDTH = Math.min(this.getHeight() / this.height, this.getWidth()
/ this.width);
X_OFFSET = (this.getWidth() - this.width * ITEM_WIDTH) / 2;
Y_OFFSET = (this.getHeight() - this.height * ITEM_WIDTH) / 2;
int data[][] = map.getMap();
int x = position.getX();
int y = position.getY();
for (int j = 0; j < height; j++) {
for (int k = 0; k < width; k++) {
// System.out.println("draw " + j + " " + k);
g.drawImage(mapItems[data[j + y][k + x]], X_OFFSET + ITEM_WIDTH
* k, Y_OFFSET + ITEM_WIDTH * j, ITEM_WIDTH, ITEM_WIDTH,
this);
}
}
}
public void setCursorImage(int index) {
this.cursorImageIndex = index;
}
class CharacterPane extends JPanel {
public CharacterPane() {
this.setOpaque(false);
this.setLayout(new BorderLayout());
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Iterator<sanguo.character.Character> itr = map.getCharacters()
.iterator();
int x = position.getX(), y = position.getY();
boolean onCharacter = false;
while (itr.hasNext()) {
sanguo.character.Character c;
Image img = (c = itr.next()).getImage();
int X = c.getPosition().getX() - x;
int Y = c.getPosition().getY() - y;
if (X < 0 || Y < 0)
continue;
g.drawImage(img, X_OFFSET + X * ITEM_WIDTH, Y_OFFSET + Y
* ITEM_WIDTH, ITEM_WIDTH, ITEM_WIDTH, this);
if (c.getPosition().getX() == cursorPosition.getX()
&& c.getPosition().getY() == cursorPosition.getY()) {
cursorPane.setMessage(" " + c.getName());
currentCharacter = c;
onCharacter = true;
}
}
if (!onCharacter) {
cursorPane.setMessage(null);
currentCharacter = null;
}
paintMovablePositions(g);
}
public void paintMovablePositions(Graphics g) {
Calendar now = Calendar.getInstance();
if (movablePositions == null)
return;
Iterator<sanguo.util.Position> itr = movablePositions.iterator();
while (itr.hasNext()) {
sanguo.util.Position pos = itr.next();
int x = (pos.getX() - position.getX());
int y = (pos.getY() - position.getY());
if (x >= 0 && y >= 0 && x < width && y < height) {
g.drawImage(movableImage, X_OFFSET + x * ITEM_WIDTH,
Y_OFFSET + y * ITEM_WIDTH, ITEM_WIDTH, ITEM_WIDTH,
this);
}
}
Calendar now1 = Calendar.getInstance();
System.out.println(now1.getTimeInMillis() - now.getTimeInMillis());
}
}
class CursorPane extends JPanel {
private String message = null;
public CursorPane() {
this.setOpaque(false);
this.setLayout(new BorderLayout());
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
int x = position.getX(), y = position.getY();
int X = cursorPosition.getX() - x;
int Y = cursorPosition.getY() - y;
g.drawImage(cursorImage[cursorImageIndex], X_OFFSET + X
* ITEM_WIDTH, Y_OFFSET + Y * ITEM_WIDTH, ITEM_WIDTH,
ITEM_WIDTH, this);
if (message != null) {
paintMessage(g);
}
if (actions != null) {
paintMenu(g);
}
}
private void paintMessage(Graphics g) {
Color c = new Color(1.0f, 1.0f, 1.0f, 0.7f);
g.setColor(c);
g.fillRoundRect(X_OFFSET + 5, Y_OFFSET + 5, ITEM_WIDTH * 3,
ITEM_WIDTH / 3, ITEM_WIDTH / 9, ITEM_WIDTH / 9);
g.setColor(Color.darkGray);
g.setFont(new Font(g.getFont().getName(), Font.PLAIN, ITEM_WIDTH / 5));
g.drawString(message, X_OFFSET + 2 + 5, Y_OFFSET + ITEM_WIDTH * 1
/ 4 + 5);
}
private void paintMenu(Graphics g) {
Color c = new Color(0f, 0f, 0f, 0.5f);
g.setColor(c);
int menu_width = 3 * ITEM_WIDTH;
int menu_height = (actions.size()) * ITEM_WIDTH / 2 + ITEM_WIDTH;
int x_offset = X_OFFSET + 7 * ITEM_WIDTH - ITEM_WIDTH / 5;
;
int y_offset = Y_OFFSET + ITEM_WIDTH * height - menu_height - 12;
g.fillRoundRect(x_offset, y_offset, menu_width, menu_height,
ITEM_WIDTH / 3, ITEM_WIDTH / 3);
g.fillRect(x_offset, y_offset + ITEM_WIDTH * (1 + actionIndex) / 2,
menu_width, ITEM_WIDTH / 2);
g.setColor(Color.lightGray);
g.setFont(new Font(g.getFont().getName(), Font.PLAIN, ITEM_WIDTH / 3));
Iterator<SanguoAction> itr = actions.iterator();
int index = 0;
while (itr.hasNext()) {
if (index == 0) {
g.drawLine(x_offset + 5, y_offset + ITEM_WIDTH
* (1 + index) / 2, x_offset + menu_width - 5,
y_offset + ITEM_WIDTH * (1 + index) / 2);
}
SanguoAction action = itr.next();
if (!action.isEnable()) {
g.setColor(Color.gray);
}
if (action.hasSubAction()) {
g.drawString(">", x_offset + menu_width - ITEM_WIDTH / 3,
y_offset + ITEM_WIDTH * (2 + index) / 2
- ITEM_WIDTH / 10);
}
g.drawString(action.getName(), x_offset + 10, y_offset
+ ITEM_WIDTH * (2 + index) / 2 - ITEM_WIDTH / 10);
g.setColor(Color.lightGray);
g.drawLine(x_offset + 5, y_offset + ITEM_WIDTH * (2 + index)
/ 2, x_offset + menu_width - 5, y_offset + ITEM_WIDTH
* (2 + index) / 2);
index++;
}
}
public void setMessage(String s) {
this.message = s;
}
}
public Position getCursorPosition() {
return cursorPosition;
}
public void setCursorPosition(Position cursorPosition) {
this.cursorPosition = cursorPosition;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -