Advertisement
Guest User

All 3 programs

a guest
Apr 29th, 2016
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.62 KB | None | 0 0
  1. CLASS PLAYER:
  2.  
  3. package monster.defense;
  4.  
  5. import java.awt.*;
  6.  
  7. /**
  8. *
  9. * @author Morgan
  10. */
  11. public class player extends Entity
  12. {
  13. private int xdir;
  14. private int ydir;
  15. public player(int x, int y) {
  16. super(x, y);
  17. w = 16; h=16;
  18. }
  19. public void draw(Graphics g)
  20. {
  21. move();
  22. g.setColor(Color.RED);
  23. g.fillOval(x, y, w, h);
  24. }
  25. private void move()
  26. {
  27. if (this.getX()>=890&&this.getXDir()==1||this.getX()<=0&&this.getXDir()==-1)
  28. {
  29. this.setXDir(0);
  30. }
  31. if (this.getY()>=510&&this.getYDir()==1||this.getY()<=0&&this.getYDir()==-1)
  32. {
  33. this.setYDir(0);
  34. }
  35. x+=xdir;
  36. y+=ydir;
  37. }
  38. public void setXDir(int value)
  39. {
  40. xdir = value;
  41. }
  42. public void setYDir(int value)
  43. {
  44. ydir = value;
  45. }
  46.  
  47. public int getXDir()
  48. {
  49. return xdir;
  50. }
  51. public int getYDir()
  52. {
  53. return ydir;
  54. }
  55.  
  56. }
  57.  
  58. CLASS ENTITY:
  59.  
  60. package monster.defense;
  61.  
  62. import java.awt.*;
  63.  
  64. public abstract class Entity {
  65. protected int x, y, w, h;
  66. protected boolean removed = false;
  67. public Entity(int x, int y)
  68. {
  69. this.x = x;
  70. this.y = y;
  71. }
  72. public void draw(Graphics g)
  73. {
  74. }
  75.  
  76. public int getX() {return x;}
  77. public int getY() {return y;}
  78. public int getW() {return w;}
  79. public int getH() {return h;}
  80. }
  81.  
  82.  
  83. CLASS GAME:
  84.  
  85. package monster.defense;
  86.  
  87. import java.awt.Graphics;
  88. import java.awt.*;
  89. import java.awt.event.KeyEvent;
  90. import java.awt.event.KeyListener;
  91. import java.awt.event.MouseAdapter;
  92. import java.awt.event.MouseEvent;
  93. import java.awt.event.MouseListener;
  94.  
  95. public class game extends javax.swing.JPanel implements KeyListener{
  96. private int x = 20;
  97. private int y = 20;
  98. private int cnt = 0;
  99. private player Player;
  100. private player maximus;
  101. Graphics g;
  102. public game() {
  103. player[] towers = new player[30];
  104. setFocusable(true);
  105. addKeyListener(this);
  106. addMouseListener(new MouseAdapter(){
  107. public void mousePressed(MouseEvent m)
  108. {
  109.  
  110. int mx=m.getX();
  111. int my=m.getY();
  112. towers[cnt]= new player(mx,my);
  113.  
  114. g.setColor(Color.ORANGE);
  115. g.fillOval(x, y, 20,20);
  116. towers[cnt].draw(g);
  117.  
  118.  
  119. repaint();
  120. cnt++;
  121. }
  122. });
  123. Player = new player(100,100);
  124. maximus = new player(490,300);
  125. initComponents();
  126. }
  127. @SuppressWarnings("unchecked")
  128. // <editor-fold defaultstate="collapsed" desc="Generated Code">
  129. private void initComponents() {
  130.  
  131. setBackground(new java.awt.Color(200, 22, 0));
  132.  
  133. javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
  134. this.setLayout(layout);
  135. layout.setHorizontalGroup(
  136. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  137. .addGap(0, 907, Short.MAX_VALUE)
  138. );
  139. layout.setVerticalGroup(
  140. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  141. .addGap(0, 521, Short.MAX_VALUE)
  142. );
  143. }// </editor-fold>
  144. public void paint(Graphics g)
  145. {
  146. //g.setColor(Color.gray);
  147. //g.fillRect(0, 0, getWidth(), getHeight());
  148.  
  149. g.setColor(Color.ORANGE);
  150. g.fillOval(x, y, 20,20);
  151. Player.draw(g);
  152. maximus.draw(g);
  153. this.draw(g);
  154.  
  155. repaint();
  156. }
  157. public void paintComponent(Graphics g)
  158. {
  159. g.setColor(Color.ORANGE);
  160. g.fillOval(x, y, 20,20);
  161. }
  162.  
  163.  
  164. @Override
  165. public void keyTyped(KeyEvent e) {
  166.  
  167. }
  168.  
  169. @Override
  170. public void keyPressed(KeyEvent e) {
  171. int c = e.getKeyCode();
  172. if(c==KeyEvent.VK_W)
  173. {
  174. y-=1;
  175. Player.setYDir(-1);
  176. }
  177. if(c==KeyEvent.VK_S)
  178. {
  179. y+=1;
  180. Player.setYDir(1);
  181. }
  182. if(c==KeyEvent.VK_A)
  183. {
  184. x-=1;
  185. Player.setXDir(-1);
  186. }
  187. if(c==KeyEvent.VK_D)
  188. {
  189. x+=1;
  190. Player.setXDir(1);
  191. }
  192.  
  193.  
  194. }
  195. @Override
  196. public void keyReleased(KeyEvent e) {
  197. Player.setXDir(0);
  198. Player.setYDir(0);
  199. }
  200.  
  201. // Variables declaration - do not modify
  202. // End of variables declaration
  203. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement