Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 309
  • Last Modified:

Java Client Applet - Painting

OK so I will be leaving my server on until I say it's offline in this topic.

What the problem? Let's say a player A logs in, and takes the sword... then player B logs in, th e server sends what to draw for the items. So what happens is EVERY ITEM is draw on the board for a SPLIT SECOND then the Sword is taken away.

I want that split second gone and if the drawSword is false... then to NOT draw AT ALL...

That function is this at the very end of tileGen in the GamePanel class:
                  if (drawFlail == true && currentMap == 1) g.drawImage(weapon[1], 32 * 4, 32 * 4, this);
				  if (drawBattleaxe == true && currentMap == 1) g.drawImage(weapon[0], 32 * 3, 32 * 2, this);
                  if (drawSword == true && currentMap == 1) g.drawImage(weapon[2], 32 * 13, 32 * 7, this);

Open in new window


It is inside the public void paintComponent(Graphics g) method. I was wondering if I could do the paintComponent BEFORE the run() is made that way.. but then I thought.. paintComponent is drawn first before run() then the server tells the client which items should be drawn.. so I don't know what to do.

Connect to the game: http://mystikrpg.com/new/play.php
Please do not take any items for this question purpose. Just observe. My character will be on the bottom right sitting idle... I will take the Sword so you will notice it flash before it disappears...

thanks.

Here is my run() method
      public void run() {

            Socket s = null;

            try {



                 String host = "69.133.110.152";
                 s = new Socket(host,Integer.valueOf(getParameter("port")));



                  in = new BufferedReader(new InputStreamReader(s.getInputStream()));

                  os = new PrintStream(s.getOutputStream());
                  System.out.println("\n******** Connecting to " + host + " on port " + getParameter("port"));

                  playerLogin();


                  int buf = -1;
                  String responseLine;

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

                        chatOne = true;

                         repaint();
                        // 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]);
System.out.println("drawFlail: " + drawFlail + " | drawSword: " + drawSword + " | drawBattleaxe: " + drawBattleaxe);
if (drawFlail || drawSword || drawBattleaxe) {
 if ("pickup".equalsIgnoreCase(temp[4]))
{
	//c.append("\n"+temp[0] + "|" + temp[1] + "|" + temp[2] + "|" + temp[3] + "|" + temp[4]);
	//int changeX = Integer.parseInt(temp[1]);
	//int changeY = Integer.parseInt(temp[2]);
	//board[changeY][changeX] = 4;

	if("drawFlail".equals(temp[2])) drawFlail = false;
	if("drawBattleaxe".equals(temp[2])) drawBattleaxe = false;
    if("drawSword".equals(temp[2])) drawSword = false;

	int theItem = Integer.parseInt(temp[1]);
	pickedUp(theItem, temp[0]);
	if("yes".equalsIgnoreCase(temp[3])) {
		c.append("\n"+ temp[0] + " picked up a " + getItem(theItem)[0]+".");
	}
	System.out.println("~~~~~~~~~~~~~~~~~~~\nITEM REMOVED " + temp[2] + "\n~~~~~~~~~~~~~~~~~~~~~~");
	System.out.println("drawFlail - " + drawFlail + " | drawSword - " + drawSword + " | drawBattleaxe - " + drawBattleaxe);

}
}
                        if ("chat".equalsIgnoreCase(temp[4])) {
							if (temp[2].indexOf( "has left" ) > -1 ) {
								c.append("\n"+temp[0] + " has logged out.");
							  		      players.remove(temp[0]);


							}else{
								String theirMap = Integer.toString(currentMap);
                              if(theirMap.equals(temp[1])) {
                              c.append("\n" + temp[0] + ": " + temp[2]);
						  }else if(temp[0].equals(me.getUsername())){
							   c.append("\n" + temp[0] + ": " + temp[2]);
						  }

                              System.out.println("CurrentMap:" + currentMap + " | temp1 " + temp[1]);
						  }
                              c.setCaretPosition(c.getDocument().getLength());
                              //c.append("\n player = " + player.getUsername() + " | me. " + me.getUsername());

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


							 if (temp[2].indexOf( "/kick" ) > -1) {
								 String[] whoKick = str.split(" ");
								 String[] kicked = whoKick[1].split("\\|");
								 players.remove(kicked[0]);
								 c.append("\n"+kicked[0] + " has been kicked by " + temp[0]);
								 c.setCaretPosition(c.getDocument().getLength());

							 }

							int realx = 0;
							for (Player playert : players.values()) realx++;
usersOn.setText("Players online: " + realx);


                        }
if(isInteger(temp[2]) == true) {
                        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);

                              int realy = 0;
							  for (Player playert : players.values()) realy++;
usersOn.setText("Players online: " + realy);

                        }
                        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]));
                        }
                        System.out.println("TEMP 4 " + temp[4]);
                        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!");
                  c.append("\nServer crashed!");
                  rwe.printStackTrace();
            } finally {
                  try {
                        s.close();
                  } catch (IOException ere) {
                        ere.printStackTrace();
                  }
            }

            c.setCaretPosition(c.getDocument().getLength());

			count = 0;
			for (Player player : players.values()) {
				System.out.println(player.getUsername() + " is still on. new count: " + count);
			count++;
			}

			usersOn.setText("Players online: " + count);
      }

