Question
Can someone give me an example of a code that draws n planets at random positions in the window and with random diameters up to 200 pixels?
It has to have these requirements:
n is an integer that will be entered by a user in a JTextField
Add a JLabel prompt so the user will know what to enter in the text field
You must use the BorderLayout for the content pane in this application.
You can use the library method drawOval to draw an individual planet or create a more
advanced shape (i.e., using Polygon class).
Planets should be painted in random colors.
Use ActionEvent (button or text field) or MouseEvent to trigger drawing the planets.
Explanation / Answer
#!/usr/bin/env python3 # life.py -- A turtle-based version of Conway's Game of Life. # # An empty board will be displayed, and the following commands are available: # E : Erase the board # R : Fill the board randomly # S : Step for a single generation # C : Update continuously until a key is struck # Q : Quit # Cursor keys : Move the cursor around the board # Space or Enter : Toggle the contents of the cursor's position # import sys import turtle import random CELL_SIZE = 10 # Measured in pixels class LifeBoard: """Encapsulates a Life board Attributes: xsize, ysize : horizontal and vertical size of the board state : set containing (x,y) coordinates for live cells. Methods: display(update_board) -- Display the state of the board on-screen. erase() -- clear the entire board makeRandom() -- fill the board randomly set(x,y) -- set the given cell to Live; doesn't refresh the screen toggle(x,y) -- change the given cell from live to dead, or vice versa, and refresh the screen display """ def __init__(self, xsize, ysize): """Create a new LifeBoard instance. scr -- curses screen object to use for display char -- character used to render live cells (default: '*') """ self.state = set() self.xsize, self.ysize = xsize, ysize def is_legal(self, x, y): "Returns true if the x,y coordinates are legal for this board." return (0