Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.19 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 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. CheckWalls();
  478. paint(this.getGraphics());
  479. }
  480. public void CheckWalls()
  481. {
  482. int WallWidth = 0;
  483. int WallHeight = 0;
  484. int WallX;
  485. int WallY;
  486. Vec2 nextpos = new Vec2(CurrPos.x + Dir.x,CurrPos.y + Dir.y); //make predictive vector
  487. for(int i = 0;i<Walls.size();i++)
  488. {
  489. WallWidth = Walls.elementAt(i).width;
  490. WallHeight = Walls.elementAt(i).height;
  491. WallX = Walls.elementAt(i).x;
  492. WallY = Walls.elementAt(i).y;
  493.  
  494. // Right side of ball--left wall top of ball above bottom of wall bottom of ball above top of wall
  495. if((nextpos.x+CurrSize+1 == WallX) && (nextpos.y-1 <= WallY+WallHeight && nextpos.y+CurrSize+1>=WallY))
  496. {
  497. System.out.print("In walls if 1 ");
  498. Dir.x*=-1;
  499. }
  500. // Left side of ball--right wall top of ball above bottom of wall bottom of ball above top of wall
  501. else if((nextpos.x-1 == WallX+WallWidth) && (nextpos.y-1 <= WallY+WallHeight && nextpos.y+CurrSize+1>=WallY))
  502. {
  503. System.out.print("In walls if 2 ");
  504. Dir.x*=-1;
  505. }
  506. // bottom of the ball --top of rectangle left side of ball/ rsrect
  507. if((nextpos.y +CurrSize+2 == WallY) && (nextpos.x <= WallX+WallWidth && nextpos.x+CurrSize >= WallX))
  508. {
  509. System.out.print("In walls if 3");
  510. Dir.y*=-1;
  511. }
  512.  
  513. if((nextpos.y == WallY+WallWidth) && (nextpos.x <= WallX+WallWidth && nextpos.x+CurrSize >= WallX))
  514. {
  515. System.out.print("In walls if 4");
  516. Dir.y*=-1;
  517. }
  518.  
  519. /*
  520. * nextpos.x == WallX+WallWidth ||
  521. if((nextpos.x+CurrSize) == Walls.elementAt(i).x && ((nextpos.y+CurrSize)<= Walls.elementAt(i).y || (nextpos.y+CurrSize)<= Walls.elementAt(i).y+WallHeight))
  522. {
  523.  
  524. }
  525.  
  526. if(nextpos.x == (Walls.elementAt(i).x +WallWidth) && (nextpos.y+)
  527.  
  528. if(nextpos.y <= (Walls.elementAt(i).y) || nextpos.y >= Walls.elementAt(i).y + Walls.elementAt(i).height)
  529. {
  530. Dir.x *=-1;
  531. }
  532.  
  533. if(nextpos.x == (Walls.elementAt(i).x + Walls.elementAt(i).width+1) || (nextpos.x + CurrSize) == (Walls.elementAt(i).x + Walls.elementAt(i).width+1))
  534. {
  535. Dir.x *=-1;
  536. }
  537. }*/
  538.  
  539. }
  540. LastPos = CurrPos;
  541. CurrPos = nextpos;
  542. }
  543.  
  544.  
  545. //======================================
  546. //=======================GETS
  547. //======================================
  548.  
  549.  
  550.  
  551. //======================================
  552. //=======================REACTORS
  553. //======================================
  554.  
  555. public void onScreenResize(int framewidth, int frameheight, Insets insets) //resizes canvas for screen
  556. {
  557. this.width = framewidth - (2*insets.left) - 10;
  558. this.height = frameheight - insets.top;
  559. this.setBounds(insets.left+5, insets.top+5, this.width, this.height);
  560. }
  561.  
  562. public void CheckEdge()
  563. {
  564.  
  565. if(CurrPos.x>=width-CurrSize)
  566. {
  567. CurrPos.x = width-CurrSize;
  568. }
  569. if(CurrPos.y>=height-CurrSize)
  570. {
  571. CurrPos.y = height-CurrSize;
  572. }
  573. }
  574.  
  575.  
  576. public void paint(Graphics g)
  577. {
  578. //under normal circumstances, LastPos and LastSize would be set at the end of this function, but
  579. //due to threading, artifacting can occur at fast enough resizes if we do not store them into temp vars
  580. //and use the temp variables as the true purpose
  581.  
  582. Vec2 templastpos = LastPos;
  583. int templastsize = LastSize;
  584.  
  585. LastPos = CurrPos;
  586. LastSize = CurrSize;
  587.  
  588. Buffer = createImage(this.width, this.height);
  589. if (og != null)
  590. {
  591. og.dispose();
  592. }
  593. og = Buffer.getGraphics();
  594. try
  595. {
  596. drawOval(og);
  597. //if(addRect == true)
  598. //{
  599. drawRect(og);
  600. //}
  601.  
  602.  
  603. }
  604. catch (Exception e)
  605. {}
  606. g.drawImage(Buffer, 0, 0, null);
  607.  
  608.  
  609. }
  610.  
  611.  
  612. public void drawOval(Graphics g) //draws oval
  613. {
  614. int size = CurrSize;
  615. Vec2 pos = CurrPos;
  616. g.setColor(Color.orange);
  617. g.fillOval(pos.x, pos.y, size, size);
  618. g.setColor(Color.darkGray);
  619. g.fillOval(pos.x+1, pos.y+1, size-2, size-2);
  620. }
  621.  
  622. public void setRectPerameters(int X1, int Y1, int X2, int Y2)
  623. {
  624. int temp = 0;
  625. if(X1 > X2)
  626. {
  627. temp = X1;
  628. X1 = X2;
  629. X2 = temp;
  630. //System.out.println(X1+ " " +X2);
  631. }
  632. if(Y1 > Y2)
  633. {
  634. temp = Y1;
  635. Y1 = Y2;
  636. Y2 = temp;
  637. ///System.out.println(Y1+ " " +Y2);
  638. }
  639. newRect.x = X1;
  640. newRect.y = Y1;
  641. newRect.width = Math.abs(X2-X1);
  642. newRect.height = Math.abs(Y2-Y1);
  643. //System.out.println(" WIDTH:" + newRect.width+ " " + "HEIGHT:" + newRect.height);
  644.  
  645.  
  646. addRect = true;
  647. paint(this.getGraphics());
  648. }
  649.  
  650.  
  651. public void drawRect(Graphics g)
  652. {
  653.  
  654. //System.out.println("In drawRect");
  655.  
  656. g.setColor(Color.PINK);
  657. g.fillRect(newRect.x, newRect.y, newRect.width, newRect.height);
  658. int i = 0;
  659. while (i < Walls.size())
  660. {
  661. g.setColor(Color.PINK);
  662. g.fillRect(Walls.elementAt(i).x, Walls.elementAt(i).y, Walls.elementAt(i).width, Walls.elementAt(i).height);
  663. i++;
  664. }
  665.  
  666.  
  667. addRect = false;
  668. }
  669.  
  670. public void storeRectangle()
  671. {
  672. Rectangle RectTemp = new Rectangle( newRect);
  673. if(!checkForBall(RectTemp))
  674. {
  675. System.out.print("Adding walls");
  676. Walls.add(RectTemp);
  677. System.out.print("Wall size:"+ Walls.size());
  678. }
  679. newRect.setBounds(0,0,0,0);
  680. paint(this.getGraphics());
  681. }
  682. public boolean checkForBall(Rectangle RectTemp)
  683. {
  684. boolean check = true;
  685. if(RectTemp.intersects(CurrPos.x, CurrPos.y, CurrSize, CurrSize))
  686. {
  687. System.out.println("IN CHECK FOR BALL");
  688. check = true;
  689. paint(this.getGraphics());
  690. }
  691. else
  692. {
  693. check = false;
  694. }
  695. return check;
  696. }
  697.  
  698. public void deleteRectangle(Point clk)
  699. {
  700. int i = 0;
  701. boolean deleted = false;
  702. while(i < Walls.size() && deleted == false)
  703. {
  704. System.out.println("INside the loop" + clk);
  705. if (Walls.elementAt(i).contains(clk))
  706. {
  707. System.out.println(Walls.elementAt(i).contains(clk));
  708. Walls.removeElementAt(i);
  709. deleted = true;
  710. }
  711. else
  712. {
  713. paint(this.getGraphics());
  714. i++;
  715. }
  716. System.out.println("After delete, wall size is: " + Walls.size());
  717. }
  718. addRect = true;
  719. paint(this.getGraphics());
  720. addRect = false;
  721. }
  722.  
  723.  
  724.  
  725. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement