Add another changeColor() method (i.e. you will now have 2 methods called "changeColor"). This one accepts an int parameter and changes the color based on that int. The valid colors are "red", "yellow", "green", "blue", "magenta" and "black". In your code, map each color to an integer (e.g. in my code 3 means green.) If the number passed to the method is not valid, change the color to red.
In the bounceTheBall() method, where you test for collisions
with top and bottom, add code to change the color of the ball each
time it hits the top or bottom (not the sides.) The new
color should be randomly decided. Use your new changeColor() method
to make the change.
Hint: The following code stores a random int
between 1 and 100 inside the integer variable "randomNumber".
randomNumber = (int)(Math.random()*100 + 1)
import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
/**
* A circle that can be manipulated and that draws itself on a
canvas.
*
* @author Derek Green (minor modification of the Circle class
written by Michael Kolling and David J. Barnes)
* @version 1.0 (Feb 15, 2004)
* Revised by William Smith Summer 2012
*/
public class Ball
{
private int diameter;
private int xPosition;
private int yPosition;
private String color;
private boolean isVisible;
/**
* Create a new ball at default position with default color.
*/
public Ball()
{
diameter = 30;
xPosition = 20;
yPosition = 20;
color = "blue";
isVisible = true;
draw();
}
/**
* Make this ball visible. If it was already visible, do
nothing.
*/
public void makeVisible()
{
isVisible = true;
draw();
}
/**
* Make this ball invisible. If it was already invisible, do
nothing.
*/
public void makeInvisible()
{
erase();
isVisible = false;
}
/**
* Move the circle horizontally by 'distance' pixels.
*/
public void moveHorizontal(int distance)
{
//erase();
xPosition += distance;
draw();
}
/**
* Move the circle vertically by 'distance' pixels.
*/
public void moveVertical(int distance)
{
//erase();
yPosition += distance;
draw();
}
/**
* Slowly move the ball horizontally by 'distance' pixels.
*/
public void slowMoveHorizontal(int distance)
{
int delta;
if(distance < 0)
{
delta = -1;
distance = -distance;
}
else
{
delta = 1;
}
for(int i = 0; i < distance; i++)
{
xPosition += delta;
draw();
}
}
/**
* Slowly move the ball vertically by 'distance' pixels.
*/
public void slowMoveVertical(int distance)
{
int delta;
if(distance < 0)
{
delta = -1;
distance = -distance;
}
else
{
delta = 1;
}
for(int i = 0; i < distance; i++)
{
yPosition += delta;
draw();
}
}
//-------------------- COSC 1010 LAB 4, add your work here
------------------------
//----------------------------------------------------------------------------------
// Danny Stanley
//Lab 4
//----------------------------------------------------------------------------------
/**
* Bounce the ball around the window.
*/
public void bounceTheBall(int time,int speed)
{
Canvas canvas = Canvas.getCanvas(); //DO NOT CHANGE
int windowWidth = canvas.getWidth();// the width of the window //DO
NOT CHANGE
int windowHeight = canvas.getHeight();// the height of the window
//DO NOT CHANGE
int deltaX = 1;// +/- velocity in x-direction //DO NOT CHANGE
int deltaY = 1;// +/- velocity in y-direction //DO NOT CHANGE
//-----------------------------------------------------------------------
//your speed validation code goes here
//----------------------------------------
if (speed > 1)
{
deltaX = 1 + speed;
deltaY = -1 + speed;
speed = speed + 1;
}
for(int i = 0; i < time; i++)// repeat the following process
//DO NOT CHANGE
{
//-----------------------------------------------------------------------
//your collision test code goes here
//test if the ball has collided with the top or bottom, if so
reflect it
//test if the ball has collided with a side, if so reflect it
//-
if (xPosition > 270)
{
deltaX = -deltaX;
}
if (xPosition < 0)
{
deltaX = -deltaX;
}
if (yPosition < 0)
{
deltaY = -deltaY;
}
if (yPosition > 570)
{
deltaY = -deltaY;
}
xPosition += deltaX;//moves the ball 1 deltaX unit in the
x-direction //DO NOT CHANGE
yPosition += deltaY;//moves the ball 1 deltaY unit in the
y-direction //DO NOT CHANGE
draw();// draws the ball at the new position //DO NOT CHANGE
}
}
//-----------------------------------------------------------------------
//add your changeColor method here.
//Valid colors will be in the range 1 - 6 for the six colors.
//You may decide which number (1-6) maps to which color.
//If the value is not in this range, make the circle red.
//-----------------------------------------------------------------------
//-------------------- END OF LAB 4 ------------------------------------------------
/**
* Change the color. Valid colors are "red", "yellow", "blue",
"green",
* "magenta" and "black".
*/
public void changeColor(String newColor)
{
if(newColor.equals("yellow") || newColor.equals("blue") ||
newColor.equals("green") || newColor.equals("magenta") ||
newColor.equals("black")){
color = newColor;
} else {
color = "red"; //make red the default
}
draw();
}
/*
* Draw the ball with current specifications on screen.
*/
private void draw()
{
if(isVisible) {
Canvas canvas = Canvas.getCanvas();
canvas.draw(this, color, new Ellipse2D.Double(xPosition,
yPosition,
diameter, diameter));
canvas.wait(10);
}
}
/*
* Erase the ball on screen.
*/
private void erase()
{
if(isVisible) {
Canvas canvas = Canvas.getCanvas();
canvas.erase(this);
}
}
Code
you method
public void changeColor(int Color)
{
if(Color>=1 && Color<=6)
{
if(Color==1)
color = "red";
else if(Color==2)
color = "yellow";
else if(Color==3)
color = "green";
else if(Color==4)
color = "blue";
else if(Color==5)
color = "magenta";
else if(Color==6)
color = "black";
} else {
color = "red"; //make red the default
}
draw();
}
your full code with method;
import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
/**
* A circle that can be manipulated and that draws itself on a
canvas.
*
* @author Derek Green (minor modification of the Circle class
written by Michael Kolling and David J. Barnes)
* @version 1.0 (Feb 15, 2004)
* Revised by William Smith Summer 2012
*/
public class Ball
{
private int diameter;
private int xPosition;
private int yPosition;
private String color;
private boolean isVisible;
/**
* Create a new ball at default position with default color.
*/
public Ball()
{
diameter = 30;
xPosition = 20;
yPosition = 20;
color = "blue";
isVisible = true;
draw();
}
/**
* Make this ball visible. If it was already visible, do
nothing.
*/
public void makeVisible()
{
isVisible = true;
draw();
}
/**
* Make this ball invisible. If it was already invisible, do
nothing.
*/
public void makeInvisible()
{
erase();
isVisible = false;
}
/**
* Move the circle horizontally by 'distance' pixels.
*/
public void moveHorizontal(int distance)
{
//erase();
xPosition += distance;
draw();
}
/**
* Move the circle vertically by 'distance' pixels.
*/
public void moveVertical(int distance)
{
//erase();
yPosition += distance;
draw();
}
/**
* Slowly move the ball horizontally by 'distance' pixels.
*/
public void slowMoveHorizontal(int distance)
{
int delta;
if(distance < 0)
{
delta = -1;
distance = -distance;
}
else
{
delta = 1;
}
for(int i = 0; i < distance; i++)
{
xPosition += delta;
draw();
}
}
/**
* Slowly move the ball vertically by 'distance' pixels.
*/
public void slowMoveVertical(int distance)
{
int delta;
if(distance < 0)
{
delta = -1;
distance = -distance;
}
else
{
delta = 1;
}
for(int i = 0; i < distance; i++)
{
yPosition += delta;
draw();
}
}
//-------------------- COSC 1010 LAB 4, add your work here
------------------------
//----------------------------------------------------------------------------------
// Danny Stanley
//Lab 4
//----------------------------------------------------------------------------------
/**
* Bounce the ball around the window.
*/
public void bounceTheBall(int time,int speed)
{
Canvas canvas = Canvas.getCanvas(); //DO NOT CHANGE
int windowWidth = canvas.getWidth();// the width of the window //DO
NOT CHANGE
int windowHeight = canvas.getHeight();// the height of the window
//DO NOT CHANGE
int deltaX = 1;// +/- velocity in x-direction //DO NOT CHANGE
int deltaY = 1;// +/- velocity in y-direction //DO NOT CHANGE
//-----------------------------------------------------------------------
//your speed validation code goes here
//----------------------------------------
if (speed > 1)
{
deltaX = 1 + speed;
deltaY = -1 + speed;
speed = speed + 1;
}
for(int i = 0; i < time; i++)// repeat the following process
//DO NOT CHANGE
{
//-----------------------------------------------------------------------
//your collision test code goes here
//test if the ball has collided with the top or bottom, if so
reflect it
//test if the ball has collided with a side, if so reflect it
//-
if (xPosition > 270)
{
deltaX = -deltaX;
}
if (xPosition < 0)
{
deltaX = -deltaX;
}
if (yPosition < 0)
{
deltaY = -deltaY;
}
if (yPosition > 570)
{
deltaY = -deltaY;
}
xPosition += deltaX;//moves the ball 1 deltaX unit in the
x-direction //DO NOT CHANGE
yPosition += deltaY;//moves the ball 1 deltaY unit in the
y-direction //DO NOT CHANGE
draw();// draws the ball at the new position //DO NOT CHANGE
}
}
//-----------------------------------------------------------------------
//add your changeColor method here.
//Valid colors will be in the range 1 - 6 for the six colors.
//You may decide which number (1-6) maps to which color.
//If the value is not in this range, make the circle red.
//-----------------------------------------------------------------------
//-------------------- END OF LAB 4 ------------------------------------------------
/**
* Change the color. Valid colors are "red", "yellow", "blue",
"green",
* "magenta" and "black".
*/
public void changeColor(String newColor)
{
if(newColor.equals("yellow") || newColor.equals("blue") ||
newColor.equals("green") || newColor.equals("magenta") ||
newColor.equals("black")){
color = newColor;
} else {
color = "red"; //make red the default
}
draw();
}
public void changeColor(int Color)
{
if(Color>=1 && Color<=6)
{
if(Color==1)
color = "red";
else if(Color==2)
color = "yellow";
else if(Color==3)
color = "green";
else if(Color==4)
color = "blue";
else if(Color==5)
color = "magenta";
else if(Color==6)
color = "black";
} else {
color = "red"; //make red the default
}
draw();
}
/*
* Draw the ball with current specifications on screen.
*/
private void draw()
{
if(isVisible) {
Canvas canvas = Canvas.getCanvas();
canvas.draw(this, color, new Ellipse2D.Double(xPosition,
yPosition,
diameter, diameter));
canvas.wait(10);
}
}
/*
* Erase the ball on screen.
*/
private void erase()
{
if(isVisible) {
Canvas canvas = Canvas.getCanvas();
canvas.erase(this);
}
}
If you have any query regarding the code please ask me in the
comment i am here for help you. Please do not direct thumbs down
just ask if you have any query. And if you like my work then please
appreciates with up vote. Thank You.
Add another changeColor() method (i.e. you will now have 2 methods called "changeColor"). This one accepts...
For this project, you will be working in on the Pacman game you created for Lab 6. See the solution to Lab 6 for starting files to this project. You should be working in a group of 3 or fewer students. Part 1: InheritanceCreate a new class called GameCharacter. Make GameCharacter a parent class of Pacman and Ghost. Make sure to move all common fields and methods from Pacman and Ghost to GameCharacter. Part 2: GameplayBegin the game with the...
import javax.swing.*; import java.awt.*; import java.util.List; import java.util.*; /** * Canvas is a class to allow for simple graphical drawing on a canvas. * This is a modification of the general purpose Canvas, specially made for * the BlueJ "shapes" example. * * @author: Bruce Quig * @author: Michael Kolling (mik) * Minor changes to canvas dimensions by William Smith 6/4/2012 * * @version: 1.6 (shapes) */ public class Canvas { // Note: The implementation of this class (specifically the...
import javax.swing.*; import java.awt.*; import java.util.List; import java.util.*; /** * Canvas is a class to allow for simple graphical drawing on a canvas. * This is a modification of the general purpose Canvas, specially made for * the BlueJ "shapes" example. * * @author: Bruce Quig * @author: Michael Kolling (mik) * Minor changes to canvas dimensions by William Smith 6/4/2012 * * @version: 1.6 (shapes) */ public class Canvas { // Note: The implementation of this class (specifically the...
In Java. CodebreakerUi.java Add member variables of type: Color colorSelected; Updated method initComponents() where it is instantiating the RoundButtons for the 1D array on the JPanel displaying the codebreaker’s colors; it should add the following Add action listener ColorListener to each round button Updated method initComponents() where it is instantiating the RoundButtons for the 2D array on the JPanel for the codebreaker’s attempt; it should add the following Add client property “row” to each RoundButton set to the current iteration...
You will write a class called Shoe.java Shoe.java should have Three instance variables String brand (cannot be blank) double size (from 5 to 12) int color (a number from 1 to 5 representing one of 5 colors) This code is to control the amount of colors. the colors represented are as follows 1. red 2. green 3. blue 4. black 5. grey One constructor, one get method per instance variable, one set method per instance variable. You will need a...
CodebreakerUi.java Add member variables of type: Color colorSelected; Updated method initComponents() where it is instantiating the RoundButtons for the 1D array on the JPanel displaying the codebreaker’s colors; it should add the following Add action listener ColorListener to each round button Updated method initComponents() where it is instantiating the RoundButtons for the 2D array on the JPanel for the codebreaker’s attempt; it should add the following Add client property “row” to each RoundButton set to the current iteration of the...
JAVA problem: Upgrade and extend the previous programs Draw.java and DrawCanvas.java used in the class to maintain a list of shapes drawn as follows: Replace and upgrade all the AWT components used in the programs to the corresponding Swing components, including Frame, Button, Label, Choice, and Panel. Add a JList to the left of the canvas to record and display the list of shapes that have been drawn on the canvas. Each entry in the list should contain the name...
Game Development: Uno For this assignment you will be creating the game of Uno (See the accompanying pdf for the instructions of the game). Your version will adhere to all the rules except that only the next player can issue a challenge against the previous player in regards to penalties, and your games must have at least three (3) players and at most nine (9) players. To begin, you must create the class Card which contains: Private string field named...
Keep the ball in bounds by adding conditional statements after the TODO comments in the paintComponent() method to check when the ball has hit the edge of the window. When the ball reaches an edge, you will need to reverse the corresponding delta direction and position the ball back in bounds. Make sure that your solution still works when you resize the window. Your bounds checking should not depend on a specific window size This is the java code that...
In this assignment, you will add several methods to the Binary Search Tree. You should have completed the following three methods in the lab: public void insert(Key key, Value value) public Value get(Key key) public void inorder(Node root) For this assignment, you will implement the following: public void remove(Node root, Key key) public Key getMin(Node n) public Key getMax(Node n) public int height(Node n) The main method contains the statements to check whether your implementation works. You need to change...