Docsity

Lab 7: 2D Graphics with Java - CSI 3471 - Prof. Paul C. Grabow, Lab Reports of Software Engineering

Toggle sidebar
bg1
1 of 8
CSI 3471
Lab 7: 2D Graphics with Java Revised 10/13/2008
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.
1. Copy the Eclipse project called Lab7_2D from the ProfData (usual) location.
2. Refactor the project name as L7Lastname. [This is a request of the TA.]
3. Upload the project to your SVN repository, then do a commit each time you
complete a section of the lab.
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):
1. What is the difference between a Frame and a JFrame?
2. If a pre-existing Java class has a name that begins with J, what can we
assume?
3. All of the Java Swing classes are located in the javax.swing package. What
does the javax indicate?
4. Simply creating a JFrame object does not make it appear. What is the reason
for this?
5. Look at MainSimple. Note that once the frame is made visible, function main
exits. However, the program itself does not terminate at this point. What does
terminate? (Hint: A program usually consists of a collection of threads.)
pf3
pf4
pf5
pf8

Partial preview of the text

Download Lab 7: 2D Graphics with Java - CSI 3471 - Prof. Paul C. Grabow and more Lab Reports Software Engineering in PDF only on Docsity!

CSI 3471

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.

  1. Copy the Eclipse project called Lab7_2D from the ProfData (usual) location.
  2. Refactor the project name as L7Lastname. [This is a request of the TA.]
  3. Upload the project to your SVN repository, then do a commit each time you complete a section of the lab.

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):

  1. What is the difference between a Frame and a JFrame?
  2. If a pre-existing Java class has a name that begins with J, what can we assume?
  3. All of the Java Swing classes are located in the javax.swing package. What does the javax indicate?
  4. Simply creating a JFrame object does not make it appear. What is the reason for this?
  5. Look at MainSimple. Note that once the frame is made visible, function main exits. However, the program itself does not terminate at this point. What does terminate? (Hint: A program usually consists of a collection of threads.)
  1. The default location for a frame is the upper-left corner, position (0,0). Look at Fig. 7-5 to see how Component and JFrame are related. The Component class contains methods to resize and reshape frames. Use the setLocation method to re-position the frame so it’s upper-left corner is at (100,100), as measures in pixels. Where should this call be made?
  2. Use the setTitle method to display “Simple Frame” at the top of the frame. Where should this call be made?

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):

  1. Compare MainSized with MainSimple (from the previous section above). What is the reason for the EventQueue? (see p. 287)
  2. Note the declaration and creation of a Toolkit object. What design pattern do you think is being used here? Give a reason for your answer.
  3. Which class contains the setIconImage method? (See p. 288.)
  4. Is the frame currently resizeable?
  5. Add a call to setResizable so that the user cannot resize the frame. What should this call look like and where should it be made?

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):

  1. When you execute MainMessage how large was the frame? Add code (in the appropriate location) so that the MessageFrame is 300 pixels wide and 300 pixels high. (Note: It is “best practice” to declare two constants, such as public static final int DEFAULT_WIDTH = 300 rather than using literals in the function call.)
  2. Look at Fig. 7-7. Frames are really designed to be containers for components, such as a menu bar. Consequently, you normally draw on a component which

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:

  1. Compare DrawComponent to the GoBearsPanel from the previous section. Do both extend the same class?
  2. Do both DrawComponent and GoBearsPanel contain paintComponent?

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.