dzeju555
dzeju555

Reputation: 11

JAVA Client-server game

I have to prepare a game inspired by this game. Of course it should be much simple, but unortunately it has to use client-server communication in order to allow two players to play together. So far I prepared textures and I'm trying to make one of cars moving. Sadly it is not as simple as it seems. I got stuck and I can't go on... I want to use just four arrows: UP - car speeds up, DOWN - car slows down, LEFT - turns left, RIGHT - turns right. Of course if we don't push UP button or DOWN button, car should slow down, but more slightly. I beg for tips!

This is my code:

package destructionderby;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

class CRX extends JFrame implements KeyListener
{
   File imageFile2;
   BufferedImage crxModel;
   public int speed;
   int posX, posY;
   JPanel crxPanel;
   public CRX()
    {
        speed = 0;
        posX=562;
        posY = 420;
        crxPanel = new JPanel();
        imageFile2 = new File("crx.png");
        try
        {
            crxModel = ImageIO.read(imageFile2);
        }
        catch (IOException e) 
        {
            System.err.println("File access error");
        }
        addKeyListener(this);
    }
    public void paint(Graphics g) 
    {
        while (true)
        {
            Graphics2D g2d = (Graphics2D) g;
            g2d.drawImage(crxModel, posX+speed, posY, null);
            repaint();
        }
    }

    //-------------------KEY LISTENER--------------------
    public void keyTyped (KeyEvent key)
    {
            if (key.getKeyCode()==38) //38 == UP; 40==DOWN; 37==LEFT; 39==RIGHT
            {
                speed +=10;
            }
            if (key.getKeyCode()==40)
            {
                if (speed >10)
                {
                    speed-=10;
                }
                else speed=0;
            }
    }
    @Override
    public void keyPressed(KeyEvent key)
    {

    }
    @Override
    public void keyReleased(KeyEvent key)
    {

    }
}

class MainWindow extends JFrame
{
    JPanel mainWindow;
    BufferedImage backgroundImage;
    CRX crx;
    MainWindow()
    {
        super("Destruction Derby");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(800,600);
        setResizable(false);
        setVisible(true);
        init();
    }
    void init()
    {
        mainWindow = new JPanel();
        File imageFile= new File("background.png");        
        //:::...ZAŁADOWANIE OBRAZKA TŁA:::...
        try 
        {
            backgroundImage = ImageIO.read(imageFile);
        } 
        catch (IOException e) 
        {
            System.err.println("File access error");
        }
        crx = new CRX();
        paint(getGraphics());
     }

    @Override
    public void paint(Graphics g) 
    {
        Graphics2D g2d = (Graphics2D) g;
        g2d.drawImage(backgroundImage, 0, 0, this);
        crx.paint(getGraphics());
    }
}

public class DestructionDerby 
{    
    public static void main(String[] args) 
    {
        new MainWindow();
    }
}

And there is my NetBeans project: http://www2.zippyshare.com/v/30402578/file.html

Upvotes: 0

Views: 858

Answers (1)

mKorbel
mKorbel

Reputation: 109813

  1. JFrame by default never to react to KeyEvent, put there JPanel

  2. don't paint() directly to JFrame, use JPanel instead

  3. override paintComponent in JPanel, add super.paintComponent() as 1st code line (same for paint())

  4. use KeyBindings (tons examples about these Keys here) instead of KeyListener

  5. for why reasons there are two JFrames, don't extend JFrame, create an local variable

  6. load Image to local variable too

Upvotes: 3

Related Questions