0

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

dzeju555
  • 11
  • 6
  • 3
    so what is the actual problem? – Breavyn May 01 '13 at 11:47
  • Well, car doesn't want to move up, move down on the track. What is more, I have no idea how to rotate my car in order to turn left/right... I'm afraid I can't use method drawImage(). – dzeju555 May 01 '13 at 11:57

1 Answers1

3
  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

mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • don't to use temporarilly getGraphics() is proper method for printing to the File or Printer, not for custom painting – mKorbel May 01 '13 at 12:21
  • To be honest I have no idea how to avoid using getGraphics method... I don;t understand point number 3 and I don't know how to load Image to local variable. Sorry, I'm still learning. I've added Jpanel, removed JFrame, added KeyBindings. So far the car is moving but it leaves something like shade. There's a code: [link]http://pastebin.com/w0VveKCP Do you have any idea, how to rotate this car? The problem is that car has to move in the direction of the hood of the car. – dzeju555 May 01 '13 at 13:47
  • [see what I'm talking about](http://stackoverflow.com/questions/7940173/how-do-i-use-keyeventdispatcher/7940227#7940227), please to ignore my point 6th(and declare JFrame the same way too), [code example about point 3rd](http://stackoverflow.com/a/9258934/714968), I'd be to use my 1st. code example by add some car image:-) – mKorbel May 01 '13 at 14:22
  • @dzeju555: [`ImageApp`](http://stackoverflow.com/a/5129757/230513) is an example of several of these points. – trashgod May 01 '13 at 16:29
  • @mKorbel I've tried to override paintComponent() method and use method repaint() instead paint. Unfortunately the car isn't even drawn. I analyzed those examples, but it didn't help. There's the latest code: http://pastebin.com/ehsewTxe Can you lead me to make it working step by step, please? So far I just want to move this car, without shades and without errors. – dzeju555 May 06 '13 at 16:19
  • @dzeju555 [see how is possible to ....](http://stackoverflow.com/search?q=user%3A418556+car), made by (@Andrew Thompson), don't copy - paste, change all my points in his great code(s) – mKorbel May 06 '13 at 17:19
  • Guys, I've been analyzing this source codes and I made my cars working. I've tried to make double buffering, because refreshing is to low, but it's not working to me. I decided to give up and go ahead. The next problem is threading(?). I can move just one car at the same moment. I tried to make classes CRX and Mahindra to implement interface Runnable but it didn't work. I have no idea how to do that. I must finish this program before the end of the next week. So, please, help me. Source code: http://pastebin.com/R5rJTHw9 NetBeans project: http://www72.zippyshare.com/v/69007168/file.html – dzeju555 May 23 '13 at 13:45