Solved

Java Client Applet - Painting

Posted on 2010-08-19
22
300 Views
Last Modified: 2012-06-27
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
Comment
Question by:UrbanTwitch
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 13
  • 9
22 Comments
 
LVL 92

Expert Comment

by:objects
ID: 33482234
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
 
LVL 92

Expert Comment

by:objects
ID: 33482340
ie. drawFlail, drawSword etc should be initially false
0
 
LVL 5

Author Comment

by:UrbanTwitch
ID: 33482407
ooo, EE mobile. Cool.

Anyways ... if I do THAT.. then the items on the ground won't draw at ALL.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 92

Expert Comment

by:objects
ID: 33482427
they will once the server tells the client whats there. at that time you can set the required flags to true
0
 
LVL 5

Author Comment

by:UrbanTwitch
ID: 33482437
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
 
LVL 5

Author Comment

by:UrbanTwitch
ID: 33482441
@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
 
LVL 92

Expert Comment

by:objects
ID: 33482448
> @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
 
LVL 5

Author Comment

by:UrbanTwitch
ID: 33482453
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
 
LVL 5

Author Comment

by:UrbanTwitch
ID: 33482456
No, I mean ALL of them won't get drawn... even if they were picked up or not.
0
 
LVL 5

Author Comment

by:UrbanTwitch
ID: 33482462
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
 
LVL 5

Author Comment

by:UrbanTwitch
ID: 33482477
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
 
LVL 92

Expert Comment

by:objects
ID: 33482482
> 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
 
LVL 5

Author Comment

by:UrbanTwitch
ID: 33482498
> 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
 
LVL 92

Expert Comment

by:objects
ID: 33482523
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
 
LVL 5

Author Comment

by:UrbanTwitch
ID: 33482554
"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
 
LVL 92

Expert Comment

by:objects
ID: 33482583
> 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
 
LVL 5

Author Comment

by:UrbanTwitch
ID: 33482695
"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
 
LVL 5

Author Comment

by:UrbanTwitch
ID: 33482701
"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
 
LVL 92

Expert Comment

by:objects
ID: 33483043
> 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
 
LVL 5

Author Comment

by:UrbanTwitch
ID: 33487835
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
 
LVL 5

Author Comment

by:UrbanTwitch
ID: 33489885
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
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 33490057
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

630 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