Question

In: Computer Science

In java processing 3, write a program that draws a circle. The center of the circle...

In java processing 3, write a program that draws a circle. The center of the circle would be first point on the canvas where the mouse is pressed on it. While the mouse is pressed, the circle can be drawn by moving the mouse cursor farther than the first point that mouse was pressed (circle center). As soon as the code is run, a timer at the bottom of the canvas should start working. Also, the radius of the circle cannot be more than 0.1 of the width of the canvas. If the player draws a large circle, the message "This is too big!" will be shown on the canvas. Still player can fix it and get back to game by moving the mouse towards the center of the circle (the point that first mouse was pressed on) and resume the game.

Solutions

Expert Solution

Answer:-

Width and height--

The width and height variables always contain a value that corresponds to the width and height of your sketch (in pixels). Generally, this will be the same as the value that you specify in the createCanvas() function.

The width and height variables are useful because they allow you to make sketches that take into account the size of the canvas.

Mouse position--

Processing provides two special variables, mouseX and mouseY, that contain the X and Y coordinates of the mouse cursor in the current frame. Remember that the code in draw() is running over and over again, very quickly: up to sixty times per second. A bit of pre-written code that is a part of the p5.js library calls this function over and over for you, and each time it does so, it updates the mouseX and mouseY variables with the current position of the mouse cursor on the screen. These two variables together make it easy to make your sketch react to user input in the form of mouse movement.

Here’s a quick example sketch that simply draws a circle underneath the current mouse position:

function setup() 
{
  createCanvas(400, 400);
}
function draw()
 {
  background(50);
  noFill();
  stroke(255);
  strokeWeight(8);
  ellipse(mouseX, mouseY, 45, 45);
}

Of course, you’re not limited to using the mouseX and mouseY to control the position of the elements that you draw. Here’s a sketch that uses the mouseX value to control the width of the stroke, and the mouseY value to control the size of the ellipse:

function setup() 
{
  createCanvas(400, 400);
}
function draw() 
{
  background(50);
  noFill();
  stroke(255);
  strokeWeight(mouseX / 10);
  ellipse(200, 200, mouseY, mouseY);
}

Mouse clicks--

Processing includes a special built-in variable called mouseIsPressed which holds the value true if the user is currently holding the mouse, and false otherwise. You can use this to make your sketch do different things depending on whether or not the user is holding down the mouse button.

function setup() 
{
  createCanvas(400, 400);
}
function draw() 
{
  background(50);
  noFill();
  stroke(255);
  strokeWeight(8);
  if (mouseIsPressed) 
{
    ellipse(200, 200, 300, 300);
  }
}


Related Solutions

Write a program that draws the circumscribed circle (also known as the circumcircle) of a given...
Write a program that draws the circumscribed circle (also known as the circumcircle) of a given triangle ABC; this circle passes through points A, B, and C. These points will be specified by the user by clicking the mouse button. Remember, the three perpendicular bisectors of the three edges of a triangle all pass through one point, the circumcenter, which is the center of the circumscribed circle.
Write a program that draws the circumscribed circle (also known as the circumcircle) of a given...
Write a program that draws the circumscribed circle (also known as the circumcircle) of a given triangle ABC; this circle passes through points A, B, and C. These points will be specified by the user by clicking the mouse button. Remember, the three perpendicular bisectors of the three edges of a triangle all pass through one point, the circumcenter, which is the center of the circumscribed circle.
In Java, Write a JavaFX application that draws multiple circles using a rubberbanding technique. The circle...
In Java, Write a JavaFX application that draws multiple circles using a rubberbanding technique. The circle size is determined by a mouse drag. Use the initial mouse press location as the fixed center point of the circle. Compute the distance between the current location of the mouse pointer and the center point to determine the current radius of the circle.Thank you and could you also show the finished product?
in a gui ' in java write a program that draws equal a simple fence with...
in a gui ' in java write a program that draws equal a simple fence with vertical, spaced slats backed by two boards. Behind the fence show a simple house support Make sure the in the und. house is visible between the slats in the fence.
Write a program that draws a fixed circle centered at (100, 60) with radius 50. Whenever...
Write a program that draws a fixed circle centered at (100, 60) with radius 50. Whenever the mouse is moved, display a message indicating whether the mouse point is inside the circle at the mouse point or outside of it. Write in Java please.
Write a program in java processing. Write a program that does the following: · Assume the...
Write a program in java processing. Write a program that does the following: · Assume the canvas size of 500X500. · The program asks the user to enter a 3 digit number. · The program then checks the value of the first and last digit of the number. · If the first and last digits are even, it makes the background green and displays the three digit number at the mouse pointer. · If the two digits are odd, it...
Write a java program that contains 3 overloaded static methods for calculating area of a circle,...
Write a java program that contains 3 overloaded static methods for calculating area of a circle, area of a cylinder and volume of a cylinder. Also create an output method which uses JOptionPaneto display instance field(s) and the result of the computing. Then code a driver class which will run and test calling each of these overloaded methods with hard-coded data and display the data and the result of the calculation by calling output method. Thanks!!
Write a JavaFX application that draws a circle using a rubberbanding technique. The circle size is...
Write a JavaFX application that draws a circle using a rubberbanding technique. The circle size is determined by a mouse drag. Use the initial mouse press location as the fixed center point of the circle. Compute the distance between the current location of the mouse pointer and the center point to determine the current radius of the circle.
(Java program) Write an applet that draws two animated cars that must race each other. The...
(Java program) Write an applet that draws two animated cars that must race each other. The race cars should move along a straight line, with each starting behind a Start Line. The user should have the option to set the speed of each car. The speed range is 0 to 100. There should be a Start button to begin the race and an End button to stop the race at any time.
java please Write a program that creates an ArrayList and adds 5 circle objects to the...
java please Write a program that creates an ArrayList and adds 5 circle objects to the list , and display all elements in the list by invoking the object’s toString() method.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT