Guest User

flickering help?

a guest
Jan 13th, 2012
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.75 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4.  
  5. class Lazers extends JFrame {
  6.    
  7.     //define rectangles for the lazers and person
  8.     int x = (int) (Math.random() * 780);
  9.     final Rectangle guy = new Rectangle (375, 400, 50, 50 );
  10.     final Rectangle lazer = new Rectangle ( x, 0, 7, 23 );
  11.     Rectangle back = new Rectangle( 0, 0, 800, 500 );
  12.    
  13.     //holds the variables for the guy's movement
  14.     int STOP = 0, LEFT = 1, RIGHT = 2;
  15.     int guyDirection;
  16.    
  17.     //variables that check if player is intersecting the sides
  18.     Rectangle leftSide = new Rectangle (780, 0, 5, 500);
  19.     Rectangle rightSide = new Rectangle (0, 0, 5, 500);
  20.    
  21.     public static void main (String []args ) {
  22.         Lazers frame = new Lazers();
  23.         frame.setVisible(true);
  24.     }
  25.        
  26.     public Lazers( ) { //DO NOT TOUCH!!!
  27.         //set the frame default properties
  28.         super ( "My Lazer Game");
  29.         setSize ( 800,500 );
  30.         setLocationRelativeTo ( null );
  31.         setResizable (false);
  32.         Image img = Toolkit.getDefaultToolkit().getImage( "guy.PNG" );
  33.         setIconImage ( img );
  34.         //register 'Exit upon closing' as a default close operation
  35.         setDefaultCloseOperation( EXIT_ON_CLOSE );
  36.        
  37.         guyMove gm = new guyMove();
  38.         gm.start();
  39.     }
  40.    
  41.     public void paint(Graphics g) {
  42.         //super.paint(g);
  43.         g.setColor(Color.BLACK);
  44.         g.fillRect(back.x, back.y, back.width, back.height);
  45.  
  46.         //put the guy's rectangle on the screen
  47.         g.setColor(Color.BLUE);
  48.         g.fill3DRect(guy.x,guy.y,guy.width,guy.height,true);
  49.         //put the lazers on the screen
  50.         g.setColor(Color.ORANGE);
  51.         g.fill3DRect(lazer.x,lazer.y,lazer.width,lazer.height,true);
  52.     }
  53.    
  54.     private class guyMove extends Thread implements KeyListener {
  55.         public void run() {
  56.             addKeyListener(this);
  57.             while (true) {
  58.                 try {
  59.                     repaint();
  60.                     if (guy.intersects(leftSide) ||
  61.                         guy.intersects(rightSide)) {
  62.                             guyDirection = STOP;
  63.                     }
  64.            
  65.                     if (guyDirection==STOP) {
  66.                     }
  67.            
  68.                     if (guyDirection==LEFT) {
  69.                         guy.x -= 5;
  70.                     }
  71.                
  72.                     if (guyDirection==RIGHT) {
  73.                         guy.x += 5;
  74.                     }
  75.                     Thread.sleep(75);
  76.                 }
  77.                 catch(Exception e) {
  78.                     break;
  79.                 }
  80.             }
  81.         }
  82.        
  83.         public void keyPressed(KeyEvent event) {}
  84.         public void keyReleased(KeyEvent event) {}
  85.         public void keyTyped(KeyEvent event) {
  86.             if (event.getKeyChar()=='a') {
  87.                 guyDirection = LEFT;
  88.             }
  89.            
  90.             if (event.getKeyChar()=='d') {
  91.                 guyDirection = RIGHT;
  92.             }
  93.         }
  94.     }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment