Results 1 to 1 of 1
  1. #1
    Join Date
    Mar 2011
    Location
    Montana
    Posts
    1
    Rep Power
    0

    Unhappy Arrays and For statements for Images in Applet

    Ok so I am trying to determine the X Y locations using for statement and a 2d array that is supposed to contain each 'redBrick' and 'yellowBrick' image based off the text file read in. I want the marker to go along the Yellow brick road to find the end of the maze but no bounds checking has to be done as this is like a 2nd java class.

    How do I say if the index value stored in Maze[x] is 1 && Maze[y] then draw a yellowBrick?? Because [x] from Maze[x][y] represents my rows does it not? And [y] from the same would represent my column data?
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.KeyEvent;
    import java.io.*;
    import java.util.*;
    
    public class MazeDriver extends JApplet {
    
    	private static final long serialVersionUID = 1L;
    
    	private Scanner mazeScan;
    
    	private final int APPLET_WIDTH = 320;
    	private final int APPLET_HEIGHT = 320;
    	private final int MINMAX = 20;
    	private int userMarkerX = 0, userMarkerY = 0;
    	private int column, row;
    	private int[][] maze;
    
    	private Image redBrick, yellowBrick, userMarker;
    
    	// The following method loads the images to be used
    	public void init() {
    		redBrick = getImage(getDocumentBase(), "RedBrick.png");
    		yellowBrick = getImage(getDocumentBase(), "YellowBrick.png");
    		userMarker = getImage(getDocumentBase(), "Marker.png");
    
    		setSize(APPLET_WIDTH, APPLET_HEIGHT);
    	}
    
    	// The following draws the images
    	public void drawPictures(Graphics page) {
    		int[][] Maze1 = new int[8][13];
    		// needed to open File
    		openFile();
    		// needed to read File
    		readFile();
    		// needed to close File
    		closeFile();
    
    	[B]	for (int x = 0; x < APPLET_WIDTH; x += 20) {
    			for (int y = 0; y < APPLET_HEIGHT; x += 20) {
    				if (Maze1[x][y] == 1) {
    					page.drawImage(yellowBrick, x, y, MINMAX, MINMAX, this);
    				} else {
    					page.drawImage(redBrick, x, y, MINMAX, MINMAX, this);
    				}
    
    			}
    		}[/B]
    	}
    
    	public void paint(Graphics page) {
    		drawPictures(page);
    	}
    
    	public void openFile() {
    		try {
    			mazeScan = new Scanner(new File("./src/Maze1.txt"));
    		} catch (Exception e) {
    			System.out.println("Could not find file.");
    		}
    	}
    
    	public void readFile() {
    		// Keeps going till it reaches the end of
    		// file. When it reaches the end it will
    		// break out of the file.
    		while (mazeScan.hasNext()) {
    			int[] col = new int[13];
    			int[] row = new int[8];
    			mazeScan.useDelimiter(",");
    			for (int x = 0; x < row.length; x++) {
    				row[x] = mazeScan.nextInt();
    				for (int y = 0; y < col.length; y++) {
    					col[y] = mazeScan.nextInt();
    				}
    			}
    		}
    	}
    
    	public void closeFile() {
    		mazeScan.close();
    	}
    
    	public void keyPressed(KeyEvent e) {
    		int uMX = 0, uMY = 0;
    
    		if (e.getKeyCode() == KeyEvent.VK_DOWN) {
    			// userMarker
    			uMY -= 20;
    			uMX = 0;
    			updateXY(uMX, uMY);
    			repaint();
    		}
    		if (e.getKeyCode() == KeyEvent.VK_LEFT) {
    			// userMarker
    			uMX += 20;
    			uMY = 0;
    			updateXY(uMX, uMY);
    			repaint();
    		}
    		if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
    			// userMarker
    			uMX -= 20;
    			uMY = 0;
    			updateXY(uMX, uMY);
    			repaint();
    		}
    		if (e.getKeyCode() == KeyEvent.VK_UP) {
    			// userMarker
    			uMY += 20;
    			uMX = 0;
    			updateXY(uMX, uMY);
    			repaint();
    		}
    	}
    
    	public void updateXY(int uMX, int uMY) {
    		uMX = 0;
    		uMY = 0;
    
    		userMarkerX += uMX;
    		userMarkerY += uMY;
    
    	}
    }

    Text file it reads in...
    1,1,1,0,1,1,0,0,0,1,1,1,1
    1,0,1,1,1,0,1,1,1,1,0,0,1
    0,0,0,0,1,0,1,0,1,0,1,0,0
    1,1,1,0,1,1,1,0,1,0,1,1,1
    1,0,1,0,0,0,0,1,1,1,0,0,1
    1,0,1,1,1,1,1,1,0,1,1,1,1
    1,0,0,0,0,0,0,0,0,0,0,0,0
    1,1,1,1,1,1,1,1,1,1,1,1,1
    Last edited by SummitWhiteMaro; 03-07-2011 at 04:02 AM.

Similar Threads

  1. Images in applet
    By Ranu in forum New To Java
    Replies: 2
    Last Post: 07-09-2010, 05:05 AM
  2. Putting Applet on web with images
    By Peetahzee in forum New To Java
    Replies: 1
    Last Post: 04-18-2010, 08:35 PM
  3. Extract images from Java applet
    By User01 in forum Java Applets
    Replies: 2
    Last Post: 12-23-2009, 06:44 AM
  4. Applet with Images in Browser
    By baron in forum Eclipse
    Replies: 0
    Last Post: 09-20-2009, 10:06 PM
  5. if else statements and using images
    By Joshr in forum New To Java
    Replies: 8
    Last Post: 10-05-2008, 06:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •