Solved

Java Client Applet - jList woes

Posted on 2010-08-15
24
318 Views
Last Modified: 2012-05-10
OK so I tried change List to JList and let me tell you... it's annoying.

It compiles fine but the list won't show.... also the Flail won't be added to the inventory even after I pick it up..

Here is what I added:
public static DefaultListModel model = new DefaultListModel();

Open in new window

list = new JList(model);
		int pos = list.getModel().getSize();
		model.add(pos, "Flail");
		arr.add("Flail");

Open in new window


and here is my new jaddInv method
	public void addInv(int e) {

		String iname = getItem(e)[0];

		String idesc = getItem(e)[1];
		int itypeOf = Integer.parseInt(getItem(e)[2]);
		int iattackAdd = Integer.parseInt(getItem(e)[3]);
		int idefenseAdd = Integer.parseInt(getItem(e)[4]);
		boolean icanSell = Boolean.parseBoolean(getItem(e)[5]);
		boolean icanEat = Boolean.parseBoolean(getItem(e)[6]);
		int iearnedCoins = Integer.parseInt(getItem(e)[7]);

		c.append("\nYou picked up a " + iname + ".");
		// for (String item : arr) list.add(item);
		int posx = list.getModel().getSize();
		model.add(posx, iname);
		arr.add(iname);
	}

Open in new window


What am I doing wrong?

I will let you know that is IS adding to the Arr ArrayList... so that works... I don't know about the jList

/**
Mystik Client
Programer: Dan J.
Many thanks to objects & CEHJ
Started May 23, 2010
(c) 2010
**/

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;

import java.awt.TextArea;
import java.awt.TextField;
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 javax.swing.event.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Random;
import javax.imageio.*;
import java.awt.Toolkit;

import javax.swing.JApplet;
import javax.swing.JPanel;
import javax.swing.JTextField;

import java.applet.AppletContext;
import java.awt.*;
import javax.swing.text.Document;
import javax.swing.*;



public class tileGen extends JApplet implements KeyListener, MouseListener,
		ActionListener, Runnable {

	Image[] tiles;
	Image[] weapon;
	Image[] userImg;
	String[] user;
	int x, y, px, py, tx, ty; // x tile - y tile // player x - player y // tile
	// x - tile y
	boolean left, right, down, up, canMove, respawn;
	boolean drawFlail, drawBattleaxe;
	public static int[][] board;
	final int NUM_TILES = 200;
	final int NUM_WEAPS = 2;
	boolean[] weaponPicked;
	Label lx, ly; // to see where we are!
	int r1, r2, u1, u2, l1, l2, d1, d2, spawnX, spawnY;
	int rightSide = 480;
	int downSide = 320;
	int mapX = 15;
	int mapY = 10;
	String txtLastFX, txtLastFY;
	boolean boolRC, boolLC, boolUC, boolDC;
	int leftMap, upMap, rightMap, downMap, currentMap, map;
	public static TextArea c = new TextArea();
	public static TextArea stats = new TextArea();
	public static String newline;
	public static JList list;

	public static Font TimesR = new Font("MonoSpaced", Font.BOLD, 15);
	public static ArrayList<String> arr;
	public static tileGen anItem;
	public static boolean firstpush = false;
	String name, desc, typeOf, attackAdd, defenseAdd, canSell, canEat,
			earnedCoins, canEquip;
	public static int blkStr;
	public static Random roll = new Random();
	//public static String me.getUsername();
	public static int[] userPX;
	public static int[] userPY;
	private Map<String, Player> players = new HashMap<String, Player>();
	public static String[] tokens;
	public static String[] temp;
	public static boolean isResp = false;
	public static String myCommand = "move";
	private Player me;
	public static boolean chatOne = false;
	public int red = 0;
	public static JTextField say = new JTextField();
	public JPanel game = new JPanel();
	int xpos, ypos;
	 int rect1xco,rect1yco,rect1width,rect1height;
	 boolean gameClicked;
	 public static DefaultListModel model = new DefaultListModel();

	// Network schtuff
	public static PrintStream os = null;
	static Socket clientSocket = null;
	static BufferedReader in;
	static boolean closed;

	int lastX, lastY, row, col;
	Label lbl1, lbl2, p1, p2, lblRC;

	int attack, defense, gold, level, exp;

	public static void showInventory(ArrayList<String> theList) {
		for (int i = 0; i < theList.size(); i++) {
			System.out.println(theList.get(i));
		}
	}

	public void init() {

	            setLayout(new BorderLayout());
	            JPanel invOne = new JPanel(); // inventory panel

	            //JPanel banner = new JPanel();

	                JPanel main = new JPanel(new BorderLayout());
	            main.add(game, BorderLayout.CENTER); // game screen;
	            main.add(c, BorderLayout.SOUTH); // chat/textarea screen
	            add(main, BorderLayout.CENTER);
            add(say, BorderLayout.SOUTH);



		/**
		 * arr.add("Hatchet"); arr.add("Sword"); arr.add("Shield"); arr.add(gold
		 * + " Gold"); System.out.println("You have " + arr.size() +
		 * " items in your inventory."); showInventory(arr);
		 **/



		anItem = new tileGen();
		arr = new ArrayList<String>();

		list = new JList(model);
		

		int pos = list.getModel().getSize();
		model.add(pos, "Flail");
		arr.add("Flail");
		for (String item : arr)
			model.add(pos, item);

		list.addMouseListener(this);
		say.addKeyListener(this);


		list.setVisible(true);

		list.setFocusable(false);

		main.add(list, BorderLayout.EAST); // inventory screen

		game.setOpaque(false);

		game.setBounds(0, 0, 416, 288);

		c.setBackground(Color.white);
		c.setForeground(Color.black);
		c.setFont(TimesR);
		c.setEditable(true);
		//c.setLineWrap(true);

		board = loadBoard(1);
		currentMap = 1;
		spawnY = 6;
		spawnX = 10;
		c.append("Welcome to the game, " + getParameter("name") + ".");

		this.setFocusable(true);
		addMouseListener(this);

		tiles = new Image[NUM_TILES];
		for (int i = 0; i < NUM_TILES; i++) {
			tiles[i] = getImage(getClass().getResource(
					String.format("line_tile/t%d.png", i)));
		}

		weapon = new Image[NUM_WEAPS];
		for (int xi = 0; xi < NUM_WEAPS; xi++) {
			weapon[xi] = getImage(getClass().getResource(
					String.format("weapon/w%d.gif", xi)));
		}

		userPX = new int[10];
		for (int i = 0; i < 10; i++) {
			userPX[i] = -100;
		}

		userPY = new int[10];
		for (int i = 0; i < 10; i++) {
			userPY[i] = -100;
		}

		// demo
		// player = getImage(getClass().getResource("me.gif")); // our player
		game.addKeyListener(this);
		canMove = true;

		String txtY = Integer.toString(spawnY);
		String txtX = Integer.toString(spawnX);

		py = spawnY * 32;
		px = spawnX * 32;

		lastY = spawnY;
		lastX = spawnX;

		drawFlail = true;
		drawBattleaxe = true;
		this.setFocusable(true);
		game.setFocusable(true);
		c.setFocusable(false);

		invOne.setVisible(true);

		// blkStr = board[5][5];
		// System.out.println("Teh black one: " +blkStr);
		// board[5][5] = 26;

		game.requestFocusInWindow();

	}

	public void start() {
		new Thread(this).start();

	}


		            public void playerLogin() throws IOException {

		                   Random roll = new Random();
		                   int newNo = roll.nextInt(200);
		                   // me.getUsername() = "Guest #" + roll.nextInt(110);
		                   //String me.getUsername() = getParameter("name");



		                   me = new Player();
		                   me.setUsername(getParameter("name"));
		                   me.setPlayerImage(ImageIO.read(getClass().getResource("me.gif")));
		                   me.setX(256);
		                   me.setY(256);
		                   me.setMap(1);
		                   me.setCommand("move");
		                   players.put(me.getUsername(), me);


		                   repaint();

		                   System.out.println(me.getUsername() + " was added. player: " + me);
		                       os.println(me.getUsername() + "|" + me.getX() + "|" + me.getY() + "|" + me.getMap() + "|" + me.getCommand());

		                   attack = 4;
		                   defense = 5;
		                   gold = 542;
		                   level = 1;
		                   exp = 53;


      }

	public void mousePressed(MouseEvent e) {
	}

	public void mouseReleased(MouseEvent e) {
	}

	public void mouseEntered(MouseEvent e) {
	}

	public void mouseExited(MouseEvent e) {
	}

	// equip - use - select
public void mouseClicked(MouseEvent e) {


		xpos = me.getX();
		ypos = me.getY();

		rect1xco = 0;
		rect1yco = 0;
		rect1width = 480;
		rect1height = 320;

		if (xpos > rect1xco && xpos < rect1xco+rect1width && ypos >rect1yco && ypos < rect1yco+rect1height) {
			game.requestFocusInWindow();
			c.append("\nYou clicked on the game.");
		}



	}


	private static final HashSet<Integer> BLOCKED_TILES = new HashSet<Integer>();
	static {

		/**
		 * BLOCKED_TILES.add(118); BLOCKED_TILES.add(67);
		 * BLOCKED_TILES.add(190); BLOCKED_TILES.add(40);
		 * BLOCKED_TILES.add(135); BLOCKED_TILES.add(520);
		 * BLOCKED_TILES.add(25); BLOCKED_TILES.add(91); BLOCKED_TILES.add(394);
		 * BLOCKED_TILES.add(1); BLOCKED_TILES.add(121);
		 **/

		BLOCKED_TILES.add(1);
		BLOCKED_TILES.add(6);
		BLOCKED_TILES.add(7);
		// add more tiles here
	}

	public void actionPerformed(ActionEvent actionevent) {
		//game.requestFocusInWindow();
	}

	public Player getPlayer(String username) {
		return players.get(username);
	}

	public void run() {

		System.out.println("run() still going..");


		Socket s = null;

		try {

			// s = new Socket(getParameter("host"),
			// Integer.valueOf(getParameter("port"));
			// s = new Socket(getParameter("host"),
			// Integer.valueOf(getParameter("port")));
			s = new Socket("localhost", 25566);
			in = new BufferedReader(new InputStreamReader(s.getInputStream()));

			os = new PrintStream(s.getOutputStream());

			playerLogin();

			int buf = -1;
			String responseLine;



			// rawr
			while ((responseLine = in.readLine()) != null) {
				isResp = true;

				chatOne = true;
				//c.append("\nxxx\n");
				// System.out.println("responseLine (|): " + responseLine);

				String str = responseLine;

				String delimiter = "\\|";

				String[] temp = str.split(delimiter);

				//c.append("\noutput: " + str);

				// pid
				Player player = players.get(temp[0]);

if ("chat".equalsIgnoreCase(temp[4])) {
c.append("\n"+temp[0] + " says: " + temp[2]);

//sendMsg(temp[0], temp[2], me.getUsername());

}


				if (player == null) {

				player = new Player();
				player.setUsername(temp[0]);
				player.setX(Integer.parseInt(temp[1]));
				player.setY(Integer.parseInt(temp[2]));
				player.setCommand(temp[4]);
				player.setPlayerImage(ImageIO.read(getClass().getResource("me.gif")));
				player.setMap(1);
				players.put(temp[0], player);
				}
if ("move".equalsIgnoreCase(temp[4])) {
	//c.append("\n"+temp[2] + " and temp 4: " + temp[4]);
				player.setX(Integer.parseInt(temp[1]));
				player.setY(Integer.parseInt(temp[2]));
				player.setMap(Integer.parseInt(temp[3]));
			}
				player.setCommand(temp[4]);
				player.setUsername(temp[0]);
				System.out.println("X set: " + player.getX() + " | Y set: "
						+ player.getY() + " | CurrMap set: " + player.getMap()
						+ " | Username set: " + player.getUsername() + " | Command set: " + temp[4]);



				repaint();





			}
			repaint();

		} catch (Exception rwe) {
			// System.exit(0);
			System.out.println("Server went down -- crap!");
			rwe.printStackTrace();
		} finally {
			try {
				s.close();
			} catch (IOException ere) {
				ere.printStackTrace();
			}
		}

	}

	public void sendMsg(String e, String msg, String getUser) {

//c.append("\n Temp0: " + temp[0] + "| me.getUsername: "+me.getUsername());

String whosay;
if (!temp[0].equals(me.getUsername())) {

	whosay = e;
}else{
	whosay = me.getUsername();
}
c.append("\n"+ e + " says: " + msg);
	}

	public void keyPressed(KeyEvent e) {

 if (e.getKeyCode() == KeyEvent.VK_ENTER) {
            Toolkit.getDefaultToolkit().beep();
			myCommand = "chat";
			os.println(me.getUsername() + "|1|"+say.getText()+"|yes|chat");
			say.setText("");
			say.setCaretPosition(say.getDocument().getLength());

		}
		// getPlayers();

		repaint();

		myCommand = "move";
		System.out.println("PRESSED" + myCommand);

		if (isInBound(lastX, lastY) == true) {
			System.out.println("\nYOU WENT OFF THE GRID.\n");
		}

		right = true;
		left = true;
		up = true;
		down = true;

		Object choice = list.getSelectedValue();

		// c.append("\nInventory selection: " + choice);

		// DESCRIBE ITEM
		if (e.getKeyCode() == KeyEvent.VK_D) {
			Object sup = 1;
			String xda = sup.toString();
			String descInv = invShow(xda);
			c.append("\n" + descInv);
		}

		if (e.getKeyCode() == KeyEvent.VK_S) {
			int numToReplace = 0;
			int replacement = 7;
			// loop through each nested array
			for (int i = 0; i < board.length; i++) {
				// loop through each element of the nested array
				for (int j = 0; j < board[i].length; j++) {
					if (board[i][j] == numToReplace) {
						board[i][j] = replacement;
					}
				}
			}
		}

		// USE
		if (e.getKeyCode() == KeyEvent.VK_U) {

			// MAP ONE
			if (currentMap == 1) {

				// OPEN BLOCKED TILES
				if (spawnX == 10 && spawnY == 7) {
					board[8][10] = 167;
					c.append("\nYou opened a door.");
				}

			}

			// MAP ONE

		}

		// PICK UP
		if (e.getKeyCode() == KeyEvent.VK_G) {

			// MAP ONE
			if (currentMap == 1) {

				if (spawnX == 4 && spawnY == 4) {
					int xav = board[4][4];
					board[4][4] = 7;
					drawFlail = false;
					try {
						anItem.addInv(1);
					} catch (Exception ere) {
						ere.printStackTrace();
					}

				}

			}

			// MAP ONE
			if (currentMap == 3) {

				if (spawnX == 3 && spawnY == 3) {
					int xav = board[3][3];
					board[3][3] = 1;
					drawBattleaxe = false;
					try {
						anItem.addInv(2);
					} catch (Exception ere) {
						ere.printStackTrace();
					}

				}

			}
			// MAP ONE

		}

		try {
			if (blocked(spawnX - 1, spawnY) == true && spawnX == mapX) {
				left = false;
				System.out.println("You can't go left!");
			}
			if (blocked(spawnX + 1, spawnY) == true && spawnX == 0) {
				right = false;
				System.out.println("You can't go right!");
			}
			if (blocked(spawnX, spawnY + 1) == true && spawnX == mapX) {
				down = false;
				System.out.println("You can't go down!");
			}
			if (blocked(spawnX, spawnY - 1) == true && spawnX == mapX) {
				up = false;
				System.out.println("You can't go up!");
			}

		} catch (ArrayIndexOutOfBoundsException xe) {
			xe.printStackTrace();
		}

		try {
			if (blocked(spawnX + 1, spawnY) == true)
				right = false;
		} catch (ArrayIndexOutOfBoundsException xe) {
			System.out.println("\n\ncc Border AIOOB Exception!!\n\n");
		}

		try {
			if (blocked(spawnX - 1, spawnY) == true)
				left = false;
		} catch (ArrayIndexOutOfBoundsException xe) {
			System.out.println("\n\ncc Border AIOOB Exception!!\n\n");
		}

		try {
			if (blocked(spawnX, spawnY + 1) == true)
				down = false;
		} catch (ArrayIndexOutOfBoundsException xe) {
			System.out.println("\n\ncc Border AIOOB Exception!!\n\n");
		}

		try {
			if (blocked(spawnX, spawnY - 1) == true)
				up = false;
		} catch (ArrayIndexOutOfBoundsException xe) {
			System.out.println("\n\ncc Border AIOOB Exception!!\n\n");
		}

		if (blocked(12, 7) == true && spawnX == mapX && spawnY - 1 == 1 - mapX) {
			up = false;
		}

		int r1 = lastX + 1;
		if (lastX > 0) {
			r1 = lastX + 1;
		} else {
			r1 = 0;
		}
		int r2 = lastY;

		int u1 = lastX;
		int u2;
		if (lastY > 0) {
			u2 = lastY - 1;
		} else {
			u2 = 0;
		}

		int l1;
		if (spawnX > 0) {
			l1 = lastX - 1;
		} else {
			l1 = 0;
		}
		int l2 = spawnY;

		int d1 = lastX;
		int d2;
		if (lastY > 0) {
			d2 = lastY + 1;
		} else {
			d2 = 0;
		}

		try {
			if (blocked(r1, r2) == true)
				right = false; // we cannot go right
			if (blocked(u1, u2) == true)
				up = false; // we cannot go up
			if (blocked(l1, l2) == true)
				left = false; // we cannot go left
			if (blocked(d1, d2) == true)
				down = false; // we cannot go down

		} catch (ArrayIndexOutOfBoundsException xe) {
			System.out
					.println("\n\nFuture block Array Index Out of Bounds Exception!!\n\n");
		}

		leftMap = 0;
		upMap = 0;
		rightMap = 0;
		downMap = 0;

		boolRC = false;
		boolLC = false;
		boolUC = false;
		boolDC = false;

		// Border Checks
		if (spawnX == mapX && e.getKeyCode() == KeyEvent.VK_RIGHT) {
			boolRC = true;
		}

		if (spawnX == 0 && e.getKeyCode() == KeyEvent.VK_LEFT) {
			boolLC = true;
		}

		if (spawnY == 0 && e.getKeyCode() == KeyEvent.VK_UP) {
			boolUC = true;
		}

		if (spawnY == mapY && e.getKeyCode() == KeyEvent.VK_DOWN) {
			boolDC = true;
		}

		/** MAP SWITCHING **/
		/** MAP SWITCHING **/
		/** MAP SWITCHING **/

		if (currentMap == 1) {
			leftMap = 3;
			upMap = 2;
			rightMap = 4;
			downMap = 5;

		} else if (currentMap == 4) {
			leftMap = 1;
		} else if (currentMap == 2) {

			downMap = 1;
		} else if (currentMap == 3) {

			downMap = 1;
		}
		if (currentMap == 5) {

			upMap = 1;
		}

		if (leftMap == 0 && spawnX == 0)
			left = false;
		if (rightMap == 0 && spawnX == mapX)
			right = false;
		if (downMap == 0 && spawnY == mapY)
			down = false;
		if (upMap == 0 && spawnY == 0)
			up = false;

		if (e.getKeyCode() == KeyEvent.VK_UP && up == true) {
			spawnY = spawnY - 1;
			System.out.println("You went up.");
		}
		if (e.getKeyCode() == KeyEvent.VK_DOWN && down == true) {
			spawnY = spawnY + 1;
			System.out.println("You went down.");
		}
		if (e.getKeyCode() == KeyEvent.VK_RIGHT && right == true) {
			spawnX = spawnX + 1;
			System.out.println("You went right.");
		}
		if (e.getKeyCode() == KeyEvent.VK_LEFT && left == true) {
			spawnX = spawnX - 1;
			System.out.println("You went left.");
		}

		// RIGHT TO LEFT
		// LEFT TO RIGHT DONE
		if (boolRC == true && right == true) {
			spawnX = 0;
			lastX = spawnX;
			spawnY = spawnY;
			board = loadBoard(rightMap);
			px = 0;
			py = py;
			currentMap = rightMap;
		}

		if (boolLC == true && left == true) {
			spawnX = mapX;
			lastX = spawnX;
			spawnY = spawnY;
			board = loadBoard(leftMap);
			px = rightSide;
			py = py;
			currentMap = leftMap;
		}

		if (boolUC == true && up == true) {
			spawnY = mapY;
			lastY = spawnY;
			spawnX = spawnX;
			board = loadBoard(upMap);
			px = px;
			py = downSide;
			currentMap = upMap;
		}

		if (boolDC == true && down == true) {
			spawnY = 0;
			lastY = spawnY;
			spawnX = spawnX;
			board = loadBoard(downMap);
			px = px;
			py = 0;
			currentMap = downMap;
		}

		/** MAP SWITCHING **/
		/** MAP SWITCHING **/
		/** MAP SWITCHING **/

		String currX = Integer.toString(spawnX);
		String currY = Integer.toString(spawnY);
		System.out.println("Current/Last Y: " + currY + "/" + lastY);
		System.out.println("Current/Last X: " + currX + "/" + lastX + " - "
				+ board[spawnY][spawnX] + "\n\n");

		if (boolLC == false) {
			if (left == true) {
				if (e.getKeyCode() == KeyEvent.VK_LEFT) {
					left = true;
					px = px - 32;
					lastX = lastX - 1;
				}
			}
		} else {
			if (leftMap > 0)
				px = rightSide;
		}

		if (boolRC == false) {
			if (right == true) {
				if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
					right = true;
					px = px + 32;
					lastX = lastX + 1;
				}
			}
		} else {
			if (rightMap > 0)
				px = 0;
		}

		if (boolDC == false) {

			if (down == true) {
				if (e.getKeyCode() == KeyEvent.VK_DOWN) {
					down = true;
					py = py + 32;
					lastY = lastY + 1;

				}
			}
		} else {
			if (downMap > 0)
				py = 0;

		}

		if (boolUC == false) {
			if (up == true) {

				if (e.getKeyCode() == KeyEvent.VK_UP) {
					up = true;
					py = py - 32;
					lastY = lastY - 1;
				}
			}
		} else {
			if (upMap > 0)
				py = downSide;
		}

System.out.println("me.getUsername(): " + me.getUsername());
				 me = players.get(me.getUsername());

				me.setX(px);
me.setY(py);

if (myCommand == "move") {
		os.println(me.getUsername() + "|" + px + "|" + py + "|" + currentMap + "|move");
	}else if (myCommand == "chat"){
		os.println(me.getUsername() + "|1|Hello, world!|yes|chat");
	}

	me.setCommand(myCommand);
	System.out.println("my curernt map! : " + currentMap);


		repaint();

	}

	public void keyReleased(KeyEvent e) {

	} // ignore

	public void keyTyped(KeyEvent e) {
	} // ignore

	public void addInv(int e) {

		String iname = getItem(e)[0];

		String idesc = getItem(e)[1];
		int itypeOf = Integer.parseInt(getItem(e)[2]);
		int iattackAdd = Integer.parseInt(getItem(e)[3]);
		int idefenseAdd = Integer.parseInt(getItem(e)[4]);
		boolean icanSell = Boolean.parseBoolean(getItem(e)[5]);
		boolean icanEat = Boolean.parseBoolean(getItem(e)[6]);
		int iearnedCoins = Integer.parseInt(getItem(e)[7]);

		c.append("\nYou picked up a " + iname + ".");
		// for (String item : arr) list.add(item);
		int posx = list.getModel().getSize();
		model.add(posx, iname);
		arr.add(iname);
	}

	public static String invShow(String e) {

		String desc = "None";

		if (e == "Flail") {
			desc = "A crushing blow to the head from this weapon can make BRAIN SLUSHIE!";
		}
		if (e == "Battleaxe") {
			desc = "Holding this weapon will make you smell like a man, man.";
		}

		return desc;

	}

	public static String[] getItem(int e) {

		String[] stats = new String[7];

		String name = "Null";
		String desc = "Nonexx";
		String typeOf = "0";
		String attackAdd = "0";
		String defenseAdd = "0";
		String canSell = "true";
		String canEat = "false";
		String earnedCoins = "0";

		if (e == 1) {

			name = "Flail";
			typeOf = "2";
			attackAdd = "2";
			earnedCoins = "5";
		}

		if (e == 2) {

			name = "Battleaxe";
			typeOf = "2";
			attackAdd = "4";
			earnedCoins = "10";
		}

		return new String[] { name, desc, typeOf, attackAdd, defenseAdd,
				canSell, canEat, earnedCoins };

	}

	public static void showInventory() {

		for (int i = 0; i < arr.size(); i++) {
			System.out.println(arr.get(i));
		}
	}

	public void paint(Graphics g) {

		for (row = 0; row < board.length; row++) {
			for (col = 0; col < board[row].length; col++) {
				int index = board[row][col];
				g.drawImage(tiles[index], 32 * col, 32 * row, this);

			}
		}

		try {
			for (Player player : players.values()) {
				System.out.println("++++++++++++++++++++++++++");
				System.out.println("current map: " + player.getMap());
				if (player.getUsername() == me.getUsername()) player.setMap(currentMap);
				if (player.getUsername() == me.getUsername()) player.setCommand(myCommand);
				if (player.getMap() == currentMap) {
					g.drawImage(player.getPlayerImage(), player.getX(), player.getY(), this);

				}
				System.out.println("Command-- " + player.getCommand() + " > " +player.getUsername() + " moved to X:" + player.getX() + " and Y: " + player.getY());

				System.out.println("++++++++++++++++++++++++++");
			}
		} catch (Exception dan) {
			System.out.println("No one is on.");
		}

		// qq
		/**
		 * for(int runx = 0;runx < 10;runx++) { g.drawImage(userImg[runx],
		 * userPX[runx], userPY[runx], this); }
		 **/

		if (drawFlail == true && currentMap == 1)
			g.drawImage(weapon[1], 32 * 4, 32 * 4, this);
		if (drawBattleaxe == true && currentMap == 3)
			g.drawImage(weapon[0], 32 * 3, 32 * 3, this);
		System.out.println(drawFlail);
	} // end paint method

	public void update(Graphics g) {
		paint(g);
	}

	public int[][] loadBoard(int map) {
		if (map == 1) {
			return new int[][] {
{ 7, 7, 7, 7, 7, 7, 13, 13, 13, 13, 7, 7, 7, 7, 7, 7
 },
{ 7, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 6
 },
{ 7, 13, 1, 13, 1, 13, 1, 13, 13, 13, 1, 1, 13, 13, 13, 6
 },
{ 7, 13, 1, 1, 1, 13, 1, 13, 13, 13, 1, 13, 13, 13, 13, 6
 },
{ 7, 13, 13, 1, 13, 13, 1, 13, 13, 13, 1, 13, 13, 13, 13, 6
 },
{ 7, 13, 1, 1, 1, 13, 1, 1, 1, 13, 1, 1, 1, 1, 13, 6
 },
{ 7, 13, 1, 13, 13, 13, 13, 13, 1, 13, 13, 1, 13, 13, 13, 6
 },
{ 7, 13, 1, 1, 13, 13, 13, 1, 1, 13, 13, 1, 13, 13, 13, 6
 },
{ 7, 13, 13, 13, 13, 13, 13, 1, 13, 13, 13, 1, 13, 13, 13, 6
 },
{ 7, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 6
 },
{ 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6
 }
};
		}else if (map == 2) {
			return new int[][] { { 145, 145, 55, 145, 145, 55, 184, 163, 55, 163, 163, 55, 145, 181, 145, 145
			 },
			{ 145, 13, 145, 145, 166, 166, 166, 163, 163, 13, 13, 13, 13, 13, 13, 145
			 },
			{ 145, 13, 13, 163, 166, 166, 166, 163, 13, 13, 13, 13, 13, 13, 13, 163
			 },
			{ 145, 13, 13, 163, 166, 166, 166, 163, 13, 145, 145, 145, 145, 145, 13, 145
			 },
			{ 145, 13, 13, 163, 166, 166, 166, 163, 13, 145, 166, 166, 166, 145, 13, 145
			 },
			{ 145, 13, 13, 163, 163, 95, 163, 145, 13, 145, 166, 166, 166, 145, 13, 145
			 },
			{ 145, 13, 13, 13, 13, 13, 13, 13, 13, 166, 166, 166, 166, 145, 13, 145
			 },
			{ 145, 145, 13, 13, 13, 13, 13, 13, 13, 163, 145, 55, 145, 145, 13, 145
			 },
			{ 145, 145, 145, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 145
			 },
			{ 145, 145, 145, 145, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 145
			 },
			{ 145, 145, 145, 145, 145, 145, 13, 13, 13, 13, 13, 145, 145, 145, 145, 145
			 }
};
}

		return board;
	}

	public boolean blocked(int tx, int ty) {
		return BLOCKED_TILES.contains(board[ty][tx]);
	}

	public boolean isInBound(int r, int c) {
		return (r >= 0) && (r < 8) && (c >= 12) && (c < 1);
	}

} // end whole thing3

Open in new window


0
Comment
Question by:UrbanTwitch
  • 15
  • 9
24 Comments
 
LVL 92

Expert Comment

by:objects
ID: 33442281
you never add the list to your guiu
where would you like it added?
0
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 33442301
try this:

/**
Mystik Client
Programer: Dan J.
Many thanks to objects & CEHJ
Started May 23, 2010
(c) 2010
 **/

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Label;
import java.awt.TextArea;
import java.awt.Toolkit;
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.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.swing.DefaultListModel;
import javax.swing.JApplet;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class tileGen extends JApplet implements KeyListener, MouseListener,
            ActionListener, Runnable {

      Image[] tiles;
      Image[] weapon;
      Image[] userImg;
      String[] user;
      int x, y, px, py, tx, ty; // x tile - y tile // player x - player y // tile
      // x - tile y
      boolean left, right, down, up, canMove, respawn;
      boolean drawFlail, drawBattleaxe;
      public static int[][] board;
      final int NUM_TILES = 200;
      final int NUM_WEAPS = 2;
      boolean[] weaponPicked;
      Label lx, ly; // to see where we are!
      int r1, r2, u1, u2, l1, l2, d1, d2, spawnX, spawnY;
      int rightSide = 480;
      int downSide = 320;
      int mapX = 15;
      int mapY = 10;
      String txtLastFX, txtLastFY;
      boolean boolRC, boolLC, boolUC, boolDC;
      int leftMap, upMap, rightMap, downMap, currentMap, map;
      public static TextArea c = new TextArea();
      public static TextArea stats = new TextArea();
      public static String newline;

      public static Font TimesR = new Font("MonoSpaced", Font.BOLD, 15);
      public static ArrayList arr;
      public static tileGen anItem;
      public static boolean firstpush = false;
      String name, desc, typeOf, attackAdd, defenseAdd, canSell, canEat,
                  earnedCoins, canEquip;
      public static int blkStr;
      public static Random roll = new Random();
      // public static String me.getUsername();
      public static int[] userPX;
      public static int[] userPY;
      private Map players = new HashMap();
      public static String[] tokens;
      public static String[] temp;
      public static boolean isResp = false;
      public static String myCommand = "move";
      private Player me;
      public static boolean chatOne = false;
      public int red = 0;
      public static JTextField say = new JTextField();
      public JPanel game = new JPanel();
      int xpos, ypos;
      int rect1xco, rect1yco, rect1width, rect1height;
      boolean gameClicked;
      public DefaultListModel model = new DefaultListModel();

      // Network schtuff
      public static PrintStream os = null;
      static Socket clientSocket = null;
      static BufferedReader in;
      static boolean closed;

      int lastX, lastY, row, col;
      Label lbl1, lbl2, p1, p2, lblRC;

      int attack, defense, gold, level, exp;

      public static void showInventory(ArrayList theList) {
            for (int i = 0; i < theList.size(); i++) {
                  System.out.println(theList.get(i));
            }
      }

      public void init() {

            setLayout(new BorderLayout());
            JPanel invOne = new JPanel(); // inventory panel

            // JPanel banner = new JPanel();

            JPanel main = new JPanel(new BorderLayout());
            main.add(game, BorderLayout.CENTER); // game screen;
            main.add(c, BorderLayout.SOUTH); // chat/textarea screen
            add(main, BorderLayout.CENTER);
            add(say, BorderLayout.SOUTH);

            /**
             * arr.add("Hatchet"); arr.add("Sword"); arr.add("Shield"); arr.add(gold
             * + " Gold"); System.out.println("You have " + arr.size() +
             * " items in your inventory."); showInventory(arr);
             **/

            anItem = new tileGen();
            arr = new ArrayList();

            JList list = new JList(model);

            model.addElement("Flail");
            arr.add("Flail");
            for (String item : arr)
                  model.addElement(item);

            list.addMouseListener(this);
            say.addKeyListener(this);

            list.setVisible(true);

            list.setFocusable(false);

            main.add(list, BorderLayout.EAST); // inventory screen

            game.setOpaque(false);

            game.setBounds(0, 0, 416, 288);

            c.setBackground(Color.white);
            c.setForeground(Color.black);
            c.setFont(TimesR);
            c.setEditable(true);
            // c.setLineWrap(true);

            board = loadBoard(1);
            currentMap = 1;
            spawnY = 6;
            spawnX = 10;
            c.append("Welcome to the game, " + getParameter("name") + ".");

            this.setFocusable(true);
            addMouseListener(this);

            tiles = new Image[NUM_TILES];
            for (int i = 0; i < NUM_TILES; i++) {
                  tiles[i] = getImage(getClass().getResource(
                              String.format("line_tile/t%d.png", i)));
            }

            weapon = new Image[NUM_WEAPS];
            for (int xi = 0; xi < NUM_WEAPS; xi++) {
                  weapon[xi] = getImage(getClass().getResource(
                              String.format("weapon/w%d.gif", xi)));
            }

            userPX = new int[10];
            for (int i = 0; i < 10; i++) {
                  userPX[i] = -100;
            }

            userPY = new int[10];
            for (int i = 0; i < 10; i++) {
                  userPY[i] = -100;
            }

            // demo
            // player = getImage(getClass().getResource("me.gif")); // our player
            game.addKeyListener(this);
            canMove = true;

            String txtY = Integer.toString(spawnY);
            String txtX = Integer.toString(spawnX);

            py = spawnY * 32;
            px = spawnX * 32;

            lastY = spawnY;
            lastX = spawnX;

            drawFlail = true;
            drawBattleaxe = true;
            this.setFocusable(true);
            game.setFocusable(true);
            c.setFocusable(false);

            invOne.setVisible(true);

            // blkStr = board[5][5];
            // System.out.println("Teh black one: " +blkStr);
            // board[5][5] = 26;

            game.requestFocusInWindow();

      }

      public void start() {
            new Thread(this).start();

      }

      public void playerLogin() throws IOException {

            Random roll = new Random();
            int newNo = roll.nextInt(200);
            // me.getUsername() = "Guest #" + roll.nextInt(110);
            // String me.getUsername() = getParameter("name");

            me = new Player();
            me.setUsername(getParameter("name"));
            me.setPlayerImage(ImageIO.read(getClass().getResource("me.gif")));
            me.setX(256);
            me.setY(256);
            me.setMap(1);
            me.setCommand("move");
            players.put(me.getUsername(), me);

            repaint();

            System.out.println(me.getUsername() + " was added. player: " + me);
            os.println(me.getUsername() + "|" + me.getX() + "|" + me.getY() + "|"
                        + me.getMap() + "|" + me.getCommand());

            attack = 4;
            defense = 5;
            gold = 542;
            level = 1;
            exp = 53;

      }

      public void mousePressed(MouseEvent e) {
      }

      public void mouseReleased(MouseEvent e) {
      }

      public void mouseEntered(MouseEvent e) {
      }

      public void mouseExited(MouseEvent e) {
      }

      // equip - use - select
      public void mouseClicked(MouseEvent e) {

            xpos = me.getX();
            ypos = me.getY();

            rect1xco = 0;
            rect1yco = 0;
            rect1width = 480;
            rect1height = 320;

            if (xpos > rect1xco && xpos < rect1xco + rect1width && ypos > rect1yco
                        && ypos < rect1yco + rect1height) {
                  game.requestFocusInWindow();
                  c.append("\nYou clicked on the game.");
            }

      }

      private static final HashSet BLOCKED_TILES = new HashSet();
      static {

            /**
             * BLOCKED_TILES.add(118); BLOCKED_TILES.add(67);
             * BLOCKED_TILES.add(190); BLOCKED_TILES.add(40);
             * BLOCKED_TILES.add(135); BLOCKED_TILES.add(520);
             * BLOCKED_TILES.add(25); BLOCKED_TILES.add(91); BLOCKED_TILES.add(394);
             * BLOCKED_TILES.add(1); BLOCKED_TILES.add(121);
             **/

            BLOCKED_TILES.add(1);
            BLOCKED_TILES.add(6);
            BLOCKED_TILES.add(7);
            // add more tiles here
      }

      public void actionPerformed(ActionEvent actionevent) {
            // game.requestFocusInWindow();
      }

      public Player getPlayer(String username) {
            return players.get(username);
      }

      public void run() {

            System.out.println("run() still going..");

            Socket s = null;

            try {

                  // s = new Socket(getParameter("host"),
                  // Integer.valueOf(getParameter("port"));
                  // s = new Socket(getParameter("host"),
                  // Integer.valueOf(getParameter("port")));
                  s = new Socket("localhost", 25566);
                  in = new BufferedReader(new InputStreamReader(s.getInputStream()));

                  os = new PrintStream(s.getOutputStream());

                  playerLogin();

                  int buf = -1;
                  String responseLine;

                  // rawr
                  while ((responseLine = in.readLine()) != null) {
                        isResp = true;

                        chatOne = true;
                        // c.append("\nxxx\n");
                        // System.out.println("responseLine (|): " + responseLine);

                        String str = responseLine;

                        String delimiter = "\\|";

                        String[] temp = str.split(delimiter);

                        // c.append("\noutput: " + str);

                        // pid
                        Player player = players.get(temp[0]);

                        if ("chat".equalsIgnoreCase(temp[4])) {
                              c.append("\n" + temp[0] + " says: " + temp[2]);

                              // sendMsg(temp[0], temp[2], me.getUsername());

                        }

                        if (player == null) {

                              player = new Player();
                              player.setUsername(temp[0]);
                              player.setX(Integer.parseInt(temp[1]));
                              player.setY(Integer.parseInt(temp[2]));
                              player.setCommand(temp[4]);
                              player.setPlayerImage(ImageIO.read(getClass().getResource(
                                          "me.gif")));
                              player.setMap(1);
                              players.put(temp[0], player);
                        }
                        if ("move".equalsIgnoreCase(temp[4])) {
                              // c.append("\n"+temp[2] + " and temp 4: " + temp[4]);
                              player.setX(Integer.parseInt(temp[1]));
                              player.setY(Integer.parseInt(temp[2]));
                              player.setMap(Integer.parseInt(temp[3]));
                        }
                        player.setCommand(temp[4]);
                        player.setUsername(temp[0]);
                        System.out.println("X set: " + player.getX() + " | Y set: "
                                    + player.getY() + " | CurrMap set: " + player.getMap()
                                    + " | Username set: " + player.getUsername()
                                    + " | Command set: " + temp[4]);

                        repaint();

                  }
                  repaint();

            } catch (Exception rwe) {
                  // System.exit(0);
                  System.out.println("Server went down -- crap!");
                  rwe.printStackTrace();
            } finally {
                  try {
                        s.close();
                  } catch (IOException ere) {
                        ere.printStackTrace();
                  }
            }

      }

      public void sendMsg(String e, String msg, String getUser) {

            // c.append("\n Temp0: " + temp[0] +
            // "| me.getUsername: "+me.getUsername());

            String whosay;
            if (!temp[0].equals(me.getUsername())) {

                  whosay = e;
            } else {
                  whosay = me.getUsername();
            }
            c.append("\n" + e + " says: " + msg);
      }

      public void keyPressed(KeyEvent e) {

            if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                  Toolkit.getDefaultToolkit().beep();
                  myCommand = "chat";
                  os.println(me.getUsername() + "|1|" + say.getText() + "|yes|chat");
                  say.setText("");
                  say.setCaretPosition(say.getDocument().getLength());

            }
            // getPlayers();

            repaint();

            myCommand = "move";
            System.out.println("PRESSED" + myCommand);

            if (isInBound(lastX, lastY) == true) {
                  System.out.println("\nYOU WENT OFF THE GRID.\n");
            }

            right = true;
            left = true;
            up = true;
            down = true;

            // c.append("\nInventory selection: " + choice);

            // DESCRIBE ITEM
            if (e.getKeyCode() == KeyEvent.VK_D) {
                  Object sup = 1;
                  String xda = sup.toString();
                  String descInv = invShow(xda);
                  c.append("\n" + descInv);
            }

            if (e.getKeyCode() == KeyEvent.VK_S) {
                  int numToReplace = 0;
                  int replacement = 7;
                  // loop through each nested array
                  for (int i = 0; i < board.length; i++) {
                        // loop through each element of the nested array
                        for (int j = 0; j < board[i].length; j++) {
                              if (board[i][j] == numToReplace) {
                                    board[i][j] = replacement;
                              }
                        }
                  }
            }

            // USE
            if (e.getKeyCode() == KeyEvent.VK_U) {

                  // MAP ONE
                  if (currentMap == 1) {

                        // OPEN BLOCKED TILES
                        if (spawnX == 10 && spawnY == 7) {
                              board[8][10] = 167;
                              c.append("\nYou opened a door.");
                        }

                  }

                  // MAP ONE

            }

            // PICK UP
            if (e.getKeyCode() == KeyEvent.VK_G) {

                  // MAP ONE
                  if (currentMap == 1) {

                        if (spawnX == 4 && spawnY == 4) {
                              int xav = board[4][4];
                              board[4][4] = 7;
                              drawFlail = false;
                              try {
                                    anItem.addInv(1);
                              } catch (Exception ere) {
                                    ere.printStackTrace();
                              }

                        }

                  }

                  // MAP ONE
                  if (currentMap == 3) {

                        if (spawnX == 3 && spawnY == 3) {
                              int xav = board[3][3];
                              board[3][3] = 1;
                              drawBattleaxe = false;
                              try {
                                    anItem.addInv(2);
                              } catch (Exception ere) {
                                    ere.printStackTrace();
                              }

                        }

                  }
                  // MAP ONE

            }

            try {
                  if (blocked(spawnX - 1, spawnY) == true && spawnX == mapX) {
                        left = false;
                        System.out.println("You can't go left!");
                  }
                  if (blocked(spawnX + 1, spawnY) == true && spawnX == 0) {
                        right = false;
                        System.out.println("You can't go right!");
                  }
                  if (blocked(spawnX, spawnY + 1) == true && spawnX == mapX) {
                        down = false;
                        System.out.println("You can't go down!");
                  }
                  if (blocked(spawnX, spawnY - 1) == true && spawnX == mapX) {
                        up = false;
                        System.out.println("You can't go up!");
                  }

            } catch (ArrayIndexOutOfBoundsException xe) {
                  xe.printStackTrace();
            }

            try {
                  if (blocked(spawnX + 1, spawnY) == true)
                        right = false;
            } catch (ArrayIndexOutOfBoundsException xe) {
                  System.out.println("\n\ncc Border AIOOB Exception!!\n\n");
            }

            try {
                  if (blocked(spawnX - 1, spawnY) == true)
                        left = false;
            } catch (ArrayIndexOutOfBoundsException xe) {
                  System.out.println("\n\ncc Border AIOOB Exception!!\n\n");
            }

            try {
                  if (blocked(spawnX, spawnY + 1) == true)
                        down = false;
            } catch (ArrayIndexOutOfBoundsException xe) {
                  System.out.println("\n\ncc Border AIOOB Exception!!\n\n");
            }

            try {
                  if (blocked(spawnX, spawnY - 1) == true)
                        up = false;
            } catch (ArrayIndexOutOfBoundsException xe) {
                  System.out.println("\n\ncc Border AIOOB Exception!!\n\n");
            }

            if (blocked(12, 7) == true && spawnX == mapX && spawnY - 1 == 1 - mapX) {
                  up = false;
            }

            int r1 = lastX + 1;
            if (lastX > 0) {
                  r1 = lastX + 1;
            } else {
                  r1 = 0;
            }
            int r2 = lastY;

            int u1 = lastX;
            int u2;
            if (lastY > 0) {
                  u2 = lastY - 1;
            } else {
                  u2 = 0;
            }

            int l1;
            if (spawnX > 0) {
                  l1 = lastX - 1;
            } else {
                  l1 = 0;
            }
            int l2 = spawnY;

            int d1 = lastX;
            int d2;
            if (lastY > 0) {
                  d2 = lastY + 1;
            } else {
                  d2 = 0;
            }

            try {
                  if (blocked(r1, r2) == true)
                        right = false; // we cannot go right
                  if (blocked(u1, u2) == true)
                        up = false; // we cannot go up
                  if (blocked(l1, l2) == true)
                        left = false; // we cannot go left
                  if (blocked(d1, d2) == true)
                        down = false; // we cannot go down

            } catch (ArrayIndexOutOfBoundsException xe) {
                  System.out
                              .println("\n\nFuture block Array Index Out of Bounds Exception!!\n\n");
            }

            leftMap = 0;
            upMap = 0;
            rightMap = 0;
            downMap = 0;

            boolRC = false;
            boolLC = false;
            boolUC = false;
            boolDC = false;

            // Border Checks
            if (spawnX == mapX && e.getKeyCode() == KeyEvent.VK_RIGHT) {
                  boolRC = true;
            }

            if (spawnX == 0 && e.getKeyCode() == KeyEvent.VK_LEFT) {
                  boolLC = true;
            }

            if (spawnY == 0 && e.getKeyCode() == KeyEvent.VK_UP) {
                  boolUC = true;
            }

            if (spawnY == mapY && e.getKeyCode() == KeyEvent.VK_DOWN) {
                  boolDC = true;
            }

            /** MAP SWITCHING **/
            /** MAP SWITCHING **/
            /** MAP SWITCHING **/

            if (currentMap == 1) {
                  leftMap = 3;
                  upMap = 2;
                  rightMap = 4;
                  downMap = 5;

            } else if (currentMap == 4) {
                  leftMap = 1;
            } else if (currentMap == 2) {

                  downMap = 1;
            } else if (currentMap == 3) {

                  downMap = 1;
            }
            if (currentMap == 5) {

                  upMap = 1;
            }

            if (leftMap == 0 && spawnX == 0)
                  left = false;
            if (rightMap == 0 && spawnX == mapX)
                  right = false;
            if (downMap == 0 && spawnY == mapY)
                  down = false;
            if (upMap == 0 && spawnY == 0)
                  up = false;

            if (e.getKeyCode() == KeyEvent.VK_UP && up == true) {
                  spawnY = spawnY - 1;
                  System.out.println("You went up.");
            }
            if (e.getKeyCode() == KeyEvent.VK_DOWN && down == true) {
                  spawnY = spawnY + 1;
                  System.out.println("You went down.");
            }
            if (e.getKeyCode() == KeyEvent.VK_RIGHT && right == true) {
                  spawnX = spawnX + 1;
                  System.out.println("You went right.");
            }
            if (e.getKeyCode() == KeyEvent.VK_LEFT && left == true) {
                  spawnX = spawnX - 1;
                  System.out.println("You went left.");
            }

            // RIGHT TO LEFT
            // LEFT TO RIGHT DONE
            if (boolRC == true && right == true) {
                  spawnX = 0;
                  lastX = spawnX;
                  spawnY = spawnY;
                  board = loadBoard(rightMap);
                  px = 0;
                  py = py;
                  currentMap = rightMap;
            }

            if (boolLC == true && left == true) {
                  spawnX = mapX;
                  lastX = spawnX;
                  spawnY = spawnY;
                  board = loadBoard(leftMap);
                  px = rightSide;
                  py = py;
                  currentMap = leftMap;
            }

            if (boolUC == true && up == true) {
                  spawnY = mapY;
                  lastY = spawnY;
                  spawnX = spawnX;
                  board = loadBoard(upMap);
                  px = px;
                  py = downSide;
                  currentMap = upMap;
            }

            if (boolDC == true && down == true) {
                  spawnY = 0;
                  lastY = spawnY;
                  spawnX = spawnX;
                  board = loadBoard(downMap);
                  px = px;
                  py = 0;
                  currentMap = downMap;
            }

            /** MAP SWITCHING **/
            /** MAP SWITCHING **/
            /** MAP SWITCHING **/

            String currX = Integer.toString(spawnX);
            String currY = Integer.toString(spawnY);
            System.out.println("Current/Last Y: " + currY + "/" + lastY);
            System.out.println("Current/Last X: " + currX + "/" + lastX + " - "
                        + board[spawnY][spawnX] + "\n\n");

            if (boolLC == false) {
                  if (left == true) {
                        if (e.getKeyCode() == KeyEvent.VK_LEFT) {
                              left = true;
                              px = px - 32;
                              lastX = lastX - 1;
                        }
                  }
            } else {
                  if (leftMap > 0)
                        px = rightSide;
            }

            if (boolRC == false) {
                  if (right == true) {
                        if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
                              right = true;
                              px = px + 32;
                              lastX = lastX + 1;
                        }
                  }
            } else {
                  if (rightMap > 0)
                        px = 0;
            }

            if (boolDC == false) {

                  if (down == true) {
                        if (e.getKeyCode() == KeyEvent.VK_DOWN) {
                              down = true;
                              py = py + 32;
                              lastY = lastY + 1;

                        }
                  }
            } else {
                  if (downMap > 0)
                        py = 0;

            }

            if (boolUC == false) {
                  if (up == true) {

                        if (e.getKeyCode() == KeyEvent.VK_UP) {
                              up = true;
                              py = py - 32;
                              lastY = lastY - 1;
                        }
                  }
            } else {
                  if (upMap > 0)
                        py = downSide;
            }

            System.out.println("me.getUsername(): " + me.getUsername());
            me = players.get(me.getUsername());

            me.setX(px);
            me.setY(py);

            if (myCommand == "move") {
                  os.println(me.getUsername() + "|" + px + "|" + py + "|"
                              + currentMap + "|move");
            } else if (myCommand == "chat") {
                  os.println(me.getUsername() + "|1|Hello, world!|yes|chat");
            }

            me.setCommand(myCommand);
            System.out.println("my curernt map! : " + currentMap);

            repaint();

      }

      public void keyReleased(KeyEvent e) {

      } // ignore

      public void keyTyped(KeyEvent e) {
      } // ignore

      public void addInv(int e) {

            String iname = getItem(e)[0];

            String idesc = getItem(e)[1];
            int itypeOf = Integer.parseInt(getItem(e)[2]);
            int iattackAdd = Integer.parseInt(getItem(e)[3]);
            int idefenseAdd = Integer.parseInt(getItem(e)[4]);
            boolean icanSell = Boolean.parseBoolean(getItem(e)[5]);
            boolean icanEat = Boolean.parseBoolean(getItem(e)[6]);
            int iearnedCoins = Integer.parseInt(getItem(e)[7]);

            c.append("\nYou picked up a " + iname + ".");
            // for (String item : arr) list.add(item);
            model.addElement(iname);
            arr.add(iname);
      }

      public static String invShow(String e) {

            String desc = "None";

            if (e == "Flail") {
                  desc = "A crushing blow to the head from this weapon can make BRAIN SLUSHIE!";
            }
            if (e == "Battleaxe") {
                  desc = "Holding this weapon will make you smell like a man, man.";
            }

            return desc;

      }

      public static String[] getItem(int e) {

            String[] stats = new String[7];

            String name = "Null";
            String desc = "Nonexx";
            String typeOf = "0";
            String attackAdd = "0";
            String defenseAdd = "0";
            String canSell = "true";
            String canEat = "false";
            String earnedCoins = "0";

            if (e == 1) {

                  name = "Flail";
                  typeOf = "2";
                  attackAdd = "2";
                  earnedCoins = "5";
            }

            if (e == 2) {

                  name = "Battleaxe";
                  typeOf = "2";
                  attackAdd = "4";
                  earnedCoins = "10";
            }

            return new String[] { name, desc, typeOf, attackAdd, defenseAdd,
                        canSell, canEat, earnedCoins };

      }

      public static void showInventory() {

            for (int i = 0; i < arr.size(); i++) {
                  System.out.println(arr.get(i));
            }
      }

      public void paint(Graphics g) {

            for (row = 0; row < board.length; row++) {
                  for (col = 0; col < board[row].length; col++) {
                        int index = board[row][col];
                        g.drawImage(tiles[index], 32 * col, 32 * row, this);

                  }
            }

            try {
                  for (Player player : players.values()) {
                        System.out.println("++++++++++++++++++++++++++");
                        System.out.println("current map: " + player.getMap());
                        if (player.getUsername() == me.getUsername())
                              player.setMap(currentMap);
                        if (player.getUsername() == me.getUsername())
                              player.setCommand(myCommand);
                        if (player.getMap() == currentMap) {
                              g.drawImage(player.getPlayerImage(), player.getX(), player
                                          .getY(), this);

                        }
                        System.out.println("Command-- " + player.getCommand() + " > "
                                    + player.getUsername() + " moved to X:" + player.getX()
                                    + " and Y: " + player.getY());

                        System.out.println("++++++++++++++++++++++++++");
                  }
            } catch (Exception dan) {
                  System.out.println("No one is on.");
            }

            // qq
            /**
             * for(int runx = 0;runx < 10;runx++) { g.drawImage(userImg[runx],
             * userPX[runx], userPY[runx], this); }
             **/

            if (drawFlail == true && currentMap == 1)
                  g.drawImage(weapon[1], 32 * 4, 32 * 4, this);
            if (drawBattleaxe == true && currentMap == 3)
                  g.drawImage(weapon[0], 32 * 3, 32 * 3, this);
            System.out.println(drawFlail);
      } // end paint method

      public void update(Graphics g) {
            paint(g);
      }

      public int[][] loadBoard(int map) {
            if (map == 1) {
                  return new int[][] {
                              { 7, 7, 7, 7, 7, 7, 13, 13, 13, 13, 7, 7, 7, 7, 7, 7 },
                              { 7, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13,
                                          13, 6 },
                              { 7, 13, 1, 13, 1, 13, 1, 13, 13, 13, 1, 1, 13, 13, 13, 6 },
                              { 7, 13, 1, 1, 1, 13, 1, 13, 13, 13, 1, 13, 13, 13, 13, 6 },
                              { 7, 13, 13, 1, 13, 13, 1, 13, 13, 13, 1, 13, 13, 13, 13, 6 },
                              { 7, 13, 1, 1, 1, 13, 1, 1, 1, 13, 1, 1, 1, 1, 13, 6 },
                              { 7, 13, 1, 13, 13, 13, 13, 13, 1, 13, 13, 1, 13, 13, 13, 6 },
                              { 7, 13, 1, 1, 13, 13, 13, 1, 1, 13, 13, 1, 13, 13, 13, 6 },
                              { 7, 13, 13, 13, 13, 13, 13, 1, 13, 13, 13, 1, 13, 13, 13,
                                          6 },
                              { 7, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13,
                                          13, 6 },
                              { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6 } };
            } else if (map == 2) {
                  return new int[][] {
                              { 145, 145, 55, 145, 145, 55, 184, 163, 55, 163, 163, 55,
                                          145, 181, 145, 145 },
                              { 145, 13, 145, 145, 166, 166, 166, 163, 163, 13, 13, 13,
                                          13, 13, 13, 145 },
                              { 145, 13, 13, 163, 166, 166, 166, 163, 13, 13, 13, 13, 13,
                                          13, 13, 163 },
                              { 145, 13, 13, 163, 166, 166, 166, 163, 13, 145, 145, 145,
                                          145, 145, 13, 145 },
                              { 145, 13, 13, 163, 166, 166, 166, 163, 13, 145, 166, 166,
                                          166, 145, 13, 145 },
                              { 145, 13, 13, 163, 163, 95, 163, 145, 13, 145, 166, 166,
                                          166, 145, 13, 145 },
                              { 145, 13, 13, 13, 13, 13, 13, 13, 13, 166, 166, 166, 166,
                                          145, 13, 145 },
                              { 145, 145, 13, 13, 13, 13, 13, 13, 13, 163, 145, 55, 145,
                                          145, 13, 145 },
                              { 145, 145, 145, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13,
                                          13, 13, 145 },
                              { 145, 145, 145, 145, 13, 13, 13, 13, 13, 13, 13, 13, 13,
                                          13, 13, 145 },
                              { 145, 145, 145, 145, 145, 145, 13, 13, 13, 13, 13, 145,
                                          145, 145, 145, 145 } };
            }

            return board;
      }

      public boolean blocked(int tx, int ty) {
            return BLOCKED_TILES.contains(board[ty][tx]);
      }

      public boolean isInBound(int r, int c) {
            return (r >= 0) && (r < 8) && (c >= 12) && (c < 1);
      }

} // end whole thing3
0
 
LVL 5

Author Comment

by:UrbanTwitch
ID: 33442313
Don't you think I can make it as a List and STILL make JTextArea work?
0
 
LVL 92

Expert Comment

by:objects
ID: 33442329
of course you can.
JList's are more flexible than a List though

You're problem with the JTextArea has nothing to do with your List.
0
 
LVL 5

Author Comment

by:UrbanTwitch
ID: 33442332
Here is what I get when you give me THAT code :)


xxxa.png
0
 
LVL 92

Expert Comment

by:objects
ID: 33442339
need more details on what it should look like :)
0
 
LVL 5

Author Comment

by:UrbanTwitch
ID: 33442456
Stretched all the way. Not just to the longest word. :P
0
 
LVL 92

Expert Comment

by:objects
ID: 33442477
you need to set the preferred size of your list

list.setPreferredSize(new Dimension(width, height));
0
 
LVL 5

Author Comment

by:UrbanTwitch
ID: 33442511
Ah yes, of course.
Now when the applet starts - the list does NOT show UNTIL I click on it... :\

I tried adding this inside the init()
this.setVisible(true);

no luck :\
0
 
LVL 92

Expert Comment

by:objects
ID: 33442819
problem appears to be that you don't call parents paint() method


      public void paint(Graphics g) {

            super.paint(g);   // <--- add this

            for (row = 0; row < board.length; row++) {
                  for (col = 0; col < board[row].length; col++) {
                        int index = board[row][col];
                        g.drawImage(tiles[index], 32 * col, 32 * row, this);

                  }
            }
                ......
0
 
LVL 92

Expert Comment

by:objects
ID: 33442823
> Now when the applet starts - the list does NOT show UNTIL I click on it... :\

thats actually an old problem
 I believe same problem as in your previous q that you prematurely closed
0
 
LVL 5

Author Comment

by:UrbanTwitch
ID: 33443048
OK that works objects. But now my drawn tiles flash every time I move (or call repaint();)


thats actually an old problem
 I believe same problem as in your previous q that you prematurely closed
-- I don't think so. :P

Don't you think it'd be easier if I left the JList as a LIST and have it a JTextArea?
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 92

Expert Comment

by:objects
ID: 33443070
> But now my drawn tiles flash every time I move (or call repaint();)

thats a different problem again :)

> Don't you think it'd be easier if I left the JList as a LIST and have it a JTextArea?

your problem here has nothing to do with that. You had this issue before you changed it, thats why your layout was changing when you clicked on it.

0
 
LVL 5

Author Comment

by:UrbanTwitch
ID: 33443080
1. OK.
2. So should I revert back to have it as a List and keep going with JTextArea or keep going?
0
 
LVL 92

Expert Comment

by:objects
ID: 33443084
Your game panel would actually be better implemented as a JPanel subclass to avoid having to override paint() which is messy (and unecessary). It'll work the way you are doing it but you need to better understanding how painting occurs in swing/awt.
0
 
LVL 92

Expert Comment

by:objects
ID: 33443086
Swing is better option. It provides a better look and more flexibility.
But as I said your current issues have nothing to do with whether you use Swing or AWT.
0
 
LVL 5

Author Comment

by:UrbanTwitch
ID: 33443118
What exactly is my issue... cause now I'm lost. :|
0
 
LVL 92

Expert Comment

by:objects
ID: 33443136
calling super.paint(), as it handles the painting of applet and the components it contains.

The flickering that you're getting is because your applet is now getting painted correctly. A lot of the strange little problems you have been having, such as things appearing when you clicked on the map would be caused by painting not being done correctly.
0
 
LVL 92

Expert Comment

by:objects
ID: 33443160
for the flickering i would suggest the first step would be to subclass jpanel, that way you don't need to overirde paint() in your applet
0
 
LVL 92

Expert Comment

by:objects
ID: 33443161
yell if I've lost you and i'll show you whats needed :)

0
 
LVL 5

Author Comment

by:UrbanTwitch
ID: 33443205
Lost me at subclass jpanel, :P

Never used those before.

(btw did you get my rar file)
0
 
LVL 92

Expert Comment

by:objects
ID: 33443220
0
 
LVL 5

Author Comment

by:UrbanTwitch
ID: 33443248
Oh, I guess I have to handle the tile painting in a new method of public void paintComponent(Graphics g)

Something like this:

      public void paintComponent(Graphics g)
        {
           if(paint == null)
           {
              try
              {
                  // Create TexturePaint instance the first time
        
                  int height = image.getHeight(this);
                  int width = image.getWidth(this);
                  bi = (BufferedImage) createImage(width, height);
                  Graphics2D biG2d = (Graphics2D) bi.getGraphics();
                  
                        for (row = 0; row < board.length; row++) {
                        for (col = 0; col < board[row].length; col++) {
                        int index = board[row][col];
                        biG2d.drawImage(tiles[index], 32 * col, 32 * row, this);

                        }
                        }
              
                  paint = new TexturePaint(bi,
                     new Rectangle(0,0,width,height));
              }
              catch (Exception ex)
              {
                  ex.printStackTrace();
              }
           }

Right?

I'll open a new Q for this.
0
 
LVL 92

Expert Comment

by:objects
ID: 33443266
yes thats on the right track.

0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Enhanced For loop vs ForEach Loop 4 56
triangle challenge 4 77
countHi challenge 25 84
mapShare challenge 13 68
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now