Open in new window


and here is the GamePanel which draws it...
class GamePanel extends JPanel {

            public void paintComponent(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 == 1) g.drawImage(weapon[0], 32 * 3, 32 * 2, this);
                  if (drawSword == true && currentMap == 1) g.drawImage(weapon[2], 32 * 13, 32 * 7, this);

            }

      }

Open in new window

0
UrbanTwitch
Asked:
UrbanTwitch
  • 13
  • 9
1 Solution
 
objectsCommented:
If I understand correctly what should be doing is displaying nothing until the new client gets details from server. is initially no items are displayed
0
 
objectsCommented:
ie. drawFlail, drawSword etc should be initially false
0
 
UrbanTwitchAuthor Commented:
ooo, EE mobile. Cool.

Anyways ... if I do THAT.. then the items on the ground won't draw at ALL.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
objectsCommented:
they will once the server tells the client whats there. at that time you can set the required flags to true
0
 
UrbanTwitchAuthor Commented:
Anyway, the server will be off now. But you can picture it in your head. I have added a new feature to toggle your stats in-game. :)
0
 
UrbanTwitchAuthor Commented:
@objects - So, so I should set to false in the INIT?
Cause if I do.. they won't even get drawn...


                  if (drawFlail == true && currentMap == 1) g.drawImage(weapon[1], 32 * 4, 32 * 4, this);
				  if (drawBattleaxe == true && currentMap == 1) g.drawImage(weapon[0], 32 * 3, 32 * 2, this);
                  if (drawSword == true && currentMap == 1) g.drawImage(weapon[2], 32 * 13, 32 * 7, this);

Open in new window

0
 
objectsCommented:
> @objects - So, so I should set to false in the INIT?

yes, whereever it gets initialised

> Cause if I do.. they won't even get drawn...

which is what you want :)
they shouldn't be drawn until the server tells the client there is something to draw
0
 
UrbanTwitchAuthor Commented:
By the way, I have this:
drawFlail = true;
            drawBattleaxe = true;
            drawSword = true;  
inside the init() file...
I guess it runs like this:

init() -> paintComponent -> run()

weird...
0
 
UrbanTwitchAuthor Commented:
No, I mean ALL of them won't get drawn... even if they were picked up or not.
0
 
UrbanTwitchAuthor Commented:
I think... let me check on that statement I just made.
"No, I mean ALL of them won't get drawn... even if they were picked up or not."
0
 
UrbanTwitchAuthor Commented:
Nope. I was right. All of the images are not even drawn.
It is because the server sends the message to OTHER clients... under the run() method inside the Server file. erm.. i think. also its under the if outsteam is not null... which means the client send back a message to server that has the word... pickup..

server java: http://www.rerw.pastebin.com/wCp7GPmm
0
 
