Advertisement
Leedwon

Untitled

Jul 18th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.79 KB | None | 0 0
  1. package com.example;
  2. import java.awt.*;
  3. import java.awt.image.BufferStrategy;
  4. import java.util.Random;
  5.  
  6. import javax.swing.Timer;
  7.  
  8. // OK this the class where we will draw
  9. public class GameCanvas extends Canvas{
  10.     // the initial position of the ball in the canvas that will be determine randomly
  11.     int ballX, ballY;
  12.     // the delta we apply to the x and y position at each repaint
  13.     // here it is set to 1... would should have a larger value in "real" life
  14.     // but for testing purpose that will give you the fastest possible value for a 1 pixel update
  15.     int deltaX = +1, deltaY = +1;
  16.     // the size of the ball in pixels
  17.     final int BALLSIZE = 25;
  18.     // a flag if repaint in progress (needed if our computation are to long)
  19.     boolean repaintInProgress = false;
  20.     // we use 2 pages to do our buffering
  21.     // just for demo purpose we will change the background color everytime we repaint a frame
  22.     // you will never do that in real life :-)
  23.     Color[] back = {Color.YELLOW, Color.MAGENTA};
  24.     // just used to determine on which buffer I am writting... will not be used in real life
  25.     int page = 0;
  26.     // a random object to determine where the initial position of the ball will be
  27.     Random ran = new Random();
  28.     // this is a Canvas but I wont't let the system when to repaint it I will do it myself
  29.     GameCanvas() {
  30.         // so ignore System's paint request I will handle them
  31.         setIgnoreRepaint(true);
  32.         // a random place to start the ball
  33.         Dimension canvasSize = getSize();
  34.         ballX = ran.nextInt(580);
  35.         ballY = ran.nextInt(380);
  36.         // build Chrono that will call me
  37.         Chrono chrono = new Chrono(this);
  38.         // ask the chrono to calll me every 60 times a second so every 16 ms
  39.         new Timer(16, chrono).start();
  40.     }
  41.  
  42.     // my own paint method that repaint off line and switch the displayed buffer
  43.     // according to the VBL
  44.     public void myRepaint() {
  45.         // wasting too much time doing the repaint... ignore it
  46.         if(repaintInProgress)
  47.             return;
  48.         // so I won't be called 2 times in a row for nothing
  49.         repaintInProgress = true;
  50.         // get actual Canvas size so I can check if I am out of bounds
  51.         Dimension size = getSize();
  52.         // test for all debordement possibilities on the X axis
  53.         if(deltaX > 0) {
  54.             if(ballX > size.width - BALLSIZE)
  55.                 deltaX = -deltaX;
  56.         }
  57.         else {
  58.             if(ballX < 0)
  59.                 deltaX = -deltaX;
  60.         }
  61.         // check on the Y axis
  62.         if(deltaY > 0) {
  63.             if(ballY > size.height - BALLSIZE)
  64.                 deltaY = -deltaY;
  65.         }
  66.         else {
  67.             if(ballY < 0)
  68.                 deltaY = -deltaY;
  69.         }
  70.         // update ball position
  71.         ballX += deltaX;
  72.         ballY += deltaY;
  73.         // ok doing the repaint on the not showed page
  74.         BufferStrategy strategy = getBufferStrategy();
  75.         Graphics graphics = strategy.getDrawGraphics();
  76.         // this is for testing purpose you would not do that in real life
  77.         // we change the background color to that you will see that the page are flipped
  78.         page++;
  79.         page %= 2;
  80.         // again testing purpose we flip backgound color every repaint
  81.         graphics.setColor(back[page]);
  82.         graphics.fillRect(0, 0, size.width, size.height);
  83.         // now we draw the ball
  84.         graphics.setColor(Color.BLACK);
  85.         graphics.fillOval(ballX, ballY, BALLSIZE, BALLSIZE);
  86.         if(graphics != null)
  87.             graphics.dispose();
  88.         // show next buffer
  89.         strategy.show();
  90.         // synchronized the blitter page shown
  91.         Toolkit.getDefaultToolkit().sync();
  92.  
  93.         // ok I can be called again
  94.         repaintInProgress = false;
  95.     }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement