Write a program that changes the color of a circle based on whether the mouse cursor is in the top half of the display or the bottom half. Here are the specific requirements. a. Set the display size to 600x400 pixels b. Using the line function, draw a horizontal line that is centered vertically from the left side to the right side of the display c. Using the ellipse function, draw a circle of size 100x100 anywhere on the display d. If the mouse cursor is in the top half of the display, the color of the circle should be blue [hint: use fill(0,0,255)] e. If the mouse cursor is in the bottom half of the display, the color of the circle should be red [hint: use fill(255,0,0)]
The code below uses pygame library of python to draw elements and achieve the mentioned objective.
The code is well commented to understand the functioning. Following are some points that would clarify understanding:
1. Pygame considers the Origin (0,0) at Top-Left Corner
2. x-coordinates increase from left to right and y-coordinates from top to bottom.
3. The function of ellipse plotting requires 4 parameters (x, y, w, h) where w, h signifies the major and minor axis of the ellipse. But, since we are using it to plot a circle, major-axis=minor-axis=circle_diameter=100. So, w=h=100 is given. x, y determine the positioning of the ellipse.
----------------------------------------------------------------------------------------------------
Python 3 Code: Change Color of Circle based on Mouse Position
----------------------------------------------------------------------------------------------------
# importing pygame library
import pygame
# To decide if the window is still running or not
running = 1
# Specifying dimensions of display screen
width = 600
height = 400
# creating a screen of above dimensions
screen = pygame.display.set_mode((width, height))
# Specifying color of line and background
linecolor = 255, 255, 0
bgcolor = 0, 0, 0
# While the screen is running
while running:
# untill the event is of Quit type, we continue
running the screen & do following
event = pygame.event.poll()
if event.type == pygame.QUIT:
running = 0
# Filling the background color as black
screen.fill(bgcolor)
# Drawing the line of yellow color at vertical
midway
pygame.draw.line(screen, linecolor, (0, 200), (600,
200))
# By defualt creating a circle of red color
pygame.draw.ellipse(screen, pygame.Color("red"), (250,
150, 100, 100))
# checking if the current mouse cordinate is in top
half then, coloring the circle blue
if pygame.mouse.get_pos()[1] < height/2:
pygame.draw.ellipse(screen,
pygame.Color("blue"), (250, 150, 100, 100))
# After drawing all the things, this function is
called to output it to screen
pygame.display.flip()
----------------------------------------------------------------------------------------------------
Sample Output Screen Screenshot was taken when the cursor was in top half:
----------------------------------------------------------------------------------------------------
Write a program that changes the color of a circle based on whether the mouse cursor...
*15.7 (Change color using a mouse) Write a program that displays the color of a circle as red when the mouse button is pressed and as blue when the mouse button is released. Please complete using Java language
USE MATLAB TO SOLVE 1. Snap a close-up color picture of a person with your phone or download a picture of your preference in JPG format, transfer/save it to a preferred location on your computer and import the picture into MATLAB. 2. Display the Red, Green and Blue components of the imported pictures in three subplots arranged horizontally; 3. Calculate the minimum and maximum values of the R, G, B components and display them in the titles of each subplots...
I mainly need help with the “Mouse Events” & “Command Buttons” sections Sqrt xA2 Cir CircleButton Clas:s The graphic circular buttons are created by drawing a filled Circle on a StackPane. So, the pictured GUI uses 9 different StackPanes tor displaying the 9 qraphic buttons. Of course, these CircleButton objects can then be placed on a single GridPane lo achieve the 3x3 layoul (see SimpleCalc class below). Creale a class narned CircleBullon thal exlends the StackPane class. The class should...
*** Please if you want to answere this question, include your program output screenshot. And follow the requirements please, thanks.**** Design a Java program: A JPanel of dimension 400 X 200. The background is initially displayed as white and there is a line drawn vertically half way through the screen. When a mouse is clicked on the left hand side of the line, the position of the click is shown with a small circle and the background changes to yellow....
PP.14 PP 3.13 Write a JavaFX application that draws a circle centered at poinit (200, 200) with a random radius in the range 50 to 150. Each time the program is run it will draw a different circle. PP 3.14 Write a JavaFX application that displays your name rotated at a random angle (0 to 360). Each time the program is run it will draw your name at a different angle. a random color created using the rgb method of...
PP.15 PP 3.13 Write a JavaFX application that draws a circle centered at poinit (200, 200) with a random radius in the range 50 to 150. Each time the program is run it will draw a different circle. PP 3.14 Write a JavaFX application that displays your name rotated at a random angle (0 to 360). Each time the program is run it will draw your name at a different angle. a random color created using the rgb method of...
Must be done in python. This will be connected to other turtle assignments. Might need a sleep at end. Swimming in a Pond In this function, you will draw a small pond and have the turtle first take a nice walk around it and then jump in. The pond will need to be drawn in the upper left quandrant of your screen. This function needs to do the following: Contain two parameters to denote the color of the path (pen...
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...
Write a program called draw_shapes.py. In your program, Create a block header with: your name the date a short description of what the program does: Assignment 5: Draw shapes using turtle Import the turtle module. Create a window and screen (canvas) where your turtle will draw. Make the window 400 pixels wide x 400 pixels high and give it an indigo background and a title of "Shapes". Use this code to create a window object: # a place for the...
Assignment Λ You shall write a Java program that accepts 5 command-line arguments and generates an image of a Sierpinski triangle, as a 24- bit RGB PNG image file. Specifications The command-line arguments shall consist of the following 1. The width (in pixels) of the image, as a positive decimal integer 2. The height (in pixels) of the image, as a positive decimal integer 3. The minimum area (in pixels) that a triangle must have in order to be drawn,...