Answer all bulleted questions. Upload your answers and program files to the VikingWeb coursework section. Besides uploading your written answers to the VikingWeb, turn in a printed copy either in class on the due date or leave a copy in the box outside my door later on during the day. (You may turn in the printed copies later than 5PM on the due date, but the files must be uploaded to VikingWeb at or before 5PM.)
One of the appealing features of Java is the ease with which it supports graphical programming. In this lab we will start with drawing lines and will learn the basics of creating a window to display drawings on the screen.
Coordinates: To begin drawing, you should understand the Java coordinate system for identifying points on the screen. By default, the upper-left corner of a window has the coordinates (0, 0). A pair of coordinates, (x, y), specifies the horizontal coordinate (x) and the vertical coordinate (y). From the upper-left corner (0, 0), the coordinates increase positively to the right and bottom of the window. Coordinate units are measured in pixels where a pixel is the monitor's smallest unit of resolution.
For a first drawing application, let us simply draw two lines from the corners. Download and compile the following class files: [DrawPanel.java] [DrawPanelTest.java]
The class DrawPanel
does the actual drawing while
the DrawPanelTest
class creates the window in
which the drawing will be displayed. DrawPanel
imports the Java Graphics
class which provides
methods for drawing text and shapes, and the
JPanel
class which provides an area on which to
draw.
The DrawPanel
class extends JPanel
in
order to define an enhanced type of panel. Every
JPanel
has a paintComponent
method
which the Java system uses whenever it needs to display the
panel. The paintComponent
method in
DrawPanel
overrides the method in
JPanel
. The first statement in such an overridden
method should usually be a call to the superclass' version of
the method:
super.paintComponent( g );
If there are further methods used in the program which you are not sure what they do, ask me or look them up in the online Java API documentation (link at the top of this page).
DrawPanel
class to have it draw a big plus sign
in the window instead of the x?
Download and compile the following class files. Run the
ShapesTest
program: [Shapes.java] [ShapesTest.java]
drawRect
and drawOval
methods?
(That is, does the first argument specify the width/height?
the second argument the radius? etc.)
To create more interesting designs than just lines and basic
shapes, the Graphics
class provides features for
drawing colors and filled shapes.
Colors: Colors displayed on the computer screen are
specified by their red, green, and blue components, called
RGB values. The RGB values have integer values between
0 and 255. The higher the value of a particular component, the
brighter the particular shade will be in the final color. Java
uses the Color
class in the java.awt
package to represent RGB-valued colors. The Color
class contains a constructor of the form:
public Color( int r, int g, int b )to create colors by specifying values for the red, green, and blue components.
Color
class contains 13
predefined static Color
objects. What are they?
Filled shapes: Filled rectangles and ovals are drawn
using the fillRect
and fillOval
methods of the Graphics
class. These methods have
the same parameters as their counterparts
drawRect
and drawOval
. Download and
run the following program, which draws a smiley face in a
window: [DrawSmiley.java]
JPanel
. The innermost circle should have a
radius of 10 pixels, and each successive circle should have
a radius 10 pixels larger than the previous one. Begin by
finding the center of the JPanel
. To get the
upper-left corner of a circle, move up one radius and to the
left one radius from the center. Turn in your modified
program files.
paintComponent
should contain a loop that
iterates 10 times. In each iteration, the loop should
determine whether to draw a filled rectangle or an oval,
create a random color, and choose coordinates and dimensions
at random. The coordinates should be chosen based on the
panel's width and height. Lengths of sides should be limited
to half the width or height of the window. (What happens
each time paintComponent
is called -- i.e. the
window is resized, uncovered, etc.?)
Based on material from Deitel & Deitel, Java How to Program, 6e.