Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

Programming II (CS300) Spring 2023

P02 Dancing Badgers I

Overview

This assignment involves developing a graphical application to simulate badgers dance show on a basketball arena. We are going to continue adding features to this program in p03 and p05. So, be sure to keep your code clean, clear, and well commented.

For this week, we’ll focus on developing an application named Dancing  Badgers  I that allows the user to add or remove a number of badgers to the display window. Users can move them arbitrary within the display window using the mouse.  The Graphical User Interface (GUI) application will be written using a provided PApplet object and PImage objects defined within the processing library. Fig. 1 shows an example of what this program might look like when it is done.

Figure 1: Dancing Badgers Display Window

Learning Objectives

The goals of this assignment include:

• Practice how to create and use objects,

• Develop the basis for creating an interactive graphical application,

• Give you experience working with callback methods to define how your program responds to mouse or key-based input.

Grading Rubric

5 points

Pre-Assignment Quiz: The P02 pre-assignment quiz is accessible through Canvas before having access to this specification by 11:59PM on Sunday 02/05/2023.  Access to the pre-assignment quiz will be unavailable passing its deadline.

25 points

Immediate Automated Tests: Upon submission of your assignment to Gradescope,  you  will  receive  feedback  from  automated  grading  tests about whether specific parts of your submission conform to this write-up specification. If these tests detect problems in your code, they will attempt to give you some feedback about the kind of defect that they noticed. Note that passing all of these tests does NOT mean your program is otherwise correct. To become more confident of this, you should run additional tests of your own.

20 points

Additional Automated Tests: When your manual grading feedback appears on Gradescope, you will also see the feedback from these additional automated grading tests. These tests are similar to the Immediate Automated Tests, but may test different parts of your submission in different ways.

Assignment Requirements and Notes

• Pair programming is NOT ALLOWED for this assignment. You MUST complete and submit your p02 individually.

• The ONLY import statements that you may include in this assignment are:

import  java .util .Random;

import  java.io.File ;

import processing .core .PApplet;

import processing .core .PImage;

• This may be your first experience with developing a graphic application using the java processing library. Make sure to read carefully through the specification provided in this write-up. This assignment requires clear understanding of its instructions. Read TWICE the instructions and do not hesitate to ask for clarification on piazza if you find any ambiguity.

• The automated grading tests in gradescope are NOT using the full processing library when grading your code. They only know about the following fields and methods (referenced directly from this assignment). The only methods that you are allowed to use from the  PImage and PApplet classes defined in the processing core library are the following:

two fields from PImage: int width, and int height.

three methods from PApplet: PImage loadImage(String), void image(PImage,int,int),

and void main(String).

If you are using any other fields, methods, or classes from the PImage or PApplet classes, this will cause problems for the automated grading tests.

• You MUST NOT add any additional fields either instance or static to your program, and any public methods either static or instance to your program, other than those defined in this write-up.

• You CAN define local variables that you may need to implement the methods defined in this program.

• You CAN define private static methods to help implement the different public static methods defined in this program, if needed.

• Any source code provided in this specification may be included verbatim in your program without attribution.

NONE of the callback methods (setup(),  draw(), mousePressed(),  and  keyPressed() defined later in this write-up should be called explicitly in this program.

• You MUST NOT add any additional fields either instance or static to your program, and any public methods either static or instance to your program, other than those defined in this write-up.

• DO NOT add any statement to the provided main() method.

• All the methods in this programming assignment MUST be static.

• There is NO tester class to be implemented for this assignment.  You can print some messages to the console to help you debugging some methods.

• You MUST adhere to the Academic Conduct Expectations and Advice

1 GETTING STARTED

To get started, let’s first create a new Java17 project within Eclipse. You can name this project whatever you like, but “p02 Dancing Badgers” is a descriptive choice. Then, create a new class named DancingBadgers with a public static void main(String[] args) method stub. This class represents the main class in your program. DO NOT include a package statement at the top of your DancingBadgers class (leave it in the default package). The DancingBadgers.java source file will be the only file that you submit for grading through Gradescope.

1.1 Download p2core.jar file and add it to your project build path

We have prepared a jar file that contains the processing library,  along with a few extra object types to help you build this and future assignments.   Download this p2core.jar file and copy it into the project folder that you just created.  Then, right-click on this file in the “Package Explorer” within Eclipse, choose “Build Path” and then “Add to Build Path” from the menu.  Note: If the .jar file is not immediately visible within Eclipse’s Package Explorer, try right-clicking on your project folder and selecting “Refresh”.

(Note that for Chrome users on MAC, Chrome may block the the jar file and incorrectly         reports it as a malicious file. To be able to copy the downloaded jar file, Go to“chrome://downloads/” and click on “Show in folder” to open the folder where your jar file is located.)

If the Build Path entry is missing when you right click on the jar file in the Package Explorer”, follow the next set of instructions to add the jar file to the build path:

1. Right-click on the project and choose “Properties”.

2. Click on the Java Build Path” option in the left side menu.

3. From the Java Build Path window, click on the “Libraries” Tab.

4. You can add the “p2core.jar”file located in your project folder by clicking “Add JARs...” from the right side menu.

5. Click on “Apply” button.

This operation is illustrated in Fig. 2 (a).

1.2 Download images for your Dancing Badgers application

Create a folder called  images within your project folder in Eclipse:  this can be done by right clicking your project in the  “Project Explorer” and selecting New  >  Folder .  Then, download these two images and copy them into your new ”images” folder: background.png, and badger.png by following the links, then right-clicking on the images, and selecting “Save

(a) Adding p2core.jar to build path

(b) P02 package explorer

Figure 2: Dancing Badgers Application - Getting Started

Image as. . . ”. If you don’t see them in Eclipse immediately, try right-clicking on your project folder and selecting “Refresh”.

The organization of your p02 project through eclipse’s package explorer is illustrated by Fig.2(b).

1.3 Check your project setup

Now, to test that the p2core.jar file library is added appropriately to the build path of your project, try running your program with the following method being called from the main() method.

Utility .runApplication();  //  starts  the  application

Note that you MUST NOT add any additional statements to your DancingBadgers .main() method. It should contain the only above statement (a call of Utility.runApplication method).

If everything is working correctly,  you should see a blank window that appears with the title, “P2 Dancing Badgers” as depicted in Fig.3 along with an error message in the console which we’ll resolve in the next steps:  ERROR:  Could not  find method named  setup  that can  take  arguments  []  in  class  DancingBadgers .

Please consult piazza or one of the consultants, if you have any problems with this setup before proceeding.  Note that the processing library provided within the jar file (p2core.jar) works with java 17.  If you work with another version of java, you must switch to java 17 for this assignment.

Figure 3: DancingBadgers - Blank Screen Window

Note that the runApplication() method from the provided Utility class, provided in the p2core jar file, creates the main window for the application, and then repeatedly updates its appearance and checks for user input. It also checks if specific callback methods have been defined in the DancingBadgers class. Callback methods specify additional computation that should happen when the program begins, the user pressed a key or a mouse button, and every time the display window is repeatedly redrawn to the screen.

2 Utility Framework and Overview of the class Badger

All of the methods within the provided p2core.jar file are documented in these javadocs. Note that the Utility .runApplication() creates the display window, sets its dimension, and checks for callback methods. A callback method specifies additional computation that should happen in response to specific events.

2.1 Overview of Callback Methods

In this assignment, we are going to implement the following callback methods.

• setup() method: This method is automatically called by Utility.runApplication() when the program begins.  It creates and initializes the different data fields defined in your program,  and configures the different graphical settings of your application,  such as loading the background image, setting the dimensions of the display window, etc.

• draw() method:  This method continuously executes the lines of code contained inside its block until the program is stopped.  It continuously draws the application display window and updates its content with respect to any change or any event which affects its appearance.

• mousePressed():  The block of code defined in this method is automatically executed each time the mouse bottom is pressed.

• keyPressed(): The block of code defined in this method is automatically executed each time a key is pressed.

Note that NONE of the above callback methods should be called explicitly in your program. They are automatically called by every processing programs in response to specific events as described above.  You can read through the documentation of the Utility class and have an idea about the above callback methods and the other methods which can be used to draw images and circles to the screen.  You can also read through some of the other methods that are available through this Utility class.  Note that many of these methods take floats rather double values as arguments. Any time that you need to express a float constant in Java, you should use a lower case f to distinguish its type: 1.3 is a double, but 1.3f is a float. Note that int values passed as float such as 10 can be automatically converted by the compiler to 10f.

2.2 Overview of the class Badger

The Badger class represents the data type for droplet objects that will be created and used in our DancingBadgers application. The documentation for this class is provided here. Make sure to read the description of the constructor and the different methods implemented in the Badger class carefully. You do not need to implement this class or any of its declared methods. The class Badger is entirely provided for you in the p2core.jar file. Its implementation details are hidden for you as users of that class.  All the important information required to use its public methods appropriately are provided in its javadocs.

3 Visualizing the Dancing Badgers Display Window

3.1 Declare DancingBadgers Static Fields

Add the following static fields (class variables) to your DancingBadgers class. Put them outside of any method including, the main(). The top of the class body is be a good placement where to declare them.

private

static

PImage backgroundImage; // PImage object that represents the // background image

private

static

Badger[] badgers; // perfect size array storing the badgers // present in this application

private

static

Random randGen; // Generator of random numbers

To avoid compile without errors, import java .util .Random, processing .core .PApplet, and processing.core.PImage classes aat the top of your DancingBadgers.java source file.

Recall that you should not add any other variable (instance or static data field) to the DancingBadgers class. You can define local variables that you may need to implement the methods that will be         defined in this program.

3.2 Define the setup and draw callback methods

At the end of the last step we saw an error related to the fact that our DancingBadgers class was missing a method called setup(). Let’s solve that problem.

• Create a public static method with named setup that takes no input arguments and has no return value.

• Next, run your program.  This should lead to a slightly different error message being displayed:  "ERROR:  Could not  find method named  draw  that  can  take  arguments []  in  class  DancingBadgers . "

• We can now fix this error in a similar way, by creating a new public static method named draw, which also takes no input arguments and has no return value.

• If you look at the documentation for the Utilityclass, you can see how the runApplication method is making use of the setup and draw methods that we have just created. Recall that these methods should not be called directly from your code.  They should only be called by the Utility class, as a result of your calling Utility.runApplication().

• To convince yourself that the setup() method is being called once when your program starts up, try adding a print statement to the setup() method, then run your program. Note that the output of your System.out.println() print statement will be displayed in the console and not on the graphic display window. After this test, you can remove the print statement.

• To convince yourself that the draw() method is being repeatedly called by the Utility class as a result of your Utility.runApplication() call, try adding a print statement to the draw() method.  Note that the text message will be repeatedly printed to the console until you terminate the program.  After this test, you can remove the print statement before proceeding.

3.3 Set the background color

• First, let’s initialize the randGen field to a new Random object within your setup() method. You do not need to use a specific seed when instantiating this random number generator, although doing so may be helpful debugging your code

• Then, within the setup() method, pass a randomly generated int value to Utility .background(). You can read about the nextInt() method here.

• Now, every time you run this program, the background will be cleared to a different random color.

• Although randomly chosen colors can be fun, let’s specify some particular colors that we’ll continue to use going forward. We are going to use the Utility .color() method to specify the mix of red, green, and blue light that make up the colors that we’ll want to use here.

Comment or delete the line of code calling Utility .background() method from the setup() method.

We are going to use Utility .color(255,218,185) for the background color.

Add the Utility .background(Utility .color(255,218,185)) method call to the draw() method.

• Before proceeding, check that your setup() method contains only one line of code which initializes the randGen data field to a new Random object.

3.4 Draw the background image at the top of the display window

To draw a background image at the top of the display window, follow the next instructions.

1. Make sure that you have a folder called“images”in your Package Explorer” and that it contains two images background.png and badger.png.  If it is not the case, go up to ”Download images for your Dancing Badgers application” subsection1.2 in the Getting Started section.  In the setup() method, load the background image and store its reference into the backgroundImage variable of type PImage by calling the method Utility .loadImage() as follows.

backgroundImage = Utility .loadImage("images" + File .separator + "background .png");

2. To avoid compile without errors, check that java .io .File and processing .core .PImage classes are already imported at the top of your DancingBadgers.java source file. If you run your program now, you may notice that even though the background image was loaded in the setup() method, it is not yet drawn at the center of the screen.

3. To draw the background image to the screen, we need to call the Utility .image() method from the draw() method. The Utility .image() method draws a PImage object at a given position of the screen.  Please read the javadocs of the Utilityclass for more details. You can find below an example of code that draws an image at the center of the display window.

//  Draw  the  background  image  to  the  center  of  the  screen

Utility .image(backgroundImage, Utility .width()/2, Utility .height()/2);

• Note that the position (0,  0) refers to the upper left corner of the display window.

• The position  (Utility .width(), Utility .height()) refers to the bottom right corner of the display window.

• The position (Utility .width()/2, Utility .height()/2) refers to the center of the display window.

4. Notice the importance of running the above line of code within the draw() method after calling the Utility .background() method, instead of before.  Utility .background() clears the display window each time it is called as first line of code within the draw() method.

Running your program now should result in a window that looks like the one shown in Fig.4.

The processing library defines the PImage class as a datatype used for storing images. A PImage