Advertisement
Guest User

Untitled

a guest
May 24th, 2018
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.08 KB | None | 0 0
  1. /*
  2. * To change this template, choose Tools | Templates
  3. * and open the template in the editor.
  4. */
  5. package javaapplication38;
  6.  
  7. import java.awt.Color;
  8. import java.awt.Graphics;
  9. import java.awt.Rectangle;
  10. import java.awt.event.ActionEvent;
  11. import java.awt.event.ActionListener;
  12. import java.awt.event.MouseEvent;
  13. import java.awt.event.MouseListener;
  14. import java.awt.event.MouseMotionListener;
  15. import java.awt.image.BufferedImage;
  16. import java.io.File;
  17. import java.io.IOException;
  18. import java.util.Random;
  19. import java.util.logging.Level;
  20. import java.util.logging.Logger;
  21. import javax.imageio.ImageIO;
  22. import javax.swing.*;
  23.  
  24. /**
  25. *
  26. * @author didiamantis
  27. */
  28. public class Game extends JFrame implements ActionListener, MouseMotionListener, MouseListener {
  29.  
  30. private MyPanel panel;
  31. private Timer timer;
  32.  
  33. public Game() {
  34. this.setBounds(0, 0, 800, 600);
  35. this.setTitle("Hello unicorn");
  36. this.setDefaultCloseOperation(EXIT_ON_CLOSE);
  37. this.panel = new MyPanel(this);
  38. this.add(this.panel);
  39. this.setVisible(true);
  40. this.timer = new Timer(15000, this);
  41. this.timer.start();
  42. this.addMouseMotionListener(this);
  43. this.addMouseListener(this);
  44. }
  45.  
  46. @Override
  47. public void actionPerformed(ActionEvent e) {
  48. // this.panel.setUnicornX(this.panel.getUnicornX()+1);
  49. this.repaint();
  50. }
  51.  
  52. @Override
  53. public void mouseDragged(MouseEvent e) {
  54. }
  55.  
  56. @Override
  57. public void mouseMoved(MouseEvent e) {
  58. }
  59.  
  60. @Override
  61. public void mouseClicked(MouseEvent e) {
  62. this.panel.doesCollide(e.getX(), e.getY());
  63.  
  64. }
  65.  
  66. @Override
  67. public void mousePressed(MouseEvent e) {
  68. }
  69.  
  70. @Override
  71. public void mouseReleased(MouseEvent e) {
  72. }
  73.  
  74. @Override
  75. public void mouseEntered(MouseEvent e) {
  76. }
  77.  
  78. @Override
  79. public void mouseExited(MouseEvent e) {
  80. }
  81.  
  82. private class MyPanel extends JPanel {
  83.  
  84. private Game gameInstance;
  85. private BufferedImage image;
  86. private int unicornX, unicornY;
  87. private Random random;
  88.  
  89. public int getUnicornX() {
  90. return unicornX;
  91. }
  92.  
  93. public void setUnicornX(int unicornX) {
  94. this.unicornX = unicornX;
  95. }
  96.  
  97. public int getUnicornY() {
  98. return unicornY;
  99. }
  100.  
  101. public void setUnicornY(int unicornY) {
  102. this.unicornY = unicornY;
  103. }
  104.  
  105. public MyPanel(Game game) {
  106. this.gameInstance = game;
  107. this.random = new Random();
  108. try {
  109. this.image = (BufferedImage) ImageIO.read(new File("C:\\Users\\didiamantis\\Downloads\\jerry.jpg"));
  110. } catch (IOException ex) {
  111. }
  112. }
  113. int score = 0;
  114.  
  115. @Override
  116. public void paint(Graphics g) {
  117. g.setColor(Color.WHITE);
  118. this.setUnicornX( this.random.nextInt(this.gameInstance.getWidth() - this.image.getWidth()));
  119. this.setUnicornY( this.random.nextInt(this.gameInstance.getHeight() - this.image.getHeight()));
  120. g.fillRect(0, 0, this.gameInstance.getWidth(), this.gameInstance.getHeight());
  121. g.drawImage(
  122. image,
  123. this.getUnicornX(),
  124. this.getUnicornY(),
  125. this);
  126. g.setColor(Color.BLACK);
  127. g.drawString("Score: " + this.score, 50, 50);
  128.  
  129. }
  130.  
  131. private void doesCollide(int x, int y) {
  132. Rectangle r = new Rectangle(x, y, 550, 550);
  133. Rectangle imageR = new Rectangle(this.getUnicornX(), this.getUnicornY(), this.image.getWidth(), this.image.getHeight());
  134. if (r.intersects(imageR)) {
  135. this.score += 10;
  136. this.gameInstance.timer.setDelay(this.gameInstance.timer.getDelay() - 250);
  137. } else {
  138. this.score -= 1;
  139. }
  140. this.gameInstance.repaint();
  141. }
  142. }
  143.  
  144. public static void main(String[] args) {
  145. Game g = new Game();
  146. }
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement