Lab: 25 - Drawing and the Java GUI (extra credit)

Assigned
Friday, 27 October 2023

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:

Create a new Java project in Eclipse and import the following file

/home/jimenezp/csc207/Lab-Drawing/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.

  1. As a starting point, create a DrawingPanel that is 800 by 800 pixels in size.
  2. Note that you can also change the background color of the DrawingPanel from the default white, with the method setBackground(color). You can use java.awt.Color predefined colors, see the Color class documentation or Table 3G.3 from today’s reading.
  3. Create a Graphics object using the getGraphics() method. This Graphics object is like a pen that will draw other objects on the DrawingPanel. See the java.awt.Graphics documentation or Table 3G.2 from today’s reading to get familiar with useful methods. 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.
    • drawRect(x, y, width, height)
    • drawOval(x, y, width, height)
    • fillRect(x, y, width, height)
    • fillOval(x, y, width, height)
  4. Once you have the basics set, practice changing the color that your Graphics object, draw filled shapes with a different outlined shape color.
  5. Note that we do not have methods for drawing squares, circles, or triangles. How would you draw those shapes? Try it!!
  6. Print any String to the GUI using the Font constructor. You can also define the font that you want to use. Don’t forget to set the font after you have defined it. See the java.awt.Font documentation or the table 3G.4-5.
  7. 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.

panel

Draw a Complex Object/Scene

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 that includes 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 should be produced by Java methods) and use at least 3 different colors. One color should be a user-defined color, one sub-image should be painted using a gradient effect. You image should include some text.
  • Your program should read as arguments the dimension of the drawing panel.

Lab Submission

(4 points) Submit the source file for your complex Object/Scene and the output of your program along with a description of your figure/scene.

Remember: Do not use magic numbers, write your code anticipating errors and print user-friendly error messages, all your public methods should be well documented (use Javadoc comments). Please include comments or organize your code and report in a way that will help the grader to find the answer to the exercises or posted questions.