Page 1 of 2 12 LastLast
Results 1 to 20 of 32

Thread: H3lp

  1. #1
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default Help.

    Hey, I have made a simple application, you can run around with a square.
    Here is the code:
    Java Code:
    package org.square.Client;
    
    import java.awt.Color;
    import java.awt.Font;
    import java.awt.Graphics;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.io.*;
    import java.net.*;
    
    import javax.swing.JApplet;
    
    public class CreateApplet extends JApplet implements MouseListener, KeyListener{
    	
    	public int[] playerIDs = {};
    	public boolean loggedIn = false;
    	public String color;
    	
    	private static final long serialVersionUID = 1L;
    	int x;
    	int y;
    	
    	public void init(){
    		setSize(500, 500);
    		this.addMouseListener(this);
    		this.addKeyListener(this);
    	}
    	
    	public void paint(Graphics g){
    		if(loggedIn == false){
    			g.setFont(new Font("Arial", Font.PLAIN, 24));
    			
    			g.drawString("Select a color", 200, 200);
    			// COLORS
    			g.setColor(Color.black);
    			g.drawString("Black", 240, 230);
    			g.setColor(Color.red);
    			g.drawString("Red", 240, 250);
    			g.setColor(Color.yellow);
    			g.drawString("Yellow", 240, 270);
    			g.setColor(Color.blue);
    			g.drawString("Blue", 240, 290);
    			
    			//COLOR BOXES
    			g.setColor(Color.black);
    			g.drawRect(240, 210, 75, 85);
    			g.drawLine(240, 231, 315, 231);
    			g.drawLine(240, 251, 315, 251);
    			g.drawLine(240, 271, 315, 271);
    			
    		}
    		if(loggedIn == true){
    			g.setColor(Color.WHITE);
    			g.fillRect(0, 0, 500, 500);
    			CreateSquare(g);
    		}
    	}
    	
    	public enum colors{
    		black, red, yellow, blue, noColor;
    	}
    	
    	public colors getColors(){
    		if(color == null)
    			return colors.noColor;
    		if(color.equals("black")){
    			return colors.black;
    		}
    		if(color.equals("red")){
    			return colors.red;
    		}
    		if(color.equals("yellow")){
    			return colors.yellow;
    		}
    		if(color.equals("blue")){
    			return colors.blue;
    		}
    		
    		return colors.noColor;
    	}
    	
    	public void CreateSquare(Graphics g){
    		switch(getColors()){
    		case noColor:
    			break;
    			
    		case black:
    			g.setColor(Color.black);
    			g.fillRect(x, y, 50, 50);
    			break;
    			
    		case red:
    			g.setColor(Color.red);
    			g.fillRect(x, y, 50, 50);
    			break;
    			
    		case yellow:
    			g.setColor(Color.yellow);
    			g.fillRect(x, y, 50, 50);
    			break;
    			
    		case blue:
    			g.setColor(Color.blue);
    			g.fillRect(x, y, 50, 50);
    			break;
    		}
    	}
    	
    	public void mouseClicked(MouseEvent e) {
    		int x = e.getX();
    		int y = e.getY();
    		int MinX = e.getX();
    		int MinY = e.getY();
    		int MaxX = e.getX();
    		int MaxY = e.getY();
    		if(loggedIn == false){
    //				xMin      xMax        yMin      yMax
    			if(MinX > 240 && MaxX < 315 && MinY > 210 && MaxY < 230){
    				System.out.println("Clicked on BLACK");
    				color = "black";
    				loggedIn = true;
    				repaint();
    			}
    			if(MinX > 240 && MaxX < 315 && MinY > 230 && MaxY < 250){
    				System.out.println("Clicked on RED");
    				color = "red";
    				loggedIn = true;
    				repaint();
    			}
    			if(MinX > 240 && MaxX < 315 && MinY > 250 && MaxY < 270){
    				System.out.println("Clicked on YELLOW");
    				color = "yellow";
    				loggedIn = true;
    				repaint();
    			}
    			if(MinX > 240 && MaxX < 315 && MinY > 270 && MaxY < 295){
    				System.out.println("Clicked on BLUE");
    				color = "blue";
    				loggedIn = true;
    				repaint();
    			}
    		}
    		
    		
    		System.out.println();
    		System.out.println("X >" + x + "<");
    		System.out.println("Y >" + y + "<");
    	}
    
    	@Override
    	public void mouseEntered(MouseEvent arg0) {
    		// NEVER USED
    		
    	}
    
    	@Override
    	public void mouseExited(MouseEvent arg0) {
    		// NEVER USED		
    	}
    
    	@Override
    	public void mousePressed(MouseEvent arg0) {
    		// NEVER USED		
    	}
    
    	@Override
    	public void mouseReleased(MouseEvent arg0) {
    		// NEVER USED		
    	}
    
    	@Override
    	public void keyPressed(KeyEvent ke) {
    		switch (ke.getKeyCode()) {
            case KeyEvent.VK_UP:
            	if(y > 0){
            		y--;
            		repaint();
            	}
            	if(y > 0){
            		y--;
            		repaint();
            	}
            	break;
            case KeyEvent.VK_DOWN:
            	if(y < 450){
            		y++;
            		repaint();
            	}
            	if(y < 450){
            		y++;
            		repaint();
            	}
            	break;
            case KeyEvent.VK_LEFT:
            	if(x > 0){
            		x--;
            		repaint();
            	}
            	if(x > 0){
            		x--;
            		repaint();
            	}
            	break;
            case KeyEvent.VK_RIGHT:
            	if(x < 450){
            		x++;
            		repaint();
            	}
            	if(x < 450){
            		x++;
            		repaint();
            	}
            	break;
    		}
    		
    	}
    
    	@Override
    	public void keyReleased(KeyEvent arg0) {
    		// NOT USED
    		
    	}
    
    	@Override
    	public void keyTyped(KeyEvent arg0) {
    		// NOT USED
    		
    	}
    }
    And I need help with hooking it up to a server and for example person1 comes on the server chooses a color, and he can run around with the square and he can also see other people running around.
    My question is what would be the best way of doing this?
    And is there any way I could improve my code?
    Last edited by PhQ; 08-04-2010 at 05:03 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    need help with hooking it up to a server
    Sounds like you need a lot more than that on the server if you want to service a number of clients running your code and to have each know of the other.

    Code improvements:
    Remove hardcoded literals from code. Use variables:
    if(y < 450){
    MinX > 240 && MaxX < 315
    g.drawRect(240, 210, 75, 85);
    g.drawLine(240, 231, 315, 231);
    g.drawLine(240, 251, 315, 251);
    g.drawLine(240, 271, 315, 271);

  3. #3
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default

    Java Code:
    package org.square.Client;
    What for this package must be in your program?

    comes on the server chooses a color
    When someone chooses a color then I guess color will be sent with GET or POST method, and that color will make changes in final HTML code that servers will give to the user back.
    But doing that for lot of users to see at the same time is not easy at all.

  4. #4
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default

    Quote Originally Posted by cselic View Post
    Java Code:
    package org.square.Client;
    What for this package must be in your program?


    When someone chooses a color then I guess color will be sent with GET or POST method, and that color will make changes in final HTML code that servers will give to the user back.
    But doing that for lot of users to see at the same time is not easy at all.
    Why do I need to use HTML code?
    and how can I do that?
    Do I need to learn about synchronization?

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    Why do I need to use HTML code
    HTML is often used for "hooking it up to a server" via a browser.
    How are the other players going to connect to the server? Will each of them have an executable app on their PC?
    person1 comes on the server
    What code will allow person1 to "come on the server"?

  6. #6
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default

    Quote Originally Posted by Norm View Post
    HTML is often used for "hooking it up to a server" via a browser.
    How are the other players going to connect to the server? Will each of them have an executable app on their PC?

    What code will allow person1 to "come on the server"?
    I'm going to make it right now. Should I make it threaded? Or should I do it later?

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    I'm going to make it right now.
    What is the "it" you mention?
    Can you get more details on what you are trying to do?

  8. #8
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default

    Quote Originally Posted by Norm View Post
    What is the "it" you mention?
    Can you get more details on what you are trying to do?
    I am going to make a connection method so person1 can connect to the server. Should I make the server threaded, or should I do it later?
    Last edited by PhQ; 08-04-2010 at 04:35 PM.

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    There are still a LOT of details you are skipping over.

    For initial testing you could leave out the thread. Later you'll need it.

  10. #10
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,380
    Blog Entries
    7
    Rep Power
    20

    Default

    @OP: I don't consider 'H3lp' an even half desriptive topic title, even if it were spelled correctly. Please don't use that childish nonsense in this forum.

    kind regards,

    Jos

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    What type of stream to use would follow from your app's needs.
    Have you designed a protocol to use between your clients and the server?

  12. #12
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default

    Quote Originally Posted by Norm View Post
    What type of stream to use would follow from your app's needs.
    Have you designed a protocol to use between your clients and the server?
    Yes, I have.

    I would have to do this:
    Whenever person1 moves (clicks up,down,left or right) it will send the color, x, y to the server and person2 will recieve the person1's color, x and y and update the panel.
    What output/input stream would be best to do this?

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    How will you send a color? As 3 RGB bytes or as a String: "RED"?
    How will you send what person the color belongs to? As a byte (max of 256) or ?
    How will you sent the x and y values? As shorts (max of 16K)?

    The record to be sent must be designed byte by byte showing what is in each byte.

    What other info needs to be passed to all the clients? Initial position and color?
    How do you differentiate between a move record and an initial position record?

  14. #14
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default

    Quote Originally Posted by Norm View Post
    How will you send a color? As 3 RGB bytes or as a String: "RED"?
    How will you send what person the color belongs to? As a byte (max of 256) or ?
    How will you sent the x and y values? As shorts (max of 16K)?

    The record to be sent must be designed byte by byte showing what is in each byte.

    What other info needs to be passed to all the clients? Initial position and color?
    How do you differentiate between a move record and an initial position record?
    I will send the color as a String.
    I could make 2 arrays one with player's ID and one with player's color so I will have to send the ID.
    I will send the x and y values as shorts.

    No other info needs to be passed to all clients, just the position and the color.
    I am not sure how about the last question.

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    Do you have a design for the record(s) to be sent?
    The record to be sent must be designed byte by byte showing what is in each byte.

  16. #16
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default

    Quote Originally Posted by Norm View Post
    Do you have a design for the record(s) to be sent?
    The record to be sent must be designed byte by byte showing what is in each byte.
    I'm confused.

    Is there any good socket libraries that I could use? I think it would be easier.
    Last edited by PhQ; 08-06-2010 at 05:38 PM.

  17. #17
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    Is there any good socket libraries that I could use?
    Sockets send any data that you want to send. What would a library provide?
    The question is "What do you want to send?" You need to be specific.
    As a programmer you need to work with bits and bytes. A designer could say send the color and names of the people. But at the byte level how is that data to be sent and received?

  18. #18
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default

    I finally got it.
    I am going to use ObjectStream.

    Last question
    How can I call a method every 0.1 seconds?

  19. #19
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    How can I call a method every 0.1 seconds?
    Use a Timer

  20. #20
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default

    Quote Originally Posted by Norm View Post
    Use a Timer
    Ok, last question

    Do you know if someone connects... How can I make a method to create a square for him and set him specific x and y values for him?

    It's hard to explain...

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •