




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





Lab 7: 2D Graphics with Java Revised 10/13/
This lab refers to concepts contained in Chapter 7 of Core Java. It assumes that you have read the chapter and that you understand what Swing is and how it came to exist. In particular, this lab considers Creating and positioning a frame Determining frame size Displaying information in a component Working with 2D shapes Using color
Directions: As you do the lab you will be asked to answer various questions. Please do these on the separate piece of paper that you have been given.
You will be modifying existing classes. The project is subdivided by package, with a particular package associated with each section of thes lab. Each package has a “Main” class that contains a main function. To run a particular “Main” class, highlight the class in the Package Explorer of Eclipse, then use your right mouse button and choose Run as Java application.
I. Creating and Positioning a Frame
Package: simple
A. Execute the MainSimple class. Then open both of the files in the package.
B. Answer the following questions using Core Java as a reference (and follow the instructions where indicated):
C. Commit your changes to SVN and close the open files in your workspace.
II. Determining Frame Size
Package: sized
A. Execute MainSized. Then open both of the files in the package.
B. Answer the following questions (and follow the instructions where indicated):
C. Change the size of the frame so that it occupies your entire screen.
D. Commit your changes to SVN and close the open files in your workspace.
III. Displaying Information in a Component
Package: message
A. Execute MainMessage. Open all three files from the package. B. Answer the following questions (and follow the instructions):
IV. Working with 2D Shapes
Package: draw
A. Execute MainDraw. You should see an empty window appear.
B. Open all three files from the package. Similar to the previous section, you will add code to paintComponent to display/paint lines. Then you will create/add a DrawComponent to the DrawFrame.
C. Answer the following questions:
D. Inside paintComponent, create a rectangle whose upper-left corner is at (100, 100), its width is 200 pixels, and its height is 150 pixels. Draw the rectangle using green (rather than black). Use the draw method (from the graphics object) to draw the rectangle.
E. Before the rectangle appears in the frame, however, you must create a DrawComponent in the DrawFrame and add the DrawComponent object to the frame. Once you do this and you execute your code you should see the following:
F. Inside paintComponent declare an Ellipse2D and place it inside the rectangle that you have already created. Make the color of the ellipse yellow. When you execute your code it should look like the following.
H. Construct a blue circle whose center is the same as that of the rectangle and whose radius is 200 pixels. After executing your code the output should look like the following:
I. Now add the following text at position (160, 100) “Sic ‘em Bears!” When you run your code it should now appear as follows:
V. Bonus: Tic-Tac-Toe Grid (5 points)
Create a tic-tac-toe grid (i.e., a 3 x 3 grid) where each square in the grid is approximately 1 inch by 1 inch. Make the lines blue. Position the grid in the center of a window – where the window takes up the upper leftmost quadrant of your computer screen and the title of the window is “tic-tac-toe”.
Note : To get the bonus this must be completed by noon on the next class day. Please submit a “new” Eclipse package called L7BonusYourname.
Some practical matters:
Create a new package called game and place all related files in this package. Each class that you create should be in its own file, similar to what you have already done in this lab. You may reuse concepts/code from other packages, but the classes for this section should be “new”. Use appropriate coding conventions, e.g., declare constants when necessary to avoid passing literals as parameters. Make sure that you have an appropriate header at the top of your classes.