Skip to main content
GameDev.net gamedev.net
🔒 Locked 🎮 Unity

Java class problem

Started by Donal Byrne Jun 3, 2014 at 8:54 PM 6 replies 1.8k views
Original Post
Donal Byrne
Donal Byrne

Everything was fine until i added in the Move classes.No idea what to do, any help would be appreciated



package g1p1;

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;


public class G1P1 extends JFrame {
        
    //screen size
    final int WIDTH = 900, HEIGHT = 650;
    
    //keep track of speed
    double p1Speed = 0.5, p2Speed = 0.5;
   
    //direction var
    final int UP=0, DOWN = 1, RIGHT=2, LEFT=3;
    
    //PLAYERS DIRECTION
    int p1Direction = UP;
    int p2Direction = UP;
    
    //gives perameters of objects
    //border
      Rectangle left = new Rectangle(0,0,50,HEIGHT);  
      Rectangle right = new Rectangle(850,0,50,HEIGHT);
      Rectangle top = new Rectangle(0,0,WIDTH, HEIGHT/9);   
      Rectangle bottom = new Rectangle(0,600,WIDTH,100);
      Rectangle center = new Rectangle(200,(int)((HEIGHT/9)*2.5),575,(HEIGHT/9)*4);
     
      
      //OBSTACLES
      Rectangle obstacle = new
            Rectangle(250,0,100,140);
      Rectangle obstacle2 = new
           Rectangle(300,400,WIDTH/10,150);
      Rectangle obstacle3 = new
            Rectangle(2*(WIDTH/3),(int)((HEIGHT/9)*5),WIDTH/10,HEIGHT/4);
     Rectangle obstacle4 = new
            Rectangle(450,525,100,100);
      Rectangle obstacle5 = new
            Rectangle(WIDTH/2,125,WIDTH/30,HEIGHT/4);
      
      //FINISH LINE
      Rectangle finish = new Rectangle(50,(HEIGHT/2)-HEIGHT/9,(int)((WIDTH/9)*1.5), HEIGHT/70);
      
      
      //p1 car
      Rectangle p1 = new Rectangle(70,330,WIDTH/30,WIDTH/30);
      
      //p2 car
      Rectangle p2 = new Rectangle(150,395,WIDTH/30,WIDTH/30);
      
      
      //STARTING LINE FOR OUTER CAR
      Rectangle lineO = new
            Rectangle(50,HEIGHT/2,(int)((WIDTH/9)*1.5)/2,HEIGHT/140);
      
      //STARTING LINE FOR INNER PLAYER
      Rectangle lineI = new Rectangle(125,(HEIGHT/2)+(HEIGHT/10),(int)((WIDTH/9)*1.5)/2, HEIGHT/140);
      
      //THE CONSTRUCTOR - makes the screen
      public G1P1()
      {
          //creates JFrame
          super("radical racing");
          setSize(WIDTH,HEIGHT);
          setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          setVisible(true);
          
        /*  Move1 m1 = new Move1();
          Move2 m2 = new Move2();
          m1.start();
          m2.start();*/
      }
      
      //THIS WILL DRAW objects of the game
      public void paint(Graphics g)
      {
          super.paint(g);
          
          //draw the background
          g.setColor(Color.DARK_GRAY);
          g.fillRect(0,0,WIDTH,HEIGHT);
          
          //make the border green
          g.setColor(Color.GREEN);
          
          //USING THE RECTANGLES TO DRAW IT
          g.fillRect(left.x,left.y,left.width,left.height);
          g.fillRect(right.x,right.y,right.width,right.height);
          g.fillRect(top.x,top.y,top.width,top.height);
          g.fillRect(bottom.x,bottom.y,bottom.width,bottom.height);
          g.fillRect(center.x,center.y,center.width,center.height);
          
          g.setColor(Color.GREEN);
          g.fillRect(obstacle.x,obstacle.y,obstacle.width,obstacle.height);
          g.fillRect(obstacle2.x,obstacle2.y,obstacle2.width,obstacle2.height);
          g.fillRect(obstacle3.x,obstacle3.y,obstacle3.width,obstacle3.height);
          g.fillRect(obstacle4.x,obstacle4.y,obstacle4.width,obstacle4.height);
          g.fillRect(obstacle5.x,obstacle5.y,obstacle5.width,obstacle5.height);
          
          //set starting line to white
          g.setColor(Color.WHITE);
          
          //DRAW THE STARTING LINE
          g.fillRect(lineO.x,lineO.y,lineO.width,lineO.height);
          g.fillRect(lineI.x,lineI.y,lineI.width,lineI.height);
          
          //SET COLOR OF FINISH LINE TO YELLOW
          g.setColor(Color.YELLOW);
          //DRAW FINISH LINE
          g.fillRect(finish.x,finish.y,finish.width,finish.height);
          
          //SET COLOR OF P1 TO BLUE
          g.setColor(Color.blue);
          g.fill3DRect(p1.x, p1.y, p1.width, p1.height,true);
          
          //SET COLOR OF P2 TO red
          g.setColor(Color.red);
          g.fill3DRect(p2.x, p2.y, p2.width, p2.height,true);
          
      }
      
