ClubEnsayos.com - Ensayos de Calidad, Tareas y Monografias
Buscar

Codigos Java

ivan25818 de Septiembre de 2012

7.374 Palabras (30 Páginas)615 Visitas

Página 1 de 30

/*

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.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.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.ImageIcon;

import javax.swing.JButton;

import javax.swing.JComponent;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.RepaintManager;

import javax.swing.SwingUtilities;

/**

* Extends the InputManagerTest demo and adds Swing buttons for pause, config

* and quit.

*/

public 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

...

Descargar como (para miembros actualizados) txt (38 Kb)
Leer 29 páginas más »
Disponible sólo en Clubensayos.com