Guest User

Untitled

a guest
Jun 17th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.40 KB | None | 0 0
  1. /***************************
  2. * Brian Capps
  3. * 05/11/2009
  4. * Description
  5. ****************************/
  6. import java.awt.*;
  7. import java.awt.image.*;
  8. import java.awt.event.*;
  9. import javax.imageio.ImageIO;
  10. import java.io.*;
  11. import java.io.File;
  12. import java.util.Scanner;
  13. import java.util.ArrayList;
  14.  
  15. public class RobotDomination extends Canvas implements Runnable
  16. {
  17. private BufferedImage[][] imageMap;
  18. private String[][] textMap;
  19. private BufferedImage sky, ground, brick, flame;
  20.  
  21. private String state;
  22. private Thread animator;
  23. private Font robotHead;
  24.  
  25. private double scrollRate;
  26. private double totalScroll;
  27.  
  28. public RobotDomination()
  29. {
  30. state = "loading";
  31. scrollRate = 0.3;
  32. totalScroll = 0.0;
  33.  
  34. load();
  35. try //make the font
  36. {
  37. FileInputStream fontStream = new FileInputStream( "robothead.ttf" );
  38. robotHead = Font.createFont(Font.TRUETYPE_FONT, fontStream );
  39. robotHead = robotHead.deriveFont( (float) 50 );
  40. }
  41. catch (Exception e) {}
  42. }
  43.  
  44. public void load()
  45. {
  46. try
  47. {
  48. sky = ImageIO.read(new File("images/sky.png"));
  49. ground = ImageIO.read(new File("images/ground.png"));
  50. brick = ImageIO.read(new File("images/brick.png"));
  51. flame = ImageIO.read(new File("images/flame.png"));
  52. }
  53. catch(Exception e){}
  54.  
  55. ArrayList<String> lines = new ArrayList<String>();
  56. try
  57. {
  58. Scanner fileIn = new Scanner(new File("map.txt"));
  59. while(fileIn.hasNext())
  60. lines.add(fileIn.next());
  61. fileIn.close();
  62. }
  63. catch(Exception e){}
  64.  
  65. //load the text file into a 2d array
  66. textMap = new String[lines.size()][lines.get(lines.size()-1).length()];
  67. for(int i =0; i<lines.size(); i++)
  68. for(int j=0; j<lines.get(i).length(); j++)
  69. textMap[i][j] = new String(""+lines.get(i).charAt(j));
  70.  
  71. //load the appropriate images for each coord
  72. imageMap = new BufferedImage[textMap.length][textMap[0].length];
  73. for(int i =0; i<imageMap.length; i++)
  74. for(int j=0; j<imageMap[0].length; j++)
  75. if(textMap[i][j]==null || textMap[i][j].equals(" "))
  76. imageMap[i][j] = sky;
  77. else if(textMap[i][j].equals("#"))
  78. imageMap[i][j] = ground;
  79. else if(textMap[i][j].equals("B"))
  80. imageMap[i][j] = brick;
  81. else if(textMap[i][j].equals("?"))
  82. imageMap[i][j] = flame;
  83. else
  84. imageMap[i][j] = null;
  85.  
  86.  
  87. state = "";
  88. }
  89. public void paint(Graphics g)
  90. {
  91. Graphics2D g2 = (Graphics2D)g;
  92.  
  93.  
  94. g2.setFont(robotHead);
  95.  
  96. g2.setColor(Color.BLACK);
  97. g2.fillRect(0,0,700,500);
  98.  
  99. for(int i =0; i<imageMap.length; i++)
  100. for(int j=0; j<imageMap[0].length; j++)
  101. if(j*50 - totalScroll >-50 && j*50 - totalScroll <50)
  102. g2.drawImage(imageMap[i][j],null, (int) (j*50 - totalScroll), i*50);
  103. totalScroll-=scrollRate;
  104. //g2.setColor(Color.WHITE);
  105. //g2.drawString("Hello!",300,100);
  106. }
  107. public void start()
  108. {
  109. animator = new Thread(this);
  110. animator.start();
  111. }
  112. public void run()
  113. {
  114.  
  115. repaint();
  116. try{
  117. Thread.sleep(1000/24);
  118. }catch(Exception e){}
  119.  
  120. }
  121.  
  122. public static void main (String [] args)
  123. {
  124. Frame frame = new Frame("Robot Domination!!!!!!!!!!!!!!!!!!");
  125. frame.add(new RobotDomination());
  126. frame.setSize(700, 500);
  127. frame.addWindowListener(new WindowAdapter() {
  128. public void windowClosing(WindowEvent e) {
  129. System.exit(0);
  130. }
  131. });
  132. frame.setVisible(true);
  133. frame.setResizable(false);
  134. }//end main
  135. }//end class
Add Comment
Please, Sign In to add comment