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

• We will be removing the functionality of drawing Squares, Ellipses, Rectangles

ID: 3730899 • Letter: #

Question

• We will be removing the functionality of drawing Squares, Ellipses, Rectangles and Triangle (just comment those key events out) • The application draws Circles when the C key is pressed • For this application, as soon as the ball is drawn it should start moving o The Circles should bounce off the perimeter of the frame o The Circles of different color should bounce off each other, and when they do they should swap colors o The Circle of same Color will merge together by absorbing the smaller circle into the bigger circle. The size and the direction of the larger circle may not change. • ShapeDriver will now need a Timer, and will also need to implement the ActionListener interface o Add the following to you ShapeDriver class: import javax.swing.Timer; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class ShapeDriver implements ActionListener { private Timer timer public ShapeDriver() { // the second argument to the Timer Constructor takes an ActionListener // the this key word informs the JVM to look inside this class for // the actionPerformed method that must be overridden when // ActionListener is implemented // Every tick of the clock will now run the actionPerformed method timer = new Timer(1000/60, this); timer.start(); } // Method that must be implemented since the class implements ActionListener public void actionPerformed(ActionEvent e) { // move each circle // check if circle is in bounds, and bounce off the borders if need be // check if circle hits another circle of different color, // bounce circles off each other and swap colors // if circle of one color hits a circle of same color, remove smaller circle // call repaint this.repaint(); } } • Note: The x and y location is actually in the top left hand corner of the circle o This is how the AWT Graphics draws a circle o In the Circle class, I created another Point called center o When moving the location of the circle I also updated the center location this makes collision detection better than using the x and y location for drawing the circle o First just work towards using the location Point in the Shape class Once this is working, add the center Point to your Circle class and update its x and y value the same as you update location Then use center to calculate the distance o For example, in the Circle Class: I added Point center instance field I overrided the move() method from the Shape class I called super.move() inside of move() to do what the method was doing in the super class, then updated the center field local to the Circle class o Note: Remember subclasses do not have access to private super class fields and methods o Adhere to good Object Oriented Principles: keep data in classes private, and access with get and set methods only have methods public if other classes use them. Make them private if only the class needs it

Explanation / Answer

** ShapeDriver class to draw Ovals

import java.awt.Color;

import java.awt.Graphics;

import javax.swing.Timer;

import javax.swing.JComponent;

import java.util.Random;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

public class ShapeDriver extends JComponent implements ActionListener {

                private int x[] = new int[10];

                private int c_pos = 20;

                private Color colors[] = new Color[10];

                private int pos = -1;

               

                private Timer timer;

               

                private static final Random randomShape = new Random();

               

                // Draw oval

                public void paintComponent(Graphics g) {

                super.paintComponent(g);

                                for(int i=0; i<=pos; i++) {

                                                g.drawOval(60 * i + 10, c_pos, x[i], x[i]);

                                                g.fillOval(60 * i + 10, c_pos, x[i], x[i]);

                                                g.setColor(colors[i]);

                                }

                               

                }

                // Method called when user press C

                public void cPressed() {

                               

                                if(pos >= 9) {

                                                System.out.println("You can not add more circles");

                                                return;

                                } else {

                                                pos++;

                                                x[pos] = 10 + 5 * randomShape.nextInt( 10 );

                                                c_pos = 20;

                                                timer = new Timer(400, this);

                                                                timer.start();

                                                colors[pos] = new Color(randomShape.nextInt( 256 ), randomShape.nextInt( 256 ), randomShape.nextInt( 256 ));

                                                repaint();

                                }

                }

               

                // Method that must be implemented since the class implements ActionListener

                public void actionPerformed(ActionEvent e) {

                               

                                c_pos = c_pos + 1;

                               

                                this.repaint();

                }

}

** mainFrame To execute program

import java.awt.*;

import java.awt.event.KeyEvent;

import java.awt.event.KeyListener;

import javax.swing.JFrame;

public class mainFrame extends JFrame implements KeyListener{

                private ShapeDriver draw;

                // When user presse key

                public void keyPressed(KeyEvent e) {

                                if(e.getKeyCode() == KeyEvent.VK_C)

                                draw.cPressed();

                }

                public void keyReleased(KeyEvent e) {

                }

                public void keyTyped(KeyEvent e) {

                }

                // Implement key listener

                public mainFrame(){

                                this.draw=new ShapeDriver();

                                addKeyListener(this);

                                setFocusable(true);

                                setFocusTraversalKeysEnabled(false);

                }

                // Draw frame

                public static void main(String[] args) {

                                javax.swing.SwingUtilities.invokeLater(new Runnable() {

                                public void run() {

                                mainFrame frame = new mainFrame();

                                frame.setTitle("Circle Move");

                                frame.setResizable(false);

                                frame.setSize(700, 700);

                                frame.setMinimumSize(new Dimension(700, 700));

                                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                                frame.getContentPane().add(frame.draw);

                                frame.pack();

                                frame.setVisible(true);

                                }

                                });

                }

}