Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /***************************
- * Brian Capps
- * 05/11/2009
- * Description
- ****************************/
- import java.awt.*;
- import java.awt.image.*;
- import java.awt.event.*;
- import javax.imageio.ImageIO;
- import java.io.*;
- import java.io.File;
- import java.util.Scanner;
- import java.util.ArrayList;
- public class RobotDomination extends Canvas implements Runnable
- {
- private BufferedImage[][] imageMap;
- private String[][] textMap;
- private BufferedImage sky, ground, brick, flame;
- private String state;
- private Thread animator;
- private Font robotHead;
- private double scrollRate;
- private double totalScroll;
- public RobotDomination()
- {
- state = "loading";
- scrollRate = 0.3;
- totalScroll = 0.0;
- load();
- try //make the font
- {
- FileInputStream fontStream = new FileInputStream( "robothead.ttf" );
- robotHead = Font.createFont(Font.TRUETYPE_FONT, fontStream );
- robotHead = robotHead.deriveFont( (float) 50 );
- }
- catch (Exception e) {}
- }
- public void load()
- {
- try
- {
- sky = ImageIO.read(new File("images/sky.png"));
- ground = ImageIO.read(new File("images/ground.png"));
- brick = ImageIO.read(new File("images/brick.png"));
- flame = ImageIO.read(new File("images/flame.png"));
- }
- catch(Exception e){}
- ArrayList<String> lines = new ArrayList<String>();
- try
- {
- Scanner fileIn = new Scanner(new File("map.txt"));
- while(fileIn.hasNext())
- lines.add(fileIn.next());
- fileIn.close();
- }
- catch(Exception e){}
- //load the text file into a 2d array
- textMap = new String[lines.size()][lines.get(lines.size()-1).length()];
- for(int i =0; i<lines.size(); i++)
- for(int j=0; j<lines.get(i).length(); j++)
- textMap[i][j] = new String(""+lines.get(i).charAt(j));
- //load the appropriate images for each coord
- imageMap = new BufferedImage[textMap.length][textMap[0].length];
- for(int i =0; i<imageMap.length; i++)
- for(int j=0; j<imageMap[0].length; j++)
- if(textMap[i][j]==null || textMap[i][j].equals(" "))
- imageMap[i][j] = sky;
- else if(textMap[i][j].equals("#"))
- imageMap[i][j] = ground;
- else if(textMap[i][j].equals("B"))
- imageMap[i][j] = brick;
- else if(textMap[i][j].equals("?"))
- imageMap[i][j] = flame;
- else
- imageMap[i][j] = null;
- state = "";
- }
- public void paint(Graphics g)
- {
- Graphics2D g2 = (Graphics2D)g;
- g2.setFont(robotHead);
- g2.setColor(Color.BLACK);
- g2.fillRect(0,0,700,500);
- for(int i =0; i<imageMap.length; i++)
- for(int j=0; j<imageMap[0].length; j++)
- if(j*50 - totalScroll >-50 && j*50 - totalScroll <50)
- g2.drawImage(imageMap[i][j],null, (int) (j*50 - totalScroll), i*50);
- totalScroll-=scrollRate;
- //g2.setColor(Color.WHITE);
- //g2.drawString("Hello!",300,100);
- }
- public void start()
- {
- animator = new Thread(this);
- animator.start();
- }
- public void run()
- {
- repaint();
- try{
- Thread.sleep(1000/24);
- }catch(Exception e){}
- }
- public static void main (String [] args)
- {
- Frame frame = new Frame("Robot Domination!!!!!!!!!!!!!!!!!!");
- frame.add(new RobotDomination());
- frame.setSize(700, 500);
- frame.addWindowListener(new WindowAdapter() {
- public void windowClosing(WindowEvent e) {
- System.exit(0);
- }
- });
- frame.setVisible(true);
- frame.setResizable(false);
- }//end main
- }//end class
Add Comment
Please, Sign In to add comment