Advertisement
axel_

JavaGame

Oct 25th, 2013
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.35 KB | None | 0 0
  1. package javaapplication1;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Font;
  5. import java.awt.Graphics;
  6. import java.awt.Image;
  7. import java.awt.event.KeyAdapter;
  8. import java.awt.event.KeyEvent;
  9. import javax.swing.ImageIcon;
  10. import javax.swing.JFrame;
  11.  
  12. public class JavaGame extends JFrame implements Runnable{
  13.  
  14. int x, y, xDirection, yDirection;
  15. private Image dbImage;
  16. private Graphics dbg;
  17. Image face;
  18.  
  19. Font font = new Font("Arial", Font.BOLD | Font.CENTER_BASELINE, 30);
  20.  
  21. public void run(){
  22. try{
  23. while(true){
  24.  
  25. move();
  26.  
  27. Thread.sleep(10);
  28.  
  29. }
  30.  
  31. }
  32. catch(Exception e){
  33. System.out.println("ERROR");
  34. }
  35. }
  36. public void move(){
  37. x += xDirection;
  38. y += yDirection;
  39. if(x <= 10)
  40. x = 10;
  41. if(x >= 200)
  42. x = 200;
  43. if(y <= 25)
  44. y = 25;
  45. if(y >= 250)
  46. y = 250;
  47. }
  48. public void setXDirection(int xdir){
  49. xDirection = xdir;
  50. }
  51. public void setYDirection(int ydir){
  52. yDirection = ydir;
  53. }
  54.  
  55. public class AL extends KeyAdapter {
  56. private int keyCode;
  57. public void keyPressed(KeyEvent e){
  58. int keyCode = e.getKeyCode();
  59. if(keyCode == e.VK_LEFT){
  60. setXDirection(-1);
  61. }
  62. if(keyCode == e.VK_RIGHT){
  63. setXDirection(+1);
  64. }
  65. if(keyCode == e.VK_UP){
  66. setYDirection(-1);
  67. }
  68. if(keyCode == e.VK_DOWN){
  69. setYDirection(+1);
  70. }
  71. }
  72. public void keyReleased(KeyEvent e){
  73. if(keyCode == e.VK_LEFT){
  74. setXDirection(0);
  75. }
  76. if(keyCode == e.VK_RIGHT){
  77. setXDirection(0);
  78. }
  79. if(keyCode == e.VK_UP){
  80. setYDirection(0);
  81. }
  82. if(keyCode == e.VK_DOWN){
  83. setYDirection(0);
  84. }
  85. }
  86. }
  87.  
  88. public JavaGame(){
  89. //Load Image
  90. ImageIcon i = new ImageIcon("C:/Documents and Settings/Administrator/My Documents/NetBeansProjects/javagame/src/javaapplication1/this.gif");
  91. face = i.getImage();
  92. //Game Properties
  93. addKeyListener(new AL());
  94. setTitle("JavaGame");
  95. setSize(250, 250);
  96. setResizable(false);
  97. setLocationRelativeTo(null);
  98. setVisible(true);
  99. getContentPane().setBackground(Color.cyan);
  100. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  101.  
  102. x = 150;
  103. y = 150;
  104. }
  105.  
  106. public void paint(Graphics g){
  107. dbImage = createImage(getWidth(),getHeight());
  108. dbg = dbImage.getGraphics();
  109. paintComponent(dbg);
  110. g.drawImage(dbImage, 0, 0, this);
  111. }
  112.  
  113. public void paintComponent(Graphics g){
  114. super.paint(g);
  115. g.setFont(font);
  116. g.setColor(Color.BLUE);
  117. g.drawString("Is this a game?", 50, 50);
  118. g.setColor(Color.GREEN);
  119. g.drawImage(face, x, y, this);
  120.  
  121. repaint();
  122. }
  123.  
  124. public static void main(String[] args) {
  125. JavaGame jg = new JavaGame();
  126. //Threads
  127. Thread t1 = new Thread(jg);
  128. t1.start();
  129.  
  130. }
  131.  
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement