JES also includes built-in functionality to add your own graphics onto images. This includes lines or shapes such as rectangles, ovals, and circles.
How to Create an Empty Canvas in JES
You can render existing images using JES, including those that a user has stored on their computer, that they select to upload. If you want to draw your shapes on a blank image, you can use the built-in makeEmptyPicture() function to create a new empty canvas instead.
In a new JES application, create a new function called createEmptyPicture(): def createEmptyPicture(): Inside it, use the built-in makeEmptyPicture() function. Pass the width and height of the new picture, in pixels, to the function. Since JES uses Jython, make sure you indent your code, just as you would in Python. You can take a look at some vital Python commands for beginners if you need to revise your Python skills. emptyPicture = makeEmptyPicture(600, 300) Use the show() function to display the picture: show(emptyPicture) Click on the Load Program button, located between the programming area and the command line. Click on Yes, and save your existing code in a new file. Enter the following code on the command line: createEmptyPicture() This will call the createEmptyPicture function, once you press Enter: The function will then run, showing you the new empty canvas.
How to Create and Add Rectangles
You can use JES to create shapes such as rectangles. In JES, x and y coordinates start from the top-left corner of the image, instead of the bottom-left corner.
Use the built-in addRect() function to draw a rectangle onto an empty picture.
Create a new function called drawRectangle(): def drawRectangle(): Inside the function, create a new empty picture: pic = makeEmptyPicture(600, 300) Use the addRect() function to draw a rectangle. The addRect() function takes in six parameters. The first parameter is the variable that stores the picture you would like to draw on. Following this are the x and y coordinates of the top-left corner of the rectangle. Finally, pass the x and y coordinates of the bottom-right corner, and the color of the border. addRect(pic, 50, 100, 200, 50, red) You can also add filled rectangles using the addRectFilled() function, instead of addRect(): addRectFilled(pic, 100, 200, 200, 50, red) Use the show() function to display the image: show(pic) Click on the Load Program button, located between the programming area and the command line. Run the drawRectangle() function via the command line: Wait for the image to open, showing the rectangles you have made.
How to Create and Add Circles and Ovals
You can use the built-in addOval() function to create both ovals and circles. If you are creating a circle, make sure the width and height of the oval are equal.
Create a new function called drawCircle(): def drawCircle(): Inside the function, create a new empty picture: pic = makeEmptyPicture(600, 300) Create an oval using the addOval() function. Add the picture you would like to draw the oval on as the first argument to the function. Input the x and y coordinates of the top-left point of the oval. Add a width and height, which do not have to be equal if you are drawing an oval. The final argument takes in the color of the oval’s outline: addOval(pic, 100, 100, 50, 100, red) To add filled ovals, use the addOvalFilled() function, instead of the addOval() function: addOvalFilled(pic, 200, 100, 50, 100, red) You can use the same addOval() function to create circles. Make sure the width and height values entered as the third and fourth arguments are the same: addOval(pic, 350, 100, 50, 50, red) Use the addOvalFilled() function to create a filled circle: addOvalFilled(pic, 450, 100, 50, 50, red) Use the show() function to display the image: show(pic) Click on the Load Program button, located between the programming area and the command line. Run the drawCircle() function on the command line: Wait for the image to open to view the drawn ovals and circles.
How to Create and Add Lines
Use the addLine() function to draw lines on your empty image in JES.
Create a new function called drawLine(): def drawLine(): Inside the function, create a new empty picture: pic = makeEmptyPicture(600, 300) Use the addLine() function to draw a Line. Input the picture that you will draw the line on. In the second and third arguments, input the x and y coordinates of the starting point. In the fourth and fifth arguments, input the x and y coordinates of the end point. Finally, enter the color of the line. This draws a line from (50, 200) to (250, 200): addLine(pic, 50, 200, 250, 200, red) Use the show() function to display the image: show(pic) Click on the Load Program button, located between the programming area and the command line. Run the drawLine() function on the command line: Wait for the image to open, showing the new line.
Drawing Your Own Shapes in JES
You should now be able to draw your own shapes in JES. You can start getting more creative with some of the other interesting image-processing techniques on offer.