I'm trying to make a tilemapper in Java using a JApplet. The problem is that it only shows a white box when I try to display the tilemap. I'm pretty sure it has something to do with me using the wrong method to load all the images into tileArray. Also, the only code that really matters here (as in, the rest works) is init(), paint(), drawTilemap() and the top, where I declared all the variables. Here is the code:
Code:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

// Set your own class name
public class Tilemap extends JApplet
  implements KeyListener, MouseListener
{
  // Your variables
  Image[] tileArray = new Image[4];
  String[] tilePictureNames = { "Air", "Water", "Earth", "Fire" };
  int[][] tilemap =
  { { 0,0,0,0 },
    { 0,3,0,0 },
    { 0,2,2,1 },
    { 2,2,1,1 } };
  Graphics tilemapBuffer;
  Image tilemapImage;
  final int TILESIZE = 10;
 
  boolean leftPressed, rightPressed, downPressed, upPressed; // Keys you want to respond to
  Graphics backBuffer;
  Image bufferImage;
  Timer myTimer;
  boolean hasBeenInInit = false;
 
  public void init()
  {
    // Your stuff to be initialized
    for (int i = 0; i < tileArray.length; i++)
    {
      tileArray[i] = getImage(getCodeBase(), tilePictureNames[i] + ".jpg");
    }
    tilemapImage = createImage(tilemap[0].length * TILESIZE, tilemap.length * TILESIZE);
    tilemapBuffer = tilemapImage.getGraphics();
   
    drawTilemap();
   
    hasBeenInInit = true;
    bufferImage = createImage(getWidth(), getHeight());
    backBuffer = bufferImage.getGraphics();
    myTimer = new Timer(30, updateStuff);
    myTimer.start();
    addMouseListener(this);
    addKeyListener(this);
  }
  public void paint(Graphics g)
  {
    if (!hasBeenInInit)
      init();
    backBuffer.fillRect(0,0,500,500);
    // Your stuff to be drawn to backBuffer
    backBuffer.drawImage(tilemapImage,0,0,this);
    g.drawImage(bufferImage,0,0,this);
  }
  public void update(Graphics g)
  {
    paint(g);
  }
  ActionListener updateStuff = new ActionListener()
  {
    public void actionPerformed(ActionEvent e)
    {
      // Your stuff to be updated
     
      repaint();
    }
  };
  public void drawTilemap()
  {
    for(int y = 0; y < tilemap.length; y++)
    {
      for(int x = 0; x < tilemap[0].length; x++)
      {
        tilemapBuffer.drawImage(tileArray[tilemap[y][x]],
                                x * TILESIZE,
                                y * TILESIZE,
                                this);
      }
    }
  }
  public void keyPressed(KeyEvent evt)
  {
    int key = evt.getKeyCode();
   
    // Your keys to be responded to
    if (key == KeyEvent.VK_LEFT)
      leftPressed = true;
    if (key == KeyEvent.VK_DOWN)
      downPressed = true;
    if (key == KeyEvent.VK_RIGHT)
      rightPressed = true;
    if (key == KeyEvent.VK_UP)
      upPressed = true;
   
  }
  public void keyReleased(KeyEvent evt)
  {
    int key = evt.getKeyCode();
   
    // Same keys, but now set each boolean to false
    if (key == KeyEvent.VK_LEFT)
      leftPressed = false;
    if (key == KeyEvent.VK_DOWN)
      downPressed = false;
    if (key == KeyEvent.VK_RIGHT)
      rightPressed = false;
    if (key == KeyEvent.VK_UP)
      upPressed = false;
   
  }
  public void mousePressed(MouseEvent evt)
  {
    requestFocus();
  }
  public void mouseEntered(MouseEvent evt) { }  // Required by the
  public void mouseExited(MouseEvent evt) { }   //    MouseListener
  public void mouseReleased(MouseEvent evt) { } //       interface.
  public void mouseClicked(MouseEvent evt) { }
  public void keyTyped(KeyEvent evt) { } // Required by KeyListener
}


Edit: I don't know why it wouldn't work before, but if I put drawTilemap() before the the command to copy the tilemapImage over to the backBuffer. Also, I have it so that I can scroll the tilemap Very Happy
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement