Guest User

Untitled

a guest
Jul 21st, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.80 KB | None | 0 0
  1. import java.awt.Graphics;
  2. import java.awt.Image;
  3. import java.util.Random;
  4.  
  5. import javax.swing.ImageIcon;
  6. import javax.swing.JPanel;
  7.  
  8. public class BackPanel extends JPanel {
  9.  
  10.     public Image Background, Grass, Stone, Water, Dirt, Bedrock; // Block images
  11.  
  12.     public boolean imagesLoaded = false; // Whether images are loaded or not
  13.  
  14.     public int tileSize = 16; // Size of the tile images
  15.     public int rectangle[]; // For later use (Collision, etc)
  16.  
  17.     private FPSCounter fps = new FPSCounter();
  18.  
  19.     public BackPanel() {
  20.         getImages();
  21.     }
  22.  
  23.     public void getImages() { // Load images
  24.         Background = new ImageIcon("res/Textures/Background.png").getImage();
  25.         Grass = new ImageIcon("res/Textures/Grass.png").getImage();
  26.         Dirt = new ImageIcon("res/Textures/Dirt.png").getImage();
  27.         Stone = new ImageIcon("res/Textures/Stone.png").getImage();
  28.         Water = new ImageIcon("res/Textures/Water.png").getImage();
  29.         Bedrock = new ImageIcon("res/Textures/Bedrock.png").getImage();
  30.  
  31.         imagesLoaded = true;
  32.     }
  33.  
  34.     // Ignore this method
  35.     public void randomNumberCreator(Graphics g) {
  36.         Random randomNumber = new Random();
  37.         Random x = new Random();
  38.         Random y = new Random();
  39.  
  40.         int xRandom = x.nextInt(Main.WIDTH);
  41.         int yRandom = y.nextInt(Main.HEIGHT);
  42.  
  43.         switch (randomNumber.nextInt(4)) {
  44.         case 1:
  45.             if (yRandom >= tileSize * 10 && yRandom <= tileSize * 25 && xRandom >= 0 && xRandom <= Main.WIDTH) {
  46.                 g.drawImage(Dirt, xRandom, yRandom, tileSize, tileSize, null);
  47.             }
  48.             break;
  49.         case 2:
  50.             if (yRandom >= tileSize * 25 && yRandom <= tileSize * 30 && xRandom >= 0 && xRandom <= Main.WIDTH) {
  51.                 g.drawImage(Stone, xRandom, yRandom, tileSize, tileSize, null);
  52.             }
  53.             break;
  54.  
  55.         }
  56.     }
  57.  
  58.     public void paintComponent(Graphics g) {
  59.         if (imagesLoaded) {
  60.             g.drawImage(Background, 0, 0, Main.WIDTH, Main.HEIGHT, null);
  61.  
  62.             fps.tick();
  63.             g.drawString("FPS: " + fps.FPS, 10, 20);
  64.  
  65.             // int y = tilesize * [Which layer to start on]; y < tileSize * Layer to end on; y += tileSize [Go to next layer]
  66.             for (int x = 0; x < Main.WIDTH; x += tileSize) {
  67.                 for (int y = tileSize * 18; y < tileSize * 19; y += tileSize) {// Grass
  68.                     g.drawImage(Grass, x, y, tileSize, tileSize, null);
  69.                     g.drawRect(x, y, tileSize, tileSize);
  70.                 }
  71.                 for (int y = tileSize * 19; y < tileSize * 25; y += tileSize) {// Dirt
  72.                     g.drawImage(Dirt, x, y, tileSize, tileSize, null);
  73.                     g.drawRect(x, y, tileSize, tileSize);
  74.                 }
  75.                 for (int y = tileSize * 25; y < tileSize * 30; y += tileSize) {// Stone
  76.                     g.drawImage(Stone, x, y, tileSize, tileSize, null);
  77.                     g.drawRect(x, y, tileSize, tileSize);
  78.                 }
  79.                 for (int y = tileSize * 30; y < tileSize * 37; y += tileSize) {// Bedrock
  80.                     g.drawImage(Bedrock, x, y, tileSize, tileSize, null);
  81.                     g.drawRect(x, y, tileSize, tileSize);
  82.                 }
  83.  
  84.             }
  85.  
  86.         }
  87.         repaint();
  88.  
  89.     }
  90. }
Add Comment
Please, Sign In to add comment