Guest User

Untitled

a guest
Mar 25th, 2019
65
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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 BouncingBall extends Frame implements ActionListener,WindowListener,AdjustmentListener,ComponentListener,Runnable, MouseListener, MouseMotionListener
  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. private int X1 = 0;
  63. private int Y1 = 0;
  64. private int X2 = 0;
  65. private int Y2 = 0;
  66.  
  67.  
  68. private boolean rightMouseClicked = false;
  69.  
  70.  
  71.  
  72. BouncingBall() {
  73. initComponents();
  74. sizeElements();
  75. start();
  76. }
  77.  
  78. public static void main(String[] args) throws IOException
  79. {
  80. new BouncingBall();
  81. }
  82.  
  83. private void initComponents() {
  84. SpeedBar = new Scrollbar(Scrollbar.HORIZONTAL);
  85. SpeedBar.setVisible(true);
  86. SpeedBar.setValues(SCROLLER_DEFAULT, SCROLLER_THUMBOFFSET, SCROLLER_MIN, SCROLLER_MAX);
  87. SpeedBar.setBlockIncrement(SCROLLER_BLOCKVAL);
  88. SpeedBar.setUnitIncrement(2);
  89. SpeedBar.setBackground(Color.ORANGE);
  90. SpeedBar.addAdjustmentListener(this);
  91. this.add(SpeedBar);
  92.  
  93. SpeedLabel.setAlignment(Label.CENTER);
  94. SpeedLabel.setVisible(true);
  95. this.add(SpeedLabel);
  96.  
  97. RunButton.setVisible(true);
  98. RunButton.addActionListener(this);
  99. this.add(RunButton);
  100.  
  101. PauseButton.setVisible(true);
  102. PauseButton.addActionListener(this);
  103. this.add(PauseButton);
  104.  
  105.  
  106. QuitButton.setVisible(true);
  107. QuitButton.addActionListener(this);
  108. this.add(QuitButton);
  109.  
  110.  
  111. SizeBar = new Scrollbar(Scrollbar.HORIZONTAL);
  112. SizeBar.setVisible(true);
  113. SizeBar.setValues(SCROLLER_DEFAULT, SCROLLER_THUMBOFFSET, SCROLLER_MIN, SCROLLER_MAX);
  114. SizeBar.setBlockIncrement(SCROLLER_BLOCKVAL);
  115. SizeBar.setUnitIncrement(2);
  116. SizeBar.setBackground(Color.ORANGE);
  117. SizeBar.addAdjustmentListener(this);
  118. this.add(SizeBar);
  119.  
  120. SizeLabel.setAlignment(Label.CENTER);
  121. SizeLabel.setVisible(true);
  122. this.add(SizeLabel);
  123.  
  124. this.setLayout(null);
  125. this.setBounds(50, 50 , 800, 500);
  126. this.setVisible(true);
  127. this.setResizable(true);
  128. this.setTitle("");
  129. this.setMinimumSize(new Dimension(700, 400));
  130. this.addWindowListener(this);
  131. this.requestFocus();
  132.  
  133. canvas = new CanObj(this.getWidth(), this.getHeight()-120, this.getInsets());
  134. this.addComponentListener(this);
  135.  
  136. this.add(canvas);
  137. canvas.setVisible(true);
  138. canvas.addMouseListener(this);
  139. canvas.addMouseMotionListener(this);
  140. }
  141.  
  142. private void sizeElements() {
  143. int FRAME_X = this.getWidth();
  144. int FRAME_Y = this.getHeight();
  145.  
  146. int ButtonSpace =(int) (0.05 * FRAME_X);
  147. int ButtonWidth = (int) (0.05 * FRAME_X);
  148. int NetBarWidth = (int) (0.175 * FRAME_X);
  149. int MenuElementYPos = FRAME_Y - 60;
  150. int LabelYPos = MenuElementYPos+20;
  151.  
  152.  
  153. SpeedBar.setBounds(ButtonSpace, MenuElementYPos, NetBarWidth, ButtonHeight);
  154. SpeedLabel.setBounds(ButtonSpace, LabelYPos, NetBarWidth, ButtonHeight);
  155.  
  156. RunButton.setBounds(2*ButtonSpace+NetBarWidth, MenuElementYPos, ButtonWidth,ButtonHeight );
  157. PauseButton.setBounds(6*ButtonSpace+NetBarWidth, MenuElementYPos, ButtonWidth, ButtonHeight);
  158.  
  159. QuitButton.setBounds(10*ButtonSpace+NetBarWidth, MenuElementYPos, ButtonWidth, ButtonHeight);
  160.  
  161. SizeBar.setBounds(12*ButtonSpace+NetBarWidth, MenuElementYPos, NetBarWidth, ButtonHeight);
  162. SizeLabel.setBounds(12*ButtonSpace+NetBarWidth, LabelYPos, NetBarWidth, ButtonHeight);
  163.  
  164. canvas.onScreenResize(FRAME_X, MenuElementYPos-20, this.getInsets());
  165. canvas.CheckEdge();
  166. }
  167. //=======================================================METHODS FOR THREAD
  168. public void start()
  169. {
  170. if(SteppingThread == null)
  171. {
  172. SteppingThread = new Thread(this);
  173. SteppingThread.start();
  174. }
  175. }
  176. public void run()
  177. {
  178. SteppingThread.setPriority(Thread.MAX_PRIORITY);
  179.  
  180. while(true) {
  181. while(!TimePause)
  182. {
  183. try
  184. {
  185. SteppingThread.sleep(MoveRate);
  186. } catch(InterruptedException e){}
  187.  
  188. canvas.setXandY();
  189. }
  190. }
  191. }
  192. //=======================================================METHOD TO QUIT
  193. public void stop()
  194. {
  195. SteppingThread.setPriority(Thread.MIN_PRIORITY);
  196. this.removeWindowListener(this);
  197. System.exit(0);
  198. this.dispose();
  199. }
  200.  
  201.  
  202.  
  203. //=======================================================METHODS FOR BUTTONS
  204. public void run_buttonclk()
  205. {
  206. SteppingThread.interrupt();
  207. TimePause = !TimePause;
  208.  
  209. if(TimePause) {
  210. RunButton.setLabel("Run");
  211. }
  212. else {
  213. RunButton.setLabel("Stop");
  214. try {
  215. SteppingThread.start();
  216. } catch(IllegalThreadStateException e) {}
  217. }
  218. }
  219. public void pause_buttonclk()
  220. {
  221. //write pause button code here
  222. }
  223.  
  224. //=======================================================WINDOW EVENTS
  225. public void windowClosed(WindowEvent e) {
  226. System.exit(0);
  227. }
  228.  
  229. public void windowClosing(WindowEvent e) {
  230. stop();
  231. }
  232.  
  233. public void windowActivated(WindowEvent e) {}
  234. public void windowDeactivated(WindowEvent e) {}
  235. public void windowDeiconified(WindowEvent e) {}
  236. public void windowIconified(WindowEvent e) {}
  237. public void windowOpened(WindowEvent e) {}
  238.  
  239.  
  240.  
  241. //=======================================================INTERACTION EVENTS
  242. public void actionPerformed(ActionEvent e) {
  243. Object click = e.getSource();
  244. if(click == RunButton) {
  245. run_buttonclk();
  246. }
  247. else if(click == PauseButton) {
  248. pause_buttonclk(); //not functional yet
  249. }
  250. else if(click == QuitButton) {
  251. stop();
  252. }
  253. }
  254.  
  255. public void adjustmentValueChanged(AdjustmentEvent e) {
  256. Scrollbar temp = (Scrollbar) e.getSource();
  257.  
  258. float SBRatio = (float) temp.getValue() / (float) SCROLLER_RANGE_VIS;
  259. if (temp == SpeedBar) {
  260. SBRatio = 1-SBRatio; //This allows the left of the bar to hold a larger value
  261. MoveRate = (int) ((SBRatio * SPEED_RANGE) + SPEED_DELTA_MIN); //Add min to percentage applied to speed
  262. } else if (temp == SizeBar) {
  263. float Size = (SBRatio * SIZE_RANGE) + SIZE_MIN;
  264. int constrainedsize = canvas.setBallSize((int)Size);
  265. if (constrainedsize != -1) {
  266. SizeBar.setValue(constrainedsize);
  267. }
  268. }
  269. }
  270.  
  271. public void componentResized(ComponentEvent e) {
  272. sizeElements();
  273. }
  274.  
  275. public void componentMoved(ComponentEvent e) {}
  276. public void componentShown(ComponentEvent e) {}
  277. public void componentHidden(ComponentEvent e) {}
  278.  
  279. //=======================================================MOUSE EVENTS
  280.  
  281.  
  282. public void mouseDragged(MouseEvent e) {
  283. if(rightMouseClicked == false)
  284. {
  285. System.out.println("Dragged");
  286. X2 = e.getX();
  287. Y2 = e.getY();
  288. System.out.println(X2 +" "+ Y2);
  289. canvas.setRectPerameters(X1,Y1, X2, Y2);
  290. }
  291. System.out.println("Right Mouse Clicked: " + rightMouseClicked);
  292. }
  293.  
  294. public void mouseMoved(MouseEvent e) {
  295. }
  296.  
  297.  
  298. public void mouseClicked(MouseEvent e) {
  299. Point clk = new Point(0, 0);
  300. if( e.getButton() == MouseEvent.BUTTON3)
  301. {
  302. System.out.println("delete");
  303. clk = e.getPoint();
  304. canvas.deleteRectangle(clk);
  305. }
  306.  
  307. }
  308.  
  309.  
  310. public void mouseEntered(MouseEvent e) {
  311.  
  312. }
  313.  
  314.  
  315. public void mouseExited(MouseEvent e) {
  316. System.out.println("Exited");
  317. }
  318.  
  319. public void mousePressed(MouseEvent e) {
  320. if(e.getButton() == MouseEvent.BUTTON3)
  321. {
  322. rightMouseClicked = true;
  323. }
  324. if(rightMouseClicked == false)
  325. {
  326. System.out.println("Pressed");
  327. X1 = e.getX();
  328. Y1 = e.getY();
  329. System.out.println(X1 +" "+ Y1);
  330. }
  331. }
  332.  
  333. public void mouseReleased(MouseEvent e) {
  334. if(rightMouseClicked == false)
  335. {
  336. canvas.storeRectangle();
  337.  
  338. }
  339. rightMouseClicked = false;
  340.  
  341. }
  342. /*public void drawRect()
  343. {
  344. int length = Math.abs(X1-X1);
  345. int height = Math.abs(Y1-Y2);
  346.  
  347. }*/
  348.  
  349.  
  350. }
  351.  
  352.  
  353.  
  354.  
  355.  
  356.  
  357.  
  358.  
  359.  
  360.  
  361.  
  362.  
  363.  
  364.  
  365.  
  366.  
  367.  
  368.  
  369.  
  370.  
  371.  
  372.  
  373.  
  374.  
  375.  
  376.  
  377.  
  378.  
  379.  
  380.  
  381.  
  382.  
  383.  
  384.  
  385.  
  386.  
  387.  
  388.  
  389.  
  390.  
  391. class CanObj extends Canvas
  392. {
  393. private static final long serialVersionUID = 1L;
  394.  
  395. private class Vec2 {
  396. public int x;
  397. public int y;
  398. Vec2(int x, int y) {
  399. this.x = x;
  400. this.y = y;
  401. }
  402. }
  403.  
  404. private Graphics og;
  405. private Image Buffer;
  406.  
  407. private Vec2 Dir = new Vec2(1,1);
  408. private Vec2 CurrPos = new Vec2(40,275);
  409. private Vec2 LastPos = new Vec2(10,10);
  410.  
  411. private int LastSize;
  412. private int CurrSize;
  413. private int width;
  414. private int height;
  415.  
  416. private boolean addRect = false;
  417.  
  418. //=======================Variables for rectangles
  419.  
  420. //===========Rectangle
  421.  
  422. private Rectangle newRect = new Rectangle(0, 0, 0, 0);
  423.  
  424. private Vector<Rectangle> Walls = new Vector<Rectangle>();
  425.  
  426. //==============================================
  427.  
  428. CanObj(int width, int height, Insets insets) {
  429. LastSize = 33;
  430. CurrSize = 33;
  431. this.setBackground(Color.lightGray);
  432. onScreenResize(width, height, insets);
  433. CheckEdge();
  434. }
  435.  
  436.  
  437.  
  438. //======================================
  439. //=======================METHODS
  440. //======================================
  441.  
  442.  
  443. //=======================SETS/MODIFIERS
  444.  
  445.  
  446. public int setBallSize(int size) { //resizes object based on sb
  447. int retval;
  448. LastSize = CurrSize;
  449.  
  450. if(size + CurrPos.x + 1 > width || size+CurrPos.y + 1 > height) //ball size too big
  451. {
  452. retval = CurrSize;
  453. }
  454. else
  455. {
  456. retval = -1;
  457. CurrSize = size;
  458. }
  459.  
  460. paint(this.getGraphics());
  461. return retval;
  462. }
  463.  
  464.  
  465. public void setXandY()
  466. {
  467. Vec2 nextpos = new Vec2(CurrPos.x + Dir.x,CurrPos.y + Dir.y); //make predictive vector
  468.  
  469. if(nextpos.x - 1 < 0 || nextpos.x + CurrSize + 1 > width)
  470. Dir.x *= -1;
  471.  
  472. if(nextpos.y - 1 < 0 || nextpos.y + CurrSize + 1 > height)
  473. Dir.y *= -1;
  474.  
  475. LastPos = CurrPos;
  476. CurrPos = nextpos;
  477.  
  478. paint(this.getGraphics());
  479. }
  480. //======================================
  481. //=======================GETS
  482. //======================================
  483.  
  484.  
  485.  
  486. //======================================
  487. //=======================REACTORS
  488. //======================================
  489.  
  490. public void onScreenResize(int framewidth, int frameheight, Insets insets) //resizes canvas for screen
  491. {
  492. this.width = framewidth - (2*insets.left) - 10;
  493. this.height = frameheight - insets.top;
  494. this.setBounds(insets.left+5, insets.top+5, this.width, this.height);
  495. }
  496.  
  497. public void CheckEdge()
  498. {
  499.  
  500. if(CurrPos.x>=width-CurrSize)
  501. {
  502. CurrPos.x = width-CurrSize;
  503. }
  504. if(CurrPos.y>=height-CurrSize)
  505. {
  506. CurrPos.y = height-CurrSize;
  507. }
  508. }
  509.  
  510. public void paint(Graphics g)
  511. {
  512. //under normal circumstances, LastPos and LastSize would be set at the end of this function, but
  513. //due to threading, artifacting can occur at fast enough resizes if we do not store them into temp vars
  514. //and use the temp variables as the true purpose
  515.  
  516. Vec2 templastpos = LastPos;
  517. int templastsize = LastSize;
  518.  
  519. LastPos = CurrPos;
  520. LastSize = CurrSize;
  521.  
  522. Buffer = createImage(this.width, this.height);
  523. if (og != null)
  524. {
  525. og.dispose();
  526. }
  527. og = Buffer.getGraphics();
  528. try
  529. {
  530. drawOval(og);
  531. //if(addRect == true)
  532. //{
  533. drawRect(og);
  534. //}
  535.  
  536.  
  537. }
  538. catch (Exception e)
  539. {}
  540. g.drawImage(Buffer, 0, 0, null);
  541.  
  542.  
  543. }
  544.  
  545.  
  546. public void drawOval(Graphics g) //draws oval
  547. {
  548. int size = CurrSize;
  549. Vec2 pos = CurrPos;
  550. g.setColor(Color.orange);
  551. g.fillOval(pos.x, pos.y, size, size);
  552. g.setColor(Color.darkGray);
  553. g.fillOval(pos.x+1, pos.y+1, size-2, size-2);
  554. }
  555.  
  556. public void setRectPerameters(int X1, int Y1, int X2, int Y2)
  557. {
  558. int temp = 0;
  559. if(X1 > X2)
  560. {
  561. temp = X1;
  562. X1 = X2;
  563. X2 = temp;
  564. //System.out.println(X1+ " " +X2);
  565. }
  566. if(Y1 > Y2)
  567. {
  568. temp = Y1;
  569. Y1 = Y2;
  570. Y2 = temp;
  571. ///System.out.println(Y1+ " " +Y2);
  572. }
  573. newRect.x = X1;
  574. newRect.y = Y1;
  575. newRect.width = Math.abs(X2-X1);
  576. newRect.height = Math.abs(Y2-Y1);
  577. //System.out.println(" WIDTH:" + newRect.width+ " " + "HEIGHT:" + newRect.height);
  578.  
  579.  
  580. addRect = true;
  581. paint(this.getGraphics());
  582. }
  583.  
  584.  
  585. public void drawRect(Graphics g)
  586. {
  587.  
  588. System.out.println("In drawRect");
  589.  
  590. g.setColor(Color.PINK);
  591. g.fillRect(newRect.x, newRect.y, newRect.width, newRect.height);
  592. int i = 0;
  593. while (i < Walls.size())
  594. {
  595. g.setColor(Color.PINK);
  596. g.fillRect(Walls.elementAt(i).x, Walls.elementAt(i).y, Walls.elementAt(i).width, Walls.elementAt(i).height);
  597. i++;
  598. }
  599.  
  600.  
  601. addRect = false;
  602. }
  603.  
  604. public void storeRectangle()
  605. {
  606. Rectangle RectTemp = new Rectangle( newRect);
  607. System.out.print("Adding walls");
  608. Walls.add(RectTemp);
  609. newRect.setBounds(0,0,0,0);
  610. System.out.print("Wall size:"+ Walls.size());
  611. }
  612.  
  613. public void deleteRectangle(Point clk)
  614. {
  615. int i = 0;
  616. boolean deleted = false;
  617. while(i < Walls.size() && deleted == false)
  618. {
  619. System.out.println("INside the loop" + clk);
  620. if (Walls.elementAt(i).contains(clk))
  621. {
  622. System.out.println(Walls.elementAt(i).contains(clk));
  623. Walls.removeElementAt(i);
  624. deleted = true;
  625. }
  626. else
  627. {
  628. paint(this.getGraphics());
  629. i++;
  630. }
  631. System.out.println("After delete, wall size is: " + Walls.size());
  632. }
  633. addRect = true;
  634. paint(this.getGraphics());
  635. addRect = false;
  636. }
  637.  
  638.  
  639.  
  640. }
RAW Paste Data