This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

Tile Example

By: a guest on Apr 18th, 2012  |  syntax: Java  |  size: 3.76 KB  |  views: 41  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import java.util.Random;
  5. import static java.awt.event.KeyEvent.*;
  6.  
  7. public class Tiles extends JPanel implements KeyListener {
  8.     static final int tilesPerRow = 8, tilesPerColumn = 6;
  9.     static final int ballDiameter = 20;
  10.     static final Color ballColor = Color.BLACK;
  11.     static Random rand = new Random();
  12.     double speedX = 0, speedY = 0;
  13.     double posX = 20, posY = 20; // position of the ball
  14.     Color[][] tileMap;
  15.     int tileWidth, tileHeight, tileMapWidth, tileMapHeight;
  16.    
  17.     public static void main(String[] args) throws Exception {
  18.         JFrame frame = new JFrame();
  19.         Tiles panel = new Tiles();
  20.         panel.setPreferredSize(new Dimension(800, 600));
  21.         frame.addKeyListener(panel);
  22.         frame.add(panel);
  23.         frame.pack();
  24.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  25.         frame.setVisible(true);
  26.         panel.run();
  27.     }
  28.    
  29.     public Tiles() {
  30.         tileMap = new Color[20][20];
  31.         for(int y = 0; y < tileMap.length; y++) {
  32.             for(int x = 0; x < tileMap[0].length; x++) {
  33.                 tileMap[y][x] = new Color(rand.nextInt(0xFFFFFF));
  34.             }
  35.         }
  36.     }
  37.    
  38.     public void run() throws Exception {
  39.         tileWidth = getWidth()/tilesPerRow;
  40.         tileHeight = getHeight()/tilesPerColumn;
  41.         tileMapWidth = tileMap[0].length*tileWidth;
  42.         tileMapHeight = tileMap.length*tileHeight;
  43.         while(true) {
  44.             posX += speedX; posY += speedY;
  45.             if(posX < 0) posX = 0;
  46.             if(posY < 0) posY = 0;
  47.             if(posX > tileMapWidth) posX = tileMapWidth;
  48.             if(posY > tileMapHeight) posY = tileMapHeight;
  49.             Thread.sleep(16);
  50.             repaint();
  51.         }
  52.     }
  53.    
  54.     public void keyPressed(KeyEvent e) {
  55.         switch(e.getKeyCode()) {
  56.             case VK_LEFT: speedX = -5; break;
  57.             case VK_RIGHT: speedX = 5; break;
  58.             case VK_UP: speedY = -5; break;
  59.             case VK_DOWN: speedY = 5; break;
  60.         }
  61.     }
  62.     public void keyReleased(KeyEvent e) {
  63.         switch(e.getKeyCode()) {
  64.             case VK_LEFT: if(speedX < 0) speedX = 0; break;
  65.             case VK_RIGHT: if(speedX > 0) speedX = 0; break;
  66.             case VK_UP: if(speedY < 0) speedY = 0; break;
  67.             case VK_DOWN: if(speedY > 0) speedY = 0; break;
  68.         }
  69.     }
  70.     public void keyTyped(KeyEvent e) {
  71.     }
  72.     public void paint(Graphics g) {
  73.        
  74.         //calculate coordinates of the upper left corner of the tileMap
  75.         int originX = getWidth()/2-(int)posX;
  76.         int originY = getHeight()/2-(int)posY;
  77.         if(originX > 0) originX = 0;
  78.         if(originY > 0) originY = 0;
  79.         //calculate the lower right corner of the tileMap
  80.         int lrX = originX + tileMapWidth;
  81.         int lrY = originY + tileMapHeight;
  82.         if(lrX < getWidth()) {
  83.             originX = getWidth()-tileMapWidth;
  84.             lrX = getWidth();
  85.         }
  86.         if(lrY < getHeight()) {
  87.             originY = getHeight()-tileMapHeight;
  88.             lrY = getHeight();
  89.         }
  90.        
  91.         //draw the tiles
  92.         for(int y = 0; y < tileMap.length; y++) {
  93.             for(int x = 0; x < tileMap[0].length; x++) {
  94.                 int tx = x*tileWidth + originX;
  95.                 int ty = y*tileHeight + originY;
  96.                 if(tx + tileWidth >= 0 && ty + tileHeight >= 0 && tx < getWidth() && ty < getHeight()) {
  97.                     g.setColor(tileMap[y][x]);
  98.                     g.fillRect(tx, ty, tileWidth, tileHeight);
  99.                 }
  100.             }
  101.         }
  102.         g.setColor(ballColor);
  103.         g.fillOval(originX+(int)posX-ballDiameter/2, originY+(int)posY-ballDiameter/2, ballDiameter, ballDiameter);
  104.     }
  105. }
clone this paste RAW Paste Data