So far on my demo you can walk around (no collision detection yet) and type a message. When I was walking around I noticed a problem. Every once in a while the character sprite will lag/jump, and im not sure if this is due to the fact that I don't double buffer, or my code is laggy. Here is all of my code.
gameMain.class
import javax.swing.JFrame; public class gameMain { public static void main(String[] args){ chatBox chatBox = new chatBox(); keyBoard kb = new keyBoard(); gui gui = new gui(); JFrame f = new JFrame("BrettScape"); f.setSize(646,500); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setResizable(false); f.add(gui); f.addKeyListener(kb); f.setFocusTraversalKeysEnabled(false); f.setVisible(true); while(true) { gui.updateScreen(chatBox.getMessages(), kb.getMessage(), kb.getX(), kb.getY()); } } }
chatBox.class
public class chatBox { String messageBox[]={"Welcome to BrettScape!","","","",""}; public String[] getMessages(){ return messageBox; } public void addMessage(String newMess){ messageBox[4] = messageBox[3]; messageBox[3] = messageBox[2]; messageBox[2] = messageBox[1]; messageBox[1] = messageBox[0]; messageBox[0] = newMess; } }
gui.class
import java.awt.*; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import javax.swing.*; public class gui extends JPanel { BufferedImage img,chatBoxImg,avatar; String messages[]; String typing; int map[][]={ //I know this map is in my gui class, but I'm going to add a different class for maps. {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0} }; int messCount,row,column,playerX,playerY; public gui(){ try { img = ImageIO.read(new File("tiles.png")); chatBoxImg = ImageIO.read(new File("chat.png")); avatar = ImageIO.read(new File("avatar.png")); } catch (IOException e) { e.printStackTrace(); } } public void updateScreen(String[] messages, String typing, int playerX, int playerY){ this.messages = messages; this.typing = typing; this.playerX = playerX; this.playerY = playerY; repaint(); } public void paintComponent(Graphics g){ //draw first tile layer for(column=0;column<=11;column++) { for(row=0;row<=19;row++){ g.drawImage(img, row*32, column*32, row*32+32, column*32+32, map[column][row]*32, 0, map[column][row]*32+32, 32, null); } } //second layer g.drawImage(avatar, playerX, playerY, null); //third layer //draws messages for chat box g.drawImage(chatBoxImg, 0, 384, null); for(messCount=0;messCount<=4;messCount++){ g.drawString(messages[messCount], 5, 400+(messCount*14)); } //draw what the person is typing g.drawString(typing+"*", 5, 468); } }
keyBoard.class
import java.awt.event.*; public class keyBoard implements KeyListener { int keyCode; int playerX,playerY=10; String message=""; public int getX(){ return playerX; } public int getY(){ return playerY; } public String getMessage(){ return message; } public void keyPressed(KeyEvent e){ keyCode = e.getKeyCode(); if (keyCode == KeyEvent.VK_LEFT) { playerX-=5; } if (keyCode == KeyEvent.VK_RIGHT) { playerX+=5; } if (keyCode == KeyEvent.VK_UP) { playerY-=5; } if (keyCode == KeyEvent.VK_DOWN) { playerY+=5; } //Detection of normal keys if ("ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*(),PeriodComma/?;:`~[]{}=+<>Space".indexOf(KeyEvent.getKeyText(keyCode)) >= 0) { message+=KeyEvent.getKeyText(keyCode).replace("Space", " ").replace("Period", ".").replace("Comma", ",").toLowerCase(); //capitalize the first letter if (message.length() == 1) message = message.toUpperCase(); } //if backspace delete last letter if (KeyEvent.getKeyText(keyCode) == "Backspace") { if(message.length() >= 1) message = message.substring(0, message.length()-1); } } public void keyReleased(KeyEvent e){ } public void keyTyped(KeyEvent e){ } }