      //add speed + movement
      private class Move1 extends Thread implements KeyListener
      {
          @Override
          public void run()
          {
              //wake up key listener
              addKeyListener(this);
              
              while(true)
              {
                  try
                  {
                      //refresh screen
                      repaint();
                      
                      //check for collision on walls, if so, make it slower
                      if(p1.intersects(left)||p1.intersects(right)||
                         p1.intersects(top) ||p1.intersects(bottom)||
                         p1.intersects(obstacle)||p1.intersects(obstacle2)
                         ||p1.intersects(obstacle3)||p1.intersects(obstacle4)
                         ||p1.intersects(obstacle5)|| p1.intersects(p2))
                      {
                          p1Speed =-4;
                      }
                      
                      //if it hits the center do the same 
                      if(p1.intersects(center))
                      {
                          p1Speed =-2.5;
                      }
                      
                      //increase speed
                      if(p1Speed<=5)
                      {
                        p1Speed+=0.2;
                      }
                      
                      //move player based on direction
                      if(p1Direction==UP)
                      {
                          p1.y-=(int)p1Speed;
                      }
                       if(p1Direction==DOWN)
                      {
                          p1.y+=(int)p1Speed;
                      }
                       if(p1Direction==LEFT)
                      {
                          p1.x-=(int)p1Speed;
                      }
                        if(p1Direction==LEFT)
                      {
                          p1.x+=(int)p1Speed;
                      }
                          
                       //delay refresh
                       Thread.sleep(75);
                      
                                        
                  }
                  
                  catch(Exception e)
                        {
                           break;   
                        }
              }
          }
          
            //IMPLEMENT METHOD FROM KEY LISTENER
          @Override
      public void keyPressed(KeyEvent event)
      {
          
      }
      
      //IMPLEMENT METHOD FROM KEY LISTENER
          @Override
      public void keyReleased(KeyEvent event)
      {
          
      }
      
      //IMPLEMENT METHOD FROM KEY LISTENER
          @Override
      public void keyTyped(KeyEvent event)
      {
         if(event.getKeyChar()=='a') 
         {
             p1Direction = LEFT;
         }
         if(event.getKeyChar()=='d') 
         {
             p1Direction = RIGHT;
         }
         if(event.getKeyChar()=='s') 
         {
             p1Direction = DOWN;
         }
         if(event.getKeyChar()=='w') 
         {
             p1Direction = UP;
         }
      }
   }
      
       //add speed + movement
      private class Move2 extends Thread implements KeyListener
      {
          @Override
          public void run()
          {
              //wake up key listener
              addKeyListener(this);
              
              while(true)
              {
                  try
                  {
                      //refresh screen
                      repaint();
                      
                      //check for collision on walls, if so, make it slower
                      if(p2.intersects(left)||p2.intersects(right)||
                         p2.intersects(top) ||p2.intersects(bottom)||
                         p2.intersects(obstacle)||p2.intersects(obstacle2)
                         ||p2.intersects(obstacle3)||p2.intersects(obstacle4)
                         ||p2.intersects(obstacle5)|| p2.intersects(p1))
                      {
                          p2Speed =-4;
                      }
                      
                      //if it hits the center do the same 
                      if(p2.intersects(center))
                      {
                          p2Speed =-2.5;
                      }
                      
                      //increase speed
                      if(p2Speed<=5)
                      {
                        p2Speed+=0.2;
                      }
                      
                      //move player based on direction
                      if(p2Direction==UP)
                      {
                          p2.y-=(int)p1Speed;
                      }
                       if(p2Direction==DOWN)
                      {
                          p2.y+=(int)p1Speed;
                      }
                       if(p1Direction==LEFT)
                      {
                          p1.x-=(int)p1Speed;
                      }
                        if(p2Direction==LEFT)
                      {
                          p2.x+=(int)p1Speed;
                      }
                          
                       //delay refresh
                       Thread.sleep(75);
                      
                                        
                  }
                  
                  catch(Exception e)
                        {
                           break;   
                        }
              }
          }
          
                //IMPLEMENT METHOD FROM KEY LISTENER
          @Override
      public void keyPressed(KeyEvent event)
      {
          
      }
      
      //IMPLEMENT METHOD FROM KEY LISTENER
          @Override
      public void keyReleased(KeyEvent event)
      {
          
      }
      
      //IMPLEMENT METHOD FROM KEY LISTENER
          @Override
      public void keyTyped(KeyEvent event)
      {
         if(event.getKeyChar()=='j') 
         {
             p2Direction = LEFT;
         }
         if(event.getKeyChar()=='l') 
         {
             p2Direction = RIGHT;
         }
         if(event.getKeyChar()=='k') 
         {
             p2Direction = DOWN;
         }
         if(event.getKeyChar()=='i') 
         {
             p2Direction = UP;
         }
      }
   }
      
   
      
      
      public static void main(String[]args)
      {
          new G1P1();
      }
    }
    







    

rip-off
rip-off
So, what is going wrong? Are you getting compile errors? Trouble running your program? Once it is running, is it doing something unexpected? If so, what did you expect it to do?
Donal Byrne
Donal Byrne

what happens is that it shows a blank screen. No compile errors just a blank screen. Its supposed to show a race track with two squares(cars) moving around it using wsad or ijkl as directional controls. Its also supposed to have collision detection.

Like i said the screen goes blank when i add the Move classes

Glass_Knife
Glass_Knife

Look at these:

http://www.gamedev.net/page/resources/_/technical/general-programming/java-games-active-rendering-r2418

http://www.gamedev.net/page/resources/_/technical/general-programming/java-games-keyboard-and-mouse-r2439

Right now you've got two threads both accessing shared resources, which will cause you problems. To start, create a single thread that updates all your game objects once every frame. Then add the keyboard/mouse polling from the second tutorial.

That should help.

Nicholas Kong
Nicholas Kong

You don't need threads for Java games and shouldn't you be using Canvas class as your canvas to draw your game objects on?

DiegoSLTS
DiegoSLTS

Read about double buffer, that's your main problem. The image is being cleared and painted over the same image that's being displayed, and the rate of drawing does not match the rate is being displayed. If you set a bigger sleep time you'll see that the game is being drawn.

Here's a description of double buffering: http://docs.oracle.com/javase/tutorial/extra/fullscreen/doublebuf.html

You can find good tutorials about it in google.

Also, the repaint in both threads will be a problem when both threads are started, follow the advises of Glass_Knife too,

And the "key" methods that you override... don't override them, KeyListener is an interface, you're not overriding methods, you're implementing them, the override is an anotation for virtual methods that are already defined in the base class.

Satharis
Satharis

And the "key" methods that you override... don't override them, KeyListener is an interface, you're not overriding methods, you're implementing them, the override is an anotation for virtual methods that are already defined in the base class.

Why would using the override annotation for implementing interface methods be bad? They changed it to be legal for java 6 and later, I would say that implies the practice has some merit.
DiegoSLTS
DiegoSLTS

And the "key" methods that you override... don't override them, KeyListener is an interface, you're not overriding methods, you're implementing them, the override is an anotation for virtual methods that are already defined in the base class.

Why would using the override annotation for implementing interface methods be bad? They changed it to be legal for java 6 and later, I would say that implies the practice has some merit.

I didn't know that, in my PC it didn't compile because of that annotation. I guess the project was setup to use a previous version of java, if it works on java 6 or higher then ignore that part of my post.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.