Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.56 KB | None | 0 0
  1. /*
  2. * Homework 4: Ball Bouncer thingy
  3. Paul MacLean (MAC7537@calu.edu), Michael Gorse (GOR9632@calu.edu), Anthony Carrola (CAR3766@calu.edu)
  4. Group 8 (2^3)
  5. CET 350 - Technical Computer using Java
  6. */
  7.  
  8.  
  9. import java.io.*;
  10. import java.awt.*;
  11. import java.util.*;
  12. import java.util.concurrent.TimeUnit;
  13. import java.lang.*;
  14. import java.awt.List;
  15. import java.awt.event.*;
  16.  
  17. class Bounce extends Frame implements ActionListener,WindowListener,AdjustmentListener,ComponentListener,Runnable
  18. {
  19. private static final long serialVersionUID = 1L;
  20.  
  21. //SCROLLER are only used to get the ratio of the scrollbar, regardless of its purpose.
  22. private final int SCROLLER_THUMBOFFSET = 10;
  23. private final int SCROLLER_MIN = 0;
  24. private final int SCROLLER_MAX = 100;
  25. private final int SCROLLER_RANGE = SCROLLER_MAX - SCROLLER_MIN;
  26. private final int SCROLLER_RANGE_VIS = SCROLLER_RANGE - SCROLLER_THUMBOFFSET;
  27. private final int SCROLLER_DEFAULT = SCROLLER_RANGE_VIS / 2;
  28. private final int SCROLLER_BLOCKVAL = SCROLLER_RANGE_VIS / 10;
  29.  
  30. //SPEED constants
  31. private final float SPEED_DELTA_MIN = 1; //milliseconds
  32. private final float SPEED_DELTA_MAX = 100; //milliseconds
  33. private final float SPEED_RANGE = Math.abs(SPEED_DELTA_MAX - SPEED_DELTA_MIN); //Range should be positive.
  34. private final int SPEED_DEFAULT = (int) (SPEED_RANGE / 2 + SPEED_DELTA_MIN); //Half of the range of values, plus the _MIN_ value
  35.  
  36. //SIZE constants
  37. private final int SIZE_MIN = 10;
  38. private final int SIZE_MAX = 100;
  39. private final int SIZE_RANGE = Math.abs(SIZE_MAX - SIZE_MIN);
  40.  
  41.  
  42. private static int ButtonHeight = 20;
  43.  
  44.  
  45. int MoveRate = SPEED_DEFAULT;
  46. private volatile boolean TimePause = true;
  47. private Thread SteppingThread;
  48.  
  49. private Button RunButton = new Button("Run");
  50. private Button PauseButton = new Button("Pause");
  51. private Button QuitButton = new Button("Quit");
  52.  
  53. private Scrollbar SpeedBar;
  54. private Scrollbar SizeBar;
  55.  
  56. private Label SpeedLabel = new Label("Speed");
  57. private Label SizeLabel = new Label("Size");
  58.  
  59. private CanObj canvas = null;
  60.  
  61.  
  62. Bounce() {
  63. initComponents();
  64. sizeElements();
  65. start();
  66. }
  67.  
  68. public static void main(String[] args) throws IOException
  69. {
  70. new Bounce();
  71. }
  72.  
  73. private void initComponents() {
  74. SpeedBar = new Scrollbar(Scrollbar.HORIZONTAL);
  75. SpeedBar.setVisible(true);
  76. SpeedBar.setValues(SCROLLER_DEFAULT, SCROLLER_THUMBOFFSET, SCROLLER_MIN, SCROLLER_MAX);
  77. SpeedBar.setBlockIncrement(SCROLLER_BLOCKVAL);
  78. SpeedBar.setUnitIncrement(2);
  79. SpeedBar.setBackground(Color.ORANGE);
  80. SpeedBar.addAdjustmentListener(this);
  81. this.add(SpeedBar);
  82.  
  83. SpeedLabel.setAlignment(Label.CENTER);
  84. SpeedLabel.setVisible(true);
  85. this.add(SpeedLabel);
  86.  
  87. RunButton.setVisible(true);
  88. RunButton.addActionListener(this);
  89. this.add(RunButton);
  90.  
  91. PauseButton.setVisible(true);
  92. PauseButton.addActionListener(this);
  93. this.add(PauseButton);
  94.  
  95.  
  96. QuitButton.setVisible(true);
  97. QuitButton.addActionListener(this);
  98. this.add(QuitButton);
  99.  
  100.  
  101. SizeBar = new Scrollbar(Scrollbar.HORIZONTAL);
  102. SizeBar.setVisible(true);
  103. SizeBar.setValues(SCROLLER_DEFAULT, SCROLLER_THUMBOFFSET, SCROLLER_MIN, SCROLLER_MAX);
  104. SizeBar.setBlockIncrement(SCROLLER_BLOCKVAL);
  105. SizeBar.setUnitIncrement(2);
  106. SizeBar.setBackground(Color.ORANGE);
  107. SizeBar.addAdjustmentListener(this);
  108. this.add(SizeBar);
  109.  
  110. SizeLabel.setAlignment(Label.CENTER);
  111. SizeLabel.setVisible(true);
  112. this.add(SizeLabel);
  113.  
  114. this.setLayout(null);
  115. this.setBounds(50, 50 , 800, 500);
  116. this.setVisible(true);
  117. this.setResizable(true);
  118. this.setTitle("");
  119. this.setMinimumSize(new Dimension(700, 400));
  120. this.addWindowListener(this);
  121. this.requestFocus();
  122.  
  123. canvas = new CanObj(this.getWidth(), this.getHeight()-120, this.getInsets());
  124. this.addComponentListener(this);
  125.  
  126. this.add(canvas);
  127. canvas.setVisible(true);
  128. }
  129.  
  130. private void sizeElements() {
  131. int FRAME_X = this.getWidth();
  132. int FRAME_Y = this.getHeight();
  133.  
  134. int ButtonSpace =(int) (0.05 * FRAME_X);
  135. int ButtonWidth = (int) (0.05 * FRAME_X);
  136. int NetBarWidth = (int) (0.175 * FRAME_X);
  137. int MenuElementYPos = FRAME_Y - 60;
  138. int LabelYPos = MenuElementYPos+20;
  139.  
  140.  
  141. SpeedBar.setBounds(ButtonSpace, MenuElementYPos, NetBarWidth, ButtonHeight);
  142. SpeedLabel.setBounds(ButtonSpace, LabelYPos, NetBarWidth, ButtonHeight);
  143.  
  144. RunButton.setBounds(2*ButtonSpace+NetBarWidth, MenuElementYPos, ButtonWidth,ButtonHeight );
  145. PauseButton.setBounds(6*ButtonSpace+NetBarWidth, MenuElementYPos, ButtonWidth, ButtonHeight);
  146.  
  147. QuitButton.setBounds(10*ButtonSpace+NetBarWidth, MenuElementYPos, ButtonWidth, ButtonHeight);
  148.  
  149. SizeBar.setBounds(12*ButtonSpace+NetBarWidth, MenuElementYPos, NetBarWidth, ButtonHeight);
  150. SizeLabel.setBounds(12*ButtonSpace+NetBarWidth, LabelYPos, NetBarWidth, ButtonHeight);
  151.  
  152. canvas.onScreenResize(FRAME_X, MenuElementYPos-20, this.getInsets());
  153. canvas.CheckEdge();
  154. }
  155. //=======================================================METHODS FOR THREAD
  156. public void start()
  157. {
  158. if(SteppingThread == null)
  159. {
  160. SteppingThread = new Thread(this);
  161. SteppingThread.start();
  162. }
  163. }
  164. public void run()
  165. {
  166. SteppingThread.setPriority(Thread.MAX_PRIORITY);
  167.  
  168. while(true) {
  169. while(!TimePause)
  170. {
  171. try
  172. {
  173. SteppingThread.sleep(MoveRate);
  174. } catch(InterruptedException e){}
  175. //canvas.repaint();
  176.  
  177. canvas.setXandY();
  178. }
  179. }
  180. }
  181. //=======================================================METHOD TO QUIT
  182. public void stop()
  183. {
  184. SteppingThread.setPriority(Thread.MIN_PRIORITY);
  185. this.removeWindowListener(this);
  186. System.exit(0);
  187. this.dispose();
  188. }
  189.  
  190.  
  191.  
  192. //=======================================================METHODS FOR BUTTONS
  193. public void run_buttonclk()
  194. {
  195. SteppingThread.interrupt();
  196. TimePause = !TimePause;
  197.  
  198. if(TimePause) {
  199. RunButton.setLabel("Run");
  200. }
  201. else {
  202. RunButton.setLabel("Stop");
  203. try {
  204. SteppingThread.start();
  205. } catch(IllegalThreadStateException e) {}
  206. }
  207. }
  208. public void pause_buttonclk()
  209. {
  210. //write pause button code here
  211. }
  212.  
  213. //=======================================================WINDOW EVENTS
  214. public void windowClosed(WindowEvent e) {
  215. System.exit(0);
  216. }
  217.  
  218. public void windowClosing(WindowEvent e) {
  219. stop();
  220. }
  221.  
  222. public void windowActivated(WindowEvent e) {}
  223. public void windowDeactivated(WindowEvent e) {}
  224. public void windowDeiconified(WindowEvent e) {}
  225. public void windowIconified(WindowEvent e) {}
  226. public void windowOpened(WindowEvent e) {}
  227.  
  228.  
  229.  
  230. //=======================================================INTERACTION EVENTS
  231. public void actionPerformed(ActionEvent e) {
  232. Object click = e.getSource();
  233. if(click == RunButton) {
  234. run_buttonclk();
  235. }
  236. else if(click == PauseButton) {
  237. pause_buttonclk(); //not functional yet
  238. }
  239. else if(click == QuitButton) {
  240. stop();
  241. }
  242. }
  243.  
  244. public void adjustmentValueChanged(AdjustmentEvent e) {
  245. Scrollbar temp = (Scrollbar) e.getSource();
  246.  
  247. float SBRatio = (float) temp.getValue() / (float) SCROLLER_RANGE_VIS;
  248. if (temp == SpeedBar) {
  249. SBRatio = 1-SBRatio; //This allows the left of the bar to hold a larger value
  250. MoveRate = (int) ((SBRatio * SPEED_RANGE) + SPEED_DELTA_MIN); //Add min to percentage applied to speed
  251. } else if (temp == SizeBar) {
  252. float Size = (SBRatio * SIZE_RANGE) + SIZE_MIN;
  253. int constrainedsize = canvas.setBallSize((int)Size);
  254. if (constrainedsize != -1) {
  255. SizeBar.setValue(constrainedsize);
  256. }
  257. }
  258. }
  259.  
  260. public void componentResized(ComponentEvent e) {
  261. sizeElements();
  262. }
  263.  
  264. public void componentMoved(ComponentEvent e) {}
  265. public void componentShown(ComponentEvent e) {}
  266. public void componentHidden(ComponentEvent e) {}
  267.  
  268. }
  269.  
  270.  
  271.  
  272.  
  273.  
  274.  
  275.  
  276.  
  277.  
  278.  
  279.  
  280.  
  281.  
  282.  
  283.  
  284.  
  285.  
  286.  
  287.  
  288.  
  289.  
  290.  
  291.  
  292.  
  293.  
  294.  
  295.  
  296.  
  297.  
  298.  
  299.  
  300.  
  301.  
  302.  
  303.  
  304.  
  305.  
  306.  
  307.  
  308.  
  309. class CanObj extends Canvas
  310. {
  311. private static final long serialVersionUID = 1L;
  312.  
  313. private class Vec2 {
  314. public int x;
  315. public int y;
  316. Vec2(int x, int y) {
  317. this.x = x;
  318. this.y = y;
  319. }
  320. }
  321.  
  322. private Vec2 Dir = new Vec2(1,1);
  323. private Vec2 CurrPos = new Vec2(40,275);
  324. private Vec2 LastPos = new Vec2(10,10);
  325.  
  326. private int LastSize;
  327. private int CurrSize;
  328. private int width;
  329. private int height;
  330.  
  331. CanObj(int width, int height, Insets insets) {
  332. LastSize = 33;
  333. CurrSize = 33;
  334. this.setBackground(Color.lightGray);
  335. onScreenResize(width, height, insets);
  336. CheckEdge();
  337. }
  338.  
  339. //======================================
  340. //=======================METHODS
  341. //======================================
  342.  
  343.  
  344. //=======================SETS/MODIFIERS
  345.  
  346.  
  347. public int setBallSize(int size) { //resizes object based on sb
  348. int retval;
  349. LastSize = CurrSize;
  350.  
  351. if(size + CurrPos.x + 1 > width || size+CurrPos.y + 1 > height) //ball size too big
  352. {
  353. retval = CurrSize;
  354. }
  355. else
  356. {
  357. retval = -1;
  358. CurrSize = size;
  359. }
  360.  
  361. update(this.getGraphics());
  362. return retval;
  363. }
  364.  
  365.  
  366. public void setXandY()
  367. {
  368. Vec2 nextpos = new Vec2(CurrPos.x + Dir.x,CurrPos.y + Dir.y); //make predictive vector
  369.  
  370. if(nextpos.x - 1 < 0 || nextpos.x + CurrSize + 1 > width)
  371. Dir.x *= -1;
  372.  
  373. if(nextpos.y - 1 < 0 || nextpos.y + CurrSize + 1 > height)
  374. Dir.y *= -1;
  375.  
  376. LastPos = CurrPos;
  377. CurrPos = nextpos;
  378.  
  379. update(this.getGraphics());
  380. }
  381. //======================================
  382. //=======================GETS
  383. //======================================
  384.  
  385.  
  386.  
  387. //======================================
  388. //=======================REACTORS
  389. //======================================
  390.  
  391. public void onScreenResize(int framewidth, int frameheight, Insets insets) //resizes canvas for screen
  392. {
  393. this.width = framewidth - (2*insets.left) - 10;
  394. this.height = frameheight - insets.top;
  395. this.setBounds(insets.left+5, insets.top+5, this.width, this.height);
  396. }
  397.  
  398.  
  399.  
  400. public void update(Graphics g)
  401. {
  402.  
  403.  
  404. g.setColor(this.getBackground());
  405. g.fillOval(LastPos.x, LastPos.y, LastSize, LastSize);
  406.  
  407. drawOval(g);
  408.  
  409. }
  410.  
  411. public void CheckEdge()
  412. {
  413.  
  414. if(CurrPos.x>=width-CurrSize)
  415. {
  416. CurrPos.x = width-CurrSize;
  417. }
  418. if(CurrPos.y>=height-CurrSize)
  419. {
  420. CurrPos.y = height-CurrSize;
  421. }
  422. }
  423.  
  424.  
  425. public void paint(Graphics g)
  426. {
  427. update(g);
  428. }
  429.  
  430.  
  431.  
  432. public void drawOval(Graphics g) //draws oval
  433. {
  434. int size = CurrSize;
  435. Vec2 pos = CurrPos;
  436. g.setColor(Color.orange);
  437. g.fillOval(pos.x, pos.y, size, size);
  438. g.setColor(Color.darkGray);
  439. g.fillOval(pos.x+1, pos.y+1, size-2, size-2);
  440. }
  441.  
  442.  
  443. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement