Guest User

flashing java screen help?

a guest
Jan 12th, 2012
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.56 KB | None | 0 0
  1. import javax.swing.*;
  2. import javax.swing.event.*;
  3. import java.awt.*;
  4. import java.awt.event.*;
  5.  
  6. public class Racing extends JFrame {
  7.     //makes the screen size
  8.     final int WIDTH = 900, HEIGHT = 650;
  9.    
  10.     //keeps track of player speed
  11.     double plSpeed = .5, p2Speed = .5;
  12.    
  13.     //numbers that represent direction
  14.     final int UP = 0, RIGHT = 1, DOWN = 2, LEFT = 3, STOP = 5;
  15.    
  16.     //keeps track of player direction
  17.     int p1Direction = STOP;
  18.     int p2Direction = STOP;
  19.    
  20.     //create rectangles
  21.     Rectangle left = new Rectangle( 0, 0, WIDTH/9, HEIGHT );
  22.     Rectangle right = new Rectangle( ( WIDTH/9 )*8, 0, WIDTH/9, HEIGHT );
  23.     Rectangle top = new Rectangle ( 0, 0, WIDTH, HEIGHT/9 );
  24.     Rectangle bottom = new Rectangle( 0, ( HEIGHT/9 ) * 8, WIDTH, HEIGHT/9 );
  25.     Rectangle center = new Rectangle( (int) ((WIDTH/9) * 2.5), (int) ((HEIGHT/9) * 2.5), (int) ((WIDTH/9) * 5), (int) ((HEIGHT/9) * 4));
  26.    
  27.     //makes the obstacles
  28.     Rectangle obstacle = new Rectangle ( WIDTH/2, (int) ((HEIGHT/9) * 7), WIDTH/10, HEIGHT/9 );
  29.     Rectangle obstacle2 = new Rectangle ( WIDTH/3, (int) ((HEIGHT/9 ) * 5), WIDTH/10, HEIGHT/4 );
  30.     Rectangle obstacle3 = new Rectangle ( 2 * (WIDTH/3), (int) ((HEIGHT/9) * 5),WIDTH/10, HEIGHT/4 );
  31.     Rectangle obstacle4 = new Rectangle ( WIDTH/3, HEIGHT/9, WIDTH/30, HEIGHT/9 );
  32.     Rectangle obstacle5 = new Rectangle ( WIDTH/2, (int) ((HEIGHT/9) * 1.5), WIDTH/30, HEIGHT/4 );
  33.    
  34.     //makes the finish line
  35.     Rectangle finish = new Rectangle ( WIDTH/9, (HEIGHT/2)-HEIGHT/9, (int) ((WIDTH/9) * 1.5), HEIGHT/70 );
  36.    
  37.     //makes player 1's car
  38.     Rectangle p1 = new Rectangle ( WIDTH/9, HEIGHT/2, WIDTH/30, WIDTH/30 );
  39.    
  40.     //makes player 2's car
  41.     Rectangle p2 = new Rectangle ((( WIDTH/9 ) + ((int) ((WIDTH/9) * 1.5) /2)),(HEIGHT/2) + (HEIGHT/10), WIDTH/30, WIDTH/30);
  42.    
  43.     //constructor
  44.     public Racing() {
  45.         //define defaults for the JFrame
  46.         super ("Racing");
  47.         setSize( WIDTH, HEIGHT );
  48.         setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
  49.         setBackground(Color.BLACK);
  50.        
  51.         //start the inner class
  52.         Move1 m1 = new Move1();
  53.         Move2 m2 = new Move2();
  54.         m1.start();
  55.         m2.start();
  56.     }
  57.    
  58.     //draws the cars and race track
  59.     public void paint(Graphics g) {
  60.         super.paint(g);
  61.        
  62.         //when we draw, the border will be green
  63.         g.setColor(Color.GREEN);
  64.        
  65.         //rectangles for start lines (lineO = outer player, lineI = inner player)
  66.         Rectangle lineO = new Rectangle( WIDTH/9, HEIGHT/2, (int) ((WIDTH/9) * 1.5)/2, HEIGHT/140 );
  67.         Rectangle lineI = new Rectangle( ((WIDTH/9)+((int) ((WIDTH/9) * 1.5)/2)), (HEIGHT/2) + (HEIGHT/10), (int) ((WIDTH/9) * 1.5)/2, HEIGHT/140 );
  68.        
  69.         //fill the rectangles
  70.         g.fillRect(left.x,left.y,left.width,left.height);
  71.         g.fillRect(right.x,right.y,right.width,right.height);
  72.         g.fillRect(top.x,top.y,top.width,top.height);
  73.         g.fillRect(bottom.x,bottom.y,bottom.width,bottom.height);
  74.         g.fillRect(center.x,center.y,center.width,center.height);
  75.         g.fillRect(obstacle.x,obstacle.y,obstacle.width,obstacle.height);
  76.         g.fillRect(obstacle2.x,obstacle2.y,obstacle2.width,obstacle2.height);
  77.         g.fillRect(obstacle3.x,obstacle3.y,obstacle3.width,obstacle3.height);
  78.         g.fillRect(obstacle4.x,obstacle4.y,obstacle4.width,obstacle4.height);
  79.         g.fillRect(obstacle5.x,obstacle5.y,obstacle5.width,obstacle5.height);
  80.        
  81.         //make the starting line color white
  82.         g.setColor(Color.WHITE);
  83.        
  84.         //draw starting line
  85.         g.fillRect(lineO.x,lineO.y,lineO.width,lineO.height);
  86.         g.fillRect(lineI.x,lineI.y,lineI.width,lineI.height);
  87.        
  88.         //make the finish line yellow
  89.         g.setColor(Color.YELLOW);
  90.        
  91.         //draw the finish line
  92.         g.fillRect(finish.x,finish.y,finish.width,finish.height);
  93.        
  94.         //set player one's color to red
  95.         g.setColor(Color.RED);
  96.         //now draw player one
  97.         g.fill3DRect(p1.x,p1.y,p1.width,p1.height,true);
  98.        
  99.         //make player two blue
  100.         g.setColor(Color.BLUE);
  101.         //now draw player two
  102.         g.fill3DRect(p2.x,p2.y,p2.width,p2.height,true);
  103.     }
  104.     private class Move1 extends Thread implements KeyListener {
  105.         public void run() {
  106.             //makes the key listener "wake up"
  107.             addKeyListener(this);
  108.            
  109.             //should be done in an infinite loop, so it repeats
  110.             while (true) {
  111.                 //make try block, so it can exit if it errors
  112.                 try {
  113.                     //refresh screen
  114.                     repaint();
  115.                    
  116.                     //check to see if car hits outside wall
  117.                     //if so, slow down
  118.                     if (p1.intersects(left) || p1.intersects(right) ||
  119.                         p1.intersects(top) || p1.intersects(bottom) ||
  120.                         p1.intersects(obstacle) || p1.intersects(obstacle2) ||
  121.                         p1.intersects(obstacle3) || p1.intersects(obstacle4) ||
  122.                         p1.intersects(obstacle5) || p1.intersects (p2)) {
  123.                             plSpeed = -4;
  124.                     }
  125.                    
  126.                     //if car hits center, make speed -2.5
  127.                     if (p1.intersects(center)) {
  128.                         plSpeed = -2.5;
  129.                     }
  130.                    
  131.                     //lets the car stop
  132.                     if (plSpeed==0) {
  133.                         p1Direction = STOP;
  134.                     }
  135.                    
  136.                     //moves player based on direction
  137.                     if (p1Direction==UP) {
  138.                         p1.y -= (int) plSpeed;
  139.                     }
  140.                     if (p1Direction==DOWN) {
  141.                         p1.y += (int) plSpeed;
  142.                     }
  143.                     if (p1Direction==LEFT) {
  144.                         p1.x -= (int) plSpeed;
  145.                     }
  146.                     if (p1Direction==RIGHT) {
  147.                         p1.x += (int) plSpeed;
  148.                     }
  149.                     if (p1Direction==STOP) {
  150.                         plSpeed = 0;
  151.                     }
  152.                    
  153.                     //delays refresh rate
  154.                     Thread.sleep(75);
  155.                 }
  156.                 catch(Exception e) {
  157.                     //if an error, exit
  158.                     break;
  159.                 }
  160.             }
  161.         }
  162.        
  163.         //have to input these (so it will compile)
  164.         public void keyPressed(KeyEvent event) {
  165.             try {
  166.                 //makes car increase speed a bit
  167.                 if (event.getKeyChar()=='a' ||
  168.                     event.getKeyChar()=='s' ||
  169.                     event.getKeyChar()=='d' ||
  170.                     event.getKeyChar()=='w') {
  171.                         plSpeed += .2;
  172.                 } else {                        //makes the car slow down
  173.                     plSpeed -= .5;
  174.                     Thread.sleep(75);
  175.                 }
  176.             } catch (Exception I) {
  177.                 plSpeed = plSpeed;
  178.             }
  179.         }
  180.         public void keyReleased(KeyEvent event) {}
  181.        
  182.         //now, to be able to set the direction
  183.         public void keyTyped(KeyEvent event) {
  184.             if (event.getKeyChar()=='a') {
  185.                 p1Direction = LEFT;
  186.             }
  187.             if (event.getKeyChar()=='s') {
  188.                 p1Direction = DOWN;
  189.             }
  190.             if (event.getKeyChar()=='d') {
  191.                 p1Direction = RIGHT;
  192.             }
  193.             if (event.getKeyChar()=='w') {
  194.                 p1Direction = UP;
  195.             }
  196.            
  197.  
  198.         }
  199.     }
  200.     private class Move2 extends Thread implements KeyListener {
  201.         public void run() {
  202.             //makes the key listener "wake up"
  203.             addKeyListener(this);
  204.            
  205.             //should be done in an infinite loop, so it repeats
  206.             while (true) {
  207.                 //make try block, so it can exit if it errors
  208.                 try {
  209.                     //refresh screen
  210.                     repaint();
  211.                    
  212.                     //check to see if car hits outside wall
  213.                     //if so, slow down
  214.                     if (p2.intersects(left) || p2.intersects(right) ||
  215.                         p2.intersects(top) || p2.intersects(bottom) ||
  216.                         p2.intersects(obstacle) || p2.intersects(obstacle2) ||
  217.                         p2.intersects(obstacle3) || p2.intersects(obstacle4) ||
  218.                         p2.intersects(obstacle5) || p2.intersects(p1)) {
  219.                             plSpeed = -4;
  220.                     }
  221.                    
  222.                     //if car hits center, make speed -2.5
  223.                     if (p2.intersects(center)) {
  224.                         p2Speed = -2.5;
  225.                     }
  226.                    
  227.                     //makes car increase speed a bit
  228.                     if (p2Speed <= 5) {
  229.                         plSpeed += .2;
  230.                     }
  231.                    
  232.                     //lets the car stop
  233.                     if (p2Speed == 0) {
  234.                         p2Direction = STOP;
  235.                     }
  236.                    
  237.                     //moves player based on direction
  238.                     if (p2Direction==UP) {
  239.                         p2.y -= (int) p2Speed;
  240.                     }
  241.                     if (p2Direction==DOWN) {
  242.                         p2.y += (int) p2Speed;
  243.                     }
  244.                     if (p2Direction==LEFT) {
  245.                         p2.x -= (int) p2Speed;
  246.                     }
  247.                     if (p2Direction==RIGHT) {
  248.                         p2.x += (int) p2Speed;
  249.                     }
  250.                     if (p2Direction==STOP) {
  251.                         p2Speed = 0;
  252.                     }
  253.                    
  254.                     //delays refresh rate
  255.                     Thread.sleep(75);
  256.                 }
  257.                 catch(Exception e) {
  258.                     //if an error, exit
  259.                     break;
  260.                 }
  261.             }
  262.         }
  263.  
  264.         public void keyPressed(KeyEvent event) {
  265.             try {
  266.                 //makes car increase speed a bit
  267.                 if (event.getKeyChar()=='j' ||
  268.                     event.getKeyChar()=='k' ||
  269.                     event.getKeyChar()=='l' ||
  270.                     event.getKeyChar()=='i') {
  271.                         plSpeed += .2;
  272.                 } else {                        //makes the car slow down
  273.                     p2Speed -= .5;
  274.                     Thread.sleep(75);
  275.                 }
  276.             } catch (Exception I) {
  277.                 p2Speed = p2Speed;
  278.             }
  279.         }
  280.         public void keyReleased(KeyEvent event) {}
  281.        
  282.         //now, to be able to set the direction
  283.         public void keyTyped(KeyEvent event) {
  284.             if (event.getKeyChar()=='j') {
  285.                 p2Direction = LEFT;
  286.             }
  287.             if (event.getKeyChar()=='k') {
  288.                 p2Direction = DOWN;
  289.             }
  290.             if (event.getKeyChar()=='l') {
  291.                 p2Direction = RIGHT;
  292.             }
  293.             if (event.getKeyChar()=='i') {
  294.                 p2Direction = UP;
  295.             }
  296.         }
  297.     }
  298.    
  299.     //finally, to start the program
  300.     public static void main(String[] args) {
  301.         Racing frame = new Racing();
  302.         frame.setVisible( true );
  303.         frame.setLocationRelativeTo( null );
  304.         frame.setResizable( false );
  305.     }
  306. }
Advertisement
Add Comment
Please, Sign In to add comment