|
/*
DEVELOPING GAME IN JAVA
Caracteristiques
Editeur : NEW RIDERS
Auteur : BRACKEEN
Parution : 09 2003
Pages : 972
Isbn : 1-59273-005-1
Reliure : Paperback
Disponibilite : Disponible a la librairie
*/
import java.awt.AWTException;
import java.awt.AlphaComposite;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Composite;
import java.awt.Container;
import java.awt.Cursor;
import java.awt.DisplayMode;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Point;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.Transparency;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.RepaintManager;
import javax.swing.SwingUtilities;
import javax.swing.border.Border;
/**
* Extends the InputManagerTest demo and adds Swing buttons for pause, config
* and quit.
*/
/**
* The KeyConfigTest class extends the MenuTest demo to add a dialog to
* configure the keyboard keys.
*/
public class KeyConfigTest extends MenuTest {
public static void main(String[] args) {
new KeyConfigTest().run();
}
private static final String INSTRUCTIONS = "<html>Click an action's input box to change it's keys."
+ "<br>An action can have at most three keys associated "
+ "with it.<br>Press Backspace to clear an action's keys.";
private JPanel dialog;
private JButton okButton;
private List inputs;
public void init() {
super.init();
inputs = new ArrayList();
// create the list of GameActions and mapped keys
JPanel configPanel = new JPanel(new GridLayout(5, 2, 2, 2));
addActionConfig(configPanel, moveLeft);
addActionConfig(configPanel, moveRight);
addActionConfig(configPanel, jump);
addActionConfig(configPanel, pause);
addActionConfig(configPanel, exit);
// create the panel containing the OK button
JPanel bottomPanel = new JPanel(new FlowLayout());
okButton = new JButton("OK");
okButton.setFocusable(false);
okButton.addActionListener(this);
bottomPanel.add(okButton);
// create the panel containing the instructions.
JPanel topPanel = new JPanel(new FlowLayout());
topPanel.add(new JLabel(INSTRUCTIONS));
// create the dialog border
Border border = BorderFactory.createLineBorder(Color.black);
// create the config dialog.
dialog = new JPanel(new BorderLayout());
dialog.add(topPanel, BorderLayout.NORTH);
dialog.add(configPanel, BorderLayout.CENTER);
dialog.add(bottomPanel, BorderLayout.SOUTH);
dialog.setBorder(border);
dialog.setVisible(false);
dialog.setSize(dialog.getPreferredSize());
// center the dialog
dialog.setLocation((screen.getWidth() - dialog.getWidth()) / 2, (screen
.getHeight() - dialog.getHeight()) / 2);
// add the dialog to the "modal dialog" layer of the
// screen's layered pane.
screen.getFullScreenWindow().getLayeredPane().add(dialog,
JLayeredPane.MODAL_LAYER);
}
/**
* Adds a label containing the name of the GameAction and an InputComponent
* used for changing the mapped keys.
*/
private void addActionConfig(JPanel configPanel, GameAction action) {
JLabel label = new JLabel(action.getName(), JLabel.RIGHT);
InputComponent input = new InputComponent(action);
configPanel.add(label);
configPanel.add(input);
inputs.add(input);
}
public void actionPerformed(ActionEvent e) {
super.actionPerformed(e);
if (e.getSource() == okButton) {
// hides the config dialog
configAction.tap();
}
}
public void checkSystemInput() {
super.checkSystemInput();
if (configAction.isPressed()) {
// hide or show the config dialog
boolean show = !dialog.isVisible();
dialog.setVisible(show);
setPaused(show);
}
}
/**
* Resets the text displayed in each InputComponent, which is the names of
* the mapped keys.
*/
private void resetInputs() {
for (int i = 0; i < inputs.size(); i++) {
((InputComponent) inputs.get(i)).setText();
}
}
/**
* The InputComponent class displays the keys mapped to a particular action
* and allows the user to change the mapped keys. The user selects an
* InputComponent by clicking it, then can press any key or mouse button
* (including the mouse wheel) to change the mapped value.
*/
class InputComponent extends JTextField {
private GameAction action;
/**
* Creates a new InputComponent for the specified GameAction.
*/
public InputComponent(GameAction action) {
this.action = action;
setText();
enableEvents(KeyEvent.KEY_EVENT_MASK | MouseEvent.MOUSE_EVENT_MASK
| MouseEvent.MOUSE_MOTION_EVENT_MASK
| MouseEvent.MOUSE_WHEEL_EVENT_MASK);
}
/**
* Sets the displayed text of this InputComponent to the names of the
* mapped keys.
*/
private void setText() {
String text = "";
List list = inputManager.getMaps(action);
if (list.size() > 0) {
for (int i = 0; i < list.size(); i++) {
text += (String) list.get(i) + ", ";
}
// remove the last comma
text = text.substring(0, text.length() - 2);
}
// make sure we don't get deadlock
synchronized (getTreeLock()) {
setText(text);
}
}
/**
* Maps the GameAction for this InputComponent to the specified key or
* mouse action.
*/
private void mapGameAction(int code, boolean isMouseMap) {
if (inputManager.getMaps(action).size() >= 3) {
inputManager.clearMap(action);
}
if (isMouseMap) {
inputManager.mapToMouse(action, code);
} else {
inputManager.mapToKey(action, code);
}
resetInputs();
screen.getFullScreenWindow().requestFocus();
}
// alternative way to intercept key events
protected void processKeyEvent(KeyEvent e) {
if (e.getID() == e.KEY_PRESSED) {
// if backspace is pressed, clear the map
if (e.getKeyCode() == KeyEvent.VK_BACK_SPACE
&& inputManager.getMaps(action).size() > 0) {
inputManager.clearMap(action);
setText("");
screen.getFullScreenWindow().requestFocus();
} else {
mapGameAction(e.getKeyCode(), false);
}
}
e.consume();
}
// alternative way to intercept mouse events
protected void processMouseEvent(MouseEvent e) {
if (e.getID() == e.MOUSE_PRESSED) {
if (hasFocus()) {
int code = InputManager.getMouseButtonCode(e);
mapGameAction(code, true);
} else {
requestFocus();
}
}
e.consume();
}
// alternative way to intercept mouse events
protected void processMouseMotionEvent(MouseEvent e) {
e.consume();
}
// alternative way to intercept mouse events
protected void processMouseWheelEvent(MouseWheelEvent e) {
if (hasFocus()) {
int code = InputManager.MOUSE_WHEEL_DOWN;
if (e.getWheelRotation() < 0) {
code = InputManager.MOUSE_WHEEL_UP;
}
mapGameAction(code, true);
}
e.consume();
}
}
}
/**
* Extends the InputManagerTest demo and adds Swing buttons for pause, config
* and quit.
*/
class MenuTest extends InputManagerTest implements ActionListener {
public static void main(String[] args) {
new MenuTest().run();
}
protected GameAction configAction;
private JButton playButton;
private JButton configButton;
private JButton quitButton;
private JButton pauseButton;
private JPanel playButtonSpace;
public void init() {
super.init();
// make sure Swing components don't paint themselves
NullRepaintManager.install();
// create an addtional GameAction for "config"
configAction = new GameAction("config");
// create buttons
quitButton = createButton("quit", "Quit");
playButton = createButton("play", "Continue");
pauseButton = createButton("pause", "Pause");
configButton = createButton("config", "Change Settings");
// create the space where the play/pause buttons go.
playButtonSpace = new JPanel();
playButtonSpace.setOpaque(false);
playButtonSpace.add(pauseButton);
JFrame frame = super.screen.getFullScreenWindow();
Container contentPane = frame.getContentPane();
// make sure the content pane is transparent
if (contentPane instanceof JComponent) {
((JComponent) contentPane).setOpaque(false);
}
// add components to the screen's content pane
contentPane.setLayout(new FlowLayout(FlowLayout.LEFT));
contentPane.add(playButtonSpace);
contentPane.add(configButton);
contentPane.add(quitButton);
// explicitly layout components (needed on some systems)
frame.validate();
}
/**
* Extends InputManagerTest's functionality to draw all Swing components.
*/
public void draw(Graphics2D g) {
super.draw(g);
JFrame frame = super.screen.getFullScreenWindow();
// the layered pane contains things like popups (tooltips,
// popup menus) and the content pane.
frame.getLayeredPane().paintComponents(g);
}
/**
* Changes the pause/play button whenever the pause state changes.
*/
public void setPaused(boolean p) {
super.setPaused(p);
playButtonSpace.removeAll();
if (isPaused()) {
playButtonSpace.add(playButton);
} else {
playButtonSpace.add(pauseButton);
}
}
/**
* Called by the AWT event dispatch thread when a button is pressed.
*/
public void actionPerformed(ActionEvent e) {
Object src = e.getSource();
if (src == quitButton) {
// fire the "exit" gameAction
super.exit.tap();
} else if (src == configButton) {
// doesn't do anything (for now)
configAction.tap();
} else if (src == playButton || src == pauseButton) {
// fire the "pause" gameAction
super.pause.tap();
}
}
/**
* Creates a Swing JButton. The image used for the button is located at
* "../images/menu/" + name + ".png". The image is modified to create a
* "default" look (translucent) and a "pressed" look (moved down and to the
* right).
* <p>
* The button doesn't use Swing's look-and-feel and instead just uses the
* image.
*/
public JButton createButton(String name, String toolTip) {
// load the image
String imagePath = "../images/menu/" + name + ".png";
ImageIcon iconRollover = new ImageIcon(imagePath);
int w = iconRollover.getIconWidth();
int h = iconRollover.getIconHeight();
// get the cursor for this button
Cursor cursor = Cursor.getPredefinedCursor(Cursor.HAND_CURSOR);
// make translucent default image
Image image = screen.createCompatibleImage(w, h,
Transparency.TRANSLUCENT);
Graphics2D g = (Graphics2D) image.getGraphics();
Composite alpha = AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
.5f);
g.setComposite(alpha);
g.drawImage(iconRollover.getImage(), 0, 0, null);
g.dispose();
ImageIcon iconDefault = new ImageIcon(image);
// make a pressed iamge
image = screen.createCompatibleImage(w, h, Transparency.TRANSLUCENT);
g = (Graphics2D) image.getGraphics();
g.drawImage(iconRollover.getImage(), 2, 2, null);
g.dispose();
ImageIcon iconPressed = new ImageIcon(image);
// create the button
JButton button = new JButton();
button.addActionListener(this);
button.setIgnoreRepaint(true);
button.setFocusable(false);
button.setToolTipText(toolTip);
button.setBorder(null);
button.setContentAreaFilled(false);
button.setCursor(cursor);
button.setIcon(iconDefault);
button.setRolloverIcon(iconRollover);
button.setPressedIcon(iconPressed);
return button;
}
}
/**
* InputManagerTest tests the InputManager with a simple run-and-jump mechanism.
* The player moves and jumps using the arrow keys and the space bar.
* <p>
* Also, InputManagerTest demonstrates pausing a game by not updating the game
* elements if the game is paused.
*/
class InputManagerTest extends GameCore {
public static void main(String[] args) {
new InputManagerTest().run();
}
protected GameAction jump;
protected GameAction exit;
protected GameAction moveLeft;
protected GameAction moveRight;
protected GameAction pause;
protected InputManager inputManager;
private Player player;
private Image bgImage;
private boolean paused;
public void init() {
super.init();
Window window = screen.getFullScreenWindow();
inputManager = new InputManager(window);
// use these lines for relative mouse mode
//inputManager.setRelativeMouseMode(true);
//inputManager.setCursor(InputManager.INVISIBLE_CURSOR);
createGameActions();
createSprite();
paused = false;
}
/**
* Tests whether the game is paused or not.
*/
public boolean isPaused() {
return paused;
}
/**
* Sets the paused state.
*/
public void setPaused(boolean p) {
if (paused != p) {
this.paused = p;
inputManager.resetAllGameActions();
}
}
public void update(long elapsedTime) {
// check input that can happen whether paused or not
checkSystemInput();
if (!isPaused()) {
// check game input
checkGameInput();
// update sprite
player.update(elapsedTime);
}
}
/**
* Checks input from GameActions that can be pressed regardless of whether
* the game is paused or not.
*/
public void checkSystemInput() {
if (pause.isPressed()) {
setPaused(!isPaused());
}
if (exit.isPressed()) {
stop();
}
}
/**
* Checks input from GameActions that can be pressed only when the game is
* not paused.
*/
public void checkGameInput() {
float velocityX = 0;
if (moveLeft.isPressed()) {
velocityX -= Player.SPEED;
}
if (moveRight.isPressed()) {
velocityX += Player.SPEED;
}
player.setVelocityX(velocityX);
if (jump.isPressed() && player.getState() != Player.STATE_JUMPING) {
player.jump();
}
}
public void draw(Graphics2D g) {
// draw background
g.drawImage(bgImage, 0, 0, null);
// draw sprite
g.drawImage(player.getImage(), Math.round(player.getX()), Math
.round(player.getY()), null);
}
/**
* Creates GameActions and maps them to keys.
*/
public void createGameActions() {
jump = new GameAction("jump", GameAction.DETECT_INITAL_PRESS_ONLY);
exit = new GameAction("exit", GameAction.DETECT_INITAL_PRESS_ONLY);
moveLeft = new GameAction("moveLeft");
moveRight = new GameAction("moveRight");
pause = new GameAction("pause", GameAction.DETECT_INITAL_PRESS_ONLY);
inputManager.mapToKey(exit, KeyEvent.VK_ESCAPE);
inputManager.mapToKey(pause, KeyEvent.VK_P);
// jump with spacebar or mouse button
inputManager.mapToKey(jump, KeyEvent.VK_SPACE);
inputManager.mapToMouse(jump, InputManager.MOUSE_BUTTON_1);
// move with the arrow keys...
inputManager.mapToKey(moveLeft, KeyEvent.VK_LEFT);
inputManager.mapToKey(moveRight, KeyEvent.VK_RIGHT);
// ... or with A and D.
inputManager.mapToKey(moveLeft, KeyEvent.VK_A);
inputManager.mapToKey(moveRight, KeyEvent.VK_D);
// use these lines to map player movement to the mouse
//inputManager.mapToMouse(moveLeft,
// InputManager.MOUSE_MOVE_LEFT);
//inputManager.mapToMouse(moveRight,
// InputManager.MOUSE_MOVE_RIGHT);
}
/**
* Load images and creates the Player sprite.
*/
private void createSprite() {
// load images
bgImage = loadImage("../images/background.jpg");
Image player1 = loadImage("../images/player1.png");
Image player2 = loadImage("../images/player2.png");
Image player3 = loadImage("../images/player3.png");
// create animation
Animation anim = new Animation();
anim.addFrame(player1, 250);
anim.addFrame(player2, 150);
anim.addFrame(player1, 150);
anim.addFrame(player2, 150);
anim.addFrame(player3, 200);
anim.addFrame(player2, 150);
player = new Player(anim);
player.setFloorY(screen.getHeight() - player.getHeight());
}
}
/**
* The Player extends the Sprite class to add states (STATE_NORMAL or
* STATE_JUMPING) and gravity.
*/
class Player extends Sprite {
public static final int STATE_NORMAL = 0;
public static final int STATE_JUMPING = 1;
public static final float SPEED = .3f;
public static final float GRAVITY = .002f;
private int floorY;
private int state;
public Player(Animation anim) {
super(anim);
state = STATE_NORMAL;
}
/**
* Gets the state of the Player (either STATE_NORMAL or STATE_JUMPING);
*/
public int getState() {
return state;
}
/**
* Sets the state of the Player (either STATE_NORMAL or STATE_JUMPING);
*/
public void setState(int state) {
this.state = state;
}
/**
* Sets the location of "floor", where the Player starts and lands after
* jumping.
*/
public void setFloorY(int floorY) {
this.floorY = floorY;
setY(floorY);
}
/**
* Causes the Player to jump
*/
public void jump() {
setVelocityY(-1);
state = STATE_JUMPING;
}
/**
* Updates the player's positon and animation. Also, sets the Player's state
* to NORMAL if a jumping Player landed on the floor.
*/
public void update(long elapsedTime) {
// set vertical velocity (gravity effect)
if (getState() == STATE_JUMPING) {
setVelocityY(getVelocityY() + GRAVITY * elapsedTime);
}
// move player
super.update(elapsedTime);
// check if player landed on floor
if (getState() == STATE_JUMPING && getY() >= floorY) {
setVelocityY(0);
setY(floorY);
setState(STATE_NORMAL);
}
}
}
/**
* The GameAction class is an abstract to a user-initiated action, like jumping
* or moving. GameActions can be mapped to keys or the mouse with the
* InputManager.
*/
class GameAction {
/**
* Normal behavior. The isPressed() method returns true as long as the key
* is held down.
*/
public static final int NORMAL = 0;
/**
* Initial press behavior. The isPressed() method returns true only after
* the key is first pressed, and not again until the key is released and
* pressed again.
&nb
|