fork(1) download
  1. import java.awt.*;
  2. import java.applet.*;
  3. import java.awt.event.*;
  4.  
  5. class Dot
  6. {
  7. int left, top;
  8. Color color = Color.RED;
  9.  
  10. Dot(int l, int t, Color c)
  11. {
  12. left = l;
  13. top = t;
  14. color = c;
  15. }
  16.  
  17. public void setWidth(int w) {}
  18. public int widthOf()
  19. {
  20. return 0;
  21. }
  22.  
  23. public void setHeight(int h) {}
  24. public int heightOf()
  25. {
  26. return 0;
  27. }
  28.  
  29. public void setLeft(int l)
  30. {
  31. left = l;
  32. }
  33. public int leftOf()
  34. {
  35. return left;
  36. }
  37.  
  38. public void setTop(int t)
  39. {
  40. top = t;
  41. }
  42. public int topOf()
  43. {
  44. return top;
  45. }
  46.  
  47. public void setColor(Color c)
  48. {
  49. color = c;
  50. }
  51. public Color colorOf()
  52. {
  53. return color;
  54. }
  55.  
  56. public void draw (Graphics g) {}
  57.  
  58. public boolean find (int x, int y)
  59. {
  60. return false;
  61. }
  62. }
  63.  
  64. class RectDot extends Dot
  65. {
  66. int width, height;
  67.  
  68. RectDot (int l, int t, int w, int h, Color c)
  69. {
  70. super(l,t,c);
  71. width = w;
  72. height = h;
  73. }
  74.  
  75. public void setWidth(int w)
  76. {
  77. width = w;
  78. }
  79. public int widthOf()
  80. {
  81. return width;
  82. }
  83.  
  84. public void setHeight(int h)
  85. {
  86. height = h;
  87. }
  88. public int heightOf()
  89. {
  90. return height;
  91. }
  92.  
  93. public boolean find(int x, int y)
  94. {
  95. if (super.leftOf() < x && x < super.leftOf()+width && super.topOf() < y && y < super.topOf()+height)
  96. return true;
  97. return false;
  98. }
  99.  
  100. public void draw(Graphics g)
  101. {
  102. g.setColor(colorOf());
  103. g.fillRect(leftOf(), topOf(), width, height);
  104. g.setColor(Color.black);
  105. }
  106. }
  107.  
  108. class CircDot extends Dot
  109. {
  110. int diam;
  111.  
  112. CircDot (int l, int t, int d, Color c)
  113. {
  114. super(l,t,c);
  115. diam = d;
  116. }
  117.  
  118. public void setWidth(int w)
  119. {
  120. diam = w;
  121. }
  122. public int widthOf()
  123. {
  124. return diam;
  125. }
  126.  
  127. public void setHeight(int h)
  128. {
  129. diam = h;
  130. }
  131. public int heightOf()
  132. {
  133. return diam;
  134. }
  135.  
  136. public void setDiam(int d)
  137. {
  138. diam = d;
  139. }
  140. public int diameterOf()
  141. {
  142. return diam;
  143. }
  144.  
  145. public boolean find (int x, int y)
  146. {
  147. if (super.leftOf() < x && x < super.leftOf()+diam && super.topOf() < y && y < super.topOf()+diam)
  148. return true;
  149. return false;
  150. }
  151.  
  152. public void draw(Graphics g)
  153. {
  154. g.setColor(colorOf());
  155. g.fillOval(leftOf(), topOf(), diam, diam);
  156. g.setColor(new Color(0,0,0));
  157. }
  158. }
  159.  
  160. public class Bounce extends Applet implements ActionListener, AdjustmentListener
  161. {
  162. //runtime variables
  163. boolean running = false;
  164. boolean currentlyCircle = true;
  165. boolean showtails = false;
  166.  
  167. //buttons
  168. Button runbutton = new Button("Run");
  169. Button tailbutton = new Button("Tail");
  170. Button shapebutton = new Button("Square");
  171. Button clearbutton = new Button("Clear");
  172. Button quitbutton = new Button("Quit");
  173.  
  174. //text
  175. Label speedlabel = new Label("Speed");
  176. Label sizelabel = new Label("Size");
  177.  
  178. //panels
  179. Panel drawingpanel = new Panel();
  180.  
  181. //scrollbars
  182. private final int barHeight = 20;
  183. private final int SLIDER_WIDTH = 10;
  184. private final int MAXSPEED = 110;
  185. private final int MINSPEED = 0;
  186. private final int MAX_SIZE = 110;
  187. private final int MIN_SIZE = 10;
  188. /* These are the default values, and do not need to be implemented.
  189. * If I wanted to use something other then default, these constants
  190. * would be used.
  191. private final int UNIT_INC = 1;
  192. private final int BLOC_INC = 10;
  193. */
  194. Scrollbar speedbar = new Scrollbar(Scrollbar.HORIZONTAL, MAXSPEED/2, SLIDER_WIDTH, MINSPEED, MAXSPEED);
  195. Scrollbar sizebar = new Scrollbar(Scrollbar.HORIZONTAL, MAX_SIZE/2, SLIDER_WIDTH, MIN_SIZE, MAX_SIZE);
  196.  
  197. //drawn objs
  198. Dot dot;
  199. private Graphics page;
  200. Point currentlocation;
  201. Point previouslocation;
  202.  
  203. //initialize the applet and draw everything
  204. public void init()
  205. {
  206. double colWeight[] = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};//15 cols
  207. double rowWeight[] = {1,1,1,1,1,1,1,1,1,1}; //10 rows
  208. int colWidth[] = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};//15 cols
  209. int rowHeight[] = {1,1,1,1,1,1,1,1,1,1}; //10 rows
  210. gbl.rowHeights = rowHeight;
  211. gbl.rowWeights = rowWeight;
  212. gbl.columnWeights = colWeight;
  213. gbl.columnWidths = colWidth;
  214. c.anchor = GridBagConstraints.CENTER;
  215.  
  216. //setBounds(0,0,480,640);
  217. setLayout(new BorderLayout());
  218. Panel p = new Panel();
  219. p.setLayout(gbl);
  220.  
  221. //speed scrollbar
  222. c.weightx = 1;
  223. c.weighty = 1;
  224. c.gridwidth = 3;
  225. c.gridheight = 1;
  226. c.gridx = 1;
  227. c.gridy = 7;
  228. c.fill= GridBagConstraints.HORIZONTAL;
  229. gbl.setConstraints(this.speedbar,c);
  230.  
  231. //run button
  232. c.weightx = 1;
  233. c.weighty = 1;
  234. c.gridwidth = 2;
  235. c.gridheight = 1;
  236. c.gridx = 5;
  237. c.gridy = 7;
  238. c.fill= GridBagConstraints.HORIZONTAL;
  239. gbl.setConstraints(this.runbutton,c);
  240.  
  241. //tail button
  242. c.weightx = 1;
  243. c.weighty = 1;
  244. c.gridwidth = 2;
  245. c.gridheight = 1;
  246. c.gridx = 8;
  247. c.gridy = 7;
  248. c.fill= GridBagConstraints.HORIZONTAL;
  249. gbl.setConstraints(this.tailbutton,c);
  250.  
  251. //size scrollbar
  252. c.weightx = 1;
  253. c.weighty = 1;
  254. c.gridwidth = 3;
  255. c.gridheight = 1;
  256. c.gridx = 11;
  257. c.gridy = 7;
  258. c.fill= GridBagConstraints.HORIZONTAL;
  259. gbl.setConstraints(this.sizebar,c);
  260.  
  261. //speed text label
  262. c.weightx = 1;
  263. c.weighty = 1;
  264. c.gridwidth = 3;
  265. c.gridheight = 1;
  266. c.gridx = 1;
  267. c.gridy = 8;
  268. c.fill= GridBagConstraints.HORIZONTAL;
  269. gbl.setConstraints(this.speedlabel,c);
  270.  
  271. //shape button
  272. c.weightx = 1;
  273. c.weighty = 1;
  274. c.gridwidth = 2;
  275. c.gridheight = 1;
  276. c.gridx = 5;
  277. c.gridy = 8;
  278. c.fill= GridBagConstraints.HORIZONTAL;
  279. gbl.setConstraints(this.shapebutton,c);
  280.  
  281. //clear button
  282. c.weightx = 1;
  283. c.weighty = 1;
  284. c.gridwidth = 2;
  285. c.gridheight = 1;
  286. c.gridx = 8;
  287. c.gridy = 8;
  288. c.fill= GridBagConstraints.HORIZONTAL;
  289. gbl.setConstraints(this.clearbutton,c);
  290.  
  291. //size text label
  292. c.weightx = 1;
  293. c.weighty = 1;
  294. c.gridwidth = 3;
  295. c.gridheight = 1;
  296. c.gridx = 11;
  297. c.gridy = 8;
  298. c.fill= GridBagConstraints.HORIZONTAL;
  299. gbl.setConstraints(this.sizelabel,c);
  300.  
  301. //quit button
  302. c.weightx = 1;
  303. c.weighty = 1;
  304. c.gridwidth = 3;
  305. c.gridheight = 1;
  306. c.gridx = 6;
  307. c.gridy = 9;
  308. c.fill= GridBagConstraints.HORIZONTAL;
  309. gbl.setConstraints(this.quitbutton,c);
  310.  
  311. //add to the screen
  312. p.add(this.speedbar);
  313. p.add(this.runbutton);
  314. p.add(this.tailbutton);
  315. p.add(this.sizebar);
  316. p.add(this.speedlabel);
  317. p.add(this.shapebutton);
  318. p.add(this.clearbutton);
  319. p.add(this.sizelabel);
  320. p.add(this.quitbutton);
  321.  
  322. //add listners
  323. this.speedbar.addAdjustmentListener(this);
  324. this.runbutton.addActionListener(this);
  325. this.tailbutton.addActionListener(this);
  326. this.sizebar.addAdjustmentListener(this);
  327. this.shapebutton.addActionListener(this);
  328. this.clearbutton.addActionListener(this);
  329. this.quitbutton.addActionListener(this);
  330.  
  331. //drawing paramaters
  332. page = getGraphics();
  333. //boundaries
  334. int bound_x = Integer.parseInt(getParameter("WIDTH"));
  335. int bound_y = Integer.parseInt(getParameter("HEIGHT"));
  336. //directions
  337. int dx = 5;
  338. int dy = 5;
  339.  
  340. //add the panels
  341. add("South", p);
  342. add("Center", drawingpanel);
  343. }
  344.  
  345. public void run()
  346. {
  347.  
  348. //while (running)
  349. //{
  350. if (!showtails)
  351. {
  352. dot.setColor(drawingpanel.getBackground());
  353. }
  354. update();//check if speed, size, or shape changed
  355. draw();//draw next dot based on move(), or initial condition
  356. pause();
  357. move();//calculate and prepare the coordinates for the next iteration to be drawn
  358. //}
  359. }
  360.  
  361. public void update()
  362. {
  363. previouslocation = currentlocation;
  364. currentlocation = new Point(dot.leftOf(), dot.topOf());
  365. }
  366.  
  367. public void draw()
  368. {
  369.  
  370. }
  371.  
  372. public void paint(Graphics page)
  373. {
  374. if(currentlyCircle)
  375. {
  376. System.err.println("here");
  377. page.drawOval(dot.leftOf(), dot.topOf(), dot.widthOf(), dot.heightOf());
  378. page.fillOval(dot.leftOf(), dot.topOf(), dot.widthOf(), dot.heightOf());
  379. }
  380. else
  381. {
  382. page.drawRect(dot.leftOf(), dot.topOf(), dot.widthOf(), dot.heightOf());
  383. page.fillRect(dot.leftOf(), dot.topOf(), dot.widthOf(), dot.heightOf());
  384. }
  385. }
  386.  
  387. public void pause()
  388. {
  389. //for (long i = 100000000; i <= i; i++){}
  390. }
  391.  
  392. public void move()
  393. {
  394.  
  395. }
  396.  
  397. public void actionPerformed(ActionEvent e)
  398. {
  399. Object source = e.getSource();
  400.  
  401. if (source == this.runbutton)
  402. {
  403. if (!running) //prevents drawing a new dot when there is already one on the screen
  404. {
  405. running = true;
  406. dot = new CircDot(100, 100, sizebar.getValue(), Color.RED);
  407. currentlyCircle = true;
  408. }
  409. run();
  410. }
  411. else if (source == this.shapebutton)
  412. {
  413. if(running)//no need to evaluate if the run button hasn't been pressed (ie no dot to switch)
  414. {
  415. if (currentlyCircle) //if circle, draw it as a rectangle
  416. {
  417. dot = new RectDot(dot.leftOf(), dot.topOf(), dot.widthOf(), dot.heightOf(), dot.colorOf());
  418. this.shapebutton.setLabel("Square");
  419. currentlyCircle = false;
  420. }
  421. else //if rectangle, draw it as a circle
  422. {
  423. dot = new CircDot(dot.leftOf(), dot.topOf(), 2*dot.widthOf(), dot.colorOf());
  424. this.shapebutton.setLabel("Circle");
  425. currentlyCircle = true;
  426. }
  427. }
  428. }
  429. else if (source == this.clearbutton)
  430. {
  431. /* Erase the dot by setting its color to the background.
  432. * Running is also set to false. This means the run button
  433. * can be pressed again and another dot will be drawn.
  434. */
  435. running = false;
  436. dot.setColor(getBackground());
  437. }
  438. else if (source == this.tailbutton)
  439. {
  440. //toggle showing tails. This is used in run
  441. showtails = true;
  442. }
  443. else if (source == quitbutton)
  444. {
  445. //remove listeners
  446. stop();
  447. }
  448.  
  449. update(drawingpanel.getGraphics());
  450. }
  451.  
  452. public void adjustmentValueChanged(AdjustmentEvent e)
  453. {
  454. Object source = e.getSource();
  455.  
  456. //set the new size. If the size is too big its adjusted in draw
  457. if (source == sizebar)
  458. {
  459. dot.setWidth(sizebar.getValue());
  460. dot.setHeight(sizebar.getValue());
  461. }
  462. if (source == speedbar)
  463. {
  464.  
  465. }
  466. }
  467.  
  468. public void stop()
  469. {
  470. this.speedbar.removeAdjustmentListener(this);
  471. this.runbutton.removeActionListener(this);
  472. this.tailbutton.removeActionListener(this);
  473. this.sizebar.removeAdjustmentListener(this);
  474. this.shapebutton.removeActionListener(this);
  475. this.clearbutton.removeActionListener(this);
  476. this.quitbutton.removeActionListener(this);
  477. }
  478. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:160: class Bounce is public, should be declared in a file named Bounce.java
public class Bounce extends Applet implements ActionListener, AdjustmentListener
       ^
1 error
stdout
Standard output is empty