objectsCommented:
> No, I mean ALL of them won't get drawn... even if they were picked up or not.

which is what you want :)

you only want to paint something once the server tells you it exists.
Until that time you don't want paint anything, o/wise you're going to get it flash up

currently you're painting *everything* until the server tells you its *not* there. Thats the wrong way around.
0
 
UrbanTwitchAuthor Commented:
> currently you're painting *everything* until the server tells you its *not* there. Thats the wrong way around.

But I AM telling it to paint... in server java here iis a snippet:
                                                            if (drawFlail == false) {
                                                            for (ClientThread client : clients) {
                                                            os.println("xxnull|1|drawFlail|no|pickup");
                                                            System.out.println("YAY SENT!");
                                                            }
                                                            }

It tells the client to NOT draw if it's there... Hm...... I think I have to make it to tell the client to DRAW the weapon... thats why I don't see drawFlail == true in my server and why i am not seeing the image being draw on the client... maybe?
0
 
objectsCommented:
but the client paints it before it hears from the server. thats why you need to initially *not* paint it, otherwise it will flash up on the map until it hears from the server.

>                                                             if (drawFlail == false) {

and the server needs to tell the client what is and isn't there, not just whats not there.
0
 
UrbanTwitchAuthor Commented:
"but the client paints it before it hears from the server. thats why you need to initially *not* paint it, otherwise it will flash up on the map until it hears from the server."

I can't really do that... init() -> paintComponent -> run()

"and the server needs to tell the client what is and isn't there, not just whats not there."

Right... honestly.. would it be the same as drawFlail == false but OPPOSITE?

Anyways -- going to bed now. See you in like... 8-10 hrs. :P
0
 
objectsCommented:
> I can't really do that... init() -> paintComponent -> run()

you can just set it to false initially
and thats not how it works btw, there are two threads involved

> Right... honestly.. would it be the same as drawFlail == false but OPPOSITE?

it would, but the client needs to know the state of the map before it can know what to draw.
currently you assume the items is on the map, thus it gets displayed initially

in fact the server should be telling the client what needs to be displayed.
Currently you tell it whats *not* there
0
 
UrbanTwitchAuthor Commented:
"you can just set it to false initially"
I already told you that I set them to false and the client didn't draw ANY of them.

So i  guess I have to tell the client to draw them from the server... right?
0
 
UrbanTwitchAuthor Commented:
"So i  guess I have to tell the client to draw them from the server... right?"
I just though of this... the cleint knows WHAT to draw and not to draw because it flashes before it makes it decision... so (like you said objects)... i have something going the opposite way... but what??
0
 
objectsCommented:
> i have something going the opposite way... but what??

you paint it and then remove when the server tells you its not there

when you should:

not paint it and then paint it when the server tells you it is there

so on the client set the flag to be initially false
and on the server send new clients a message telling them what is on the map
0
 
UrbanTwitchAuthor Commented:
OK so let me get this straight:

> so on the client set the flag to be initially false
yes I can do that.

> and on the server send new clients a message telling them what is on the map
It's not painting because I tell the client whats NOT there when really i should tell the client what IS there... right? should be easy to do -- right?
0
 
UrbanTwitchAuthor Commented:
OK I think I found a solution. Instead of dealing the the draws whatever... I should do this.

On the server side it should have an arraylist like this
1|5|6|1
1 is the ID of item
5 is the X position I drop it at
6 is the Y position I drop it at
1 is the currentMap its dropped at

Now the server side's arraylist if for the people who connect AFTER the item is dropped and the client side arraylist is for the people who are on while it is dropped.

So whenever a person has dropped an item it will send a message to the server of the a new item being dropped and then somewhre in the tileGen I will do a for-loop through EACH item in arraylist when someone pressed G (to pick up an item) and to pick up the item and them remove iit...

did that make sense?
0
 
objectsCommented:
Thats doing what I suggested in your earlier q, ie. have the server track the location of all items on the maps
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 13
  • 9
Tackle projects and never again get stuck behind a technical roadblock.
Join Now