Java-Gaming.org    
Featured games (78)
games approved by the League of Dukes
Games in Showcase (427)
Games in Android Showcase (89)
games submitted by our members
Games in WIP (466)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: 1 2 [3]
  ignore  |  Print  
  Networked Gaming  (Read 12825 times)
0 Members and 1 Guest are viewing this topic.
Offline Mr_Light

Senior Member




shiny.


« Reply #60 - Posted 2008-12-11 19:15:20 »

first of all wow thanks for the help i cant imagine how long it took you to write all that.

creating widgets seems simple enough however textField seems like it could be a bit complicated.  For instance scrolling? in the chat i would like each new message to be on a new line and obviously when the text reaches the bottom of the screen or widget it should be scrollable. how do you implement that?
keep separate components for separate things - do one thing and do it well / separation of concerns.
Your console thing probably doesn't require input etc. There are a couple of different approaches that work and there isn't really a consensus about what's best. Have a look at GoF and not just the MVC cause that can be implemented a zillion ways. But the idea of how complex components are just a collection of simple components.

as for transfering the userlist i already have each player objected created with a userName to identify it and yes thats all i need to transfer not the actual object.  I believe i will go with when a user connects the server sends the current user list and later on sends an update with a leave/join.

getting into protocol is kinda where things get confusing because the server and client have to send so many different types of messages.  im having trouble making the client understand what to do with the data it recieves.  it seems like an easy way to do this (tell me if this is even possible)  would be to say:

update: userName
remove: userName
message: "message"
etc. 

preface the type of message like above or even shorten it to just one letter and a colon to represent the type of message. however after that is there a way to remove a portion of a string?  for instance with message: "message" i would have to remove "message:" and display the rest to the user.

sorry if thats a little confusing...
I think I get where your going, being 'smart' doesn't necessary work well and will spin the complexity out of the  control.

update:userName
remove:userName

u:username might look like a good idea but there are pretty much only 26 options for identifiers going from update to u throws human readability out the window so why stick to 'u' at all then? you might as well use numbers (IRC is based on this if I remember correctly.) but note that it is performance optimization (which might well be premature) and is an implementation detail that shouldn't spread in your code.(which is a code smell,to use a hip word)

enums work well and also it allows you to use in a switch

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
public class Protocol {
   
   enum MessageType {
      AddUser(1),
      RemoveUser(2);
     
      private final int indentifier;

      private MessageType(int indentifier) {
         this.indentifier = indentifier;
      }
     
      public MessageType getMessageByInt(int indentifier) {
         for(MessageType type : values()) {
            if(type.indentifier == indentifier) return type;
         }
         return null;
      }
   }

}






It's harder to read code than to write it. - it's even harder to write readable code.

The gospel of brother Riven: "The guarantee that all bugs are in *your* code is worth gold." Amen brother a-m-e-n.
Offline neoskunk

Junior Member





« Reply #61 - Posted 2008-12-11 21:24:48 »

all that makes sense but i still dont understand how the client will interpret the data....

say it recieves:

'1 me'
'1 you'
'1 hi'

1 being add user like in your example. how does the client get rid of the 1 in front of everything?  you wouldn't want a 1 to appear in front of every username.  the only way i can think of would be to send multiple messages, the first one being a 1 then followed by a username but that seems uneccasary and not optimized.

just to make sure we are on the same page i have only been sending and recieving strings like this...

to send a message...
1  
out.println(text);


and to recieve...
1  
text = in.readLine().trim();
Offline stef569

Junior Member





« Reply #62 - Posted 2008-12-12 03:12:20 »

Interesting topic! Mr_Light your making this into a tutorial or what  Smiley

To answer your q
well, you can split on spaces and have a string for each parameter.
search for array split

But that won't work for user W A C K O because it will return 5 whitespaces.
So to work around that you can prefix the username with the length of the username

1 = login
9 = amount to read
W A C K O = name
1 9W A C K O

I've been struggling with the same problem.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline neoskunk

Junior Member





« Reply #63 - Posted 2008-12-12 10:30:11 »

awesome thanks for the help.   ill see what i can find on google but why is it called array split?  aren't i using strings? or is it that you split the strings different parts into an array?

edit: nevermind i answered my own question.  However, would it not be easier to do it something like this:

1*W A C K O

and split it at the "*" instead of spaces
Offline Mr_Light

Senior Member




shiny.


« Reply #64 - Posted 2008-12-12 17:16:00 »

Interesting topic! Mr_Light your making this into a tutorial or what  Smiley

Might actually do that at some point but considering other work I also need to finish work for well euh work  Wink

awesome thanks for the help.   ill see what i can find on google but why is it called array split?  aren't i using strings? or is it that you split the strings different parts into an array?
The result is probably an array of strings Wink

edit: nevermind i answered my own question.  However, would it not be easier to do it something like this:

1*W A C K O

and split it at the "*" instead of spaces
As you can probably tell there are a couple of approaches to arrange the data in the protocol:

You can use a predefined fixed length fixed format.
You can encode the lengths in the message.
EndOfXXX / separator 'bit'(can be a character or a certain series of bytes).

Depending on the data your sending any of the above might work well. (There are a couple of other variants but those don't really make sense considering games)

It's probably wise to use an EndOfXXX / separator 'bit' that doesn't occur often (never presume that you never send it, in a different context) When you do send the 'bit' make sure you escape it. (Google for escape characters)

It's harder to read code than to write it. - it's even harder to write readable code.

The gospel of brother Riven: "The guarantee that all bugs are in *your* code is worth gold." Amen brother a-m-e-n.
Offline neoskunk

Junior Member





« Reply #65 - Posted 2008-12-13 02:04:03 »

in terms of creating widgets what should my class extend? canvas? panel? component?

each widget is its own object correct?  and then is added to the applet?


maybe you know a good website on creating custom widgets?
Offline Mr_Light

Senior Member




shiny.


« Reply #66 - Posted 2008-12-13 05:43:16 »

in terms of creating widgets what should my class extend? canvas? panel? component?

each widget is its own object correct?  and then is added to the applet?


maybe you know a good website on creating custom widgets?
Well if you create your own they really don't need to extend anything. You probably want an interface with adleast a method draw(Graphics g) or similar.

It's harder to read code than to write it. - it's even harder to write readable code.

The gospel of brother Riven: "The guarantee that all bugs are in *your* code is worth gold." Amen brother a-m-e-n.
Offline neoskunk

Junior Member





« Reply #67 - Posted 2008-12-13 06:21:29 »

but without extending anything what graphics object am i drawing to?
Offline Mr_Light

Senior Member




shiny.


« Reply #68 - Posted 2008-12-13 21:18:05 »

Well because you do active rendering your eventually draw on the bufferstrategy

But overall you just pass a graphics context to a draw method; you'll call the graphics context with instructions and it eventually ends up at your screen.

'Drawing to.. something' is a matter of abstraction, 't is in the eye of the beholder really.

Perhaps an example will work better: (from memory)

1  
2  
3  
4  
5  
6  
7  
8  
9  
Graphics g = null; // ... get somewhere.
for(Drawable drawable : drawables) {
   Location location = drawable.getLocation();
   Graphics localGraphics = g.create();
   localGraphics.translate(location.x, location.y);
   localGraphics.setClip(0, 0, drawable.getWidth(), drawable.getHeight());
   drawable.draw(localGraphics);
   localGraphics.dispose();
}

You'll have to look if setClip works as advertised.

Which will make the draw(Graphics g) work the same as any paint()(AWT)/paintComponent()(swing)

It's harder to read code than to write it. - it's even harder to write readable code.

The gospel of brother Riven: "The guarantee that all bugs are in *your* code is worth gold." Amen brother a-m-e-n.
Offline neoskunk

Junior Member





« Reply #69 - Posted 2008-12-15 09:31:43 »

so a much simpler example just to clarify this is what you are talking about...

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
import java.applet.*;
import java.awt.*;
import java.awt.Graphics;

public class Widget extends Applet
{
    TextBox text = new TextBox();
   
    public void paint(Graphics g)
    {
        text.draw(g);
    }
}

class TextBox
{  
    public void draw(Graphics g)
    {
        g.drawRect(10,10,100,100);
    }
       
}


i know thats extremely simple but at least for the drawing of the widget am i heading in the right direction?

also i found this website that talks about creating widgets by extending canvas and composite

http://www.eclipse.org/articles/Article-Writing%20Your%20Own%20Widget/Writing%20Your%20Own%20Widget.htm

is this a bad strategy or would it be easier?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Mr_Light

Senior Member




shiny.


« Reply #70 - Posted 2008-12-15 15:51:19 »


That is based on writing custom components for SWT since SWT is based on native implementation it's probably not going to work with applets. Also it will draw in to much complexity and thus will kill the advantage as to why you where going to write your own.

Your code is in the right direction.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
import java.applet.Applet;
import java.awt.Graphics;
import java.util.ArrayList;
import java.util.List;

public class Game extends Applet {
   
   private List<Widget> widgets = new ArrayList<Widget>();

   @Override
   public void init() {
      widgets.add(new NeoLabel("Hello world",Location.getLocation(10, 80),10,100));
      widgets.add(new NeoLabel("foobar",Location.getLocation(10, 20),10,100));
   }  
   
   @Override
   public void paint(Graphics g) {
      drawWidgets(g);
   }
   
   public void drawWidgets(Graphics g) {
      for(Widget widget : widgets) {
         Location location = widget.getPosition();
         Graphics localGraphics = g.create();
         localGraphics.translate(location.x, location.y);
         localGraphics.setClip(0, 0, widget.getWidth(), widget.getHeight());
         widget.draw(localGraphics);
         localGraphics.dispose();
      }
   }
   
}

interface Widget {
   void draw(Graphics g);
   Location getPosition();
   int getWidth();
   int getHeight();
}

class NeoLabel implements Widget {

   private Location position;
   private String value;
   private int height;
   private int width;

   public NeoLabel(String value, Location position, int height, int width) {
      this.value = value;
      this.position = position;
      this.height = height;
      this.width = width;
   }
   
   @Override
   public void draw(Graphics g) {
      g.drawString(value, 0, 10);
   }

   @Override
   public int getHeight() {
      return height;
   }

   @Override
   public Location getPosition() {
      return position;
   }

   @Override
   public int getWidth() {
      return width;
   }
}

class Location {
   public final int x,y;
   
   private Location(int x, int y) {
      this.x = x;
      this.y = y;
   }
   
   public static Location getLocation(int x, int y) {
      return new Location(x,y);
   }
}


But you might want to code against an interface.
Also translate(x,y) helps to allow you to move your widgets around. To be able to transelate you need to know the widgets position hence the method getPosition() on the interface.
SetClipping helps to improve performance and 'sandboxes' components. to be able to use it you need to know the height and width.

You probably want to add some methods so you can pass input to the components.

It's harder to read code than to write it. - it's even harder to write readable code.

The gospel of brother Riven: "The guarantee that all bugs are in *your* code is worth gold." Amen brother a-m-e-n.
Offline neoskunk

Junior Member





« Reply #71 - Posted 2008-12-17 09:50:34 »

thanks for the example i understand now.   however i am wondering what the advantage of doing all this is?  essentially i am just drawing everything anyways so why not put it all in the same class?  only reason i can think of is to be able to reuse my widgets but thats not really an issue.   seems simplest to me to just create the user interface as one image and then based on events act accordingly.
Offline Mr_Light

Senior Member




shiny.


« Reply #72 - Posted 2008-12-17 19:08:31 »

thanks for the example i understand now.   however i am wondering what the advantage of doing all this is?  essentially i am just drawing everything anyways so why not put it all in the same class?  only reason i can think of is to be able to reuse my widgets but thats not really an issue.   seems simplest to me to just create the user interface as one image and then based on events act accordingly.

Separation of concerns(entangleness, complexity, change leads to more change) - single point of definition(or DRY) - reuse(compound components). Well that's slinging hyped and not so hyped words at ya - but there is no easy way to explain why, there isn't really a need for iether as that is something that comes with experience. So tbh going for the monster class approach might not be a bad idea in your case. You need to rewire your brain a bit anyway else either way is not going to be faster.


hmm almost forgot an old favourite spaghetti code - that does look tasty ey?

It's harder to read code than to write it. - it's even harder to write readable code.

The gospel of brother Riven: "The guarantee that all bugs are in *your* code is worth gold." Amen brother a-m-e-n.
Offline neoskunk

Junior Member





« Reply #73 - Posted 2008-12-18 10:27:36 »

Quote
You need to rewire your brain a bit anyway else either way is not going to be faster.

^^^ haha are you calling me stupid?  im jk but seriously thanks so much for helping me out these past few weeks i really appreciate it. im gonna try to stay off of here and stop bugging you for a while.   plus i cant think of anything else i could possibly need help with  Smiley
Offline Mr_Light

Senior Member




shiny.


« Reply #74 - Posted 2008-12-18 18:30:51 »

^^^ haha are you calling me stupid?  im jk but seriously thanks so much for helping me out these past few weeks i really appreciate it. im gonna try to stay off of here and stop bugging you for a while.   plus i cant think of anything else i could possibly need help with  Smiley

No, I'm not calling you stupid.  It's like going from java to a dynamic language or back.

It's harder to read code than to write it. - it's even harder to write readable code.

The gospel of brother Riven: "The guarantee that all bugs are in *your* code is worth gold." Amen brother a-m-e-n.
Offline neoskunk

Junior Member





« Reply #75 - Posted 2008-12-25 10:34:04 »

after working on things a bit i can definately see why it is advantagous to write each widget as a separate class.  in the monster class approach something as simple as making a button loook different when clicked becomes difficult because not only do you have to redraw the button but also the entire interface.  so going back to the separate class approach im a little confused on where the actionlisteners go.  are they all in the main applet class and based on the user input you have methods in the widgets to handle input?  or does each widget have its own actionlisteners that are passed as parameters maybe  Huh
Offline Mr_Light

Senior Member




shiny.


« Reply #76 - Posted 2008-12-25 15:49:51 »

To handle the 'feel' I'd probably go with some method on the widget class that one needs to override or write delegating code in for handling user actions go with 'traditional' ActionListeners.
1  
2  
3  
4  
5  
6  
7  
8  
9  
protected final Collections<XxxListener> listeners = new CopyOnWriteArrayList<XxxListener>();

protected void fireXxx() {
   for(XxxListener listener : listeners) {
      listener.onXxx();
   }
}

protected void addListener(XxxListener listener) {...}

It's harder to read code than to write it. - it's even harder to write readable code.

The gospel of brother Riven: "The guarantee that all bugs are in *your* code is worth gold." Amen brother a-m-e-n.
Pages: 1 2 [3]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (75 views)
2014-04-15 18:08:23

BurntPizza (68 views)
2014-04-15 03:46:01

UprightPath (80 views)
2014-04-14 17:39:50

UprightPath (66 views)
2014-04-14 17:35:47

Porlus (81 views)
2014-04-14 15:48:38

tom_mai78101 (105 views)
2014-04-10 04:04:31

BurntPizza (165 views)
2014-04-08 23:06:04

tom_mai78101 (261 views)
2014-04-05 13:34:39

trollwarrior1 (210 views)
2014-04-04 12:06:45

CJLetsGame (220 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!