Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- import javax.swing.WindowConstants;
- import java.awt.Dimension;
- import java.awt.Color;
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import java.awt.Point;
- import java.awt.event.MouseListener;
- import java.awt.event.MouseEvent;
- import java.awt.event.KeyAdapter;
- import java.awt.event.KeyEvent;
- import java.awt.Font;
- import java.awt.FontMetrics;
- public class RunBubble {
- static int tsWidth = 600;
- static int tsHeight = 400;
- //This sets up the JFrame
- private JFrame frame;
- static long time = System.currentTimeMillis();
- public RunBubble() {
- frame = new JFrame("Bar Graph");
- frame.setSize(tsWidth, tsHeight);
- frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
- frame.setPreferredSize(frame.getSize());
- frame.add(new BubbleGame(frame.getSize()));
- frame.pack();
- frame.setVisible(true);
- }
- public static void main(String... argv) {
- new RunBubble();
- }
- public static class BubbleGame extends JPanel implements Runnable, MouseListener {
- /*
- * Declare Class Variables Here***********
- */
- private Thread animator;
- Bubble[] b = new Bubble[50];//array of bubbles created
- int teamNum = 0;
- boolean move = true;
- boolean wipe = false;
- int sWidth = tsWidth;//width annd height of the screen
- int sHeight = tsHeight;
- public BubbleGame(Dimension dimension) {
- setSize(dimension);
- setPreferredSize(dimension);
- addMouseListener(this);
- addKeyListener(new TAdapter());
- setFocusable(true);
- //Array of Bubbles initialize with default values
- for(int i = 0; i<b.length; i++){
- Bubble bub = new Bubble();
- b[i] = bub;
- }
- //for animating the screen - you won't need to edit
- if (animator == null) {
- animator = new Thread(this);
- animator.start();
- }
- setDoubleBuffered(true);
- }
- @Override
- public void paintComponent(Graphics g) {
- Graphics2D g2 = (Graphics2D)g;//g2 is the graphics object that we need to use
- //to draw things to the screen
- Dimension d = getSize();
- //create a background
- g2.setColor(Color.white);
- g2.fillRect(0, 0, d.width, d.height);
- /*team functions are called based on number entered from keyboard*/
- if(teamNum==0)
- runTeam0(g2);
- if(teamNum==1)
- runTeam1(g2);
- if(teamNum==2)
- runTeam2(g2);
- if(teamNum==3)
- runTeam3(g2);
- if(teamNum==4)
- runTeam4(g2);
- if(teamNum==5)
- runTeam5(g2);
- //TEXT EXAMPLE BELOW
- //Color
- Color purple= new Color(102,0, 102); //instance variable purple
- //display Text
- g2.setColor(purple);
- //for Text click here
- g2.setFont (new Font("TimesRoman", Font.PLAIN, 20));
- g2.drawString("Team: " + teamNum , 10,30);//text to display, x and y coordinates
- }//end of paint
- //TEAM METHODs BELOW*************************
- public void runTeam0(Graphics g){
- Graphics2D g2 = (Graphics2D)g;
- moveAround();
- wallBounce();
- g2.setColor(b[1].c);
- g2.drawOval(b[1].x-b[1].r,b[1].y-b[1].r,b[1].r,b[1].r);
- g2.fillOval(b[1].x-b[1].r,b[1].y-b[1].r,b[1].r,b[1].r);
- }
- public void runTeam1(Graphics g){
- Graphics2D g2 = (Graphics2D)g;
- }
- public void runTeam2(Graphics g){
- Graphics2D g2 = (Graphics2D)g;
- }
- public void runTeam3(Graphics g){
- Graphics2D g2 = (Graphics2D)g;
- //System.out.println(time);
- if(move == true) {
- moveAround();
- wallBounce();
- }
- for(int i = 0; i<10;i++) {
- g2.setColor(b[i].c);
- g2.drawOval(b[i].x-b[i].r,b[i].y-b[i].r,b[i].r,b[i].r);
- g2.fillOval(b[i].x-b[i].r,b[i].y-b[i].r,b[i].r,b[i].r);
- }
- long s = System.currentTimeMillis();
- int q = (int) ((s- time)/1000);
- System.out.println(q);
- if(q%3==0)
- {
- for(int i =0;i<b.length;i++) {
- b[i].c =new Color(b[i].produceRandom(0, 255),b[i].produceRandom(0, 255),b[i].produceRandom(0, 255));
- }
- }
- if(wipe==true) {
- for(int i = 0; i<b.length;i++) {
- b[i]=new Bubble();
- b[i].show = false;
- b[i].x=getX();
- b[i].y=getY();
- }
- wipe=false;
- }
- }
- public void runTeam4(Graphics g){
- Graphics2D g2 = (Graphics2D)g;
- }
- public void runTeam5(Graphics g){
- Graphics2D g2 = (Graphics2D)g;
- }
- //TEAM METHODs ABOVE*************************
- //Team 0 - MR. MEMMO created
- //moves balls around the screen according to x and y speed
- public void moveAround(){
- for(int i = 0; i<b.length; i++){
- if(b[i].show==true){
- b[i].x += b[i].xSpeed;
- b[i].y += b[i].ySpeed;
- }
- }
- }
- //Team 0 - MR. MEMMO created
- //controls bouncing of bubbles off the walls
- public void wallBounce(){
- for(int i = 0; i<b.length; i++){
- if(b[i].show==true){
- b[i].x += b[i].xSpeed;
- b[i].y += b[i].ySpeed;
- }
- if(b[i].x-b[i].r<0 || b[i].x + (b[i].r) > sWidth){
- b[i].xSpeed *= -1;
- }
- if(b[i].y-b[i].r<0 || b[i].y + (b[i].r)>sHeight){
- b[i].ySpeed *= -1;
- }
- }
- }
- //you can add mouse interactions via this function
- //team 0 has an example
- public void mousePressed(MouseEvent e) {
- int x = e.getX();
- int y = e.getY();
- //team 0
- if(teamNum==0){
- b[1] = new Bubble();
- b[1].show=true;
- b[1].x=x;
- b[1].y=y;
- }else if(teamNum==3) {
- for(int i = 0; i<10;i++) {
- b[i]=new Bubble();
- b[i].show=true;
- b[i].x=x;
- b[i].y=y;
- }
- }
- }
- public void mouseReleased(MouseEvent e) {
- }
- public void mouseEntered(MouseEvent e) {
- }
- public void mouseExited(MouseEvent e) {
- }
- public void mouseClicked(MouseEvent e) {
- }
- //get input from keyboard
- private class TAdapter extends KeyAdapter {
- public void keyReleased(KeyEvent e) {
- int keyr = e.getKeyCode();
- }
- public void keyPressed(KeyEvent e) {
- int key = e.getKeyCode();
- System.out.println( key);
- if(key==48){//num 0
- teamNum=0;
- }
- if(key==70) {
- if(move == true) {
- move = false;
- }
- else {
- move = true;
- }
- }
- if(key==82) {
- if(wipe == false) {
- wipe =true;
- }
- }
- if(key==49){//num 1
- teamNum=1;
- }
- if(key==50){
- teamNum=2;
- }
- if(key==51){
- teamNum=3;
- }
- if(key==52){
- teamNum=4;
- }
- if(key==53){
- teamNum=5;
- }
- // message = "Key Pressed: " + e.getKeyCode();
- }
- }//end of adapter
- //Below controls the speed of the animation
- public void run() {
- long beforeTime, timeDiff, sleep;
- beforeTime = System.currentTimeMillis();
- int animationDelay = 35;
- long time = System.currentTimeMillis();
- System.out.println(time);
- while (true) {// infinite loop
- // spriteManager.update();
- repaint();
- try {
- time += animationDelay;
- Thread.sleep(Math.max(0, time - System.currentTimeMillis()));
- } catch (InterruptedException e) {
- System.out.println(e);
- } // end catch
- } // end while loop
- }// end of run
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement