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!