Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Graphics;
- import java.awt.Image;
- import java.util.Random;
- import javax.swing.ImageIcon;
- import javax.swing.JPanel;
- public class BackPanel extends JPanel {
- public Image Background, Grass, Stone, Water, Dirt, Bedrock; // Block images
- public boolean imagesLoaded = false; // Whether images are loaded or not
- public int tileSize = 16; // Size of the tile images
- public int rectangle[]; // For later use (Collision, etc)
- private FPSCounter fps = new FPSCounter();
- public BackPanel() {
- getImages();
- }
- public void getImages() { // Load images
- Background = new ImageIcon("res/Textures/Background.png").getImage();
- Grass = new ImageIcon("res/Textures/Grass.png").getImage();
- Dirt = new ImageIcon("res/Textures/Dirt.png").getImage();
- Stone = new ImageIcon("res/Textures/Stone.png").getImage();
- Water = new ImageIcon("res/Textures/Water.png").getImage();
- Bedrock = new ImageIcon("res/Textures/Bedrock.png").getImage();
- imagesLoaded = true;
- }
- // Ignore this method
- public void randomNumberCreator(Graphics g) {
- Random randomNumber = new Random();
- Random x = new Random();
- Random y = new Random();
- int xRandom = x.nextInt(Main.WIDTH);
- int yRandom = y.nextInt(Main.HEIGHT);
- switch (randomNumber.nextInt(4)) {
- case 1:
- if (yRandom >= tileSize * 10 && yRandom <= tileSize * 25 && xRandom >= 0 && xRandom <= Main.WIDTH) {
- g.drawImage(Dirt, xRandom, yRandom, tileSize, tileSize, null);
- }
- break;
- case 2:
- if (yRandom >= tileSize * 25 && yRandom <= tileSize * 30 && xRandom >= 0 && xRandom <= Main.WIDTH) {
- g.drawImage(Stone, xRandom, yRandom, tileSize, tileSize, null);
- }
- break;
- }
- }
- public void paintComponent(Graphics g) {
- if (imagesLoaded) {
- g.drawImage(Background, 0, 0, Main.WIDTH, Main.HEIGHT, null);
- fps.tick();
- g.drawString("FPS: " + fps.FPS, 10, 20);
- // int y = tilesize * [Which layer to start on]; y < tileSize * Layer to end on; y += tileSize [Go to next layer]
- for (int x = 0; x < Main.WIDTH; x += tileSize) {
- for (int y = tileSize * 18; y < tileSize * 19; y += tileSize) {// Grass
- g.drawImage(Grass, x, y, tileSize, tileSize, null);
- g.drawRect(x, y, tileSize, tileSize);
- }
- for (int y = tileSize * 19; y < tileSize * 25; y += tileSize) {// Dirt
- g.drawImage(Dirt, x, y, tileSize, tileSize, null);
- g.drawRect(x, y, tileSize, tileSize);
- }
- for (int y = tileSize * 25; y < tileSize * 30; y += tileSize) {// Stone
- g.drawImage(Stone, x, y, tileSize, tileSize, null);
- g.drawRect(x, y, tileSize, tileSize);
- }
- for (int y = tileSize * 30; y < tileSize * 37; y += tileSize) {// Bedrock
- g.drawImage(Bedrock, x, y, tileSize, tileSize, null);
- g.drawRect(x, y, tileSize, tileSize);
- }
- }
- }
- repaint();
- }
- }
Add Comment
Please, Sign In to add comment