
Simple demo of Ranaship Game, only for education purposes.
This blog shows many examples with code source of several techniques to develop video games and apps for mobile devices like iPhone, iPad, Android, Blackberry using different programming lenguajes as: Java, J2ME, C++, C# and Objective-C. Enjoy and comment! The examples are not tutorials, but shows specific issues and topis related with programming in these lenguajes.
package mx.gcross.samples;Test the code and comment, thanks!.
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Image;
/**
*
* @author René Cruz - G-Cross Studio 2011
*/
public class MoveImage extends Applet {
private Image dobleBuffer;
private Graphics myGraphics;
private Image background;
private Image nave;
int cordX;
int cordY;
@Override
public void init() {
background = getImage(getDocumentBase(), "background.jpg");
nave = getImage(getDocumentBase(), "nave.png");
// Init first coordinates
cordX = (int) (Math.random() * getWidth());
cordY = getHeight();
// Init doble buffer
dobleBuffer = createImage(getWidth(), getHeight());
myGraphics = dobleBuffer.getGraphics();
//Draw images, only the first time
drawImages();
animateShip();
}
@Override
public void update(Graphics g) {
//Draw images in the new positions
drawImages();
//Draw the buffer in the screen
g.drawImage(dobleBuffer, 0, 0, this);
}
public void animateShip() {
Thread hilo = new Thread() {
@Override
public void run() {
try {
super.run();
while (true) {
// change position of Y, and validate if need calculate new coordinates of ship...
cordY--;
if (cordY < -30) {
cordX = (int) (Math.random() * getWidth());
cordY = getHeight();
}
repaint();
// make a pause for 10 milliseconds
sleep(10);
}
} catch (InterruptedException ex) {
// do something...
}
}
};
hilo.start();
}
public void drawImages() {
myGraphics.drawImage(background, 0, 0, getWidth(), getHeight(), this);
myGraphics.drawImage(nave, cordX, cordY, this);
}
}
package mx.camobile.bluetooth;
import emo.comm.connector.bluetooth.server.BTMultiServer;
import emo.comm.message.common.IMsgProcessor;
import emo.comm.message.common.Message;
import emo.comm.message.common.chat.ChatRoster;
/**
*
* @author René Cruz - G-Cross Studio 2011
*/
public class MyBTServer {
//Bluetooth Server Object
private final BTMultiServer btServer;
public MyBTServer(String nickName, IMsgProcessor processor) {
/* Create an instance of Bluetooth MultiServer
Parameters:
1) The UUID (0x11111) for the published bluetooth service (it will be the same in the mobile app)
2) One MsgProcessor (in this case, the same class process all incomming messages)
3) One Roster instance, the Mobile API includes a simple ChatRoster class.
4) The mode of messages propagation, in this case the sender of messages
* is excludes of propagation to avoid repeated messages.
5) Optional, the name of this server. If not specifies a nickname, the bluetooth
* device ID will be used like nickName.
*/
btServer = new BTMultiServer(0x11111, processor, new ChatRoster(),
BTMultiServer.EXCLUDE_SENDER_BROADCAST, nickName);
}
public void startServer() {
//Start Bluetooth Server
btServer.startServer();
}
public void sendMessage(Message msg) {
//Send one message to all connected peers.
btServer.sendMessage(msg);
}
}
package mx.camobile.gui;
import emo.comm.message.common.IMsgProcessor;
import emo.comm.message.common.Message;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import mx.camobile.bluetooth.MyBTServer;
/**
*
* @author René Cruz - G-Cross Studio 2011
*/
public class SimpleGUI extends JFrame implements ActionListener, IMsgProcessor {
private JTextField txtNickName;
private JTextField txtMessage;
private JButton btnConnect;
private JButton btnSend;
private JTextPane txtHistory;
private JScrollPane panel;
private JPanel panelNorth;
private JPanel panelSouth;
private MyBTServer btServer;
public SimpleGUI() {
setSize(400, 200);
setLayout(new BorderLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panelNorth = new JPanel();
panelNorth.add(new JLabel("Nickname:"));
txtNickName = new JTextField(20);
panelNorth.add(txtNickName);
btnConnect = new JButton("Connect");
btnConnect.addActionListener(this);
panelNorth.add(btnConnect);
add(panelNorth, BorderLayout.NORTH);
txtHistory = new JTextPane();
panel = new JScrollPane();
panel.setViewportView(txtHistory);
add(panel, BorderLayout.CENTER);
panelSouth = new JPanel();
panelSouth.add(new JLabel("Message:"));
txtMessage = new JTextField(20);
panelSouth.add(txtMessage);
btnSend = new JButton("Send");
btnSend.addActionListener(this);
panelSouth.add(btnSend);
add(panelSouth, BorderLayout.SOUTH);
setVisible(true);
}
public static void main(String[] args) {
SimpleGUI simpleGUI = new SimpleGUI();
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btnConnect) {
//Create a instance of BTServer controller
btServer = new MyBTServer(txtNickName.getText(), this);
//Start BTServer
btServer.startServer();
btnConnect.setEnabled(false);
txtHistory.setText("Server started\n");
} else if (e.getSource() == btnSend) {
/*
* Create an instance of Message class, to send it over BluetoothServer
* Parameters:
* 1) Type of message (LOGIN_MESSAGE, LOGOUT_MESSAGE, NORMAL_MESSAGE, ROSTER_MESSATE, SYSTEM_MESSAGE)
* 2) NickName of sender
* 3) Body or content of Message
*/
Message message = new Message(Message.NORMAL_MESSAGE, txtNickName.getText(), txtMessage.getText());
//Send message over bluetooth server
btServer.sendMessage(message);
txtMessage.setText("");
}
}
public void processMsg(String string) {
// not used in this example...
}
public void processMsg(Message msg) {
// Extract the Type of Message
switch (msg.getType()) {
case Message.LOGIN_MESSAGE: {
txtHistory.setText(txtHistory.getText() + msg.getSender() + " is connected\n");
}
break;
case Message.LOGOUT_MESSAGE: {
txtHistory.setText(txtHistory.getText() + msg.getSender() + " was disconnected\n");
}
break;
case Message.NORMAL_MESSAGE: {
txtHistory.setText(txtHistory.getText() + msg.getSender() + ": " + msg.getMsgBody() + "\n");
}
break;
}
}
public void processMsg(int i) {
// not used in this example...
}
}
package mx.gcross.samples;
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
/**
*
* @author René Cruz - G-Cross Studio 2011
*/
public class DobleBufferApplet extends Applet implements KeyListener {
//Empty image for doblebuffer
private Image dobleBuffer;
private Graphics myGraphics;
private Image background;
private Image bird;
int cordX = 20;
int cordY = 30;
public void init() {
background = getImage(getDocumentBase(), "background.jpg");
bird = getImage(getDocumentBase(), "bird.png");
addKeyListener(this);
// Init doble buffer
dobleBuffer = createImage(getWidth(), getHeight());
myGraphics = dobleBuffer.getGraphics();
//Draw images, only the first time
drawImages();
}
@Override
public void update(Graphics g) {
//Draw images in the new positions
drawImages();
//Draw the buffer in the screen
g.drawImage(dobleBuffer, 0, 0, this);
}
public void drawImages() {
myGraphics.drawImage(background, 0, 0, getWidth(), getHeight(), this);
myGraphics.drawImage(bird, cordX, cordY, this);
}
//While a key is pressed
public void keyPressed(KeyEvent ke) {
switch (ke.getKeyCode()) {
//if the right arrow in keyboard is pressed...
case KeyEvent.VK_RIGHT: {
cordX +=3;
}
break;
//if the left arrow in keyboard is pressed...
case KeyEvent.VK_LEFT: {
cordX -=3;
}
break;
//if the down arrow in keyboard is pressed...
case KeyEvent.VK_DOWN: {
cordY +=3;
}
break;
//if the up arrow in keyboard is pressed...
case KeyEvent.VK_UP: {
cordY -=3;
}
break;
}
//call explicit to paint
repaint();
}
//When a key is typed (once)
public void keyTyped(KeyEvent ke) {
}
//When a key is released (typed or pressed)
public void keyReleased(KeyEvent ke) {
}
}
package mx.gcross.samples;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
/**
*
* @author René Cruz - G-Cross Studio 2011
*/
public class DobleBufferJFrame extends JFrame implements KeyListener {
//Objects for images
private BufferedImage background;
private BufferedImage bird;
//First coordinates of bird image
private int cordX = 50;
private int cordY = 250;
//Objects for doble buffer
private BufferStrategy myBuffer;
public DobleBufferJFrame() {
setTitle("Doble Buffer Image Sample");
//set window dimension 480x320px
setSize(512, 512);
//load images...
loadImages();
//make window visible
setVisible(true);
//Ignore OS paint calls
setIgnoreRepaint(true);
//create a doble buffer objects
//NOTE: It's very important to create bufferStrategy after
//JFrame is visible, in other case fail!
this.createBufferStrategy(2);
myBuffer = this.getBufferStrategy();
//call explicit to paint first time
repaint();
}
public void loadImages() {
try {
//path for image file
String pathBackground = "images/background.jpg";
background = ImageIO.read(new File(pathBackground));
String pathBird = "images/bird.png";
bird = ImageIO.read(new File(pathBird));
} catch (IOException ex) {
ex.printStackTrace();
}
//asociate the keyboard listener with this JFrame
addKeyListener(this);
}
public void drawImages(Graphics2D g) {
//draw brackground image (first image)
g.drawImage(background, 0, 0, this);
//draw bird image (second image, in this order)
g.drawImage(bird, cordX, cordY, this);
}
public void paint(Graphics g) {
Graphics2D g2 = null;
if (myBuffer != null) {
try {
//get the graphics2d object of the buffer
g2 = (Graphics2D) myBuffer.getDrawGraphics();
//draw images in buffer
drawImages(g2);
} finally {
g2.dispose();
}
//draw the content of buffer in the screen
myBuffer.show();
}
}
public static void main(String[] args) {
new DobleBufferJFrame();
}
//While a key is pressed
public void keyPressed(KeyEvent ke) {
switch (ke.getKeyCode()) {
//if the right arrow in keyboard is pressed...
case KeyEvent.VK_RIGHT: {
cordX ++;
}
break;
//if the left arrow in keyboard is pressed...
case KeyEvent.VK_LEFT: {
cordX --;
}
break;
//if the down arrow in keyboard is pressed...
case KeyEvent.VK_DOWN: {
cordY ++;
}
break;
//if the up arrow in keyboard is pressed...
case KeyEvent.VK_UP: {
cordY --;
}
break;
}
//call explicit to paint
repaint();
}
//When a key is typed (once)
public void keyTyped(KeyEvent ke) {
}
//When a key is released (typed or pressed)
public void keyReleased(KeyEvent ke) {
}
}
package mx.gcross.samples;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
/**
*
* @author René Cruz - G-Cross Studio 2011
*/
public class MoveImage extends JFrame implements KeyListener {
//Objects for images
private BufferedImage background;
private BufferedImage bird;
//First coordinates of bird image
private int cordX = 50;
private int cordY = 250;
public MoveImage() {
setTitle("Move Image Sample");
setSize(512, 512); //set window dimension 480x320px
loadImages();
setVisible(true); //make window visible
}
public void loadImages() {
try {
//path for image file
String pathBackground = "images/background.jpg";
background = ImageIO.read(new File(pathBackground));
String pathBird = "images/bird.png";
bird = ImageIO.read(new File(pathBird));
} catch (IOException ex) {
ex.printStackTrace();
}
//asociate the keyboard listener with this JFrame
addKeyListener(this);
}
@Override
public void paint(Graphics g) {
super.paint(g);
//draw brackground image (first image)
g.drawImage(background, 0, 0, this);
//draw bird image (second image, in this order)
g.drawImage(bird, cordX, cordY, this);
}
public static void main(String[] args) {
new MoveImage();
}
//While a key is pressed
public void keyPressed(KeyEvent ke) {
switch (ke.getKeyCode()) {
//if the right arrow in keyboard is pressed...
case KeyEvent.VK_RIGHT: {
cordX+=3;
}
break;
//if the left arrow in keyboard is pressed...
case KeyEvent.VK_LEFT: {
cordX-=3;
}
break;
//if the down arrow in keyboard is pressed...
case KeyEvent.VK_DOWN: {
cordY+=3;
}
break;
//if the up arrow in keyboard is pressed...
case KeyEvent.VK_UP: {
cordY-=3;
}
break;
}
repaint();
}
//When a key is typed (once)
public void keyTyped(KeyEvent ke) {}
//When a key is released (typed or pressed)
public void keyReleased(KeyEvent ke) {}
}
package mx.gcross.samples;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
/**
*
* @author Rene Cruz - G-Cross Studio 2011
*/
public class MyWindow extends JFrame {
//Object for image
private BufferedImage background;
public MyWindow() {
setSize(480, 320); //set window dimension 480x320px
loadImages();
setVisible(true); //make window visible
}
public void loadImages() {
try {
//path for image file
String path = "images/background.png";
background = ImageIO.read(new File(path));
} catch (IOException ex) {
ex.printStackTrace();
}
}
@Override
public void paint(Graphics g) {
super.paint(g);
//draw the bufferedImage object
g.drawImage(background, 0, 0, this);
}
public static void main(String[] args) {
new MyWindow();
}
}
public class Hello {
public static void main(String args[]) {
System.out.println("Hello Blog World!");
}
}