fork(1) download
  1. import java.awt.*;
  2. import java.applet.*;
  3. import java.awt.event.*;
  4. import java.util.Vector;
  5. import java.awt.Rectangle;
  6.  
  7. {
  8. //runtime variables
  9. boolean running = false;
  10. boolean kill = false;
  11.  
  12. //buttons
  13. Button runbutton = new Button("Run");
  14. Button pausebutton = new Button("Pause");
  15. Button quitbutton = new Button("Quit");
  16.  
  17. //text
  18. Label speedlabel = new Label("Speed");
  19. Label sizelabel = new Label("Size");
  20.  
  21. Panel drawingpanel;
  22.  
  23. //scrollbars
  24. private final int barHeight = 20, SLIDER_WIDTH = 10, MAXSPEED = 110, MINSPEED = 0, MAX_SIZE = 110, MIN_SIZE = 10;
  25. Scrollbar speedbar = new Scrollbar(Scrollbar.HORIZONTAL, MAXSPEED/2, SLIDER_WIDTH, MINSPEED, MAXSPEED);
  26. Scrollbar sizebar = new Scrollbar(Scrollbar.HORIZONTAL, MAX_SIZE/2, SLIDER_WIDTH, MIN_SIZE, MAX_SIZE);
  27.  
  28. //drawn objs
  29. Ball ball;
  30.  
  31. private Thread ballThread;
  32.  
  33. //boundaries
  34. static int boundx = 640;
  35. static int boundy = 400;
  36.  
  37. //speed
  38. int speed = MAXSPEED/2;
  39. int delay = MAXSPEED - speed;
  40.  
  41. //initialize the applet and draw everything
  42. public void init()
  43. {
  44. double colWeight[] = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};//15 cols
  45. double rowWeight[] = {1,1,1,1,1,1,1,1,1,1}; //10 rows
  46. int colWidth[] = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};//15 cols
  47. int rowHeight[] = {1,1,1,1,1,1,1,1,1,1}; //10 rows
  48. gbl.rowHeights = rowHeight;
  49. gbl.rowWeights = rowWeight;
  50. gbl.columnWeights = colWeight;
  51. gbl.columnWidths = colWidth;
  52. c.anchor = GridBagConstraints.CENTER;
  53.  
  54. setBounds(0,0,480,640);
  55. setLayout(new BorderLayout());
  56. Panel controlpanel = new Panel();
  57. controlpanel.setLayout(gbl);
  58. controlpanel.setSize(640,80);
  59.  
  60. drawingpanel = new Panel();
  61. drawingpanel.setLayout(new BorderLayout(0,0));
  62. drawingpanel.setSize(640,400);
  63. ball = new Ball();
  64. drawingpanel.add("Center",ball);
  65. drawingpanel.setVisible(true);
  66.  
  67. //speed scrollbar
  68. c.weightx = 1;
  69. c.weighty = 1;
  70. c.gridwidth = 3;
  71. c.gridheight = 1;
  72. c.gridx = 1;
  73. c.gridy = 7;
  74. c.fill= GridBagConstraints.HORIZONTAL;
  75. gbl.setConstraints(this.speedbar,c);
  76.  
  77. //run button
  78. c.weightx = 1;
  79. c.weighty = 1;
  80. c.gridwidth = 2;
  81. c.gridheight = 1;
  82. c.gridx = 5;
  83. c.gridy = 7;
  84. c.fill= GridBagConstraints.HORIZONTAL;
  85. gbl.setConstraints(this.runbutton,c);
  86.  
  87. //pause button
  88. c.weightx = 1;
  89. c.weighty = 1;
  90. c.gridwidth = 2;
  91. c.gridheight = 1;
  92. c.gridx = 8;
  93. c.gridy = 7;
  94. c.fill= GridBagConstraints.HORIZONTAL;
  95. gbl.setConstraints(this.pausebutton,c);
  96.  
  97. //size scrollbar
  98. c.weightx = 1;
  99. c.weighty = 1;
  100. c.gridwidth = 3;
  101. c.gridheight = 1;
  102. c.gridx = 11;
  103. c.gridy = 7;
  104. c.fill= GridBagConstraints.HORIZONTAL;
  105. gbl.setConstraints(this.sizebar,c);
  106.  
  107. //speed text label
  108. c.weightx = 1;
  109. c.weighty = 1;
  110. c.gridwidth = 3;
  111. c.gridheight = 1;
  112. c.gridx = 1;
  113. c.gridy = 8;
  114. c.fill= GridBagConstraints.HORIZONTAL;
  115. gbl.setConstraints(this.speedlabel,c);
  116.  
  117. //size text label
  118. c.weightx = 1;
  119. c.weighty = 1;
  120. c.gridwidth = 3;
  121. c.gridheight = 1;
  122. c.gridx = 11;
  123. c.gridy = 8;
  124. c.fill= GridBagConstraints.HORIZONTAL;
  125. gbl.setConstraints(this.sizelabel,c);
  126.  
  127. //quit button
  128. c.weightx = 1;
  129. c.weighty = 1;
  130. c.gridwidth = 3;
  131. c.gridheight = 1;
  132. c.gridx = 6;
  133. c.gridy = 9;
  134. c.fill= GridBagConstraints.HORIZONTAL;
  135. gbl.setConstraints(this.quitbutton,c);
  136.  
  137. //add to the screen
  138. controlpanel.add(this.speedbar);
  139. controlpanel.add(this.runbutton);
  140. controlpanel.add(this.pausebutton);
  141. controlpanel.add(this.sizebar);
  142. controlpanel.add(this.speedlabel);
  143. controlpanel.add(this.sizelabel);
  144. controlpanel.add(this.quitbutton);
  145.  
  146. //add listners
  147. speedbar.addAdjustmentListener(this);
  148. runbutton.addActionListener(this);
  149. pausebutton.addActionListener(this);
  150. sizebar.addAdjustmentListener(this);
  151. quitbutton.addActionListener(this);
  152. ball.addMouseListener(this);
  153. ball.addMouseMotionListener(this);
  154.  
  155. //add the panels
  156. add("South", controlpanel);
  157. add("Center", drawingpanel);
  158.  
  159.  
  160. //drawing paramaters
  161. //loc = new Point(loc.x+dx, loc.y+dy);
  162. }
  163.  
  164. public void start()
  165. {
  166. if (ballThread == null)
  167. {
  168. ballThread = new Thread(this);
  169. ballThread.start();
  170. }
  171. }
  172.  
  173. public void run()
  174. {
  175. Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
  176. while (!kill)
  177. {
  178. if (running)
  179. {
  180. ball.repaint();
  181. }
  182. try
  183. {
  184. Thread.sleep(delay);
  185. }
  186. catch(InterruptedException e){System.err.println("Interrupted.");}
  187. }
  188. stop();
  189. }
  190.  
  191. public void paint(Graphics g)
  192. {
  193. super.paint(g);
  194. }
  195.  
  196. public void mouseClicked(MouseEvent m)
  197. {
  198. if (!ball.flag)
  199. {
  200. Point p;
  201. p = new Point(m.getPoint());
  202. Vector<Rectangle> v;
  203. v = (Vector<Rectangle>)ball.r;
  204. boolean done = false;
  205. int i = 0;
  206.  
  207. while (!done)
  208. {
  209. if(v.elementAt(i).contains(p))
  210. {
  211. v.removeElement(i);
  212. System.err.print("Element removed");
  213. }
  214. i++;
  215. if(i>=v.size())
  216. done=true;
  217. }
  218. ball.r = v;
  219. }
  220. }
  221.  
  222. public void mousePressed(MouseEvent m)
  223. {
  224. ball.flag = true;
  225. ball.start = m.getPoint();
  226. ball.end = ball.start;
  227. ball.cur = ball.start;
  228. }
  229.  
  230. public void mouseDragged(MouseEvent m)
  231. {
  232. if (ball.flag == true)
  233. {
  234. ball.cur = m.getPoint();
  235. }
  236. }
  237.  
  238. public void mouseReleased(MouseEvent m)
  239. {
  240. //add rect method in ball
  241. ball.end = ball.cur;
  242. if (ball.flag)
  243. {
  244. ball.addrect();
  245. }
  246. ball.flag = false;
  247. }
  248.  
  249. public void mouseEntered(MouseEvent m){}
  250. public void mouseExited(MouseEvent m){}
  251. public void mouseMoved(MouseEvent m){}
  252.  
  253. public void actionPerformed(ActionEvent e)
  254. {
  255. Object source = e.getSource();
  256. if (source == this.runbutton)
  257. {
  258. running = true;
  259. }
  260. else if (source == this.pausebutton)
  261. {
  262. running = false;
  263. }
  264. else if (source == this.quitbutton)
  265. {
  266. //kill processes
  267. kill = true;
  268. //remove listeners
  269. stop();
  270. }
  271. }
  272.  
  273. public void adjustmentValueChanged(AdjustmentEvent e)
  274. {
  275. Object source = e.getSource();
  276. //set the new size.
  277. if (source == sizebar)
  278. {
  279. //check for clipping
  280. int newsize = sizebar.getValue();
  281. newsize = ball.resize(newsize);
  282. sizebar.setValue(newsize);
  283. }
  284. if (source == speedbar)
  285. {
  286. speed = speedbar.getValue();
  287. delay = MAXSPEED - speed;
  288. }
  289. }
  290.  
  291. public void stop()
  292. {
  293. this.speedbar.removeAdjustmentListener(this);
  294. this.runbutton.removeActionListener(this);
  295. this.pausebutton.removeActionListener(this);
  296. this.sizebar.removeAdjustmentListener(this);
  297. this.quitbutton.removeActionListener(this);
  298. this.removeMouseListener(this);
  299. this.removeMouseMotionListener(this);
  300. Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
  301. }
  302. }
  303.  
  304. //class to handle animations
  305. class Ball extends Canvas
  306. {
  307. Rectangle temp;
  308. static int size = 50;
  309. private Graphics obj;
  310. Image offscreen = null;
  311. static Point loc = new Point(100,100); //location of the ball
  312.  
  313. //boundaries
  314. static int boundx = 640;
  315. static int boundy = 400;
  316.  
  317. //directions
  318. int dx = 1; //1 = left, -1 = right
  319. int dy = 1; //1 = up, -1 = down
  320.  
  321. static boolean flag = false;
  322.  
  323. //rectangle vector and associated things
  324. Vector<Rectangle> r = new Vector<Rectangle>();
  325. Point start, cur, end;
  326.  
  327. public int resize(int newsize)
  328. {
  329. // x
  330. if (loc.x+newsize >= boundx)
  331. {
  332. newsize = boundx - loc.x - 1;
  333. return newsize;
  334. }
  335.  
  336. // y
  337. if (loc.y+newsize >= boundy + 100)
  338. {
  339. newsize = boundy+100 - loc.y - 1;
  340. return newsize;
  341. }
  342. size = newsize;
  343. return newsize;
  344. }
  345.  
  346. public void move()
  347. {
  348. //if it will hit the right or left boundary, flip the x direction and set it
  349. if (loc.x+size >= boundx || loc.x <= 0)
  350. { dx *= -1; }
  351. //if it will hit the top or bottom boundray, flip the y direction and set it
  352. if (loc.y+size >= boundy || loc.y <= 0)
  353. { dy *= -1; }
  354.  
  355.  
  356. for (int i = 0; i < r.size(); i++)
  357. {
  358.  
  359. temp = new Rectangle(r.elementAt(i));
  360. int rx = temp.x;
  361. int ry = temp.y;
  362. int rh = temp.height;
  363. int rw = temp.width;
  364. //If the ball hits either side of the rectangle, change the x direction
  365. if((loc.x > rx && loc.x > ry && loc.x < (ry + rh))||(loc.x < (rx + rw) && loc.x > rx && loc.x <(ry + rh)))
  366. {dx *= -1;}
  367. //If the ball hits either the top or bottom, change the y direction
  368. if((loc.y > ry && loc.y > rx && loc.y < (rx + rw))||(loc.y < (ry + rh) && loc.y > ry && loc.y <(rx + rw)))
  369. {dy *= -1;}
  370. }
  371. //Increment or decrement the location of the ball based on the X and Y directions.
  372. loc.x += dx;
  373. loc.y += dy;
  374. }
  375.  
  376. public void addrect()
  377. {
  378. //What was wrong here is that for the final parameter, I had Math.abs(end.y-start.x).
  379. if (end.x > start.x && end.y > start.y)
  380. r.add(new Rectangle(start.x,start.y, Math.abs(end.x-start.x),Math.abs(end.y-start.y)));
  381. if (end.x > start.x && end.y < start.y)
  382. r.add(new Rectangle(start.x,end.y, Math.abs(end.x-start.x),Math.abs(end.y-start.y)));
  383. if (end.x < start.x && end.y > start.y)
  384. r.add(new Rectangle(end.x, start.y, Math.abs(end.x-start.x),Math.abs(end.y-start.y)));
  385. if (end.x < start.x && end.y < start.y)
  386. r.add(new Rectangle(end.x, end.y, Math.abs(end.x-start.x),Math.abs(end.y-start.y)));
  387. }
  388.  
  389. public void update(Graphics g)
  390. {
  391. //super.update(g); //Getting rid of this fixed double buffering
  392. // I looked at P-dawg's code and he didn't have it.
  393. Graphics buffer;
  394. if (offscreen == null)
  395. {
  396. offscreen = createImage(getWidth(), getHeight());
  397. }
  398. buffer = offscreen.getGraphics();
  399. buffer.setColor(getBackground());
  400. buffer.fillRect(0,0,640, 410);
  401. mypaint(buffer);
  402. g.drawImage(offscreen, 0, 0, this);
  403. }
  404.  
  405. public void mypaint(Graphics g)
  406. {
  407. //update loc
  408. move();
  409.  
  410. //draw
  411. g.setColor(Color.RED);
  412. for (int i = 0; i < r.size(); i++)
  413. {
  414. t = new Rectangle(r.elementAt(i));
  415. g.fillRect(t.x,t.y,t.width,t.height);
  416. }
  417. g.setColor(Color.BLACK);
  418. g.drawOval(loc.x, loc.y, size, size);
  419. g.fillOval(loc.x, loc.y, size, size);
  420.  
  421. if (flag)
  422. {
  423. if (!r.isEmpty())
  424. {
  425. for (int i = 0; i < r.size(); i++)
  426. {
  427. if (cur.x > start.x && cur.y > start.y)
  428. {
  429. g.drawRect(start.x, start.y, Math.abs(cur.x - start.x), Math.abs(cur.y - start.y));
  430. g.setColor(Color.gray);
  431. g.fillRect(start.x, start.y, Math.abs(cur.x - start.x), Math.abs(cur.y - start.y));
  432. }
  433. if (cur.x > start.x && cur.y < start.y)
  434. {
  435. g.drawRect(start.x, cur.y, Math.abs(cur.x - start.x), Math.abs(cur.y - start.y));
  436. g.setColor(Color.gray);
  437. g.fillRect(start.x, cur.y, Math.abs(cur.x - start.x), Math.abs(cur.y - start.y));
  438. }
  439. if (cur.x < start.x && cur.y > start.y)
  440. {
  441. g.drawRect(cur.x, start.y, Math.abs(cur.x - start.x), Math.abs(cur.y - start.y));
  442. g.setColor(Color.gray);
  443. g.fillRect(cur.x, start.y, Math.abs(cur.x - start.x), Math.abs(cur.y - start.y));
  444. }
  445. if (cur.x < start.x && cur.y < start.y)
  446. {
  447. g.drawRect(cur.x, cur.y, Math.abs(cur.x - start.x), Math.abs(cur.y - start.y));
  448. g.setColor(Color.gray);
  449. g.fillRect(cur.x, cur.y, Math.abs(cur.x - start.x), Math.abs(cur.y - start.y));
  450. }
  451. }
  452. }
  453. }
  454. }
  455. }
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty