This is an archived, read-only copy of the United-TI subforum , including posts and topic from May 2003 to April 2012. If you would like to discuss any of the topics in this forum, you can visit Cemetech's Calculator Programming subforum. Some of these topics may also be directly-linked to active Cemetech topics. If you are a Cemetech member with a linked United-TI account, you can link United-TI topics here with your current Cemetech topics.

This forum is locked: you cannot post, reply to, or edit topics. General Coding and Design => Calculator Programming
Author Message
magicdanw
pcGuru()


Calc Guru


Joined: 14 Feb 2007
Posts: 1110

Posted: 18 Dec 2007 02:06:10 pm    Post subject:

Hey. My friend recently wrote a Tron game in Java, and I'm trying to make it work over a network. I'm using the standard java.net sockets, and when I run two instances of the game on the same computer, it connects fine (there are some synchronization issues, but that's another story). When I run it on two networked computers, however, the client can't connect to the host and it times out. I know that the two computer can connect, because I also wrote a Java chat program that works fine on them. The only relevant difference I can think of between the two programs is that the chat program is a Java Swing application, and Tron is a Java applet being run either under Internet Explorer or (for testing purposes) Java's appletviewer. Is there any reason that I should have this kind of problem across a network and not on one computer connecting to itself, or why I would have this problem with applets and not with Swing?
Back to top
JoeImp
Enlightened


Active Member


Joined: 24 May 2003
Posts: 747

Posted: 19 Dec 2007 12:58:27 pm    Post subject:

What is the winsock error message?
Back to top
magicdanw
pcGuru()


Calc Guru


Joined: 14 Feb 2007
Posts: 1110

Posted: 19 Dec 2007 02:02:19 pm    Post subject:

Hmm. I hadn't been displaying error messages, so I thought that the client was timing out, but I displayed the error and it's getting an access restriction. When my dad gets home, I'll try running the program across my network (IDK his laptop password), and see if it's just a school programming lab network restriction.
Back to top
elfprince13
Retired


Super Elite (Last Title)


Joined: 11 Apr 2005
Posts: 3500

Posted: 20 Dec 2007 09:50:32 pm    Post subject:

applets can not access sockets, except, sometimes if you sign it, which is a PITA.


just add a canvas to a frame, and make it an application instead


Last edited by Guest on 20 Dec 2007 09:51:47 pm; edited 1 time in total
Back to top
magicdanw
pcGuru()


Calc Guru


Joined: 14 Feb 2007
Posts: 1110

Posted: 20 Dec 2007 10:18:40 pm    Post subject:

Ahh, thank you very much. I'll try that out in class tomorrow. Then, I have to learn how to do actual network synchronization, so that one game doesn't run faster than the other and screw up the board.
Back to top
magicdanw
pcGuru()


Calc Guru


Joined: 14 Feb 2007
Posts: 1110

Posted: 21 Dec 2007 02:02:39 pm    Post subject:

Thanks, elfprince13. It works over the network now. After the winter vacation is over, my friend and I will try to make the graphics better (right now, the canvas is displaying over the title bar, making it difficult to move or close the window), and finally we'll test it over the internet to see if our synchronization problems are gone, as they seem to be at least improved with the new graphics methods.
Back to top
elfprince13
Retired


Super Elite (Last Title)


Joined: 11 Apr 2005
Posts: 3500

Posted: 29 Dec 2007 01:06:24 am    Post subject:

btw, are you doing this in Swing or AWT?
Back to top
magicdanw
pcGuru()


Calc Guru


Joined: 14 Feb 2007
Posts: 1110

Posted: 29 Dec 2007 01:43:30 pm    Post subject:

Hmm... We were doing in an applet, using Graphics2D. Now, I think we are using Swing. Only, I'm not sure I know the difference between AWT and Swing. Don't you import AWT objects for a Swing project?

Last edited by Guest on 29 Dec 2007 01:44:17 pm; edited 1 time in total
Back to top
elfprince13
Retired


Super Elite (Last Title)


Joined: 11 Apr 2005
Posts: 3500

Posted: 29 Dec 2007 08:39:55 pm    Post subject:

magicdanw wrote:
Hmm...  We were doing in an applet, using Graphics2D.  Now, I think we are using Swing.  Only, I'm not sure I know the difference between AWT and Swing.  Don't you import AWT objects for a Swing project?
[post="117866"]<{POST_SNAPBACK}>[/post]



Swing is a mostly lightweight (e.g. the component doesn't have a native peer) extension to AWT, with far more advanced features, and better native L&F.
Back to top
magicdanw
pcGuru()


Calc Guru


Joined: 14 Feb 2007
Posts: 1110

Posted: 29 Dec 2007 10:22:50 pm    Post subject:

L&F? Afraid I'm not sure what you mean, except the part about Swing extending AWT, which I get. So I guess I'm using Swing, then. I think right now, my friend and I basically modified our update routine to just paint to a Canvas object like you said we should do.

Next, we need to figure out how to get the window working right (I think the Canvas is somehow painting over the title bar). And then, we will probably write a real protocol to make it properly synchronized (right now, it's really crappy because the host just keeps sending out information needed to update the game, and the client just keeps sending out the player's keypresses, and so dropped packets will probably be a major issue once we go from our local network to the real internet.)
Back to top
elfprince13
Retired


Super Elite (Last Title)


Joined: 11 Apr 2005
Posts: 3500

Posted: 02 Jan 2008 11:43:46 am    Post subject:

magicdanw wrote:
L&F?

Look & Feel aka themeing

magicdanw wrote:
So I guess I'm using Swing, then.

are you using JFrames, JButtons, etc? and are you importing javax.swing?

magicdanw wrote:
Next, we need to figure out how to get the window working right (I think the Canvas is somehow painting over the title bar).


how are you handling layout/positioning? I can probably help more if you post your current code
Back to top
magicdanw
pcGuru()


Calc Guru


Joined: 14 Feb 2007
Posts: 1110

Posted: 02 Jan 2008 01:58:06 pm    Post subject:

I don't really remember what we're using, but I could bring home the code tomorrow and post it.
Back to top
magicdanw
pcGuru()


Calc Guru


Joined: 14 Feb 2007
Posts: 1110

Posted: 03 Jan 2008 01:20:04 pm    Post subject:

I won't post the entire program's code. Here's the routines responsible for displaying the game:


Code:
public static void main(String[] args)
   {
  javax.swing.SwingUtilities.invokeLater(new Runnable()
  {
     public void run()
     {
    runGUI();
     }
  });
   }
        
public static void runGUI()
   {
  JFrame.setDefaultLookAndFeelDecorated(true);
  Tron window = new Tron();
   }
        
public Tron()
   {
  frame = new JFrame("Tron");
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  contentPane = new JPanel();
                canvas = new Canvas();
                contentPane.add(canvas);
  frame.setContentPane(contentPane);
  frame.pack();
                frame.setSize(600,600);
  frame.setVisible(true);
                init();
   }

public void init() {                
  frame.setBackground (Color.BLACK);
  frame.setSize(500,525);
   }

public void update(Graphics g){

                canvas.paint(g);
                BufferedImage buffer = (BufferedImage) frame.createImage(frame.getSize().width, frame.getSize().height);
                Graphics2D g2 = buffer.createGraphics();
                
                //Code that updates the game position and draws to g2
                                          
                ((Graphics2D)g).drawImage(buffer,0,0,frame);

   }
Back to top
elfprince13
Retired


Super Elite (Last Title)


Joined: 11 Apr 2005
Posts: 3500

Posted: 03 Jan 2008 01:35:14 pm    Post subject:

here's the code from my helicopter game, that you can use as a reference for your layout stuff. my only real comment is that there's no reason to have a separate init() method for an application.

HelicopterGame extends Canvas and takes a Window (or an extension thereof) as the single argument for its constructor


Code:
public HelicopterGame(Window p){   
  super();
  
  WINDOWHEIGHT = 480;
  WINDOWWIDTH = 640;
  STEEPNESS = WINDOWHEIGHT / 40;
  DELTA = 2 * HELIWIDTH;
  OBHEIGHT = WINDOWHEIGHT / 5;
  
  parent = p;
  parent.add(this);
  Insets pInsets = parent.getInsets();
  parent.setSize(WINDOWWIDTH + pInsets.left + pInsets.right, WINDOWHEIGHT + pInsets.top + pInsets.bottom);
  setBounds(pInsets.left, pInsets.top, WINDOWWIDTH - pInsets.left - pInsets.right, WINDOWHEIGHT - pInsets.top - pInsets.bottom);
  score = 0;
  projectile = new ImageIcon(HelicopterGame.class.getResource(PROJPATH));
  heli = new ImageIcon(HelicopterGame.class.getResource(HELIPATH));
  helicopter = new GameObject(DELTA * 2 + HELIWIDTH, WINDOWHEIGHT / 2, HELIWIDTH * 2, HELIHEIGHT * 2,  true, heli, this);
  explode = new ImageIcon(HelicopterGame.class.getResource(EXPLPATH));
  jet = new ImageIcon(HelicopterGame.class.getResource(JETPATH));
  
  background = new ImageIcon(HelicopterGame.class.getResource(BCKGPATH));
  //background.load();
  scaledbg = new ImageIcon(background.getImage());
  
  projectiles = Collections.synchronizedList(new ThreadList<GameObject>());
  map = Collections.synchronizedList(new ThreadList<LevelColumn>());
  enemies = Collections.synchronizedList(new ThreadList<GameObject>());
  r = new Random();
  int offset = DELTA;
  globaloffset = offset;
  for(int x = 0; x < WINDOWWIDTH; x += DELTA){
     map.add(new LevelColumn(offset));
     offset = offset + STEEPNESS * (r.nextInt(5) - 2);
  }
  addKeyListener(this);
  addMouseListener(this);
  addMouseMotionListener(this);
  parent.addKeyListener(this);
  parent.addMouseListener(this);
  parent.addMouseMotionListener(this);
  parent.setVisible(true);
  parent.addComponentListener(this);
  thread = new ScrollThread(this);
  thread.pause = true;
  thread.start();
  
   }


Last edited by Guest on 03 Jan 2008 01:37:16 pm; edited 1 time in total
Back to top
magicdanw
pcGuru()


Calc Guru


Joined: 14 Feb 2007
Posts: 1110

Posted: 07 Jan 2008 01:24:49 pm    Post subject:

I tried to implement your solution, and now my Tron object (which extends Canvas) is returning null for getGraphics(), so I can't draw to it. Do you know why this might happen, or how I can fix it? (If it helps, I can post the whole project code, but I'm sure you don't want to pick through someone else's entire uncommented project).
Back to top
elfprince13
Retired


Super Elite (Last Title)


Joined: 11 Apr 2005
Posts: 3500

Posted: 08 Jan 2008 08:55:47 am    Post subject:

magicdanw wrote:
I tried to implement your solution, and now my Tron object (which extends Canvas) is returning null for getGraphics(), so I can't draw to it.  Do you know why this might happen, or how I can fix it?  (If it helps, I can post the whole project code, but I'm sure you don't want to pick through someone else's entire uncommented project).
[post="118321"]<{POST_SNAPBACK}>[/post]



make sure your canvas is visible on screen before trying to call getGraphics()
Back to top
magicdanw
pcGuru()


Calc Guru


Joined: 14 Feb 2007
Posts: 1110

Posted: 08 Jan 2008 01:17:48 pm    Post subject:

And how would I make sure it's visible on the screen?
Back to top
magicdanw
pcGuru()


Calc Guru


Joined: 14 Feb 2007
Posts: 1110

Posted: 11 Jan 2008 01:28:33 pm    Post subject:

Hi! My friend and I have successfully gotten the graphics to work. Now, we have an applicatin version that works over the network. However, the client lags very often. This is likely because my friend and I have little network programming experience, and we don't fully understand how network programming works in Java. If you could point us in the right direction in how to handle the protocol, we'd be grateful.

If it helps, I've attached the source code.
Back to top
elfprince13
Retired


Super Elite (Last Title)


Joined: 11 Apr 2005
Posts: 3500

Posted: 11 Jan 2008 06:15:24 pm    Post subject:

sending only the direction is bad. I'd say you need to do more synchronizing with the other computer (e.g. via timestamps), and send the coordinates instead of the direction. each computer should maintain a buffer of say....the previous ten moves, and then you can tell if instructions go missing by comparing the difference in the timestamps, and request the missing instructions that occured at specific times in between the current and last received instruction. if the gap is too far, you have a laggy network and you should disconnect, and tell the user.
Back to top
JoeImp
Enlightened


Active Member


Joined: 24 May 2003
Posts: 747

Posted: 11 Jan 2008 06:32:34 pm    Post subject:

Send udp packets to the other player containing the new coordinates whenever you update your position.
Back to top
Display posts from previous:   
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
    »
» View previous topic :: View next topic  
Page 1 of 1 » All times are UTC - 5 Hours

 

Advertisement