Advertisement
Guest User

Untitled

a guest
May 25th, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.90 KB | None | 0 0
  1. public class Eat {
  2.  
  3. public static void main(String[] args) {
  4.  
  5. // Creating the main frame
  6. JFrame main = new JFrame("Eat 'Em All - Version 1.0.2");
  7. main.setSize(497, 599);
  8. main.setLocationRelativeTo(null);
  9. main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  10. main.setResizable(false);
  11.  
  12. // Colours and borders
  13. Border areaBorder = new LineBorder(Color.LIGHT_GRAY, 3);
  14.  
  15. // Creating main JPanel
  16. JPanel area = new JPanel();
  17. area.setLayout(new BoxLayout(area, BoxLayout.PAGE_AXIS));
  18. area.setBackground(Color.WHITE);
  19. main.setContentPane(area);
  20.  
  21. // Creating the drawing/image/player
  22. DrawPlayer player = new DrawPlayer();
  23. player.setPreferredSize(new Dimension(497, 539));
  24. player.setOpaque(false);
  25.  
  26. // Enemies
  27. DrawEnemy enemy = new DrawEnemy();
  28. enemy.setPreferredSize(new Dimension(497, 539));
  29. enemy.setBackground(Color.WHITE);
  30.  
  31. // Creating the control panel for buttons, etc
  32. JPanel control = new JPanel();
  33. control.setPreferredSize(new Dimension(497, 60));
  34. control.setLayout(new GridLayout(1, 2, 0, 0));
  35. control.setBorder(areaBorder);
  36.  
  37. JLabel welcome = new JLabel(" Welcome to Eat 'Em All |--| Press 'Start'");
  38. JButton start = new JButton("Start");
  39.  
  40. // Adding it all to the frame
  41. main.add(enemy);
  42. enemy.add(player);
  43. control.add(welcome);
  44. control.add(start);
  45. area.add(control);
  46.  
  47. // Adding keylistener and making button false
  48. player.addKeyListener(player);
  49. player.setFocusable(true);
  50. start.setFocusable(false);
  51. enemy.setFocusable(false);
  52.  
  53. // Bring frame to front and visible
  54. main.toFront();
  55. main.setVisible(true);
  56.  
  57. System.out.println(player.getWidth() / 2);
  58. System.out.println(player.getHeight() / 2);
  59.  
  60. }
  61.  
  62. }
  63.  
  64. public class DrawPlayer extends JPanel implements KeyListener {
  65.  
  66. long xPosition = 0;
  67. long yPosition = 0;
  68.  
  69. public void paintComponent(Graphics g) {
  70. super.paintComponent(g);
  71.  
  72. // Making loop to get points and move it
  73. // Center of area is x: 245 y: 255
  74. int xPoints[] = {235, 255, 255, 235, 235, 255};
  75. int yPoints[] = {265, 265, 245, 245, 265, 245};
  76. for (int i = 0; i < xPoints.length; i++) {
  77. xPoints[i] += xPosition;
  78. yPoints[i] += yPosition;
  79. }
  80.  
  81. g.setColor(Color.BLUE);
  82. g.drawPolygon(xPoints, yPoints, xPoints.length);
  83. }
  84.  
  85. public void keyPressed(KeyEvent e) {
  86. switch (e.getKeyCode()) {
  87. case KeyEvent.VK_DOWN:
  88. if (yPosition == 245) {
  89. yPosition -= 5;
  90. } else {
  91. yPosition += 5;
  92. }
  93. break;
  94. case KeyEvent.VK_UP:
  95. if (yPosition == -245) {
  96. yPosition += 5;
  97. } else {
  98. yPosition -= 5;
  99. }
  100. break;
  101. case KeyEvent.VK_LEFT:
  102. if (xPosition == -235) {
  103. xPosition += 5;
  104. } else {
  105. xPosition -= 5;
  106. }
  107. break;
  108. case KeyEvent.VK_RIGHT:
  109. if (xPosition == 235) {
  110. xPosition -= 5;
  111. } else {
  112. xPosition += 5;
  113. }
  114. break;
  115. }
  116. repaint();
  117. }
  118.  
  119. public void keyReleased(KeyEvent e) {
  120.  
  121. }
  122.  
  123. public void keyTyped(KeyEvent e) {
  124.  
  125. }
  126. }
  127.  
  128. public class DrawEnemy extends JPanel {
  129.  
  130. public void paintComponent(Graphics f) {
  131. super.paintComponent(f);
  132.  
  133. for (int i = 0; i < 10; i++ ){
  134. f.setColor(Color.RED);
  135. f.drawOval((int)(Math.random() * ((440 - 0) + 0) + 0), (int)(Math.random() * ((500 - 0) + 0) + 0), 50, 50);
  136. }
  137. }
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement