Homework Instructions: Refer to the Ch 01 PDF file for the instructions.
© 2022 Linda Shepherd 1
© 2015 Deitel, Paul and Harvey, Java How to Program 10th Edition Late Objects Version, Pearson Education
Inc.: Tic-Tac-Toe, Painter, Ch 1 pp 22-25.
IS 2063 Chapter 1: Using Windows Command Prompt to Run Java Programs
Purpose: To familiarize you with how to execute a Java application from the Windows Command
Prompt.
Prep Work:
1. You should’ve already installed DrJava and Amazon Corretto 8 on your own computer per
instructions for Software Option 1 given to you in CHAPTER COURSEWORK >> First Things
First (Getting Started).
2. Otherwise, UTSA IT has installed DrJava and Amazon Corretto 8 on UTSA’s VDI (Virtual Desktop
Infrastructure). Please refer to Software Option 2 in CHAPTER COURSEWORK >> First
Things First (Getting Started).
3. Download the zip file posted with these instructions. Save it to a folder for chapter 1. Extract its
content and review the file on Command Prompt commands and how to change directories in
Command Prompt. All you have to do is point to the zip file, right click, and “Extract Here”.
During the extraction make sure your files are not nested within multiple folders of the same
name.
Instructions:
1. Open a Word document saved with your LastNameFirstInitialYourSecNoCmdPrompt. Example:
JonesE001CmdPrompt. Create 2 sections. One called TicTacToeTest with screenshots for
running the program and similarly for Painter.
2. TicTacToeTest Screenshots Section
a. In Command Prompt show that you can navigate to the correct drive, then the correct folder
containing the program, and type the command to run the program, but don’t hit enter just yet.
i. Screenshot the navigation commands in the Command Prompt window and insert in the
section of the Word document for TicTacToeTest.
ii. Now, hit enter to Run the program.
b. First Move: Keep in mind that the computing begins counting at 0, so 0 is row or column 1; 1
is row or column 2; 2 is row or column 3.
i. Player 1 marks row 3, column 2.
ii. Player 2 marks row 1, column 3.
iii. Screenshot each player’s move including the row and column entries, insert into the Word
document.
c. Second Move:
i. Player 1 marks row 2, column 2.
ii. Player 2 marks row 1, column 2.
iii. Screenshot each player’s move including the row and column entries, insert into the Word
document
3. Painter Screenshots Section
a. Go to the Deitel Ch 1 PDF in the attached zip file for pp 22-25 and follow the instructions from
numbers 4 to 9.
b. Screenshot the Painter window (must include the title bar with the app name, dropdown lists
and buttons) showing each step of how you draw the face per the instructions in 4a and insert
in the section of the Word document for Painter.
© 2022 Linda Shepherd 2
© 2015 Deitel, Paul and Harvey, Java How to Program 10th Edition Late Objects Version, Pearson Education
Inc.: Tic-Tac-Toe, Painter, Ch 1 pp 22-25.
Submission Requirements: Upload your Word document with the screenshots to Blackboard by
the due date.
Files:
• Zip file content:
o Tic-Tac-Toe and painter folders that have the programs you’ll be running, and a
o document called Command Prompt Commands and Changing Directories that
instructs you on how to run Windows Command Prompt, navigate to drives and directories,
and compile and run your Java programs, and
o Deitel Ch 1 PP 22-25 on running the Painter program.
Chapter 1/Command Prompt Commands and Changing Directories
© Linda Shepherd, Dept. of ISCS, Alvarez COB, UTSA. 1
Windows Command Prompt Commands and Changing Directories
Click Above Link for Different MS-DOS Commands
1. The System Environment variable settings you made to your computer enable you to compile or run a Java
program at the Command Prompt window.
2. Go to the folder containing the Java files to be compiled or run.
Click ATM in the path field until it’s highlighted
and the path is visible.
C:\Users\YourComputersName\Desktop\ATM
In place of the path type cmd. The Windows
Command Prompt Window defaulted to the
highlighted path (directory).
3. OR Clicking Start, Run, then type cmd in the Run window.
4. OR Clicking Start, Programs, Accessories, then Command Prompt.
5. javac is a Java command used from the Command Prompt to compile a source code program. Your source
code programs end with a .java file extension. Source code files have to be compiled first to flush out any
syntax errors (language errors). If there are no errors, then javac produces a .class version of the .java file.
6. java is a Java command used from the Command Prompt to run/execute the bytecode or .class file. Below
are the formats for the Java commands.
a. javac NameOfProgram.java
b. java NameOfProgram
Compiles a Java source file – requires .java file extension
Runs a .class bytecode file – requires no file extension
https://www.computerhope.com/msdos.htm#02
© Linda Shepherd, Dept. of ISCS, Alvarez COB, UTSA. 2
HOW TO NAVIGATE TO DRIVES AND DIRECTORIES
Type the drive letter for your jump drive
followed by a colon.
Use cd\ (no space after) or cd (space after) followed by the name of the folder containing your Java files.
You are now in the ATM folder (directory) on the G: drive. Ready to use javac or java .
Navigating to the Correct Folder: Use OneDrive in path if files are stored on it. abc123 applies to school computers.
Navigating to Your Jump/Flash Drive
C:\Documents and Settings\ abc123>cd\Documents and Settings\abc123\My Documents\ATM
C:\Documents and Settings\ abc123\My Documents\ATM>
Use cd\ followed by all the folder and subfolder names until you get to your Java files.
You are now in the ATM folder (directory) that is within the folder on the
drive for your computer or the school’s computer. Keep in mind the path
is dictated by where you downloaded/saved/unzipped the ATM folder.
Now you’re ready to use javac (to compile) or java (to run) a Java file.
C:\Documents and Settings\ abc123>cd My Documents\ATM
Use cd followed by the folder and subfolder names within the current path. OR
C:\Documents and Settings\ abc123\cd Desktop
C:\Documents and Settings\ abc123\Desktop>
Use cd Desktop to change to the Desktop.
Use cd.. to get back to previous directory. cd\ goes back to your root directory.
Use dir to list what is in the directory. Use cls to
clear the screen.
OR C:\Users\YourComputersName\OneDrive\Desktop\ATM
Chapter 1/Deitel Ch 1 PP 22-25
22 Chapter I Introduction to Computers . the Internet and Java
Select a shape
Specify
whether a
rectangle or
ova l is filled
with color
Close
button
a) Painter app runn ing on Windows
b) Painter app runn ing on Linux.
c) Painter app running on OS X.
Close – F{:l n n Painter
button 1…:..-__:.-~——;..;:;;=———–1
~lack __jJ (, Line _i) Cl Filled ~do
0 0)
Fig. 1.11 I Painter app executing in Windows 7. Lin ux and OS X.
Close button
entire drawing
Undo the last
shape that was
added to the
drawing
[Note: Java commands are case sensitive-that is, uppercase letters are different
from lowercase letters. It’s important to type the name of this application as Paint
er with a capital P. Otherwise, the application will not execute. Specifying the
. class extension when using the java command results in an error. Also, if you
receive the error message, “Exception in thread “main” java .lang. NoCl ass
DefFoundError: Painter ,” your system has a CLASSPATH problem. Please refer to
the Before You Begin section for instructions to help you ftx this problem.]
4. Drawing a filled yellow oval for the face. Select Yellow as the drawing color, Oval ‘
as the shape and check the Filled checkbox, then drag the mouse to draw a large
oval (Fig. 1.12).
1. 10 Test-Driving a j ava Appli cation 23
~~~
1 • . … .. 1 ( ~· -· t • ) 0 Fil led [ Und~ J
(435,295)
l)
Fig. 1.1 2 I Drawing a fi lled yellow ova l for the face.
5. Drawing blue eyes. Select Blue as the drawing color, then draw two small ovals as
the eyes (Fig. 1.1 3).
GJ[§l~
l)
(37 1,131)
Fig. 1. 13 I Drawing blue eyes.
6. D rawing black eyebrows and a nose. Select Black as the drawing color and Line
as the shape, then draw eyebrows and a nose (Fig. 1.14) . Lines do not have fi ll ,
so leaving the Filled checkbox checked has no effect when drawing lines.
24 Chapter I Introduction to Computers , the Internet and Java
Fig. 1.14 I Drawing black eyebrows and a nose.
7. Drawing a magenta mouth. Select Magenta as the drawing color and Oval as the
shape, then draw a mouth (Fig. 1.15).
(Magenta ! • ]( ‘-ov_a_t _……_
(435,169)
Fig. 1.1 S I Drawing a magenta mouth.
8. Drawing a yellow oval on the mouth to make a smile. Select Yellow as the drawing
color, then draw an oval to change the magenta oval into a smile (Fig. 1.16) .
——
(328,252)
1. 1 I Internet and World Wide Web :ZS
l£]@~
[ C l~ar J
——
Fig. 1.16 I Drawing a yel low oval on the mouth to make a sm ile.
9. Exiting the Painter application. To exit the Painter application, click the Close
button (in the window’s upper-right corner on Windows and the upper-left cor
ner on Linux and OS X). Closing the window causes the Painter application to
terminate.
1.11 Internet and World Wide Web
In the late 1960s, ARP A-the Advanced Research Projects Agency of the United States De
partment of Defense-rolled out plans for networking the main computer systems of ap
proximately a dozen ARP A-funded universities and research institutions. The computers
were to be connected with communications lines operating at speeds on the order of 50,000
bits per second, a stunning rate at a time when most people (of the few who even had net
working access) were connecting over telephone lines to computers at a rate of 110 bits per
second. Academic research was about to rake a giant leap forward. ARPA proceeded to im
plement what quickly became known as the ARPANET, the precursor to today’ s Internet.
Today’s fastest Internet speeds are on the order of billions of bits per second with trillion
bit-per-second speeds on the horizon!
Things worked out differently from the original plan. Although the ARPANET
enabled researchers to network their computers, irs main benefit proved to be the capa
bility for quick and easy communication via what came to be known as electronic mail (e
mail). This is true even on today’s Internet, with e-mail, instant messaging, file transfer
and social media such as Facebook and Twitter enabling billions of people worldwide to
communicate quickly and easily.
The protocol (set of rules) for communicating over the ARPANET became known as
the Transmission Control Protocol (TCP) . TCP ensured that messages, consisting of
sequentially numbered pieces called packets, were properly routed from sender to receiver,
arrived intact and were assembled in the correct order.
Chapter 1/painter/DrawFrame.class
public
synchronized
class DrawFrame
extends javax.swing.JFrame
implements java.awt.event.ItemListener, java.awt.event.ActionListener {
private java.awt.Color[]
colors;
private String[]
colorNames;
private String[]
shapes;
private DrawPanel
drawPanel;
private javax.swing.JButton
undoButton;
private javax.swing.JButton
clearButton;
private javax.swing.JComboBox
colorChoices;
private javax.swing.JComboBox
shapeChoices;
private javax.swing.JCheckBox
filledCheckBox;
public void DrawFrame();
public void
itemStateChanged(java.awt.event.ItemEvent);
public void
actionPerformed(java.awt.event.ActionEvent);
}
Chapter 1/painter/DrawFrame.java
Chapter 1/painter/DrawFrame.java
// GCS Exercise 14.17 Solution: DrawFrame.java
// Program that creates a panel for the user to draw shapes.
// Allows the user to choose the shape and color.
import
java
.
awt
.
BorderLayout
;
import
java
.
awt
.
Color
;
import
java
.
awt
.
FlowLayout
;
import
java
.
awt
.
event
.
ActionEvent
;
import
java
.
awt
.
event
.
ActionListener
;
import
java
.
awt
.
event
.
ItemEvent
;
import
java
.
awt
.
event
.
ItemListener
;
import
javax
.
swing
.
JButton
;
import
javax
.
swing
.
JCheckBox
;
import
javax
.
swing
.
JComboBox
;
import
javax
.
swing
.
JFrame
;
import
javax
.
swing
.
JLabel
;
import
javax
.
swing
.
JPanel
;
public
class
DrawFrame
extends
JFrame
implements
ItemListener
,
ActionListener
{
// Array of possible colors
private
Color
[]
colors
=
{
Color
.
BLACK
,
Color
.
BLUE
,
Color
.
CYAN
,
Color
.
DARK_GRAY
,
Color
.
GRAY
,
Color
.
GREEN
,
Color
.
LIGHT_GRAY
,
Color
.
MAGENTA
,
Color
.
ORANGE
,
Color
.
PINK
,
Color
.
RED
,
Color
.
WHITE
,
Color
.
YELLOW
};
// Array of names corresponding to the possible colors
private
String
[]
colorNames
=
{
“Black”
,
“Blue”
,
“Cyan”
,
“Dark Gray”
,
“Gray”
,
“Green”
,
“Light Gray”
,
“Magenta”
,
“Orange”
,
“Pink”
,
“Red”
,
“White”
,
“Yellow”
};
// Array of possible shapes
private
String
[]
shapes
=
{
“Line”
,
“Oval”
,
“Rectangle”
};
private
DrawPanel
drawPanel
;
// panel that handles the drawing
private
JButton
undoButton
;
// button to undo the last shape drawn
private
JButton
clearButton
;
// button to clear all shapes
private
JComboBox
<
String
>
colorChoices
;
// combo box for selecting the color
private
JComboBox
<
String
>
shapeChoices
;
// combo box for selecting shapes
private
JCheckBox
filledCheckBox
;
// check box to toggle filled shapes
// constructor
public
DrawFrame
()
{
super
(
“Painter”
);
// create a panel to store the components at the top of the frame
JPanel
topPanel
=
new
JPanel
();
// create a combobox for choosing colors
colorChoices
=
new
JComboBox
<
String
>
(
colorNames
);
colorChoices
.
addItemListener
(
this
);
topPanel
.
add
(
colorChoices
);
// create a combobox for choosing shapes
shapeChoices
=
new
JComboBox
<
String
>
(
shapes
);
shapeChoices
.
addItemListener
(
this
);
topPanel
.
add
(
shapeChoices
);
// create a checkbox to determine whether the shape is filled
filledCheckBox
=
new
JCheckBox
(
“Filled”
);
filledCheckBox
.
addItemListener
(
this
);
topPanel
.
add
(
filledCheckBox
);
// create a button for clearing the last drawing
undoButton
=
new
JButton
(
“Undo”
);
undoButton
.
addActionListener
(
this
);
topPanel
.
add
(
undoButton
);
// create a button for clearing all drawings
clearButton
=
new
JButton
(
“Clear”
);
clearButton
.
addActionListener
(
this
);
topPanel
.
add
(
clearButton
);
// add the top panel to the frame
add
(
topPanel
,
BorderLayout
.
NORTH
);
// create a label for the status bar
JLabel
statusLabel
=
new
JLabel
(
“(0,0)”
);
// add the status bar at the bottom
add
(
statusLabel
,
BorderLayout
.
SOUTH
);
// create the DrawPanel with its status bar label
drawPanel
=
new
DrawPanel
(
statusLabel
);
add
(
drawPanel
);
// add the drawing area to the center
}
// end DrawFrame constructor
// handle selections made to a combobox or checkbox
public
void
itemStateChanged
(
ItemEvent
e
)
{
if
(
e
.
getSource
()
==
shapeChoices
)
// choosing a shape
drawPanel
.
setShapeType
(
shapeChoices
.
getSelectedIndex
());
else
if
(
e
.
getSource
()
==
colorChoices
)
// choosing a color
drawPanel
.
setDrawingColor
(
colors
[
colorChoices
.
getSelectedIndex
()]);
else
if
(
e
.
getSource
()
==
filledCheckBox
)
// filled/unfilled
drawPanel
.
setFilledShape
(
filledCheckBox
.
isSelected
());
}
// end method itemStateChanged
// handle button clicks
public
void
actionPerformed
(
ActionEvent
e
)
{
if
(
e
.
getSource
()
==
undoButton
)
drawPanel
.
clearLastShape
();
else
if
(
e
.
getSource
()
==
clearButton
)
drawPanel
.
clearDrawing
();
}
// end method actionPerformed
}
// end class DrawFrame
/**************************************************************************
* (C) Copyright 1992-2014 by Deitel & Associates, Inc. and *
* Pearson Education, Inc. All Rights Reserved. *
* *
* DISCLAIMER: The authors and publisher of this book have used their *
* best efforts in preparing the book. These efforts include the *
* development, research, and testing of the theories and programs *
* to determine their effectiveness. The authors and publisher make *
* no warranty of any kind, expressed or implied, with regard to these *
* programs or to the documentation contained in these books. The authors *
* and publisher shall not be liable in any event for incidental or *
* consequential damages in connection with, or arising out of, the *
* furnishing, performance, or use of these programs. *
**************************************************************************/
Chapter 1/painter/DrawPanel$1.class
synchronized
class DrawPanel$1 {
}
Chapter 1/painter/DrawPanel$MouseHandler.class
synchronized
class DrawPanel$MouseHandler
extends java.awt.event.MouseAdapter
implements java.awt.event.MouseMotionListener {
private void DrawPanel$MouseHandler(DrawPanel);
public void
mousePressed(java.awt.event.MouseEvent);
public void
mouseReleased(java.awt.event.MouseEvent);
public void
mouseDragged(java.awt.event.MouseEvent);
public void
mouseMoved(java.awt.event.MouseEvent);
}
Chapter 1/painter/DrawPanel.class
public
synchronized
class DrawPanel
extends javax.swing.JPanel {
private MyShape[]
shapes;
private int
shapeCount;
private int
shapeType;
private MyShape
currentShape;
private java.awt.Color
currentColor;
private boolean
filledShape;
private javax.swing.JLabel
statusLabel;
public void DrawPanel(javax.swing.JLabel);
public void
paintComponent(java.awt.Graphics);
public void
setShapeType(int);
public void
setDrawingColor(java.awt.Color);
public void
clearLastShape();
public void
clearDrawing();
public void
setFilledShape(boolean);
}
Chapter 1/painter/DrawPanel.java
Chapter 1/painter/DrawPanel.java
// GCS Exercise 14.17 Solution: DrawPanel.java
// JPanel that allows the user to draw shapes with the mouse.
import
java
.
awt
.
Color
;
import
java
.
awt
.
Graphics
;
import
java
.
awt
.
event
.
MouseAdapter
;
import
java
.
awt
.
event
.
MouseEvent
;
import
java
.
awt
.
event
.
MouseMotionListener
;
import
javax
.
swing
.
JLabel
;
import
javax
.
swing
.
JPanel
;
public
class
DrawPanel
extends
JPanel
{
private
MyShape
[]
shapes
;
// array containing all the shapes
private
int
shapeCount
;
// total number of shapes
private
int
shapeType
;
// the type of shape to draw
private
MyShape
currentShape
;
// the current shape being drawn
private
Color
currentColor
;
// the color of the shape
private
boolean
filledShape
;
// whether this shape is filled
private
JLabel
statusLabel
;
// label displaying mouse coordinates
// constructor
public
DrawPanel
(
JLabel
status
)
{
shapes
=
new
MyShape
[
100
];
// create the array
shapeCount
=
0
;
// initially we have no shapes
setShapeType
(
0
);
// initially draw lines
setDrawingColor
(
Color
.
BLACK
);
// start drawing with black
setFilledShape
(
false
);
// not filled by default
currentShape
=
null
;
// not drawing anything initially
setBackground
(
Color
.
WHITE
);
// set a white background
// add the mouse listeners
MouseHandler
mouseHandler
=
new
MouseHandler
();
addMouseListener
(
mouseHandler
);
addMouseMotionListener
(
mouseHandler
);
// set the status label for displaying mouse coordinates
statusLabel
=
status
;
}
// end DrawPanel constructor
// draw shapes using polymorphism
public
void
paintComponent
(
Graphics
g
)
{
super
.
paintComponent
(
g
);
for
(
int
i
=
0
;
i
<
shapeCount
;
i
++
)
shapes
[
i
].
draw
(
g
);
if
(
currentShape
!=
null
)
currentShape
.
draw
(
g
);
}
// end method paintComponent
// sets the type of shape to draw
public
void
setShapeType
(
int
shapeType
)
{
if
(
shapeType
<
0
||
shapeType
>
2
)
shapeType
=
0
;
this
.
shapeType
=
shapeType
;
}
// end method setShapeType
// sets the drawing color
public
void
setDrawingColor
(
Color
c
)
{
currentColor
=
c
;
}
// end method setDrawingColor
// clears the last shape drawn
public
void
clearLastShape
()
{
if
(
shapeCount
>
0
)
{
shapeCount
—
;
repaint
();
}
// end if
}
// end method clearLastShape
// clears all drawings on this panel
public
void
clearDrawing
()
{
shapeCount
=
0
;
repaint
();
}
// end method clearDrawing
// sets whether to draw a filled shape
public
void
setFilledShape
(
boolean
isFilled
)
{
filledShape
=
isFilled
;
}
// end method setFilledShape
// handles mouse events for this JPanel
private
class
MouseHandler
extends
MouseAdapter
implements
MouseMotionListener
{
// creates and sets the initial position for the new shape
public
void
mousePressed
(
MouseEvent
e
)
{
if
(
currentShape
!=
null
)
return
;
// create the appropriate shape based on shapeType
switch
(
shapeType
)
{
case
0
:
currentShape
=
new
MyLine
(
e
.
getX
(),
e
.
getY
(),
e
.
getX
(),
e
.
getY
(),
currentColor
);
break
;
case
1
:
currentShape
=
new
MyOval
(
e
.
getX
(),
e
.
getY
(),
e
.
getX
(),
e
.
getY
(),
currentColor
,
filledShape
);
break
;
case
2
:
currentShape
=
new
MyRect
(
e
.
getX
(),
e
.
getY
(),
e
.
getX
(),
e
.
getY
(),
currentColor
,
filledShape
);
break
;
}
// end switch
}
// end method mousePressed
// fixes the current shape onto the panel
public
void
mouseReleased
(
MouseEvent
e
)
{
if
(
currentShape
==
null
)
return
;
// set the second point on the shape
currentShape
.
setX2
(
e
.
getX
());
currentShape
.
setY2
(
e
.
getY
());
// set the shape only if there is room in the array
if
(
shapeCount
<
shapes
.
length
)
{
shapes
[
shapeCount
]
=
currentShape
;
shapeCount
++
;
}
// end if
currentShape
=
null
;
// clear the temporary drawing shape
repaint
();
}
// end method mouseReleased
// update the shape to the current mouse position while dragging
public
void
mouseDragged
(
MouseEvent
e
)
{
if
(
currentShape
!=
null
)
{
currentShape
.
setX2
(
e
.
getX
());
currentShape
.
setY2
(
e
.
getY
());
repaint
();
}
// end if
mouseMoved
(
e
);
// update status bar
}
// end method mouseDragged
// updates the status bar to show the current mouse coordinates
public
void
mouseMoved
(
MouseEvent
e
)
{
statusLabel
.
setText
(
String
.
format
(
"(%d,%d)"
,
e
.
getX
(),
e
.
getY
()));
}
// end method mouseMoved
}
// end class MouseHandler
}
// end class DrawPanel
/**************************************************************************
* (C) Copyright 1992-2014 by Deitel & Associates, Inc. and *
* Pearson Education, Inc. All Rights Reserved. *
* *
* DISCLAIMER: The authors and publisher of this book have used their *
* best efforts in preparing the book. These efforts include the *
* development, research, and testing of the theories and programs *
* to determine their effectiveness. The authors and publisher make *
* no warranty of any kind, expressed or implied, with regard to these *
* programs or to the documentation contained in these books. The authors *
* and publisher shall not be liable in any event for incidental or *
* consequential damages in connection with, or arising out of, the *
* furnishing, performance, or use of these programs. *
**************************************************************************/
Chapter 1/painter/MyBoundedShape.class
public
abstract
synchronized
class MyBoundedShape
extends MyShape {
private boolean
filled;
public void MyBoundedShape();
public void MyBoundedShape(int, int, int, int, java.awt.Color, boolean);
public int
getUpperLeftX();
public int
getUpperLeftY();
public int
getWidth();
public int
getHeight();
public boolean
isFilled();
public void
setFilled(boolean);
}
Chapter 1/painter/MyBoundedShape.java
Chapter 1/painter/MyBoundedShape.java
// GCS Exercise 10.2 Solution: MyBoundedShape.java
// Declaration of class MyBoundedShape.
import
java
.
awt
.
Color
;
import
java
.
awt
.
Graphics
;
public
abstract
class
MyBoundedShape
extends
MyShape
{
private
boolean
filled
;
// whether this shape is filled
// call default superclass constructor
public
MyBoundedShape
()
{
super
();
setFilled
(
false
);
}
// end MyBoundedShape no-argument constructor
// call superclass constructor passing parameters
public
MyBoundedShape
(
int
x1
,
int
y1
,
int
x2
,
int
y2
,
Color
color
,
boolean
isFilled
)
{
super
(
x1
,
y1
,
x2
,
y2
,
color
);
setFilled
(
isFilled
);
}
// end MyBoundedShape constructor
// get upper left x coordinate
public
int
getUpperLeftX
()
{
return
Math
.
min
(
getX1
(),
getX2
());
}
// end method getUpperLeftX
// get upper left y coordinate
public
int
getUpperLeftY
()
{
return
Math
.
min
(
getY1
(),
getY2
());
}
// end method getUpperLeftY
// get shape width
public
int
getWidth
()
{
return
Math
.
abs
(
getX2
()
-
getX1
());
}
// end method getWidth
// get shape height
public
int
getHeight
()
{
return
Math
.
abs
(
getY2
()
-
getY1
());
}
// end method getHeight
// determines whether this shape is filled
public
boolean
isFilled
()
{
return
filled
;
}
// end method is filled
// sets whether this shape is filled
public
void
setFilled
(
boolean
isFilled
)
{
filled
=
isFilled
;
}
// end method setFilled
}
// end class MyBoundedShape
/**************************************************************************
* (C) Copyright 1992-2014 by Deitel & Associates, Inc. and *
* Pearson Education, Inc. All Rights Reserved. *
* *
* DISCLAIMER: The authors and publisher of this book have used their *
* best efforts in preparing the book. These efforts include the *
* development, research, and testing of the theories and programs *
* to determine their effectiveness. The authors and publisher make *
* no warranty of any kind, expressed or implied, with regard to these *
* programs or to the documentation contained in these books. The authors *
* and publisher shall not be liable in any event for incidental or *
* consequential damages in connection with, or arising out of, the *
* furnishing, performance, or use of these programs. *
**************************************************************************/
Chapter 1/painter/MyLine.class
public
synchronized
class MyLine
extends MyShape {
public void MyLine();
public void MyLine(int, int, int, int, java.awt.Color);
public void
draw(java.awt.Graphics);
}
Chapter 1/painter/MyLine.java
Chapter 1/painter/MyLine.java
// GCS Exercise 10.2 Solution: MyLine.java
// Declaration of class MyLine.
import
java
.
awt
.
Color
;
import
java
.
awt
.
Graphics
;
public
class
MyLine
extends
MyShape
{
// call default superclass constructor
public
MyLine
()
{
super
();
}
// end MyLine no-argument constructor
// call superclass constructor passing parameters
public
MyLine
(
int
x1
,
int
y1
,
int
x2
,
int
y2
,
Color
color
)
{
super
(
x1
,
y1
,
x2
,
y2
,
color
);
}
// end MyLine constructor
// draw line in specified color
public
void
draw
(
Graphics
g
)
{
g
.
setColor
(
getColor
());
g
.
drawLine
(
getX1
(),
getY1
(),
getX2
(),
getY2
());
}
// end method draw
}
// end class MyLine
/**************************************************************************
* (C) Copyright 1992-2014 by Deitel & Associates, Inc. and *
* Pearson Education, Inc. All Rights Reserved. *
* *
* DISCLAIMER: The authors and publisher of this book have used their *
* best efforts in preparing the book. These efforts include the *
* development, research, and testing of the theories and programs *
* to determine their effectiveness. The authors and publisher make *
* no warranty of any kind, expressed or implied, with regard to these *
* programs or to the documentation contained in these books. The authors *
* and publisher shall not be liable in any event for incidental or *
* consequential damages in connection with, or arising out of, the *
* furnishing, performance, or use of these programs. *
**************************************************************************/
Chapter 1/painter/MyOval.class
public
synchronized
class MyOval
extends MyBoundedShape {
public void MyOval();
public void MyOval(int, int, int, int, java.awt.Color, boolean);
public void
draw(java.awt.Graphics);
}
Chapter 1/painter/MyOval.java
Chapter 1/painter/MyOval.java
// GCS Exercise 10.2 Solution: MyOval.java
// Declaration of class MyOval.
import
java
.
awt
.
Color
;
import
java
.
awt
.
Graphics
;
public
class
MyOval
extends
MyBoundedShape
{
// call default superclass constructor
public
MyOval
()
{
super
();
}
// end MyOval no-argument constructor
// call superclass constructor passing parameters
public
MyOval
(
int
x1
,
int
y1
,
int
x2
,
int
y2
,
Color
color
,
boolean
isFilled
)
{
super
(
x1
,
y1
,
x2
,
y2
,
color
,
isFilled
);
}
// end MyOval constructor
// draw oval
public
void
draw
(
Graphics
g
)
{
g
.
setColor
(
getColor
());
if
(
isFilled
())
g
.
fillOval
(
getUpperLeftX
(),
getUpperLeftY
(),
getWidth
(),
getHeight
());
else
g
.
drawOval
(
getUpperLeftX
(),
getUpperLeftY
(),
getWidth
(),
getHeight
());
}
// end method draw
}
// end class MyOval
/**************************************************************************
* (C) Copyright 1992-2014 by Deitel & Associates, Inc. and *
* Pearson Education, Inc. All Rights Reserved. *
* *
* DISCLAIMER: The authors and publisher of this book have used their *
* best efforts in preparing the book. These efforts include the *
* development, research, and testing of the theories and programs *
* to determine their effectiveness. The authors and publisher make *
* no warranty of any kind, expressed or implied, with regard to these *
* programs or to the documentation contained in these books. The authors *
* and publisher shall not be liable in any event for incidental or *
* consequential damages in connection with, or arising out of, the *
* furnishing, performance, or use of these programs. *
**************************************************************************/
Chapter 1/painter/MyRect.class
public
synchronized
class MyRect
extends MyBoundedShape {
public void MyRect();
public void MyRect(int, int, int, int, java.awt.Color, boolean);
public void
draw(java.awt.Graphics);
}
Chapter 1/painter/MyRect.java
Chapter 1/painter/MyRect.java
// GCS Exercise 10.2 Solution: MyRect.java
// Declaration of class MyRect.
import
java
.
awt
.
Color
;
import
java
.
awt
.
Graphics
;
public
class
MyRect
extends
MyBoundedShape
{
// call default superclass constructor
public
MyRect
()
{
super
();
}
// end MyRect no-argument constructor
// call superclass constructor passing parameters
public
MyRect
(
int
x1
,
int
y1
,
int
x2
,
int
y2
,
Color
color
,
boolean
isFilled
)
{
super
(
x1
,
y1
,
x2
,
y2
,
color
,
isFilled
);
}
// end MyRect constructor
// draw rectangle
public
void
draw
(
Graphics
g
)
{
g
.
setColor
(
getColor
());
if
(
isFilled
())
g
.
fillRect
(
getUpperLeftX
(),
getUpperLeftY
(),
getWidth
(),
getHeight
());
else
g
.
drawRect
(
getUpperLeftX
(),
getUpperLeftY
(),
getWidth
(),
getHeight
());
}
// end method draw
}
// end class MyRect
/**************************************************************************
* (C) Copyright 1992-2014 by Deitel & Associates, Inc. and *
* Pearson Education, Inc. All Rights Reserved. *
* *
* DISCLAIMER: The authors and publisher of this book have used their *
* best efforts in preparing the book. These efforts include the *
* development, research, and testing of the theories and programs *
* to determine their effectiveness. The authors and publisher make *
* no warranty of any kind, expressed or implied, with regard to these *
* programs or to the documentation contained in these books. The authors *
* and publisher shall not be liable in any event for incidental or *
* consequential damages in connection with, or arising out of, the *
* furnishing, performance, or use of these programs. *
**************************************************************************/
Chapter 1/painter/MyShape.class
public
abstract
synchronized
class MyShape {
private int
x1;
private int
y1;
private int
x2;
private int
y2;
private java.awt.Color
myColor;
public void MyShape();
public void MyShape(int, int, int, int, java.awt.Color);
public void
setX1(int);
public int
getX1();
public void
setX2(int);
public int
getX2();
public void
setY1(int);
public int
getY1();
public void
setY2(int);
public int
getY2();
public void
setColor(java.awt.Color);
public java.awt.Color
getColor();
public
abstract void
draw(java.awt.Graphics);
}
Chapter 1/painter/MyShape.java
Chapter 1/painter/MyShape.java
// GCS Exercise 10.2 Solution: MyShape.java
// Declaration of class MyShape.
import
java
.
awt
.
Color
;
import
java
.
awt
.
Graphics
;
public
abstract
class
MyShape
{
private
int
x1
;
// x coordinate of first endpoint
private
int
y1
;
// y coordinate of first endpoint
private
int
x2
;
// x coordinate of second endpoint
private
int
y2
;
// y coordinate of second endpoint
private
Color
myColor
;
// color of this shape
// default constructor initializes values with 0
public
MyShape
()
{
this
(
0
,
0
,
0
,
0
,
Color
.
BLACK
);
// call constructor to set values
}
// end MyShape no-argument constructor
// constructor
public
MyShape
(
int
x1
,
int
y1
,
int
x2
,
int
y2
,
Color
color
)
{
setX1
(
x1
);
// set x coordinate of first endpoint
setY1
(
y1
);
// set y coordinate of first endpoint
setX2
(
x2
);
// set x coordinate of second endpoint
setY2
(
y2
);
// set y coordinate of second endpoint
setColor
(
color
);
// set the color
}
// end MyShape constructor
// set the x-coordinate of the first point
public
void
setX1
(
int
x1
)
{
this
.
x1
=
(
x1
>=
0
?
x1
:
0
);
}
// end method setX1
// get the x-coordinate of the first point
public
int
getX1
()
{
return
x1
;
}
// end method getX1
// set the x-coordinate of the second point
public
void
setX2
(
int
x2
)
{
this
.
x2
=
(
x2
>=
0
?
x2
:
0
);
}
// end method setX2
// get the x-coordinate of the second point
public
int
getX2
()
{
return
x2
;
}
// end method getX2
// set the y-coordinate of the first point
public
void
setY1
(
int
y1
)
{
this
.
y1
=
(
y1
>=
0
?
y1
:
0
);
}
// end method setY1
// get the y-coordinate of the first point
public
int
getY1
()
{
return
y1
;
}
// end method getY1
// set the y-coordinate of the second point
public
void
setY2
(
int
y2
)
{
this
.
y2
=
(
y2
>=
0
?
y2
:
0
);
}
// end method setY2
// get the y-coordinate of the second point
public
int
getY2
()
{
return
y2
;
}
// end method getY2
// set the color
public
void
setColor
(
Color
color
)
{
myColor
=
color
;
}
// end method setColor
// get the color
public
Color
getColor
()
{
return
myColor
;
}
// end method getColor
// abstract draw method
public
abstract
void
draw
(
Graphics
g
);
}
// end class MyShape
/**************************************************************************
* (C) Copyright 1992-2014 by Deitel & Associates, Inc. and *
* Pearson Education, Inc. All Rights Reserved. *
* *
* DISCLAIMER: The authors and publisher of this book have used their *
* best efforts in preparing the book. These efforts include the *
* development, research, and testing of the theories and programs *
* to determine their effectiveness. The authors and publisher make *
* no warranty of any kind, expressed or implied, with regard to these *
* programs or to the documentation contained in these books. The authors *
* and publisher shall not be liable in any event for incidental or *
* consequential damages in connection with, or arising out of, the *
* furnishing, performance, or use of these programs. *
**************************************************************************/
Chapter 1/painter/Painter.class
public
synchronized
class Painter {
public void Painter();
public
static void
main(String[]);
}
Chapter 1/painter/Painter.java
Chapter 1/painter/Painter.java
// GCS Exercise 14.17 Solution: Painter.java
// Test application to display a DrawFrame
import
javax
.
swing
.
JFrame
;
public
class
Painter
{
public
static
void
main
(
String
[]
args
)
{
DrawFrame
application
=
new
DrawFrame
();
application
.
setDefaultCloseOperation
(
JFrame
.
EXIT_ON_CLOSE
);
application
.
setSize
(
500
,
400
);
application
.
setVisible
(
true
);
}
// end main
}
// end class Painter
/**************************************************************************
* (C) Copyright 1992-2014 by Deitel & Associates, Inc. and *
* Pearson Education, Inc. All Rights Reserved. *
* *
* DISCLAIMER: The authors and publisher of this book have used their *
* best efforts in preparing the book. These efforts include the *
* development, research, and testing of the theories and programs *
* to determine their effectiveness. The authors and publisher make *
* no warranty of any kind, expressed or implied, with regard to these *
* programs or to the documentation contained in these books. The authors *
* and publisher shall not be liable in any event for incidental or *
* consequential damages in connection with, or arising out of, the *
* furnishing, performance, or use of these programs. *
**************************************************************************/
Chapter 1/Tic-Tac-Toe/TicTacToe$Status.class
final
synchronized
enum TicTacToe$Status {
public
static
final TicTacToe$Status
WIN;
public
static
final TicTacToe$Status
DRAW;
public
static
final TicTacToe$Status
CONTINUE;
public
static
final TicTacToe$Status[]
values();
public
static TicTacToe$Status
valueOf(String);
private void TicTacToe$Status(String, int);
static void
}
Chapter 1/Tic-Tac-Toe/TicTacToe.class
public
synchronized
class TicTacToe {
private
final int
BOARDSIZE;
private int[][]
board;
private boolean
firstPlayer;
private boolean
gameOver;
public void TicTacToe();
public void
play();
public void
printStatus(int);
public TicTacToe$Status
gameStatus();
public void
printBoard();
public void
printSymbol(int, int);
public boolean
validMove(int, int);
}
Chapter 1/Tic-Tac-Toe/TicTacToeTest.class
public
synchronized
class TicTacToeTest {
public void TicTacToeTest();
public
static void
main(String[]);
}