Results 1 to 5 of 5
Thread: Camera System
- 05-01-2012, 02:54 PM #1
Member
- Join Date
- Mar 2012
- Location
- The Netherlands
- Posts
- 10
- Rep Power
- 0
Camera System
Hey guys,
I've made a beginning of a simple game in 2d.
You can connect with a server and then you have to type your username and then you can walk around in the room.
Now my question is:
How do I make a camera system that follows the player but not draw the whole room.
I'll show you the code that I have right now.
Server
DataPackageJava Code:import java.net.InetAddress; import java.net.ServerSocket; import java.net.Socket; import java.util.ArrayList; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import javax.swing.JFrame; import javax.swing.JLabel; public class Server extends JFrame { public static int port = 2406; public static String ip; public static ServerSocket server; public static ArrayList<Socket> list_sockets = new ArrayList<Socket>(); public static ArrayList<DataPackage> list_data = new ArrayList<DataPackage>(); private static Runnable accept = new Runnable() { public void run() { new Thread(send).start(); new Thread(receive).start(); while(true) { try { Socket socket = server.accept(); ObjectInputStream ois = new ObjectInputStream(socket.getInputStream()); String username = (String) ois.readObject(); boolean accept = true; for(int i=0; i < list_data.size(); i++) { if(list_data.get(i).username.toLowerCase().equals(username.toLowerCase())) { accept = false; break; } } ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream()); if(accept) { oos.writeObject("Welcome To This Server..."); list_data.add(new DataPackage()); list_sockets.add(socket); } else { oos.writeObject("Your name is already taken!"); } } catch(Exception ex) { System.err.println("Accept error: 01 \n" + ex); } } } }; private static Runnable send = new Runnable() { public void run() { ObjectOutputStream oos; while(true) { for(int i = 0; i < list_sockets.size(); i++) { try { oos = new ObjectOutputStream(list_sockets.get(i).getOutputStream()); oos.writeObject(list_data); } catch(Exception ex) { System.err.println("Send error 01: \n" + ex); } } } } }; private static Runnable receive = new Runnable() { public void run() { ObjectInputStream ois; while(true) { for(int i=0; i < list_sockets.size(); i++) { try { ois = new ObjectInputStream(list_sockets.get(i).getInputStream()); DataPackage dp = (DataPackage) ois.readObject(); list_data.set(i, dp); } catch(Exception ex) { System.err.println("Receive error 01: \n"+ ex); disconnectClient(i); i--; } } } } }; public static void disconnectClient(int index) { try { list_data.remove(index); list_sockets.remove(index); } catch(Exception ex) { System.err.println("DisconnectClient error 01: \n" + ex); } } public static void main(String[] args) { Server serverFrame = new Server(); serverFrame.setTitle("Server"); serverFrame.setSize(200, 40); serverFrame.setFocusable(true); serverFrame.setVisible(true); serverFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); serverFrame.setLayout(null); JLabel serverStatus = new JLabel(); serverStatus.setBounds(20, 0, 180, 20); serverFrame.add(serverStatus); try { ip = InetAddress.getLocalHost().getHostAddress() + ":" + port; server = new ServerSocket(port, 0, InetAddress.getLocalHost()); serverStatus.setText("Server is running"); new Thread(accept).start(); } catch(Exception ex) { System.err.println("Main error 01: \n"+ ex); serverStatus.setText("Error: " + ex.getMessage()); } } }
ClientJava Code:import java.io.Serializable; public class DataPackage implements Serializable { public int x = 0; public int y = 0; public String username; public String image; }
PlayerJava Code:import java.util.ArrayList; import java.net.*; import java.awt.event.*; import java.awt.image.BufferedImage; import java.awt.*; import javax.swing.*; import java.io.*; public class Client extends JPanel implements KeyListener { public static Socket socket; public String local; public static int port = 2406; public static String ip = ""; public boolean connected = true; public String username = "Jeffrey"; public Player player; public Image tileSet; public int[][] map = { {9, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 10}, {11, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 12}, {11, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 12}, {11, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 12}, {11, 1, 1, 1, 1, 1, 1, 1, 1, 3, 3, 3, 3, 3, 1, 1, 1, 1, 1, 12}, {11, 1, 1, 1, 1, 1, 1, 1, 1, 3, 9, 14, 10, 3, 1, 1, 1, 1, 1, 12}, {11, 1, 1, 1, 1, 1, 1, 1, 1, 3, 11, 6, 12, 3, 1, 1, 1, 1, 1, 12}, {11, 1, 1, 1, 1, 1, 1, 1, 1, 3, 7, 13, 8, 3, 1, 1, 1, 1, 1, 12}, {11, 1, 1, 1, 1, 1, 1, 1, 1, 3, 3, 3, 3, 3, 1, 1, 1, 1, 1, 12}, {11, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 12}, {11, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 12}, {11, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 12}, {11, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 1, 1, 1, 12}, {11, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 12}, {7, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 8} }; public ArrayList<DataPackage> others = new ArrayList<DataPackage>(); public BufferedImage buffer; public static void main(String[] args) { Client client = new Client(); client.setIgnoreRepaint(true); client.setFocusable(true); JFrame frame = new JFrame("Client"); frame.setSize(640, 500); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(client); frame.setVisible(true); } public Client() { this.addKeyListener(this); try { try { local = InetAddress.getLocalHost().getHostAddress() + ":" + port; } catch(Exception ex) { System.err.println("Client error 02: \n"+ ex); } ip = (String) JOptionPane.showInputDialog(null, "IP: ", "Info", JOptionPane.INFORMATION_MESSAGE, null, null, local); port = Integer.parseInt(ip.substring(ip.indexOf(":") + 1)); ip = ip.substring(0, ip.indexOf(":")); socket = new Socket(ip, port); String set_username = System.getProperty("user.name"); set_username = (String) JOptionPane.showInputDialog(null, "Username: ", "Info", JOptionPane.INFORMATION_MESSAGE, null, null, set_username); username = set_username; ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream()); oos.writeObject(username); ObjectInputStream ois = new ObjectInputStream(socket.getInputStream()); String response = (String) ois.readObject(); JOptionPane.showMessageDialog(null, response, "Message", JOptionPane.INFORMATION_MESSAGE); if(response.equals("Your name is already taken!")) { System.exit(0); } player = new Player(0, 0, 12, 20, username); buffer = new BufferedImage(640, 480, BufferedImage.TYPE_INT_RGB); tileSet = new ImageIcon(this.getClass().getResource("/images/standardTile.png")).getImage(); new Thread(send).start(); new Thread(receive).start(); new Thread(game).start(); } catch(Exception ex) { System.err.println("Client error 01: \n"+ ex); } } public void update() { player.move(); } Runnable send = new Runnable() { public void run() { ObjectOutputStream oos; while(connected) { if(socket != null) { try { DataPackage dp = new DataPackage(); dp.x = player.getX(); dp.y = player.getY(); dp.username = username; dp.image = player.image; oos = new ObjectOutputStream(socket.getOutputStream()); oos.writeObject(dp); } catch(Exception ex) { System.err.println("Send error 01: \n"+ ex); } } else { break; } } } }; Runnable receive = new Runnable() { public void run() { ObjectInputStream ois; while(connected) { try { ois = new ObjectInputStream(socket.getInputStream()); ArrayList<DataPackage> list_data = (ArrayList<DataPackage>) ois.readObject(); for(int i = 0; i < list_data.size(); i++) { DataPackage dp = list_data.get(i); if(list_data.size() != others.size()) { if(list_data.size() > others.size()) { others.add(dp); } if(list_data.size() < others.size()) { others.remove(0); } } else { others.set(i, dp); } } } catch(Exception ex) { System.err.println("Receive error 01: \n"+ ex); } } } }; Runnable game = new Runnable() { public void run() { while(connected) { try { update(); drawBuffer(); drawScreen(); Thread.sleep(15); } catch(Exception ex) { System.err.println("Game error 01: \n"+ ex); } } } }; public void drawBuffer() { Graphics2D b = buffer.createGraphics(); b.setColor(Color.WHITE); b.fillRect(0, 0, 640, 480); for (int row=0; row < map.length; row++){ for(int column=0; column < map[row].length; column++){ b.drawImage(tileSet, column*32, row*32, column*32+32, row*32+32, map[row][column]*32, 0, map[row][column]*32+32, 32, null); } } for(int i = 0; i < others.size(); i++) { try { DataPackage dp = others.get(i); Player enemy = new Player(dp.x, dp.y, 12, 20, dp.username); enemy.image = dp.image; if(!dp.username.toLowerCase().equals(username.toLowerCase())) { b.drawImage(enemy.getImage(), enemy.getX(), enemy.getY(), null); b.setColor(Color.BLACK); b.drawString(dp.username, dp.x - 13, dp.y - 20); } } catch(Exception ex) { System.err.println("Paint error 01: \n"+ex); } } b.drawImage(player.getImage(), player.getX(), player.getY(), null); b.setColor(Color.BLACK); b.drawString(username, player.getX() - 13, player.getY() - 20); try { Thread.sleep(1); } catch(Exception ex) { System.err.println("Paint error 02: \n" + ex); } b.dispose(); } public void drawScreen() { Graphics2D g = (Graphics2D)this.getGraphics(); g.drawImage(buffer, 0, 0, this); Toolkit.getDefaultToolkit().sync(); g.dispose(); } @Override public void keyPressed(KeyEvent e) { int key = e.getKeyCode(); if(key == KeyEvent.VK_RIGHT) { player.right = true; } if(key == KeyEvent.VK_LEFT) { player.left = true; } if(key == KeyEvent.VK_UP) { player.up = true; } if(key == KeyEvent.VK_DOWN) { player.down = true; } } @Override public void keyReleased(KeyEvent e) { int key = e.getKeyCode(); if(key == KeyEvent.VK_RIGHT) { player.right = false; player.stop(Player.STOP_RIGHT); } if(key == KeyEvent.VK_LEFT) { player.left = false; player.stop(Player.STOP_LEFT); } if(key == KeyEvent.VK_UP) { player.up = false; player.stop(Player.STOP_UP); } if(key == KeyEvent.VK_DOWN) { player.down = false; player.stop(Player.STOP_DOWN); } } @Override public void keyTyped(KeyEvent e){} }
The images can be found at:Java Code:import java.awt.Image; import java.awt.Rectangle; import javax.swing.ImageIcon; public class Player { public static int STOP_DOWN = 0, STOP_UP = 1, STOP_LEFT = 2, STOP_RIGHT = 3; public int x, y, speed = 2, width, height; public boolean up, down, left, right, collision; public String username, image; public Player(int x, int y, int width, int height, String username) { this.x = x; this.y = y; this.width = width; this.height = height; this.username = username; this.up = false; this.down = false; this.left = false; this.right = false; this.image = "/images/stop_down.gif"; } public int getX() { return this.x; } public void setX(int x) { this.x = x; } public int getY() { return this.y; } public void setY(int y) { this.y = y; } public int getWidth() { return this.width; } public void setWidth(int width) { this.width = width; } public int getHeight() { return this.height; } public void setHeight(int height) { this.height = height; } public Rectangle getBounds() { return new Rectangle(this.x, this.y, this.width, this.height); } public Image getImage() { return new ImageIcon(this.getClass().getResource(this.image)).getImage(); } public void move() { if(this.up) { if(!this.collision) { this.y -= this.speed; this.image = "/images/move_up.gif"; } } if(this.down) { if(!this.collision) { this.y += this.speed; this.image = "/images/move_down.gif"; } } if(this.left) { if(!this.collision) { this.x -= this.speed; this.image = "/images/move_left.gif"; } } if(this.right) { if(!this.collision) { this.x += this.speed; this.image = "/images/move_right.gif"; } } } public void stop(int direction) { switch(direction) { case 0: this.image="/images/stop_down.gif"; break; case 1: this.image="/images/stop_up.gif"; break; case 2: this.image="http://www.java-forums.org/images/stop_left.gif"; break; case 3: this.image="/images/stop_right.gif"; break; default: this.image="/images/stop_down.gif"; break; } } }
http://www.data.kazuhiko.nl/images/stop_down.gif
http://www.data.kazuhiko.nl/images/stop_up.gif
http://www.data.kazuhiko.nl/images/stop_right.gif
http://www.data.kazuhiko.nl/images/stop_left.gif
http://www.data.kazuhiko.nl/images/move_down.gif
http://www.data.kazuhiko.nl/images/move_up.gif
http://www.data.kazuhiko.nl/images/move_left.gif
http://www.data.kazuhiko.nl/images/move_right.gif
http://www.data.kazuhiko.nl/images/standardTile.pngLast edited by kazuhiko; 05-01-2012 at 02:57 PM. Reason: update servercode
- 05-01-2012, 03:02 PM #2
Re: Camera System
If you want help, you'll have to provide an SSCCE that demonstrates the problem in a way that we can simply copy and paste to run. Posting hundreds of lines of code probably won't encouarge many people to help you, for free, in their spare time. Make it easier for people to help you by eliminating any code that doesn't immediately deal with your problem.
But the basic answer is that you're going to have to separate the model coordinate system from the view coordinate system. What you want at 0,0 of the view is not always what's at 0,0 in the model. Draw out some different scenarios and try to establish a pattern or relationship between the two.How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 05-02-2012, 11:54 AM #3
Member
- Join Date
- Mar 2012
- Location
- The Netherlands
- Posts
- 10
- Rep Power
- 0
Re: Camera System
I've got it kinda working now
Java Code:int cameraWidth = screenWidth / 32; int cameraHeight = screenHeight /32; int playerY = player.getY() / 32; int playerX = player.getX() / 32; for(int row =0; row < cameraHeight && row + playerY < map.length; row++) { for(int column = 0; column < cameraWidth && column+playerX < map[row].length; column++) { b.drawImage(tileSet, column *32, row*32, column*32+32, row*32+32, map[playerY + row][playerX + column]*32, 0, map[playerY+row][playerX+column]*32+32, 32, null); } }
- 05-02-2012, 02:01 PM #4
Re: Camera System
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 05-02-2012, 02:03 PM #5
Member
- Join Date
- Mar 2012
- Location
- The Netherlands
- Posts
- 10
- Rep Power
- 0
Similar Threads
-
Capture Video from ip camera
By nesrine18 in forum Advanced JavaReplies: 3Last Post: 03-12-2012, 08:08 PM -
IP Camera
By BMWTouring in forum Advanced JavaReplies: 0Last Post: 08-28-2011, 05:03 PM -
get Hardware camera sound
By ilyas in forum Advanced JavaReplies: 0Last Post: 12-10-2010, 07:53 AM -
camera pic
By Aveekdtt in forum CLDC and MIDPReplies: 0Last Post: 01-10-2010, 12:52 AM -
Loading a camera
By mrvigneshmca in forum Java AppletsReplies: 0Last Post: 03-18-2009, 10:46 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks