fork(2) download
  1. import java.awt.BorderLayout;
  2. import java.awt.Color;
  3. import java.awt.Dimension;
  4. import java.awt.EventQueue;
  5. import java.awt.Graphics;
  6. import java.awt.Graphics2D;
  7. import java.awt.Point;
  8. import java.awt.RenderingHints;
  9. import java.awt.event.MouseAdapter;
  10. import java.awt.event.MouseEvent;
  11. import java.util.*;
  12. import javax.swing.*;
  13.  
  14. class SimpleBalls {
  15.  
  16. private Point mousePoint;
  17.  
  18. public static void main(String[] args) {
  19. new SimpleBalls();
  20. }
  21.  
  22. public SimpleBalls() {
  23. EventQueue.invokeLater(new Runnable() {
  24. @Override
  25. public void run() {
  26. try {
  27. UIManager.setLookAndFeel(UIManager
  28. .getSystemLookAndFeelClassName());
  29. } catch (ClassNotFoundException ex) {
  30. } catch (InstantiationException ex) {
  31. } catch (IllegalAccessException ex) {
  32. }
  33.  
  34. JFrame frame = new JFrame("Spot");
  35. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  36. frame.setLayout(new BorderLayout());
  37. Balls balls = new Balls();
  38. frame.add(balls);
  39. frame.setSize(400, 400);
  40. frame.setVisible(true);
  41.  
  42. new Thread(new BounceEngine(balls)).start();
  43.  
  44. }
  45. });
  46. }
  47.  
  48. public static int random(int maxRange) {
  49. return (int) Math.round((Math.random() * maxRange));
  50. }
  51.  
  52. public class Balls extends JPanel {
  53.  
  54. private List<Ball> ballsUp;
  55.  
  56. public Balls() {
  57. ballsUp = new ArrayList<Ball>(25);
  58.  
  59. MouseAdapter handler = new MouseAdapter() {
  60.  
  61. @Override
  62. public void mouseMoved(MouseEvent e) {
  63. mousePoint = e.getPoint();
  64. // System.out.println(mousePoint);
  65. }
  66.  
  67. @Override
  68. public void mouseExited(MouseEvent e) {
  69. mousePoint = null;
  70. }
  71.  
  72. };
  73.  
  74. addMouseListener(handler);
  75. addMouseMotionListener(handler);
  76.  
  77. for (int index = 0; index < 10 + random(10); index++) {
  78. ballsUp.add(new Ball(new Color(random(255), random(255),
  79. random(255))));
  80. }
  81.  
  82. }
  83.  
  84. @Override
  85. protected void paintComponent(Graphics g) {
  86. super.paintComponent(g);
  87. Graphics2D g2d = (Graphics2D) g.create();
  88. g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
  89. RenderingHints.VALUE_ANTIALIAS_ON);
  90. for (Ball ball : ballsUp) {
  91. ball.paint(g2d);
  92. }
  93. g2d.dispose();
  94. }
  95.  
  96. public List<Ball> getBalls() {
  97. return ballsUp;
  98. }
  99. }
  100.  
  101. public class BounceEngine implements Runnable {
  102.  
  103. private Balls parent;
  104.  
  105. public BounceEngine(Balls parent) {
  106. this.parent = parent;
  107. }
  108.  
  109. @Override
  110. public void run() {
  111.  
  112. int width = getParent().getWidth();
  113. int height = getParent().getHeight();
  114.  
  115. for (Ball ball : getParent().getBalls()) {
  116. int x = random(width);
  117. int y = random(height);
  118.  
  119. Dimension size = ball.getSize();
  120.  
  121. if (x + size.width > width) {
  122. x = width - size.width;
  123. }
  124. if (y + size.height > height) {
  125. y = height - size.height;
  126. }
  127.  
  128. ball.setLocation(new Point(x, y));
  129.  
  130. }
  131.  
  132. while (getParent().isVisible()) {
  133.  
  134. SwingUtilities.invokeLater(new Runnable() {
  135. @Override
  136. public void run() {
  137. getParent().repaint();
  138. }
  139. });
  140.  
  141. for (Ball ball : getParent().getBalls()) {
  142. move(ball, mousePoint);
  143. }
  144.  
  145. // for (Ball ball : getParent().getBalls()) {
  146. // move(ball, mousePoint);
  147. // }
  148.  
  149. try {
  150. Thread.sleep(100);
  151. } catch (InterruptedException ex) {
  152. ex.printStackTrace();
  153. }
  154.  
  155. }
  156.  
  157. }
  158.  
  159. public Balls getParent() {
  160. return parent;
  161. }
  162.  
  163. public void move(Ball ball, Point mouse) {
  164.  
  165. try {
  166. Point p = ball.getLocation();
  167. Point speed = ball.getSpeed();
  168. Dimension size = ball.getSize();
  169.  
  170. int vx = speed.x;
  171. int vy = speed.y;
  172.  
  173. int x = p.x;
  174. int y = p.y;
  175.  
  176. // ----------------------
  177. if (mouse != null) {
  178.  
  179. int xDistance = Math.abs(x + size.width - mouse.x);
  180. int yDistance = Math.abs(y + size.height - mouse.y);
  181.  
  182. if (xDistance < yDistance) {
  183. if (x + size.width < mouse.x) {
  184. if (vx > 0) {
  185. vx *= -1;
  186. }
  187. } else {
  188. if (vx > 0) {
  189. vx *= -1;
  190. }
  191. }
  192. } else {
  193. if (y + size.height < mouse.y) {
  194. if (vy > 0) {
  195. vy *= -1;
  196. }
  197. } else {
  198. if (vy > 0) {
  199. vy *= -1;
  200. }
  201. }
  202. }
  203.  
  204. }
  205. // ----------------------
  206.  
  207. if (x + vx < 0 || x + size.width + vx > getParent().getWidth()) {
  208. vx *= -1;
  209. }
  210. if (y + vy < 0
  211. || y + size.height + vy > getParent().getHeight()) {
  212. vy *= -1;
  213. }
  214. x += vx;
  215. y += vy;
  216.  
  217. ball.setSpeed(new Point(vx, vy));
  218. ball.setLocation(new Point(x, y));
  219.  
  220. } catch (Exception e) {
  221. e.printStackTrace();
  222. }
  223.  
  224. }
  225.  
  226. }
  227.  
  228. public class Ball {
  229.  
  230. private Color color;
  231. private Point location;
  232. private Dimension size;
  233. private Point speed;
  234. private int dimeter;
  235.  
  236. public Ball(Color color) {
  237. Random rnd = new Random();
  238. dimeter = 10 + rnd.nextInt(50);
  239.  
  240. setColor(color);
  241.  
  242. speed = new Point(10 - random(20), 10 - random(20));
  243. size = new Dimension(dimeter, dimeter);
  244.  
  245. }
  246.  
  247. public Dimension getSize() {
  248. return size;
  249. }
  250.  
  251. public void setColor(Color color) {
  252. this.color = color;
  253. }
  254.  
  255. public void setLocation(Point location) {
  256. this.location = location;
  257. }
  258.  
  259. public Color getColor() {
  260. return color;
  261. }
  262.  
  263. public Point getLocation() {
  264. return location;
  265. }
  266.  
  267. public Point getSpeed() {
  268. return speed;
  269. }
  270.  
  271. public void setSpeed(Point speed) {
  272. this.speed = speed;
  273. }
  274.  
  275. protected void paint(Graphics2D g2d) {
  276.  
  277. Point p = getLocation();
  278. if (p != null) {
  279. g2d.setColor(getColor());
  280. Dimension size = getSize();
  281. g2d.fillOval(p.x, p.y, size.width, size.height);
  282. }
  283.  
  284. }
  285. }
  286. }
Success #stdin #stdout #stderr 0.14s 382208KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Exception in thread "AWT-EventQueue-0" java.awt.HeadlessException: 
No X11 DISPLAY variable was set, but this program performed an operation which requires it.
	at java.awt.GraphicsEnvironment.checkHeadless(GraphicsEnvironment.java:207)
	at java.awt.Window.<init>(Window.java:535)
	at java.awt.Frame.<init>(Frame.java:420)
	at javax.swing.JFrame.<init>(JFrame.java:224)
	at SimpleBalls$1.run(Main.java:35)
	at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:733)
	at java.awt.EventQueue.access$200(EventQueue.java:103)
	at java.awt.EventQueue$3.run(EventQueue.java:694)
	at java.awt.EventQueue$3.run(EventQueue.java:692)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
	at java.awt.EventQueue.dispatchEvent(EventQueue.java:703)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
	at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)