CSC 207 CSC 207.01 & .02- Algorithms and Object-Oriented Design Spring 2022 Home Schedule Class Slides Labs Assignments Resources Lab: Drawing and the Java GUI Getting Started While we will not use most of the features of the AWT, it is a good idea to bookmark a few essential classes that may come in handy during the lab: java.awt.Color: predefined colors and methods used to manage them java.awt.Font: fonts available and methods used to manage them java.awt.Graphics: methods available to draw shapes and print Strings to the panel Create a new Java project in Eclipse and import the following file /home/johnsonba/csc207/startupcode/DrawingPanel.java Don't worry if you don't understand most of what is contained in this file. Mostly, we will use the constructor as a convenient, simplified means of starting our own GUI. Drawing Shapes Create a new Java class that you will use to play around with creating a variety of shapes in order to get used to working with the DrawingPanel. You should include your main method in this file. Using the code in the Overview reading for today (not Weiss) as a starting point, create a DrawingPanel that is 800 by 800 pixels in size. Create a Graphics object using the getGraphics() method. This Graphics object is like a pen that will draw other objects on the DrawingPanel. You will also need to define the font that you will use if you want to print any String to the GUI using the Font constructor. Don't forget to set the font after you have defined it. Note that you can also change the background color of the DrawingPanel from the default white, with the method setBackground(color). Once you have the basics set, practice changing the color that your Graphics object and draw a line under the text. The method that will draw a line is drawLine(x1, y1, x2, y2) where x1 and y1 represent the starting point and x2 and y2 are the end point. The default color is black, but there are many predefined colors that you can use by name. You can find these in the Color class documentation, or Eclipse can also give you the list. Try changing the drawing color several times and draw different shapes. Some basic shape drawing methods are: drawRect(x, y, width, height) drawOval(x, y, width, height) fillRect(x, y, width, height) fillOval(x, y, width, height) Note that we do not have methods for drawing squares, circles, or triangles. How would you draw those shapes? Try it!! You can create your own colors in addition to the ones available in the Color class. Each color is defined by its red, green, and blue values. For instance, brown is not a predefined color in the Color class, but you can make brown with the statement Color brown = new Color(139, 69, 19). I use the internet to search for the rgb values of common colors. Drawing with Loops Create a new Java file in the same project. You will use this to create an image that repeats over the DrawingPanel. The image colors should change gradually with each repetition of the image so that it produces a nice gradient effect. This will look best if you overlap the shapes slightly as they are drawn in sequence from top to bottom, for instance. You could do a gradient from black to white, if you wish, or try changing one of the color values (red, green, or blue) such as this fade from red to blue. Draw a Complex Object/Scene (5 points) Now, take what you have learned so far and get creative!! Choose an image to create using the basic shapes you have learned. The choice of image is up to you, but consider a couple of starting points: You could create a (non-responsive) GUI for the Vending Machine Simulation You could also create a (non-responsive) GUI for the Bunco Game Find a simple cartoon image such as a smiley face, a car, a rainbow, a house*, or a landscape If you took a version of CSC151 that used image generation for labs or projects, you could duplicate one of those images (just give credit for the assignment and any student or mentors who were your collaborators) Your image must contain no fewer than 5 smaller sub-images (which could be produced by Java methods) and use at least 3 different colors. One color should be a user-defined color. *Note: it is possible, but not easy, to draw a filled triangle in Java. We will find out a way to do that in the next lab. Lab Submission Submit your code for the Complex Object/Scene via Gradescope. You do not need to upload the DrawingPanel.java file. Just upload your new class. Write an estimate of the amount of time you spent on this lab (submit as a text file with the rest of the assignment). Acknowledgements The DrawingPanel.java program was created by Stuart Reges and Marty Stepp and appears in their textbook: Building Java Programs, 5th edition This derivative work is used and licensed under a Creative Commons Attribution-NonCommercialShareAlike 4.0 International license. This course is adapted from prior courses taught by Shervin Hajiamini, John Stone, Sam Rebelsky, Anya Vostinar, and Jerod Weinman and used by permission. Other authors' contributions are noted when used or adapted.