Advertisement

PvP, flickering applet, and updating lives Java

Started by December 16, 2015 12:21 AM
7 comments, last by jim8ean 8 years, 11 months ago

I need some pointers on how to stop this applet from flickering that's driving me mad! I had some trouble setting this up as a JFrame so I continued working on it as an applet (probably my main mistake). I'm relatively new with handling data on and off a server.

Also need some advice on how to approach this simple program, my code feels messy and don't know the proper way to set this up (my book is no help).

I neglected to include any commenting in my java files besides for what I written below.


public class Client extends Applet implements Runnable, KeyListener {

   public void init() {
      //Start everything and establish a connection with the server
      // Start this thread
      // Start thread that gets the data back from the server
   }
 
   public void updateCoordinates(int pid, int y2, int bxin, int byin, int lives2, boolean b) {
      // Update new values retrieved from server
      // About to be re-painted
   }
   
   // Where the flickering may ocurr
   public void paint(Graphics g) {
	Graphics2D g2 = (Graphics2D) g;  
	   
	for(int i = 0; i < playerLimit; i++) { 
         if(i % 2 == 0) {
            g.drawImage(playerImgLeft, x[i], y[i], 80, 80, null); 
            if(shoot[i]==true)
                bullet = new Ellipse2D.Double(bx[i]+35, by[i]+35, 10, 10);
         }
         if(i % 2 != 0) {
            g.drawImage(playerImgRight, x[i], y[i], 80, 80, null); 
            if(shoot[i]==true)
                bullet = new Ellipse2D.Double(bx[i]+35, by[i]+35, 10, 10);
         }
         g2.fill(bullet);
         g2.draw(bullet);
      }
         // Draw stuff to the screen
   }
   
   private void hits() 
   {

   // When the bullet falls within range of a player lives-- 
   // NOT WORKING
 
   if( ((locX+80 <= locBx) && (locX <= locBx+10)) && ((locY+80 >= locBy+10) && (locY <= locBy)) )
    	lives--;
      
   }

   public void run() {
      
      while(true) {
    	 // Check if a player was hit by the bullet
         // Check if player moves up
         // Check if player moves down

         // Check if it fired
         // If fired let bullet finish run
         // Disable bullet once it leaves range
         // Update bullet

         if(up || down || b) {
               // Write the data out to the server
         }
         
         repaint();

         // Sleep
      }
   }
   public void keyPressed(KeyEvent e) {
   }
   public void keyReleased(KeyEvent e) {
   }
   public void keyTyped(KeyEvent e) {
   }
}

class Input implements Runnable {
   public Input(DataInputStream in, Client c) {
   }
   public void run() {
      // Get Feedback back from the server
      // Update locations
   }
}

////////////***********************************////////////////
// And there's a server .java file that loops through both or more users and does work on each user so they both see the same thing.

Any input to one or both of my problems would be greatly appreciated!

First, Applets are really old technology, and are being deprecated. Many browsers do not load them by default, and the higher security settings do not initialize anything Java, ever. So, if the problem is "redrawing Applets flickers in the browser," then it's not clear whether you can solve that problem at all. If you're dead set on going forward with an Applet, then I suggest googling for "applet draw flicker" and see what comes up. Typically, you'll want to invalidate the applet area without clearing it, and then only clear it right before you draw the sprites in it. If you can turn on double-buffering, so much the better.

Currently, for a "real browser game," you will want to use either the Canvas tag with JavaScript, or the WebGL feature on top of Canvas, with JavaScript or some other language (such as C++ that can target JavaScript using Emscripten.) There are a number of game engines available for this -- from Unreal Engine 4 and Unity 5, to three.js, to pixi.js.

I'm not seeing any networking code, so I have no idea what you're trying to do there.

Also: Have you successfully built a single-player game on a local machine? If not, I suggest you start there, because it doesn't help if you have to learn game programming, animation, browser programming, AND networking, all at the same time. In fact, I haven't ever seen anyone succeed in trying all of those things at the same time!
enum Bool { True, False, FileNotFound };
Advertisement

Awesome thanks for those first tips, I might have posted this problem in the wrong forum but thought it seemed somewhat appropriate here.

I have built successful single players on a local machine with Java, C# and I made a couple using html and javascript but never working with sockets and multiple connecting clients.

I'm not actually trying to make a browser game either although it does look like I am using an applet. I'd love to transfer this into a JFrame if anyone has any input on how I can approach that because I can see that my uploaded game has 6 downloads already.

I forgot to include the player images so here is the re-upload now you can see the players.

I have built successful single players on a local machine with Java, C# and I made a couple using html and javascript but never working with sockets and multiple connecting clients.


Great!

If you don't need networking, how about using an existing Java game engine or UI library?
JMonkeyEngine? Slick2D/ LWJGL?

Anyway, you should be able to make JFrame windows not flicker, by following good drawing practices. Mainly, don't pre-erase dirty regions, and draw everything in one go once it's time to redraw.
You should be able to keep queuing deferred tasks to redraw the window, without invalidation.
enum Bool { True, False, FileNotFound };

Okay I am going be honest so this is for my final project and I have to have a running server and two connected clients. I could get this done if I wasn't under time restraints cause I'm a full time online student and I have other finals to work to finish up and physics work.

I really just need as much feedback and fix what I can fix before I have to submit this at the end of today and the flickering is my main concern then the collision detection (which shouldn't be too bad if I could spent enough time staring at it).

I will try again to convert back into a JFrame but I tried once and I had trouble getting my existing components to draw to my frame.

* I dont know where to create the connected clients frame (at in my clients main?)

* I dont know where to manage the frame and the drawed component

* I dont know how to start the running thread of the client and the client's input's source using the same instance of the frame and the clients game(it seemed more straight forward at first using the applet)

I wish I started this using a Jframe my stupid mistake. Anyways thanks hplus0603 your suggestions gave me better insurance I what actions I should try to take!

Advertisement
Create a "game" object that contains the state of the game.
Create a "draw" function in the JFrame that simply draws the current state of the game to the screen.
Create a "simulate" object that, given inputs, updates the state of a player in the game state.
Feed the "simulate" object the input from the window.
Redraw the window 30 times a second, and call simulate at 30 times per second.

Now, if you have that working, you can write a "networkclient" object that does two things:
1) send the state of the local player to the other end (server)
2) receive information about other players from the other end (server) and updates their state in the game object

Now, create a server, which also creates a "game" object instance.
For each client that connects to the server, make a "player" object in the "game" world.
When you receive state from the players, update the objects in the game world.
15 times a second, formulate a packet that contains the state of all the objects in the game world, and send that to each client (filtering out the player object being controlled by that client.)

Now, you have a working multiplayer game!
enum Bool { True, False, FileNotFound };

Very helpful! Ill give it a try later today and post back how I make out.

Hey Howard, this is Dustin, I posted this question for some external help, sorry for the misconception!

This topic is closed to new replies.

Advertisement