Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.70 KB | None | 0 0
  1. import javax.swing.JFrame;
  2. import javax.swing.JPanel;
  3. import javax.swing.WindowConstants;
  4. import java.awt.Dimension;
  5. import java.awt.Color;
  6. import java.awt.Graphics;
  7. import java.awt.Graphics2D;
  8. import java.awt.Point;
  9. import java.awt.event.MouseListener;
  10. import java.awt.event.MouseEvent;
  11. import java.awt.event.KeyAdapter;
  12. import java.awt.event.KeyEvent;
  13. import java.awt.Font;
  14. import java.awt.FontMetrics;
  15.  
  16. public class RunBubble {
  17. static int tsWidth = 600;
  18. static int tsHeight = 400;
  19. //This sets up the JFrame
  20. private JFrame frame;
  21. static long time = System.currentTimeMillis();
  22. public RunBubble() {
  23. frame = new JFrame("Bar Graph");
  24. frame.setSize(tsWidth, tsHeight);
  25. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  26. frame.setPreferredSize(frame.getSize());
  27. frame.add(new BubbleGame(frame.getSize()));
  28. frame.pack();
  29. frame.setVisible(true);
  30. }
  31.  
  32. public static void main(String... argv) {
  33. new RunBubble();
  34. }
  35.  
  36. public static class BubbleGame extends JPanel implements Runnable, MouseListener {
  37. /*
  38. * Declare Class Variables Here***********
  39. */
  40. private Thread animator;
  41. Bubble[] b = new Bubble[50];//array of bubbles created
  42.  
  43. int teamNum = 0;
  44. boolean move = true;
  45. boolean wipe = false;
  46. int sWidth = tsWidth;//width annd height of the screen
  47. int sHeight = tsHeight;
  48.  
  49. public BubbleGame(Dimension dimension) {
  50. setSize(dimension);
  51. setPreferredSize(dimension);
  52. addMouseListener(this);
  53. addKeyListener(new TAdapter());
  54. setFocusable(true);
  55.  
  56. //Array of Bubbles initialize with default values
  57. for(int i = 0; i<b.length; i++){
  58. Bubble bub = new Bubble();
  59. b[i] = bub;
  60. }
  61. //for animating the screen - you won't need to edit
  62. if (animator == null) {
  63. animator = new Thread(this);
  64. animator.start();
  65. }
  66.  
  67. setDoubleBuffered(true);
  68.  
  69. }
  70.  
  71. @Override
  72. public void paintComponent(Graphics g) {
  73. Graphics2D g2 = (Graphics2D)g;//g2 is the graphics object that we need to use
  74. //to draw things to the screen
  75. Dimension d = getSize();
  76. //create a background
  77. g2.setColor(Color.white);
  78. g2.fillRect(0, 0, d.width, d.height);
  79. /*team functions are called based on number entered from keyboard*/
  80. if(teamNum==0)
  81. runTeam0(g2);
  82.  
  83. if(teamNum==1)
  84. runTeam1(g2);
  85. if(teamNum==2)
  86. runTeam2(g2);
  87.  
  88. if(teamNum==3)
  89. runTeam3(g2);
  90.  
  91. if(teamNum==4)
  92. runTeam4(g2);
  93.  
  94. if(teamNum==5)
  95. runTeam5(g2);
  96.  
  97. //TEXT EXAMPLE BELOW
  98. //Color
  99. Color purple= new Color(102,0, 102); //instance variable purple
  100. //display Text
  101. g2.setColor(purple);
  102.  
  103. //for Text click here
  104. g2.setFont (new Font("TimesRoman", Font.PLAIN, 20));
  105. g2.drawString("Team: " + teamNum , 10,30);//text to display, x and y coordinates
  106.  
  107. }//end of paint
  108.  
  109. //TEAM METHODs BELOW*************************
  110. public void runTeam0(Graphics g){
  111. Graphics2D g2 = (Graphics2D)g;
  112. moveAround();
  113. wallBounce();
  114. g2.setColor(b[1].c);
  115. g2.drawOval(b[1].x-b[1].r,b[1].y-b[1].r,b[1].r,b[1].r);
  116. g2.fillOval(b[1].x-b[1].r,b[1].y-b[1].r,b[1].r,b[1].r);
  117.  
  118. }
  119.  
  120. public void runTeam1(Graphics g){
  121. Graphics2D g2 = (Graphics2D)g;
  122.  
  123.  
  124. }
  125.  
  126. public void runTeam2(Graphics g){
  127. Graphics2D g2 = (Graphics2D)g;
  128.  
  129. }
  130.  
  131. public void runTeam3(Graphics g){
  132. Graphics2D g2 = (Graphics2D)g;
  133.  
  134. //System.out.println(time);
  135.  
  136. if(move == true) {
  137. moveAround();
  138.  
  139. wallBounce();
  140. }
  141. for(int i = 0; i<10;i++) {
  142. g2.setColor(b[i].c);
  143. g2.drawOval(b[i].x-b[i].r,b[i].y-b[i].r,b[i].r,b[i].r);
  144. g2.fillOval(b[i].x-b[i].r,b[i].y-b[i].r,b[i].r,b[i].r);
  145.  
  146.  
  147. }
  148.  
  149.  
  150. long s = System.currentTimeMillis();
  151. int q = (int) ((s- time)/1000);
  152. System.out.println(q);
  153. if(q%3==0)
  154. {
  155. for(int i =0;i<b.length;i++) {
  156.  
  157. b[i].c =new Color(b[i].produceRandom(0, 255),b[i].produceRandom(0, 255),b[i].produceRandom(0, 255));
  158. }
  159. }
  160.  
  161.  
  162. if(wipe==true) {
  163. for(int i = 0; i<b.length;i++) {
  164. b[i]=new Bubble();
  165. b[i].show = false;
  166. b[i].x=getX();
  167. b[i].y=getY();
  168. }
  169. wipe=false;
  170. }
  171.  
  172.  
  173.  
  174.  
  175.  
  176.  
  177.  
  178. }
  179.  
  180. public void runTeam4(Graphics g){
  181. Graphics2D g2 = (Graphics2D)g;
  182.  
  183. }
  184.  
  185. public void runTeam5(Graphics g){
  186. Graphics2D g2 = (Graphics2D)g;
  187.  
  188. }
  189.  
  190. //TEAM METHODs ABOVE*************************
  191.  
  192.  
  193. //Team 0 - MR. MEMMO created
  194. //moves balls around the screen according to x and y speed
  195. public void moveAround(){
  196. for(int i = 0; i<b.length; i++){
  197. if(b[i].show==true){
  198. b[i].x += b[i].xSpeed;
  199. b[i].y += b[i].ySpeed;
  200. }
  201.  
  202. }
  203. }
  204. //Team 0 - MR. MEMMO created
  205. //controls bouncing of bubbles off the walls
  206. public void wallBounce(){
  207. for(int i = 0; i<b.length; i++){
  208. if(b[i].show==true){
  209. b[i].x += b[i].xSpeed;
  210. b[i].y += b[i].ySpeed;
  211. }
  212. if(b[i].x-b[i].r<0 || b[i].x + (b[i].r) > sWidth){
  213. b[i].xSpeed *= -1;
  214. }
  215.  
  216. if(b[i].y-b[i].r<0 || b[i].y + (b[i].r)>sHeight){
  217. b[i].ySpeed *= -1;
  218. }
  219. }
  220.  
  221. }
  222.  
  223.  
  224.  
  225. //you can add mouse interactions via this function
  226. //team 0 has an example
  227. public void mousePressed(MouseEvent e) {
  228. int x = e.getX();
  229. int y = e.getY();
  230. //team 0
  231. if(teamNum==0){
  232. b[1] = new Bubble();
  233. b[1].show=true;
  234. b[1].x=x;
  235. b[1].y=y;
  236. }else if(teamNum==3) {
  237. for(int i = 0; i<10;i++) {
  238. b[i]=new Bubble();
  239. b[i].show=true;
  240. b[i].x=x;
  241. b[i].y=y;
  242. }
  243. }
  244.  
  245. }
  246.  
  247. public void mouseReleased(MouseEvent e) {
  248. }
  249.  
  250. public void mouseEntered(MouseEvent e) {
  251. }
  252.  
  253. public void mouseExited(MouseEvent e) {
  254. }
  255.  
  256. public void mouseClicked(MouseEvent e) {
  257. }
  258.  
  259.  
  260. //get input from keyboard
  261. private class TAdapter extends KeyAdapter {
  262.  
  263.  
  264. public void keyReleased(KeyEvent e) {
  265. int keyr = e.getKeyCode();
  266.  
  267. }
  268.  
  269. public void keyPressed(KeyEvent e) {
  270. int key = e.getKeyCode();
  271. System.out.println( key);
  272.  
  273.  
  274. if(key==48){//num 0
  275. teamNum=0;
  276. }
  277.  
  278. if(key==70) {
  279.  
  280. if(move == true) {
  281. move = false;
  282. }
  283. else {
  284. move = true;
  285. }
  286. }
  287. if(key==82) {
  288.  
  289. if(wipe == false) {
  290. wipe =true;
  291. }
  292.  
  293. }
  294.  
  295.  
  296. if(key==49){//num 1
  297. teamNum=1;
  298. }
  299.  
  300. if(key==50){
  301. teamNum=2;
  302. }
  303.  
  304. if(key==51){
  305. teamNum=3;
  306. }
  307.  
  308. if(key==52){
  309. teamNum=4;
  310. }
  311.  
  312. if(key==53){
  313. teamNum=5;
  314. }
  315.  
  316. // message = "Key Pressed: " + e.getKeyCode();
  317.  
  318. }
  319. }//end of adapter
  320.  
  321.  
  322. //Below controls the speed of the animation
  323.  
  324. public void run() {
  325. long beforeTime, timeDiff, sleep;
  326. beforeTime = System.currentTimeMillis();
  327. int animationDelay = 35;
  328. long time = System.currentTimeMillis();
  329. System.out.println(time);
  330. while (true) {// infinite loop
  331. // spriteManager.update();
  332. repaint();
  333. try {
  334. time += animationDelay;
  335. Thread.sleep(Math.max(0, time - System.currentTimeMillis()));
  336. } catch (InterruptedException e) {
  337. System.out.println(e);
  338. } // end catch
  339. } // end while loop
  340. }// end of run
  341.  
  342.  
  343.  
  344. }
  345. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement