Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

use java creates a circle moving from the top left corner to right corner in the

ID: 3606684 • Letter: U

Question

use java creates a circle moving from the top left corner to right corner in the bottomIf we don't use any button on keyboard. If we use right, left, up, down button, the circle will increase their speed and change the direction. Here are the tips.

As described in lecture you start from Yorld.iava (unchanged) BigBang.iva (unchanged) You then need to define a Game. java plus ancillary classes (if any) This is the starting template for your Game. java: import java. awt. *; import java. awt. event. * import java. util. *; public class Game implements World i public GameO public void draw(Graphics g) I public void teh0 public void meh (MouseEvent e) public void keh (KeyEvent e) l public boolean hasEndedO I return false; public void sayBye) public static void main (Stringl] args) [ BigBang b = new BigBang (new Game()) b.start 50, // delay 400 // size

Explanation / Answer

Hello, I have an answer for you. But please note that in your question, the details were not sufficient about the mouse events and program ending. Anyway I have implemented it according to my understandings.

//BigBang.java

import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.Graphics;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.KeyEvent;

import java.awt.event.KeyListener;

import java.awt.event.MouseEvent;

import java.awt.event.MouseListener;

import java.awt.event.MouseMotionListener;

import javax.swing.JComponent;

import javax.swing.JFrame;

import javax.swing.Timer;

public class BigBang extends JComponent implements MouseListener, MouseMotionListener, KeyListener, ActionListener {

                World world;

                Timer timer;

                public BigBang(World world) {

                                this.world = world;

                                this.addMouseListener(this);

                                this.addMouseMotionListener(this);

                                this.addKeyListener(this);

                                this.setFocusable(true);

                                this.requestFocus();

                                this.setBackground(Color.WHITE);

                }

                public void start(int delay, int size){

                                JFrame a =new JFrame();

                                a.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                                a.setLayout(new BorderLayout());

                                a.getContentPane().setBackground(Color.WHITE);

                                a.getContentPane().add(this,BorderLayout.CENTER);

                                //a.add(this);

                                a.setVisible(true);

                                a.setSize(size,size);                                                        

                                this.timer=new Timer(delay,this);

                                this.timer.start();

                               

                               

                }

               

                @Override

                public void keyPressed(KeyEvent e) {

                                this.world.keh(e);

                                this.repaint();

                               

                }

                @Override

                public void keyReleased(KeyEvent arg0) {

                                // TODO Auto-generated method stub

                               

                }

                @Override

                public void keyTyped(KeyEvent arg0) {

                                // TODO Auto-generated method stub

                               

                }

                @Override

                public void mouseDragged(MouseEvent e) {

                                this.world.meh(e);

                                this.repaint();

                               

                }

                @Override

                public void mouseMoved(MouseEvent arg0) {

                                // TODO Auto-generated method stub

                               

                }

                @Override

                public void mouseClicked(MouseEvent arg0) {

                                // TODO Auto-generated method stub

                               

                }

                @Override

                public void mouseEntered(MouseEvent arg0) {

                                // TODO Auto-generated method stub

                               

                }

                @Override

                public void mouseExited(MouseEvent arg0) {

                                // TODO Auto-generated method stub

                               

                }

                @Override

                public void mousePressed(MouseEvent e) {

                                this.world.meh(e);

                                this.repaint();

                               

                }

                @Override

                public void mouseReleased(MouseEvent e) {

                                this.world.meh(e);

                                this.repaint();

                               

                }

                @Override

                public void actionPerformed(ActionEvent arg0) {

                                if(this.world.hasEnded()){

                                                this.timer.stop();

                                                this.world.sayBye();

                                }else{

                                                this.world.teh();

                                }

                                this.repaint();

                }

                @Override

                public void paint(Graphics g) {

                                this.world.draw(g);

                                super.paint(g);

                               

                }

               

               

}

//Game.java

import java.awt.Graphics;

import java.awt.event.KeyEvent;

import java.awt.event.MouseEvent;

public interface World {

                void draw(Graphics g);

                void teh();

                void meh(MouseEvent e);

                void keh(KeyEvent e);

                boolean hasEnded();

                void sayBye();

}

//Game.java

import java.awt.Color;

import java.awt.Graphics;

import java.awt.event.KeyEvent;

import java.awt.event.MouseEvent;

public class Game implements World {

                int x,y,radius;

                int v_speed,h_speed;

                int max;

                Graphics g;

                public Game(int size) {

                                x=0;

                                y=0;

                                radius=100;

                                v_speed=1; //vertical speed

                                h_speed=1; //horizontal speed

                                max=size;

                }

                public static void main(String[] args) {

                                /**

                                * creating a BigBang object

                                */

                                BigBang b=new BigBang(new Game(400));

                                b.start(50, 400);

                }

                @Override

                public void draw(Graphics g) {

                                /**

                                * if the game is over, display a goodbye message, else, draw a circle at the x, y coordinates

                                */

                                this.g=g;

                                if(hasEnded()){

                                                sayBye();

                                }else{

                                                g.setColor(Color.RED);

                                                g.fillOval(x, y, radius, radius);

                                }

                               

                               

                               

                               

                }

                @Override

                public void teh() {

                                /**

                                * updating the speeds

                                */

                                x=x+h_speed;

                                y=y+v_speed;

                               

                               

                }

                @Override

                public void meh(MouseEvent e) {

                                /**

                                * mouse pressed and mouse released events

                                */

                                switch (e.getID()) {                         

                                case MouseEvent.MOUSE_PRESSED:

                                                int tmp_x=e.getX();

                                                int tmp_y=e.getY();

                                                if(x>tmp_x){

                                                                h_speed--;

                                                }else if(x<tmp_x){

                                                                h_speed++;

                                                }

                                                if(y>tmp_y){

                                                                v_speed--;

                                                }else if(y<tmp_y){

                                                                v_speed++;

                                                }

                                                break;

                                case MouseEvent.MOUSE_RELEASED:

                                                /**

                                                * placing the circle at the position

                                                */

                                                tmp_x=e.getX();

                                                tmp_y=e.getY();

                                                x=tmp_x;

                                                y=tmp_y;

                                                h_speed=0;

                                                v_speed=0;

                                                break;

                                default:

                                                break;

                                }

                               

                }

                @Override

                public void keh(KeyEvent e) {

                               

                                switch (e.getKeyCode()) {

                                case KeyEvent.VK_UP:

                                               

                                                v_speed--;

                                                if(v_speed<-10){ /*setting a maximum speed in up direction*/

                                                                v_speed=-10;

                                                }

                                                break;

                                case KeyEvent.VK_DOWN:

                                               

                                                v_speed++;

                                                if(v_speed>10){/*setting a maximum speed in down direction*/

                                                                v_speed=10;

                                                }

                                                break;

                                case KeyEvent.VK_LEFT:

                                               

                                                h_speed--;

                                                if(h_speed<-10){/*setting a maximum speed in down direction*/

                                                                h_speed=-10;

                                                }

                                                break;

                                case KeyEvent.VK_RIGHT:

                                               

                                                h_speed++;

                                                if(h_speed>10){/*setting a maximum speed in right direction*/

                                                                h_speed=10;

                                                }

                                                break;

                               

                                default:

                                                break;

                                }

                               

                }

                @Override

                public boolean hasEnded() {

                                if(x>max || y>max){

                                                /**

                                                * hit the right or bottom border

                                                */

                                                return true;

                                }

                                return false;

                }

                @Override

                public void sayBye() {

                                /**

                                * displaying the goodbye message

                                */

                                g.drawString("Thanks, Good bye! ", (max/2)-20, max/2);

                               

                }

}

/*output*/