Advertisement
mattmb

Circular hitbox jakelee

Mar 28th, 2017
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.64 KB | None | 0 0
  1. //-----------------
  2. //
  3. // Circular Hitbox
  4. //
  5. //-----------------
  6.  
  7. import java.applet.*;
  8. import java.awt.*;
  9. import java.awt.event.*;
  10. import java.util.*;
  11.  
  12. public class CircularHitbox extends Applet implements Runnable, KeyListener, MouseListener, MouseWheelListener
  13. {
  14. Random gen = new Random();
  15. int a = gen.nextInt(256), b = gen.nextInt(256), d = gen.nextInt(256);
  16. int rad = 50;
  17. int xDim = 500, yDim = 500, time = 20;
  18. Color color = new Color(a, b, d);
  19. Circle c = new Circle(250, 250, rad);
  20. Color antiColor = new Color (256 - a, 256 - b, 256 - d);
  21.  
  22. Font font = new Font("Arial", Font.BOLD, 20);
  23.  
  24. int mouseX = 0, mouseY = 0;
  25.  
  26. int distance = 5000;
  27.  
  28. int xDiff = 1000;
  29. int yDiff = 1000;
  30.  
  31. Image i;
  32. Graphics gg;
  33.  
  34. public CircularHitbox()
  35. {
  36.  
  37. setFocusable(true);
  38. setFocusTraversalKeysEnabled(false);
  39. }
  40.  
  41. public void init()
  42. {
  43. this.setSize(xDim, yDim);
  44. setBackground(Color.black);
  45. }
  46.  
  47. public void start()
  48. {
  49. Thread time = new Thread(this);
  50. time.start();
  51.  
  52. addKeyListener (this);
  53. addMouseListener (this);
  54. addMouseWheelListener (this);
  55. }
  56.  
  57. public void stop(){}
  58.  
  59. public void keyPressed(KeyEvent e)
  60. {
  61. if (e.getKeyCode() == KeyEvent.VK_RIGHT)
  62. {
  63.  
  64. }
  65. }
  66.  
  67. public void mouseWheelMoved(MouseWheelEvent e)
  68. {
  69. int notches = e.getWheelRotation();
  70. if (notches < 0)
  71. rad += 5;
  72. else
  73. rad -= 5;
  74. }
  75.  
  76. public void keyTyped(KeyEvent e)
  77. {
  78.  
  79. }
  80.  
  81. public void keyReleased(KeyEvent e)
  82. {
  83.  
  84. }
  85.  
  86. public void mouseEntered(MouseEvent e)
  87. {
  88.  
  89. }
  90.  
  91. public void mouseExited(MouseEvent e)
  92. {
  93.  
  94. }
  95.  
  96. public void mouseClicked(MouseEvent e)
  97. {
  98.  
  99. }
  100.  
  101. public void mouseReleased(MouseEvent e)
  102. {
  103.  
  104. }
  105.  
  106. public void mousePressed(MouseEvent e)
  107. {
  108. xDiff = e.getX() - c.x();
  109. yDiff = e.getY() - c.y();
  110.  
  111. distance = (int) Math.sqrt((Math.pow(xDiff, 2) + Math.pow(yDiff, 2)));
  112.  
  113. if (distance < c.radius)
  114. {
  115. a = gen.nextInt(256);
  116. b = gen.nextInt(256);
  117. d = gen.nextInt(256);
  118. color = new Color(a, b, d);
  119. antiColor = new Color( 256 - a, 256 - b, 256 - d);
  120. if (e.getButton() == e.BUTTON1)
  121. {
  122. rad += 5;
  123. }
  124. if (e.getButton() == e.BUTTON3)
  125. {
  126. if (rad > 6)
  127. {
  128. rad -= 5;
  129. }
  130. }
  131. }
  132. }
  133.  
  134. public void paint(Graphics g)
  135. {
  136. g.setColor(color);
  137. g.fillOval(c.x() - c.radius(), c.y() - c.radius(), c.diameter(), c.diameter());
  138. g.setFont(font);
  139. g.setColor(antiColor);
  140. g.drawString("Click Me!", xDim / 2 - 43, yDim / 2);
  141. }
  142.  
  143. public void update(Graphics g){
  144. if(i == null){
  145. i = createImage(getSize().width, getSize().height);
  146. gg = i.getGraphics();}
  147. gg.setColor(getBackground());
  148. gg.fillRect(0,0, this.getSize().width, this.getSize().height);
  149. gg.setColor(getForeground());
  150. paint(gg);
  151.  
  152. g.drawImage(i, 0, 0, this);
  153.  
  154. }
  155.  
  156. public void run()
  157. {
  158. Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
  159. while(true)
  160. {
  161. c.setRadius(rad);
  162.  
  163. try
  164. {
  165. Thread.sleep(time);
  166. }
  167. catch(InterruptedException Ex){}
  168. repaint();
  169. }
  170. }
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement