first commit

This commit is contained in:
Boris
2024-01-15 20:14:10 +00:00
commit 8c81ee28b7
3106 changed files with 474415 additions and 0 deletions

View File

@@ -0,0 +1,8 @@
#BlueJ class context
comment0.params=
comment0.target=BallDemo()
comment0.text=\n\ Create\ a\ BallDemo\ object.\ Creates\ a\ fresh\ canvas\ and\ makes\ it\ visible.\n
comment1.params=
comment1.target=void\ bounce()
comment1.text=\n\ Simulate\ two\ bouncing\ balls\n
numComments=2

View File

@@ -0,0 +1,54 @@
import java.awt.Color;
/**
* Class BallDemo - a short demonstration showing animation with the
* Canvas class.
*
* @author Michael Kölling and David J. Barnes
* @version 2016.02.29
*/
public class BallDemo
{
private Canvas myCanvas;
/**
* Create a BallDemo object. Creates a fresh canvas and makes it visible.
*/
public BallDemo()
{
myCanvas = new Canvas("Ball Demo", 600, 500);
}
/**
* Simulate two bouncing balls
*/
public void bounce()
{
int ground = 400; // position of the ground line
myCanvas.setVisible(true);
// draw the ground
myCanvas.setForegroundColor(Color.BLACK);
myCanvas.drawLine(50, ground, 550, ground);
// create and show the balls
BouncingBall ball = new BouncingBall(50, 50, 16, Color.BLUE, ground, myCanvas);
ball.draw();
BouncingBall ball2 = new BouncingBall(70, 80, 20, Color.RED, ground, myCanvas);
ball2.draw();
// make them bounce
boolean finished = false;
while (!finished) {
myCanvas.wait(50); // small delay
ball.move();
ball2.move();
// stop once ball has travelled a certain distance on x axis
if(ball.getXPosition() >= 550 || ball2.getXPosition() >= 550) {
finished = true;
}
}
}
}

View File

@@ -0,0 +1,20 @@
#BlueJ class context
comment0.params=xPos\ yPos\ ballDiameter\ ballColor\ groundPos\ drawingCanvas
comment0.target=BouncingBall(int,\ int,\ int,\ java.awt.Color,\ int,\ Canvas)
comment0.text=\n\ Constructor\ for\ objects\ of\ class\ BouncingBall\n\n\ @param\ xPos\ \ the\ horizontal\ coordinate\ of\ the\ ball\n\ @param\ yPos\ \ the\ vertical\ coordinate\ of\ the\ ball\n\ @param\ ballDiameter\ \ the\ diameter\ (in\ pixels)\ of\ the\ ball\n\ @param\ ballColor\ \ the\ color\ of\ the\ ball\n\ @param\ groundPos\ \ the\ position\ of\ the\ ground\ (where\ the\ wall\ will\ bounce)\n\ @param\ drawingCanvas\ \ the\ canvas\ to\ draw\ this\ ball\ on\n
comment1.params=
comment1.target=void\ draw()
comment1.text=\n\ Draw\ this\ ball\ at\ its\ current\ position\ onto\ the\ canvas.\n
comment2.params=
comment2.target=void\ erase()
comment2.text=\n\ Erase\ this\ ball\ at\ its\ current\ position.\n
comment3.params=
comment3.target=void\ move()
comment3.text=\n\ Move\ this\ ball\ according\ to\ its\ position\ and\ speed\ and\ redraw.\n
comment4.params=
comment4.target=int\ getXPosition()
comment4.text=\n\ return\ the\ horizontal\ position\ of\ this\ ball\n
comment5.params=
comment5.target=int\ getYPosition()
comment5.text=\n\ return\ the\ vertical\ position\ of\ this\ ball\n
numComments=6

View File

@@ -0,0 +1,109 @@
import java.awt.*;
import java.awt.geom.*;
/**
* Class BouncingBall - a graphical ball that observes the effect of gravity. The ball
* has the ability to move. Details of movement are determined by the ball itself. It
* will fall downwards, accelerating with time due to the effect of gravity, and bounce
* upward again when hitting the ground.
*
* This movement can be initiated by repeated calls to the "move" method.
*
* @author Michael Kölling (mik)
* @author David J. Barnes
* @author Bruce Quig
*
* @version 2016.02.29
*/
public class BouncingBall
{
private static final int GRAVITY = 3; // effect of gravity
private int ballDegradation = 2;
private Ellipse2D.Double circle;
private Color color;
private int diameter;
private int xPosition;
private int yPosition;
private final int groundPosition; // y position of ground
private Canvas canvas;
private int ySpeed = 1; // initial downward speed
/**
* Constructor for objects of class BouncingBall
*
* @param xPos the horizontal coordinate of the ball
* @param yPos the vertical coordinate of the ball
* @param ballDiameter the diameter (in pixels) of the ball
* @param ballColor the color of the ball
* @param groundPos the position of the ground (where the wall will bounce)
* @param drawingCanvas the canvas to draw this ball on
*/
public BouncingBall(int xPos, int yPos, int ballDiameter, Color ballColor,
int groundPos, Canvas drawingCanvas)
{
xPosition = xPos;
yPosition = yPos;
color = ballColor;
diameter = ballDiameter;
groundPosition = groundPos;
canvas = drawingCanvas;
}
/**
* Draw this ball at its current position onto the canvas.
**/
public void draw()
{
canvas.setForegroundColor(color);
canvas.fillCircle(xPosition, yPosition, diameter);
}
/**
* Erase this ball at its current position.
**/
public void erase()
{
canvas.eraseCircle(xPosition, yPosition, diameter);
}
/**
* Move this ball according to its position and speed and redraw.
**/
public void move()
{
// remove from canvas at the current position
erase();
// compute new position
ySpeed += GRAVITY;
yPosition += ySpeed;
xPosition +=2;
// check if it has hit the ground
if (yPosition >= (groundPosition - diameter) && ySpeed > 0) {
yPosition = (int)(groundPosition - diameter);
ySpeed = -ySpeed + ballDegradation;
}
// draw again at new position
draw();
}
/**
* return the horizontal position of this ball
*/
public int getXPosition()
{
return xPosition;
}
/**
* return the vertical position of this ball
*/
public int getYPosition()
{
return yPosition;
}
}

View File

@@ -0,0 +1,83 @@
#BlueJ class context
comment0.params=title
comment0.target=Canvas(java.lang.String)
comment0.text=\n\ Create\ a\ Canvas\ with\ default\ height,\ width\ and\ background\ color\ \n\ (300,\ 300,\ white).\n\ @param\ title\ \ title\ to\ appear\ in\ Canvas\ Frame\ \ \ \ \ \n
comment1.params=title\ width\ height
comment1.target=Canvas(java.lang.String,\ int,\ int)
comment1.text=\n\ Create\ a\ Canvas\ with\ default\ background\ color\ (white).\n\ @param\ title\ \ title\ to\ appear\ in\ Canvas\ Frame\n\ @param\ width\ \ the\ desired\ width\ for\ the\ canvas\n\ @param\ height\ \ the\ desired\ height\ for\ the\ canvas\n
comment10.params=xPos\ yPos\ diameter
comment10.target=void\ eraseCircle(int,\ int,\ int)
comment10.text=\n\ Erase\ the\ internal\ dimensions\ of\ the\ given\ circle.\ This\ is\ a\ \n\ convenience\ method.\ A\ similar\ effect\ can\ be\ achieved\ with\n\ the\ "erase"\ method.\n
comment11.params=xPos\ yPos\ width\ height
comment11.target=void\ eraseRectangle(int,\ int,\ int,\ int)
comment11.text=\n\ Erase\ the\ internal\ dimensions\ of\ the\ given\ rectangle.\ This\ is\ a\ \n\ convenience\ method.\ A\ similar\ effect\ can\ be\ achieved\ with\n\ the\ "erase"\ method.\n
comment12.params=shape
comment12.target=void\ erase(java.awt.Shape)
comment12.text=\n\ Erase\ a\ given\ shape's\ interior\ on\ the\ screen.\n\ @param\ \ shape\ \ the\ shape\ object\ to\ be\ erased\ \n
comment13.params=shape
comment13.target=void\ eraseOutline(java.awt.Shape)
comment13.text=\n\ Erases\ a\ given\ shape's\ outline\ on\ the\ screen.\n\ @param\ \ shape\ \ the\ shape\ object\ to\ be\ erased\ \n
comment14.params=image\ x\ y
comment14.target=boolean\ drawImage(java.awt.Image,\ int,\ int)
comment14.text=\n\ Draws\ an\ image\ onto\ the\ canvas.\n\ @param\ \ image\ \ \ the\ Image\ object\ to\ be\ displayed\ \n\ @param\ \ x\ \ \ \ \ \ \ x\ co-ordinate\ for\ Image\ placement\ \n\ @param\ \ y\ \ \ \ \ \ \ y\ co-ordinate\ for\ Image\ placement\ \n\ @return\ \ returns\ boolean\ value\ representing\ whether\ the\ image\ was\ \n\ \ \ \ \ \ \ \ \ \ completely\ loaded\ \n
comment15.params=text\ x\ y
comment15.target=void\ drawString(java.lang.String,\ int,\ int)
comment15.text=\n\ Draws\ a\ String\ on\ the\ Canvas.\n\ @param\ \ text\ \ \ the\ String\ to\ be\ displayed\ \n\ @param\ \ x\ \ \ \ \ \ x\ co-ordinate\ for\ text\ placement\ \n\ @param\ \ y\ \ \ \ \ \ y\ co-ordinate\ for\ text\ placement\n
comment16.params=text\ x\ y
comment16.target=void\ eraseString(java.lang.String,\ int,\ int)
comment16.text=\n\ Erases\ a\ String\ on\ the\ Canvas.\n\ @param\ \ text\ \ \ \ \ the\ String\ to\ be\ displayed\ \n\ @param\ \ x\ \ \ \ \ \ \ \ x\ co-ordinate\ for\ text\ placement\ \n\ @param\ \ y\ \ \ \ \ \ \ \ y\ co-ordinate\ for\ text\ placement\n
comment17.params=x1\ y1\ x2\ y2
comment17.target=void\ drawLine(int,\ int,\ int,\ int)
comment17.text=\n\ Draws\ a\ line\ on\ the\ Canvas.\n\ @param\ \ x1\ \ \ x\ co-ordinate\ of\ start\ of\ line\ \n\ @param\ \ y1\ \ \ y\ co-ordinate\ of\ start\ of\ line\ \n\ @param\ \ x2\ \ \ x\ co-ordinate\ of\ end\ of\ line\ \n\ @param\ \ y2\ \ \ y\ co-ordinate\ of\ end\ of\ line\ \n
comment18.params=newColor
comment18.target=void\ setForegroundColor(java.awt.Color)
comment18.text=\n\ Sets\ the\ foreground\ color\ of\ the\ Canvas.\n\ @param\ \ newColor\ \ \ the\ new\ color\ for\ the\ foreground\ of\ the\ Canvas\ \n
comment19.params=
comment19.target=java.awt.Color\ getForegroundColor()
comment19.text=\n\ Returns\ the\ current\ color\ of\ the\ foreground.\n\ @return\ \ \ the\ color\ of\ the\ foreground\ of\ the\ Canvas\ \n
comment2.params=title\ width\ height\ bgColor
comment2.target=Canvas(java.lang.String,\ int,\ int,\ java.awt.Color)
comment2.text=\n\ Create\ a\ Canvas.\n\ @param\ title\ \ title\ to\ appear\ in\ Canvas\ Frame\n\ @param\ width\ \ the\ desired\ width\ for\ the\ canvas\n\ @param\ height\ \ the\ desired\ height\ for\ the\ canvas\n\ @param\ bgClour\ \ the\ desired\ background\ color\ of\ the\ canvas\n
comment20.params=newColor
comment20.target=void\ setBackgroundColor(java.awt.Color)
comment20.text=\n\ Sets\ the\ background\ color\ of\ the\ Canvas.\n\ @param\ \ newColor\ \ \ the\ new\ color\ for\ the\ background\ of\ the\ Canvas\ \n
comment21.params=
comment21.target=java.awt.Color\ getBackgroundColor()
comment21.text=\n\ Returns\ the\ current\ color\ of\ the\ background\n\ @return\ \ \ the\ color\ of\ the\ background\ of\ the\ Canvas\ \n
comment22.params=newFont
comment22.target=void\ setFont(java.awt.Font)
comment22.text=\n\ changes\ the\ current\ Font\ used\ on\ the\ Canvas\n\ @param\ \ newFont\ \ \ new\ font\ to\ be\ used\ for\ String\ output\n
comment23.params=
comment23.target=java.awt.Font\ getFont()
comment23.text=\n\ Returns\ the\ current\ font\ of\ the\ canvas.\n\ @return\ \ \ \ \ the\ font\ currently\ in\ use\n
comment24.params=width\ height
comment24.target=void\ setSize(int,\ int)
comment24.text=\n\ Sets\ the\ size\ of\ the\ canvas.\n\ @param\ \ width\ \ \ \ new\ width\ \n\ @param\ \ height\ \ \ new\ height\ \n
comment25.params=
comment25.target=java.awt.Dimension\ getSize()
comment25.text=\n\ Returns\ the\ size\ of\ the\ canvas.\n\ @return\ \ \ \ \ The\ current\ dimension\ of\ the\ canvas\n
comment26.params=milliseconds
comment26.target=void\ wait(int)
comment26.text=\n\ Waits\ for\ a\ specified\ number\ of\ milliseconds\ before\ finishing.\n\ This\ provides\ an\ easy\ way\ to\ specify\ a\ small\ delay\ which\ can\ be\n\ used\ when\ producing\ animations.\n\ @param\ \ milliseconds\ \ the\ number\ \n
comment3.params=visible
comment3.target=void\ setVisible(boolean)
comment3.text=\n\ Set\ the\ canvas\ visibility\ and\ brings\ canvas\ to\ the\ front\ of\ screen\n\ when\ made\ visible.\ This\ method\ can\ also\ be\ used\ to\ bring\ an\ already\n\ visible\ canvas\ to\ the\ front\ of\ other\ windows.\n\ @param\ visible\ \ boolean\ value\ representing\ the\ desired\ visibility\ of\n\ the\ canvas\ (true\ or\ false)\ \n
comment4.params=
comment4.target=boolean\ isVisible()
comment4.text=\n\ Provide\ information\ on\ visibility\ of\ the\ Canvas.\n\ @return\ \ true\ if\ canvas\ is\ visible,\ false\ otherwise\n
comment5.params=shape
comment5.target=void\ draw(java.awt.Shape)
comment5.text=\n\ Draw\ the\ outline\ of\ a\ given\ shape\ onto\ the\ canvas.\n\ @param\ \ shape\ \ the\ shape\ object\ to\ be\ drawn\ on\ the\ canvas\n
comment6.params=shape
comment6.target=void\ fill(java.awt.Shape)
comment6.text=\n\ Fill\ the\ internal\ dimensions\ of\ a\ given\ shape\ with\ the\ current\ \n\ foreground\ color\ of\ the\ canvas.\n\ @param\ \ shape\ \ the\ shape\ object\ to\ be\ filled\ \n
comment7.params=xPos\ yPos\ diameter
comment7.target=void\ fillCircle(int,\ int,\ int)
comment7.text=\n\ Fill\ the\ internal\ dimensions\ of\ the\ given\ circle\ with\ the\ current\ \n\ foreground\ color\ of\ the\ canvas.\n\ @param\ \ xPos\ \ The\ x-coordinate\ of\ the\ circle\ center\ point\n\ @param\ \ yPos\ \ The\ y-coordinate\ of\ the\ circle\ center\ point\n\ @param\ \ diameter\ \ The\ diameter\ of\ the\ circle\ to\ be\ drawn\n
comment8.params=xPos\ yPos\ width\ height
comment8.target=void\ fillRectangle(int,\ int,\ int,\ int)
comment8.text=\n\ Fill\ the\ internal\ dimensions\ of\ the\ given\ rectangle\ with\ the\ current\ \n\ foreground\ color\ of\ the\ canvas.\ This\ is\ a\ convenience\ method.\ A\ similar\ \n\ effect\ can\ be\ achieved\ with\ the\ "fill"\ method.\n
comment9.params=
comment9.target=void\ erase()
comment9.text=\n\ Erase\ the\ whole\ canvas.\n
numComments=27

View File

@@ -0,0 +1,364 @@
import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
/**
* Class Canvas - a class to allow for simple graphical
* drawing on a canvas.
*
* @author Michael Kölling (mik)
* @author Bruce Quig
*
* @version 2016.02.29
*/
public class Canvas
{
private JFrame frame;
private CanvasPane canvas;
private Graphics2D graphic;
private Color backgroundColor;
private Image canvasImage;
/**
* Create a Canvas with default height, width and background color
* (300, 300, white).
* @param title title to appear in Canvas Frame
*/
public Canvas(String title)
{
this(title, 300, 300, Color.white);
}
/**
* Create a Canvas with default background color (white).
* @param title title to appear in Canvas Frame
* @param width the desired width for the canvas
* @param height the desired height for the canvas
*/
public Canvas(String title, int width, int height)
{
this(title, width, height, Color.white);
}
/**
* Create a Canvas.
* @param title title to appear in Canvas Frame
* @param width the desired width for the canvas
* @param height the desired height for the canvas
* @param bgClour the desired background color of the canvas
*/
public Canvas(String title, int width, int height, Color bgColor)
{
frame = new JFrame();
canvas = new CanvasPane();
frame.setContentPane(canvas);
frame.setTitle(title);
canvas.setPreferredSize(new Dimension(width, height));
backgroundColor = bgColor;
frame.pack();
setVisible(true);
}
/**
* Set the canvas visibility and brings canvas to the front of screen
* when made visible. This method can also be used to bring an already
* visible canvas to the front of other windows.
* @param visible boolean value representing the desired visibility of
* the canvas (true or false)
*/
public void setVisible(boolean visible)
{
if(graphic == null) {
// first time: instantiate the offscreen image and fill it with
// the background color
Dimension size = canvas.getSize();
canvasImage = canvas.createImage(size.width, size.height);
graphic = (Graphics2D)canvasImage.getGraphics();
graphic.setColor(backgroundColor);
graphic.fillRect(0, 0, size.width, size.height);
graphic.setColor(Color.black);
}
frame.setVisible(true);
}
/**
* Provide information on visibility of the Canvas.
* @return true if canvas is visible, false otherwise
*/
public boolean isVisible()
{
return frame.isVisible();
}
/**
* Draw the outline of a given shape onto the canvas.
* @param shape the shape object to be drawn on the canvas
*/
public void draw(Shape shape)
{
graphic.draw(shape);
canvas.repaint();
}
/**
* Fill the internal dimensions of a given shape with the current
* foreground color of the canvas.
* @param shape the shape object to be filled
*/
public void fill(Shape shape)
{
graphic.fill(shape);
canvas.repaint();
}
/**
* Fill the internal dimensions of the given circle with the current
* foreground color of the canvas.
* @param xPos The x-coordinate of the circle center point
* @param yPos The y-coordinate of the circle center point
* @param diameter The diameter of the circle to be drawn
*/
public void fillCircle(int xPos, int yPos, int diameter)
{
Ellipse2D.Double circle = new Ellipse2D.Double(xPos, yPos, diameter, diameter);
fill(circle);
}
/**
* Fill the internal dimensions of the given rectangle with the current
* foreground color of the canvas. This is a convenience method. A similar
* effect can be achieved with the "fill" method.
*/
public void fillRectangle(int xPos, int yPos, int width, int height)
{
fill(new Rectangle(xPos, yPos, width, height));
}
/**
* Erase the whole canvas.
*/
public void erase()
{
Color original = graphic.getColor();
graphic.setColor(backgroundColor);
Dimension size = canvas.getSize();
graphic.fill(new Rectangle(0, 0, size.width, size.height));
graphic.setColor(original);
canvas.repaint();
}
/**
* Erase the internal dimensions of the given circle. This is a
* convenience method. A similar effect can be achieved with
* the "erase" method.
*/
public void eraseCircle(int xPos, int yPos, int diameter)
{
Ellipse2D.Double circle = new Ellipse2D.Double(xPos, yPos, diameter, diameter);
erase(circle);
}
/**
* Erase the internal dimensions of the given rectangle. This is a
* convenience method. A similar effect can be achieved with
* the "erase" method.
*/
public void eraseRectangle(int xPos, int yPos, int width, int height)
{
erase(new Rectangle(xPos, yPos, width, height));
}
/**
* Erase a given shape's interior on the screen.
* @param shape the shape object to be erased
*/
public void erase(Shape shape)
{
Color original = graphic.getColor();
graphic.setColor(backgroundColor);
graphic.fill(shape); // erase by filling background color
graphic.setColor(original);
canvas.repaint();
}
/**
* Erases a given shape's outline on the screen.
* @param shape the shape object to be erased
*/
public void eraseOutline(Shape shape)
{
Color original = graphic.getColor();
graphic.setColor(backgroundColor);
graphic.draw(shape); // erase by drawing background color
graphic.setColor(original);
canvas.repaint();
}
/**
* Draws an image onto the canvas.
* @param image the Image object to be displayed
* @param x x co-ordinate for Image placement
* @param y y co-ordinate for Image placement
* @return returns boolean value representing whether the image was
* completely loaded
*/
public boolean drawImage(Image image, int x, int y)
{
boolean result = graphic.drawImage(image, x, y, null);
canvas.repaint();
return result;
}
/**
* Draws a String on the Canvas.
* @param text the String to be displayed
* @param x x co-ordinate for text placement
* @param y y co-ordinate for text placement
*/
public void drawString(String text, int x, int y)
{
graphic.drawString(text, x, y);
canvas.repaint();
}
/**
* Erases a String on the Canvas.
* @param text the String to be displayed
* @param x x co-ordinate for text placement
* @param y y co-ordinate for text placement
*/
public void eraseString(String text, int x, int y)
{
Color original = graphic.getColor();
graphic.setColor(backgroundColor);
graphic.drawString(text, x, y);
graphic.setColor(original);
canvas.repaint();
}
/**
* Draws a line on the Canvas.
* @param x1 x co-ordinate of start of line
* @param y1 y co-ordinate of start of line
* @param x2 x co-ordinate of end of line
* @param y2 y co-ordinate of end of line
*/
public void drawLine(int x1, int y1, int x2, int y2)
{
graphic.drawLine(x1, y1, x2, y2);
canvas.repaint();
}
/**
* Sets the foreground color of the Canvas.
* @param newColor the new color for the foreground of the Canvas
*/
public void setForegroundColor(Color newColor)
{
graphic.setColor(newColor);
}
/**
* Returns the current color of the foreground.
* @return the color of the foreground of the Canvas
*/
public Color getForegroundColor()
{
return graphic.getColor();
}
/**
* Sets the background color of the Canvas.
* @param newColor the new color for the background of the Canvas
*/
public void setBackgroundColor(Color newColor)
{
backgroundColor = newColor;
graphic.setBackground(newColor);
}
/**
* Returns the current color of the background
* @return the color of the background of the Canvas
*/
public Color getBackgroundColor()
{
return backgroundColor;
}
/**
* changes the current Font used on the Canvas
* @param newFont new font to be used for String output
*/
public void setFont(Font newFont)
{
graphic.setFont(newFont);
}
/**
* Returns the current font of the canvas.
* @return the font currently in use
**/
public Font getFont()
{
return graphic.getFont();
}
/**
* Sets the size of the canvas.
* @param width new width
* @param height new height
*/
public void setSize(int width, int height)
{
canvas.setPreferredSize(new Dimension(width, height));
Image oldImage = canvasImage;
canvasImage = canvas.createImage(width, height);
graphic = (Graphics2D)canvasImage.getGraphics();
graphic.setColor(backgroundColor);
graphic.fillRect(0, 0, width, height);
graphic.drawImage(oldImage, 0, 0, null);
frame.pack();
}
/**
* Returns the size of the canvas.
* @return The current dimension of the canvas
*/
public Dimension getSize()
{
return canvas.getSize();
}
/**
* Waits for a specified number of milliseconds before finishing.
* This provides an easy way to specify a small delay which can be
* used when producing animations.
* @param milliseconds the number
*/
public void wait(int milliseconds)
{
try
{
Thread.sleep(milliseconds);
}
catch (InterruptedException e)
{
// ignoring exception at the moment
}
}
/************************************************************************
* Inner class CanvasPane - the actual canvas component contained in the
* Canvas frame. This is essentially a JPanel with added capability to
* refresh the image drawn on it.
*/
private class CanvasPane extends JPanel
{
public void paint(Graphics g)
{
g.drawImage(canvasImage, 0, 0, null);
}
}
}

View File

@@ -0,0 +1,19 @@
Bouncing Balls - a demonstration of graphical output on a Canvas class.
This project is part of the material for the book
Objects First with Java - A Practical Introduction using BlueJ
Sixth edition
David J. Barnes and Michael Kölling
Pearson Education, 2016
This project is discussed in chapter 6.
This project provides a short graphical demo in the BallDemo class.
This project provides scope for extension. The Canvas class does not
usually need to be changed. It may be best to treat it as a library
class (open the editor and switch to "documentation" view).
Other classes can be changed or added. The book contains suggestions
for various exercises.

View File

@@ -0,0 +1,64 @@
#BlueJ package file
dependency1.from=BallDemo
dependency1.to=Canvas
dependency1.type=UsesDependency
dependency2.from=BouncingBall
dependency2.to=Canvas
dependency2.type=UsesDependency
dependency3.from=BallDemo
dependency3.to=BouncingBall
dependency3.type=UsesDependency
objectbench.height=76
objectbench.width=724
package.editor.height=394
package.editor.width=616
package.editor.x=70
package.editor.y=80
package.numDependencies=3
package.numTargets=3
package.showExtends=true
package.showUses=true
project.charset=UTF-8
readme.editor.height=707
readme.editor.width=917
readme.editor.x=65
readme.editor.y=79
target1.editor.height=714
target1.editor.width=1075
target1.editor.x=53
target1.editor.y=42
target1.height=60
target1.name=BallDemo
target1.naviview.expanded=true
target1.showInterface=false
target1.type=ClassTarget
target1.typeParameters=
target1.width=110
target1.x=110
target1.y=50
target2.editor.height=776
target2.editor.width=931
target2.editor.x=78
target2.editor.y=23
target2.height=60
target2.name=BouncingBall
target2.naviview.expanded=true
target2.showInterface=false
target2.type=ClassTarget
target2.typeParameters=
target2.width=110
target2.x=260
target2.y=140
target3.editor.height=756
target3.editor.width=948
target3.editor.x=53
target3.editor.y=51
target3.height=60
target3.name=Canvas
target3.naviview.expanded=true
target3.showInterface=false
target3.type=ClassTarget
target3.typeParameters=
target3.width=100
target3.x=400
target3.y=230

View File

@@ -0,0 +1,83 @@
#BlueJ class context
comment0.params=title
comment0.target=Canvas(java.lang.String)
comment0.text=\n\ Create\ a\ Canvas\ with\ default\ height,\ width\ and\ background\ color\ \n\ (300,\ 300,\ white).\n\ @param\ title\ \ title\ to\ appear\ in\ Canvas\ Frame\ \ \ \ \ \n
comment1.params=title\ width\ height
comment1.target=Canvas(java.lang.String,\ int,\ int)
comment1.text=\n\ Create\ a\ Canvas\ with\ default\ background\ color\ (white).\n\ @param\ title\ \ title\ to\ appear\ in\ Canvas\ Frame\n\ @param\ width\ \ the\ desired\ width\ for\ the\ canvas\n\ @param\ height\ \ the\ desired\ height\ for\ the\ canvas\n
comment10.params=xPos\ yPos\ diameter
comment10.target=void\ eraseCircle(int,\ int,\ int)
comment10.text=\n\ Erase\ the\ internal\ dimensions\ of\ the\ given\ circle.\ This\ is\ a\ \n\ convenience\ method.\ A\ similar\ effect\ can\ be\ achieved\ with\n\ the\ "erase"\ method.\n
comment11.params=xPos\ yPos\ width\ height
comment11.target=void\ eraseRectangle(int,\ int,\ int,\ int)
comment11.text=\n\ Erase\ the\ internal\ dimensions\ of\ the\ given\ rectangle.\ This\ is\ a\ \n\ convenience\ method.\ A\ similar\ effect\ can\ be\ achieved\ with\n\ the\ "erase"\ method.\n
comment12.params=shape
comment12.target=void\ erase(java.awt.Shape)
comment12.text=\n\ Erase\ a\ given\ shape's\ interior\ on\ the\ screen.\n\ @param\ \ shape\ \ the\ shape\ object\ to\ be\ erased\ \n
comment13.params=shape
comment13.target=void\ eraseOutline(java.awt.Shape)
comment13.text=\n\ Erases\ a\ given\ shape's\ outline\ on\ the\ screen.\n\ @param\ \ shape\ \ the\ shape\ object\ to\ be\ erased\ \n
comment14.params=image\ x\ y
comment14.target=boolean\ drawImage(java.awt.Image,\ int,\ int)
comment14.text=\n\ Draws\ an\ image\ onto\ the\ canvas.\n\ @param\ \ image\ \ \ the\ Image\ object\ to\ be\ displayed\ \n\ @param\ \ x\ \ \ \ \ \ \ x\ co-ordinate\ for\ Image\ placement\ \n\ @param\ \ y\ \ \ \ \ \ \ y\ co-ordinate\ for\ Image\ placement\ \n\ @return\ \ returns\ boolean\ value\ representing\ whether\ the\ image\ was\ \n\ \ \ \ \ \ \ \ \ \ completely\ loaded\ \n
comment15.params=text\ x\ y
comment15.target=void\ drawString(java.lang.String,\ int,\ int)
comment15.text=\n\ Draws\ a\ String\ on\ the\ Canvas.\n\ @param\ \ text\ \ \ the\ String\ to\ be\ displayed\ \n\ @param\ \ x\ \ \ \ \ \ x\ co-ordinate\ for\ text\ placement\ \n\ @param\ \ y\ \ \ \ \ \ y\ co-ordinate\ for\ text\ placement\n
comment16.params=text\ x\ y
comment16.target=void\ eraseString(java.lang.String,\ int,\ int)
comment16.text=\n\ Erases\ a\ String\ on\ the\ Canvas.\n\ @param\ \ text\ \ \ \ \ the\ String\ to\ be\ displayed\ \n\ @param\ \ x\ \ \ \ \ \ \ \ x\ co-ordinate\ for\ text\ placement\ \n\ @param\ \ y\ \ \ \ \ \ \ \ y\ co-ordinate\ for\ text\ placement\n
comment17.params=x1\ y1\ x2\ y2
comment17.target=void\ drawLine(int,\ int,\ int,\ int)
comment17.text=\n\ Draws\ a\ line\ on\ the\ Canvas.\n\ @param\ \ x1\ \ \ x\ co-ordinate\ of\ start\ of\ line\ \n\ @param\ \ y1\ \ \ y\ co-ordinate\ of\ start\ of\ line\ \n\ @param\ \ x2\ \ \ x\ co-ordinate\ of\ end\ of\ line\ \n\ @param\ \ y2\ \ \ y\ co-ordinate\ of\ end\ of\ line\ \n
comment18.params=newColor
comment18.target=void\ setForegroundColor(java.awt.Color)
comment18.text=\n\ Sets\ the\ foreground\ color\ of\ the\ Canvas.\n\ @param\ \ newColor\ \ \ the\ new\ color\ for\ the\ foreground\ of\ the\ Canvas\ \n
comment19.params=
comment19.target=java.awt.Color\ getForegroundColor()
comment19.text=\n\ Returns\ the\ current\ color\ of\ the\ foreground.\n\ @return\ \ \ the\ color\ of\ the\ foreground\ of\ the\ Canvas\ \n
comment2.params=title\ width\ height\ bgColor
comment2.target=Canvas(java.lang.String,\ int,\ int,\ java.awt.Color)
comment2.text=\n\ Create\ a\ Canvas.\n\ @param\ title\ \ title\ to\ appear\ in\ Canvas\ Frame\n\ @param\ width\ \ the\ desired\ width\ for\ the\ canvas\n\ @param\ height\ \ the\ desired\ height\ for\ the\ canvas\n\ @param\ bgClour\ \ the\ desired\ background\ color\ of\ the\ canvas\n
comment20.params=newColor
comment20.target=void\ setBackgroundColor(java.awt.Color)
comment20.text=\n\ Sets\ the\ background\ color\ of\ the\ Canvas.\n\ @param\ \ newColor\ \ \ the\ new\ color\ for\ the\ background\ of\ the\ Canvas\ \n
comment21.params=
comment21.target=java.awt.Color\ getBackgroundColor()
comment21.text=\n\ Returns\ the\ current\ color\ of\ the\ background\n\ @return\ \ \ the\ color\ of\ the\ background\ of\ the\ Canvas\ \n
comment22.params=newFont
comment22.target=void\ setFont(java.awt.Font)
comment22.text=\n\ changes\ the\ current\ Font\ used\ on\ the\ Canvas\n\ @param\ \ newFont\ \ \ new\ font\ to\ be\ used\ for\ String\ output\n
comment23.params=
comment23.target=java.awt.Font\ getFont()
comment23.text=\n\ Returns\ the\ current\ font\ of\ the\ canvas.\n\ @return\ \ \ \ \ the\ font\ currently\ in\ use\n
comment24.params=width\ height
comment24.target=void\ setSize(int,\ int)
comment24.text=\n\ Sets\ the\ size\ of\ the\ canvas.\n\ @param\ \ width\ \ \ \ new\ width\ \n\ @param\ \ height\ \ \ new\ height\ \n
comment25.params=
comment25.target=java.awt.Dimension\ getSize()
comment25.text=\n\ Returns\ the\ size\ of\ the\ canvas.\n\ @return\ \ \ \ \ The\ current\ dimension\ of\ the\ canvas\n
comment26.params=milliseconds
comment26.target=void\ wait(int)
comment26.text=\n\ Waits\ for\ a\ specified\ number\ of\ milliseconds\ before\ finishing.\n\ This\ provides\ an\ easy\ way\ to\ specify\ a\ small\ delay\ which\ can\ be\n\ used\ when\ producing\ animations.\n\ @param\ \ milliseconds\ \ the\ number\ \n
comment3.params=visible
comment3.target=void\ setVisible(boolean)
comment3.text=\n\ Set\ the\ canvas\ visibility\ and\ brings\ canvas\ to\ the\ front\ of\ screen\n\ when\ made\ visible.\ This\ method\ can\ also\ be\ used\ to\ bring\ an\ already\n\ visible\ canvas\ to\ the\ front\ of\ other\ windows.\n\ @param\ visible\ \ boolean\ value\ representing\ the\ desired\ visibility\ of\n\ the\ canvas\ (true\ or\ false)\ \n
comment4.params=
comment4.target=boolean\ isVisible()
comment4.text=\n\ Provide\ information\ on\ visibility\ of\ the\ Canvas.\n\ @return\ \ true\ if\ canvas\ is\ visible,\ false\ otherwise\n
comment5.params=shape
comment5.target=void\ draw(java.awt.Shape)
comment5.text=\n\ Draw\ the\ outline\ of\ a\ given\ shape\ onto\ the\ canvas.\n\ @param\ \ shape\ \ the\ shape\ object\ to\ be\ drawn\ on\ the\ canvas\n
comment6.params=shape
comment6.target=void\ fill(java.awt.Shape)
comment6.text=\n\ Fill\ the\ internal\ dimensions\ of\ a\ given\ shape\ with\ the\ current\ \n\ foreground\ color\ of\ the\ canvas.\n\ @param\ \ shape\ \ the\ shape\ object\ to\ be\ filled\ \n
comment7.params=xPos\ yPos\ diameter
comment7.target=void\ fillCircle(int,\ int,\ int)
comment7.text=\n\ Fill\ the\ internal\ dimensions\ of\ the\ given\ circle\ with\ the\ current\ \n\ foreground\ color\ of\ the\ canvas.\n\ @param\ \ xPos\ \ The\ x-coordinate\ of\ the\ circle\ center\ point\n\ @param\ \ yPos\ \ The\ y-coordinate\ of\ the\ circle\ center\ point\n\ @param\ \ diameter\ \ The\ diameter\ of\ the\ circle\ to\ be\ drawn\n
comment8.params=xPos\ yPos\ width\ height
comment8.target=void\ fillRectangle(int,\ int,\ int,\ int)
comment8.text=\n\ Fill\ the\ internal\ dimensions\ of\ the\ given\ rectangle\ with\ the\ current\ \n\ foreground\ color\ of\ the\ canvas.\ This\ is\ a\ convenience\ method.\ A\ similar\ \n\ effect\ can\ be\ achieved\ with\ the\ "fill"\ method.\n
comment9.params=
comment9.target=void\ erase()
comment9.text=\n\ Erase\ the\ whole\ canvas.\n
numComments=27

View File

@@ -0,0 +1,364 @@
import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
/**
* Class Canvas - a class to allow for simple graphical
* drawing on a canvas.
*
* @author Michael Kölling (mik)
* @author Bruce Quig
*
* @version 2016.02.29
*/
public class Canvas
{
private JFrame frame;
private CanvasPane canvas;
private Graphics2D graphic;
private Color backgroundColor;
private Image canvasImage;
/**
* Create a Canvas with default height, width and background color
* (300, 300, white).
* @param title title to appear in Canvas Frame
*/
public Canvas(String title)
{
this(title, 300, 300, Color.white);
}
/**
* Create a Canvas with default background color (white).
* @param title title to appear in Canvas Frame
* @param width the desired width for the canvas
* @param height the desired height for the canvas
*/
public Canvas(String title, int width, int height)
{
this(title, width, height, Color.white);
}
/**
* Create a Canvas.
* @param title title to appear in Canvas Frame
* @param width the desired width for the canvas
* @param height the desired height for the canvas
* @param bgClour the desired background color of the canvas
*/
public Canvas(String title, int width, int height, Color bgColor)
{
frame = new JFrame();
canvas = new CanvasPane();
frame.setContentPane(canvas);
frame.setTitle(title);
canvas.setPreferredSize(new Dimension(width, height));
backgroundColor = bgColor;
frame.pack();
setVisible(true);
}
/**
* Set the canvas visibility and brings canvas to the front of screen
* when made visible. This method can also be used to bring an already
* visible canvas to the front of other windows.
* @param visible boolean value representing the desired visibility of
* the canvas (true or false)
*/
public void setVisible(boolean visible)
{
if(graphic == null) {
// first time: instantiate the offscreen image and fill it with
// the background color
Dimension size = canvas.getSize();
canvasImage = canvas.createImage(size.width, size.height);
graphic = (Graphics2D)canvasImage.getGraphics();
graphic.setColor(backgroundColor);
graphic.fillRect(0, 0, size.width, size.height);
graphic.setColor(Color.black);
}
frame.setVisible(true);
}
/**
* Provide information on visibility of the Canvas.
* @return true if canvas is visible, false otherwise
*/
public boolean isVisible()
{
return frame.isVisible();
}
/**
* Draw the outline of a given shape onto the canvas.
* @param shape the shape object to be drawn on the canvas
*/
public void draw(Shape shape)
{
graphic.draw(shape);
canvas.repaint();
}
/**
* Fill the internal dimensions of a given shape with the current
* foreground color of the canvas.
* @param shape the shape object to be filled
*/
public void fill(Shape shape)
{
graphic.fill(shape);
canvas.repaint();
}
/**
* Fill the internal dimensions of the given circle with the current
* foreground color of the canvas.
* @param xPos The x-coordinate of the circle center point
* @param yPos The y-coordinate of the circle center point
* @param diameter The diameter of the circle to be drawn
*/
public void fillCircle(int xPos, int yPos, int diameter)
{
Ellipse2D.Double circle = new Ellipse2D.Double(xPos, yPos, diameter, diameter);
fill(circle);
}
/**
* Fill the internal dimensions of the given rectangle with the current
* foreground color of the canvas. This is a convenience method. A similar
* effect can be achieved with the "fill" method.
*/
public void fillRectangle(int xPos, int yPos, int width, int height)
{
fill(new Rectangle(xPos, yPos, width, height));
}
/**
* Erase the whole canvas.
*/
public void erase()
{
Color original = graphic.getColor();
graphic.setColor(backgroundColor);
Dimension size = canvas.getSize();
graphic.fill(new Rectangle(0, 0, size.width, size.height));
graphic.setColor(original);
canvas.repaint();
}
/**
* Erase the internal dimensions of the given circle. This is a
* convenience method. A similar effect can be achieved with
* the "erase" method.
*/
public void eraseCircle(int xPos, int yPos, int diameter)
{
Ellipse2D.Double circle = new Ellipse2D.Double(xPos, yPos, diameter, diameter);
erase(circle);
}
/**
* Erase the internal dimensions of the given rectangle. This is a
* convenience method. A similar effect can be achieved with
* the "erase" method.
*/
public void eraseRectangle(int xPos, int yPos, int width, int height)
{
erase(new Rectangle(xPos, yPos, width, height));
}
/**
* Erase a given shape's interior on the screen.
* @param shape the shape object to be erased
*/
public void erase(Shape shape)
{
Color original = graphic.getColor();
graphic.setColor(backgroundColor);
graphic.fill(shape); // erase by filling background color
graphic.setColor(original);
canvas.repaint();
}
/**
* Erases a given shape's outline on the screen.
* @param shape the shape object to be erased
*/
public void eraseOutline(Shape shape)
{
Color original = graphic.getColor();
graphic.setColor(backgroundColor);
graphic.draw(shape); // erase by drawing background color
graphic.setColor(original);
canvas.repaint();
}
/**
* Draws an image onto the canvas.
* @param image the Image object to be displayed
* @param x x co-ordinate for Image placement
* @param y y co-ordinate for Image placement
* @return returns boolean value representing whether the image was
* completely loaded
*/
public boolean drawImage(Image image, int x, int y)
{
boolean result = graphic.drawImage(image, x, y, null);
canvas.repaint();
return result;
}
/**
* Draws a String on the Canvas.
* @param text the String to be displayed
* @param x x co-ordinate for text placement
* @param y y co-ordinate for text placement
*/
public void drawString(String text, int x, int y)
{
graphic.drawString(text, x, y);
canvas.repaint();
}
/**
* Erases a String on the Canvas.
* @param text the String to be displayed
* @param x x co-ordinate for text placement
* @param y y co-ordinate for text placement
*/
public void eraseString(String text, int x, int y)
{
Color original = graphic.getColor();
graphic.setColor(backgroundColor);
graphic.drawString(text, x, y);
graphic.setColor(original);
canvas.repaint();
}
/**
* Draws a line on the Canvas.
* @param x1 x co-ordinate of start of line
* @param y1 y co-ordinate of start of line
* @param x2 x co-ordinate of end of line
* @param y2 y co-ordinate of end of line
*/
public void drawLine(int x1, int y1, int x2, int y2)
{
graphic.drawLine(x1, y1, x2, y2);
canvas.repaint();
}
/**
* Sets the foreground color of the Canvas.
* @param newColor the new color for the foreground of the Canvas
*/
public void setForegroundColor(Color newColor)
{
graphic.setColor(newColor);
}
/**
* Returns the current color of the foreground.
* @return the color of the foreground of the Canvas
*/
public Color getForegroundColor()
{
return graphic.getColor();
}
/**
* Sets the background color of the Canvas.
* @param newColor the new color for the background of the Canvas
*/
public void setBackgroundColor(Color newColor)
{
backgroundColor = newColor;
graphic.setBackground(newColor);
}
/**
* Returns the current color of the background
* @return the color of the background of the Canvas
*/
public Color getBackgroundColor()
{
return backgroundColor;
}
/**
* changes the current Font used on the Canvas
* @param newFont new font to be used for String output
*/
public void setFont(Font newFont)
{
graphic.setFont(newFont);
}
/**
* Returns the current font of the canvas.
* @return the font currently in use
**/
public Font getFont()
{
return graphic.getFont();
}
/**
* Sets the size of the canvas.
* @param width new width
* @param height new height
*/
public void setSize(int width, int height)
{
canvas.setPreferredSize(new Dimension(width, height));
Image oldImage = canvasImage;
canvasImage = canvas.createImage(width, height);
graphic = (Graphics2D)canvasImage.getGraphics();
graphic.setColor(backgroundColor);
graphic.fillRect(0, 0, width, height);
graphic.drawImage(oldImage, 0, 0, null);
frame.pack();
}
/**
* Returns the size of the canvas.
* @return The current dimension of the canvas
*/
public Dimension getSize()
{
return canvas.getSize();
}
/**
* Waits for a specified number of milliseconds before finishing.
* This provides an easy way to specify a small delay which can be
* used when producing animations.
* @param milliseconds the number
*/
public void wait(int milliseconds)
{
try
{
Thread.sleep(milliseconds);
}
catch (InterruptedException e)
{
// ignoring exception at the moment
}
}
/************************************************************************
* Inner class CanvasPane - the actual canvas component contained in the
* Canvas frame. This is essentially a JPanel with added capability to
* refresh the image drawn on it.
*/
private class CanvasPane extends JPanel
{
public void paint(Graphics g)
{
g.drawImage(canvasImage, 0, 0, null);
}
}
}

View File

@@ -0,0 +1,20 @@
#BlueJ class context
comment0.params=
comment0.target=DrawDemo()
comment0.text=\n\ Prepare\ the\ drawing\ demo.\ Create\ a\ fresh\ canvas\ and\ make\ it\ visible.\n
comment1.params=
comment1.target=void\ drawSquare()
comment1.text=\n\ Draw\ a\ square\ on\ the\ screen.\n
comment2.params=
comment2.target=void\ drawWheel()
comment2.text=\n\ Draw\ a\ wheel\ made\ of\ many\ squares.\n
comment3.params=pen
comment3.target=void\ square(Pen)
comment3.text=\n\ Draw\ a\ square\ in\ the\ pen's\ color\ at\ the\ pen's\ location.\n
comment4.params=
comment4.target=void\ colorScribble()
comment4.text=\n\ Draw\ some\ random\ squiggles\ on\ the\ screen,\ in\ random\ colors.\n
comment5.params=
comment5.target=void\ clear()
comment5.text=\n\ Clear\ the\ screen.\n
numComments=6

View File

@@ -0,0 +1,87 @@
import java.awt.Color;
import java.util.Random;
/**
* Class DrawDemo - provides some short demonstrations showing how to use the
* Pen class to create various drawings.
*
* @author Michael Kölling and David J. Barnes
* @version 2016.02.29
*/
public class DrawDemo
{
private Canvas myCanvas;
private Random random;
/**
* Prepare the drawing demo. Create a fresh canvas and make it visible.
*/
public DrawDemo()
{
myCanvas = new Canvas("Drawing Demo", 500, 400);
random = new Random();
}
/**
* Draw a square on the screen.
*/
public void drawSquare()
{
Pen pen = new Pen(320, 260, myCanvas);
pen.setColor(Color.BLUE);
square(pen);
}
/**
* Draw a wheel made of many squares.
*/
public void drawWheel()
{
Pen pen = new Pen(250, 200, myCanvas);
pen.setColor(Color.RED);
for (int i=0; i<36; i++) {
square(pen);
pen.turn(10);
}
}
/**
* Draw a square in the pen's color at the pen's location.
*/
private void square(Pen pen)
{
for (int i=0; i<4; i++) {
pen.move(100);
pen.turn(90);
}
}
/**
* Draw some random squiggles on the screen, in random colors.
*/
public void colorScribble()
{
Pen pen = new Pen(250, 200, myCanvas);
for (int i=0; i<10; i++) {
// pick a random color
int red = random.nextInt(256);
int green = random.nextInt(256);
int blue = random.nextInt(256);
pen.setColor(new Color(red, green, blue));
pen.randomSquiggle();
}
}
/**
* Clear the screen.
*/
public void clear()
{
myCanvas.erase();
}
}

View File

@@ -0,0 +1,32 @@
#BlueJ class context
comment0.params=
comment0.target=Pen()
comment0.text=\n\ Create\ a\ new\ Pen\ with\ its\ own\ canvas.\ The\ pen\ will\ create\ a\ new\ canvas\ for\ \n\ itself\ to\ draw\ on,\ and\ start\ in\ the\ default\ state\ (centre\ of\ canvas,\ direction\n\ right,\ color\ black,\ pen\ down).\n
comment1.params=xPos\ yPos\ drawingCanvas
comment1.target=Pen(int,\ int,\ Canvas)
comment1.text=\n\ Create\ a\ new\ Pen\ for\ a\ given\ canvas.\ The\ direction\ is\ initially\ 0\ (to\ the\ right),\n\ the\ color\ is\ black,\ and\ the\ pen\ is\ down.\n\n\ @param\ xPos\ \ the\ initial\ horizontal\ coordinate\ of\ the\ pen\n\ @param\ yPos\ \ the\ initial\ vertical\ coordinate\ of\ the\ pen\n\ @param\ drawingCanvas\ \ the\ canvas\ to\ draw\ on\n
comment2.params=distance
comment2.target=void\ move(int)
comment2.text=\n\ Move\ the\ specified\ distance\ in\ the\ current\ direction.\ If\ the\ pen\ is\ down,\ \n\ leave\ a\ line\ on\ the\ canvas.\n\ \n\ @param\ distance\ \ The\ distance\ to\ move\ forward\ from\ the\ current\ location.\n
comment3.params=x\ y
comment3.target=void\ moveTo(int,\ int)
comment3.text=\n\ Move\ to\ the\ specified\ location.\ If\ the\ pen\ is\ down,\ leave\ a\ line\ on\ the\ canvas.\n\ \n\ @param\ x\ \ \ The\ x-coordinate\ to\ move\ to.\n\ @param\ y\ \ \ The\ y-coordinate\ to\ move\ to.\n
comment4.params=degrees
comment4.target=void\ turn(int)
comment4.text=\n\ Turn\ the\ specified\ amount\ (out\ of\ a\ 360\ degree\ circle)\ clockwise\ from\ the\ current\ \n\ rotation.\n\ \n\ @param\ degrees\ \ The\ amount\ of\ degrees\ to\ turn.\ (360\ is\ a\ full\ circle.)\n
comment5.params=angle
comment5.target=void\ turnTo(int)
comment5.text=\n\ Turn\ to\ the\ specified\ direction.\ 0\ is\ right,\ 90\ is\ down,\ 180\ is\ left,\ 270\ is\ up.\n\ \n\ @param\ angle\ \ The\ angle\ to\ turn\ to.\n
comment6.params=newColor
comment6.target=void\ setColor(java.awt.Color)
comment6.text=\n\ Set\ the\ drawing\ color.\n\ \n\ @param\ newColor\ \ The\ color\ to\ use\ for\ subsequent\ drawing\ operations.\n
comment7.params=
comment7.target=void\ penUp()
comment7.text=\n\ Lift\ the\ pen\ up.\ Moving\ afterwards\ will\ not\ leave\ a\ line\ on\ the\ canvas.\n
comment8.params=
comment8.target=void\ penDown()
comment8.text=\n\ Put\ the\ pen\ down.\ Moving\ afterwards\ will\ leave\ a\ line\ on\ the\ canvas.\n
comment9.params=
comment9.target=void\ randomSquiggle()
comment9.text=\n\ Scribble\ on\ the\ canvas\ in\ the\ current\ color.\ The\ size\ and\ complexity\ of\ the\ \n\ squiggle\ produced\ is\ defined\ by\ the\ constants\ SQIGGLE_SIZE\ and\ SQIGGLE_COUNT.\n
numComments=10

View File

@@ -0,0 +1,148 @@
import java.awt.Color;
import java.util.Random;
/**
* A pen can be used to draw on a canvas. The pen maintains a position, direction, color,
* and an up/down state. The pen can be moved across the canvas. If the pen is down, it
* leaves a line on the canvas when moved. (If it is up, it will not draw a line.)
*
* @author Michael Kölling & David J. Barnes
* @version 2016.02.29
*/
public class Pen
{
// constants for randomSquiggle method
private static final int SQIGGLE_SIZE = 40;
private static final int SQIGGLE_COUNT = 30;
private int xPosition;
private int yPosition;
private int rotation;
private Color color;
private boolean penDown;
private Canvas canvas;
private Random random;
/**
* Create a new Pen with its own canvas. The pen will create a new canvas for
* itself to draw on, and start in the default state (centre of canvas, direction
* right, color black, pen down).
*/
public Pen()
{
this (280, 220, new Canvas("My Canvas", 560, 440));
}
/**
* Create a new Pen for a given canvas. The direction is initially 0 (to the right),
* the color is black, and the pen is down.
*
* @param xPos the initial horizontal coordinate of the pen
* @param yPos the initial vertical coordinate of the pen
* @param drawingCanvas the canvas to draw on
*/
public Pen(int xPos, int yPos, Canvas drawingCanvas)
{
xPosition = xPos;
yPosition = yPos;
rotation = 0;
penDown = true;
color = Color.BLACK;
canvas = drawingCanvas;
random = new Random();
}
/**
* Move the specified distance in the current direction. If the pen is down,
* leave a line on the canvas.
*
* @param distance The distance to move forward from the current location.
*/
public void move(int distance)
{
double angle = Math.toRadians(rotation);
int newX = (int) Math.round(xPosition + Math.cos(angle) * distance);
int newY = (int) Math.round(yPosition + Math.sin(angle) * distance);
moveTo(newX, newY);
}
/**
* Move to the specified location. If the pen is down, leave a line on the canvas.
*
* @param x The x-coordinate to move to.
* @param y The y-coordinate to move to.
*/
public void moveTo(int x, int y)
{
if (penDown) {
canvas.setForegroundColor(color);
canvas.drawLine(xPosition, yPosition, x, y);
}
xPosition = x;
yPosition = y;
}
/**
* Turn the specified amount (out of a 360 degree circle) clockwise from the current
* rotation.
*
* @param degrees The amount of degrees to turn. (360 is a full circle.)
*/
public void turn(int degrees)
{
rotation = rotation + degrees;
}
/**
* Turn to the specified direction. 0 is right, 90 is down, 180 is left, 270 is up.
*
* @param angle The angle to turn to.
*/
public void turnTo(int angle)
{
rotation = angle;
}
/**
* Set the drawing color.
*
* @param newColor The color to use for subsequent drawing operations.
*/
public void setColor(Color newColor)
{
color = newColor;
}
/**
* Lift the pen up. Moving afterwards will not leave a line on the canvas.
*/
public void penUp()
{
penDown = false;
}
/**
* Put the pen down. Moving afterwards will leave a line on the canvas.
*/
public void penDown()
{
penDown = true;
}
/**
* Scribble on the canvas in the current color. The size and complexity of the
* squiggle produced is defined by the constants SQIGGLE_SIZE and SQIGGLE_COUNT.
*/
public void randomSquiggle()
{
for (int i=0; i<SQIGGLE_COUNT; i++) {
move(random.nextInt(SQIGGLE_SIZE));
turn(160 + random.nextInt(40));
}
}
}

View File

@@ -0,0 +1,19 @@
Scribble - a demonstration of graphical output on a Canvas class.
This project is part of the material for the book
Objects First with Java - A Practical Introduction using BlueJ
Sixth edition
David J. Barnes and Michael Kölling
Pearson Education, 2016
This project is discussed in chapter 6.
This project provides some short graphical demos in the DrawDemo class.
This project provides great scope for extension. The Canvas class does
not usually need to be changed. It may be best to treat it as a library
class (open the editor and switch to "documentation" view).
Other classes can be changed or added. The book contains suggestions
for various exercises.

View File

@@ -0,0 +1,748 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<!--NewPage-->
<HTML>
<HEAD>
<!-- Generated by javadoc (build 1.8.0_31) on Mon Oct 05 10:02:33 BST 2015 -->
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
<TITLE>
Canvas
</TITLE>
<META NAME="date" CONTENT="2015-10-05">
<LINK REL ="stylesheet" TYPE="text/css" HREF="stylesheet.css" TITLE="Style">
<SCRIPT type="text/javascript">
function windowTitle()
{
if (location.href.indexOf('is-external=true') == -1) {
parent.document.title="Canvas";
}
}
</SCRIPT>
<NOSCRIPT>
</NOSCRIPT>
</HEAD>
<BODY BGCOLOR="white" onload="windowTitle();">
<HR>
<HR>
<!-- ======== START OF CLASS DATA ======== -->
<H2>
Class Canvas</H2>
<PRE>
java.lang.Object
<IMG SRC="./resources/inherit.gif" ALT="extended by "><B>Canvas</B>
</PRE>
<HR>
<DL>
<DT><PRE>public class <B>Canvas</B><DT>extends java.lang.Object</DL>
</PRE>
<P>
Class Canvas - a class to allow for simple graphical
drawing on a canvas.
<P>
<P>
<DL>
<DT><B>Version:</B></DT>
<DD>2016.02.29</DD>
<DT><B>Author:</B></DT>
<DD>Michael Kölling (mik), Bruce Quig</DD>
</DL>
<HR>
<P>
<!-- ======== CONSTRUCTOR SUMMARY ======== -->
<A NAME="constructor_summary"><!-- --></A>
<TABLE BORDER="1" WIDTH="100%" CELLPADDING="3" CELLSPACING="0" SUMMARY="">
<TR BGCOLOR="#CCCCFF" CLASS="TableHeadingColor">
<TH ALIGN="left" COLSPAN="2"><FONT SIZE="+2">
<B>Constructor Summary</B></FONT></TH>
</TR>
<TR BGCOLOR="white" CLASS="TableRowColor">
<TD><CODE><B><A HREF="Canvas.html#Canvas(java.lang.String)">Canvas</A></B>(java.lang.String&nbsp;title)</CODE>
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Create a Canvas with default height, width and background color
(300, 300, white).</TD>
</TR>
<TR BGCOLOR="white" CLASS="TableRowColor">
<TD><CODE><B><A HREF="Canvas.html#Canvas(java.lang.String, int, int)">Canvas</A></B>(java.lang.String&nbsp;title,
int&nbsp;width,
int&nbsp;height)</CODE>
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Create a Canvas with default background color (white).</TD>
</TR>
<TR BGCOLOR="white" CLASS="TableRowColor">
<TD><CODE><B><A HREF="Canvas.html#Canvas(java.lang.String, int, int, java.awt.Color)">Canvas</A></B>(java.lang.String&nbsp;title,
int&nbsp;width,
int&nbsp;height,
java.awt.Color&nbsp;bgColor)</CODE>
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Create a Canvas.</TD>
</TR>
</TABLE>
&nbsp;
<!-- ========== METHOD SUMMARY =========== -->
<A NAME="method_summary"><!-- --></A>
<TABLE BORDER="1" WIDTH="100%" CELLPADDING="3" CELLSPACING="0" SUMMARY="">
<TR BGCOLOR="#CCCCFF" CLASS="TableHeadingColor">
<TH ALIGN="left" COLSPAN="2"><FONT SIZE="+2">
<B>Method Summary</B></FONT></TH>
</TR>
<TR BGCOLOR="white" CLASS="TableRowColor">
<TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
<CODE>&nbsp;void</CODE></FONT></TD>
<TD><CODE><B><A HREF="Canvas.html#draw(java.awt.Shape)">draw</A></B>(java.awt.Shape&nbsp;shape)</CODE>
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Draw the outline of a given shape onto the canvas.</TD>
</TR>
<TR BGCOLOR="white" CLASS="TableRowColor">
<TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
<CODE>&nbsp;boolean</CODE></FONT></TD>
<TD><CODE><B><A HREF="Canvas.html#drawImage(java.awt.Image, int, int)">drawImage</A></B>(java.awt.Image&nbsp;image,
int&nbsp;x,
int&nbsp;y)</CODE>
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Draws an image onto the canvas.</TD>
</TR>
<TR BGCOLOR="white" CLASS="TableRowColor">
<TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
<CODE>&nbsp;void</CODE></FONT></TD>
<TD><CODE><B><A HREF="Canvas.html#drawLine(int, int, int, int)">drawLine</A></B>(int&nbsp;x1,
int&nbsp;y1,
int&nbsp;x2,
int&nbsp;y2)</CODE>
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Draws a line on the Canvas.</TD>
</TR>
<TR BGCOLOR="white" CLASS="TableRowColor">
<TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
<CODE>&nbsp;void</CODE></FONT></TD>
<TD><CODE><B><A HREF="Canvas.html#drawString(java.lang.String, int, int)">drawString</A></B>(java.lang.String&nbsp;text,
int&nbsp;x,
int&nbsp;y)</CODE>
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Draws a String on the Canvas.</TD>
</TR>
<TR BGCOLOR="white" CLASS="TableRowColor">
<TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
<CODE>&nbsp;void</CODE></FONT></TD>
<TD><CODE><B><A HREF="Canvas.html#erase()">erase</A></B>()</CODE>
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Erase the whole canvas.</TD>
</TR>
<TR BGCOLOR="white" CLASS="TableRowColor">
<TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
<CODE>&nbsp;void</CODE></FONT></TD>
<TD><CODE><B><A HREF="Canvas.html#erase(java.awt.Shape)">erase</A></B>(java.awt.Shape&nbsp;shape)</CODE>
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Erase a given shape's interior on the screen.</TD>
</TR>
<TR BGCOLOR="white" CLASS="TableRowColor">
<TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
<CODE>&nbsp;void</CODE></FONT></TD>
<TD><CODE><B><A HREF="Canvas.html#eraseCircle(int, int, int)">eraseCircle</A></B>(int&nbsp;xPos,
int&nbsp;yPos,
int&nbsp;diameter)</CODE>
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Erase the internal dimensions of the given circle.</TD>
</TR>
<TR BGCOLOR="white" CLASS="TableRowColor">
<TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
<CODE>&nbsp;void</CODE></FONT></TD>
<TD><CODE><B><A HREF="Canvas.html#eraseOutline(java.awt.Shape)">eraseOutline</A></B>(java.awt.Shape&nbsp;shape)</CODE>
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Erases a given shape's outline on the screen.</TD>
</TR>
<TR BGCOLOR="white" CLASS="TableRowColor">
<TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
<CODE>&nbsp;void</CODE></FONT></TD>
<TD><CODE><B><A HREF="Canvas.html#eraseRectangle(int, int, int, int)">eraseRectangle</A></B>(int&nbsp;xPos,
int&nbsp;yPos,
int&nbsp;width,
int&nbsp;height)</CODE>
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Erase the internal dimensions of the given rectangle.</TD>
</TR>
<TR BGCOLOR="white" CLASS="TableRowColor">
<TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
<CODE>&nbsp;void</CODE></FONT></TD>
<TD><CODE><B><A HREF="Canvas.html#eraseString(java.lang.String, int, int)">eraseString</A></B>(java.lang.String&nbsp;text,
int&nbsp;x,
int&nbsp;y)</CODE>
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Erases a String on the Canvas.</TD>
</TR>
<TR BGCOLOR="white" CLASS="TableRowColor">
<TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
<CODE>&nbsp;void</CODE></FONT></TD>
<TD><CODE><B><A HREF="Canvas.html#fill(java.awt.Shape)">fill</A></B>(java.awt.Shape&nbsp;shape)</CODE>
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Fill the internal dimensions of a given shape with the current
foreground color of the canvas.</TD>
</TR>
<TR BGCOLOR="white" CLASS="TableRowColor">
<TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
<CODE>&nbsp;void</CODE></FONT></TD>
<TD><CODE><B><A HREF="Canvas.html#fillCircle(int, int, int)">fillCircle</A></B>(int&nbsp;xPos,
int&nbsp;yPos,
int&nbsp;diameter)</CODE>
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Fill the internal dimensions of the given circle with the current
foreground color of the canvas.</TD>
</TR>
<TR BGCOLOR="white" CLASS="TableRowColor">
<TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
<CODE>&nbsp;void</CODE></FONT></TD>
<TD><CODE><B><A HREF="Canvas.html#fillRectangle(int, int, int, int)">fillRectangle</A></B>(int&nbsp;xPos,
int&nbsp;yPos,
int&nbsp;width,
int&nbsp;height)</CODE>
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Fill the internal dimensions of the given rectangle with the current
foreground color of the canvas.</TD>
</TR>
<TR BGCOLOR="white" CLASS="TableRowColor">
<TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
<CODE>&nbsp;java.awt.Color</CODE></FONT></TD>
<TD><CODE><B><A HREF="Canvas.html#getBackgroundColor()">getBackgroundColor</A></B>()</CODE>
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Returns the current color of the background</TD>
</TR>
<TR BGCOLOR="white" CLASS="TableRowColor">
<TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
<CODE>&nbsp;java.awt.Font</CODE></FONT></TD>
<TD><CODE><B><A HREF="Canvas.html#getFont()">getFont</A></B>()</CODE>
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Returns the current font of the canvas.</TD>
</TR>
<TR BGCOLOR="white" CLASS="TableRowColor">
<TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
<CODE>&nbsp;java.awt.Color</CODE></FONT></TD>
<TD><CODE><B><A HREF="Canvas.html#getForegroundColor()">getForegroundColor</A></B>()</CODE>
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Returns the current color of the foreground.</TD>
</TR>
<TR BGCOLOR="white" CLASS="TableRowColor">
<TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
<CODE>&nbsp;java.awt.Dimension</CODE></FONT></TD>
<TD><CODE><B><A HREF="Canvas.html#getSize()">getSize</A></B>()</CODE>
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Returns the size of the canvas.</TD>
</TR>
<TR BGCOLOR="white" CLASS="TableRowColor">
<TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
<CODE>&nbsp;boolean</CODE></FONT></TD>
<TD><CODE><B><A HREF="Canvas.html#isVisible()">isVisible</A></B>()</CODE>
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Provide information on visibility of the Canvas.</TD>
</TR>
<TR BGCOLOR="white" CLASS="TableRowColor">
<TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
<CODE>&nbsp;void</CODE></FONT></TD>
<TD><CODE><B><A HREF="Canvas.html#setBackgroundColor(java.awt.Color)">setBackgroundColor</A></B>(java.awt.Color&nbsp;newColor)</CODE>
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Sets the background color of the Canvas.</TD>
</TR>
<TR BGCOLOR="white" CLASS="TableRowColor">
<TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
<CODE>&nbsp;void</CODE></FONT></TD>
<TD><CODE><B><A HREF="Canvas.html#setFont(java.awt.Font)">setFont</A></B>(java.awt.Font&nbsp;newFont)</CODE>
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;changes the current Font used on the Canvas</TD>
</TR>
<TR BGCOLOR="white" CLASS="TableRowColor">
<TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
<CODE>&nbsp;void</CODE></FONT></TD>
<TD><CODE><B><A HREF="Canvas.html#setForegroundColor(java.awt.Color)">setForegroundColor</A></B>(java.awt.Color&nbsp;newColor)</CODE>
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Sets the foreground color of the Canvas.</TD>
</TR>
<TR BGCOLOR="white" CLASS="TableRowColor">
<TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
<CODE>&nbsp;void</CODE></FONT></TD>
<TD><CODE><B><A HREF="Canvas.html#setSize(int, int)">setSize</A></B>(int&nbsp;width,
int&nbsp;height)</CODE>
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Sets the size of the canvas.</TD>
</TR>
<TR BGCOLOR="white" CLASS="TableRowColor">
<TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
<CODE>&nbsp;void</CODE></FONT></TD>
<TD><CODE><B><A HREF="Canvas.html#setVisible(boolean)">setVisible</A></B>(boolean&nbsp;visible)</CODE>
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Set the canvas visibility and brings canvas to the front of screen
when made visible.</TD>
</TR>
<TR BGCOLOR="white" CLASS="TableRowColor">
<TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
<CODE>&nbsp;void</CODE></FONT></TD>
<TD><CODE><B><A HREF="Canvas.html#wait(int)">wait</A></B>(int&nbsp;milliseconds)</CODE>
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Waits for a specified number of milliseconds before finishing.</TD>
</TR>
</TABLE>
&nbsp;<A NAME="methods_inherited_from_class_java.lang.Object"><!-- --></A>
<TABLE BORDER="1" WIDTH="100%" CELLPADDING="3" CELLSPACING="0" SUMMARY="">
<TR BGCOLOR="#EEEEFF" CLASS="TableSubHeadingColor">
<TH ALIGN="left"><B>Methods inherited from class java.lang.Object</B></TH>
</TR>
<TR BGCOLOR="white" CLASS="TableRowColor">
<TD><CODE>clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait</CODE></TD>
</TR>
</TABLE>
&nbsp;
<P>
<!-- ========= CONSTRUCTOR DETAIL ======== -->
<A NAME="constructor_detail"><!-- --></A>
<TABLE BORDER="1" WIDTH="100%" CELLPADDING="3" CELLSPACING="0" SUMMARY="">
<TR BGCOLOR="#CCCCFF" CLASS="TableHeadingColor">
<TH ALIGN="left" COLSPAN="1"><FONT SIZE="+2">
<B>Constructor Detail</B></FONT></TH>
</TR>
</TABLE>
<A NAME="Canvas(java.lang.String)"><!-- --></A><H3>
Canvas</H3>
<PRE>
public <B>Canvas</B>(java.lang.String&nbsp;title)</PRE>
<DL>
<DD>Create a Canvas with default height, width and background color
(300, 300, white).
<P>
<DL>
<DT><B>Parameters:</B><DD><CODE>title</CODE> - title to appear in Canvas Frame</DL>
</DL>
<HR>
<A NAME="Canvas(java.lang.String, int, int)"><!-- --></A><H3>
Canvas</H3>
<PRE>
public <B>Canvas</B>(java.lang.String&nbsp;title,
int&nbsp;width,
int&nbsp;height)</PRE>
<DL>
<DD>Create a Canvas with default background color (white).
<P>
<DL>
<DT><B>Parameters:</B><DD><CODE>title</CODE> - title to appear in Canvas Frame<DD><CODE>width</CODE> - the desired width for the canvas<DD><CODE>height</CODE> - the desired height for the canvas</DL>
</DL>
<HR>
<A NAME="Canvas(java.lang.String, int, int, java.awt.Color)"><!-- --></A><H3>
Canvas</H3>
<PRE>
public <B>Canvas</B>(java.lang.String&nbsp;title,
int&nbsp;width,
int&nbsp;height,
java.awt.Color&nbsp;bgColor)</PRE>
<DL>
<DD>Create a Canvas.
<P>
<DL>
<DT><B>Parameters:</B><DD><CODE>title</CODE> - title to appear in Canvas Frame<DD><CODE>width</CODE> - the desired width for the canvas<DD><CODE>height</CODE> - the desired height for the canvas<DD><CODE>bgClour</CODE> - the desired background color of the canvas</DL>
</DL>
<!-- ============ METHOD DETAIL ========== -->
<A NAME="method_detail"><!-- --></A>
<TABLE BORDER="1" WIDTH="100%" CELLPADDING="3" CELLSPACING="0" SUMMARY="">
<TR BGCOLOR="#CCCCFF" CLASS="TableHeadingColor">
<TH ALIGN="left" COLSPAN="1"><FONT SIZE="+2">
<B>Method Detail</B></FONT></TH>
</TR>
</TABLE>
<A NAME="draw(java.awt.Shape)"><!-- --></A><H3>
draw</H3>
<PRE>
public void <B>draw</B>(java.awt.Shape&nbsp;shape)</PRE>
<DL>
<DD>Draw the outline of a given shape onto the canvas.
<P>
<DD><DL>
<DT><B>Parameters:</B><DD><CODE>shape</CODE> - the shape object to be drawn on the canvas</DL>
</DD>
</DL>
<HR>
<A NAME="drawImage(java.awt.Image, int, int)"><!-- --></A><H3>
drawImage</H3>
<PRE>
public boolean <B>drawImage</B>(java.awt.Image&nbsp;image,
int&nbsp;x,
int&nbsp;y)</PRE>
<DL>
<DD>Draws an image onto the canvas.
<P>
<DD><DL>
<DT><B>Parameters:</B><DD><CODE>image</CODE> - the Image object to be displayed<DD><CODE>x</CODE> - x co-ordinate for Image placement<DD><CODE>y</CODE> - y co-ordinate for Image placement
<DT><B>Returns:</B><DD>returns boolean value representing whether the image was
completely loaded</DL>
</DD>
</DL>
<HR>
<A NAME="drawLine(int, int, int, int)"><!-- --></A><H3>
drawLine</H3>
<PRE>
public void <B>drawLine</B>(int&nbsp;x1,
int&nbsp;y1,
int&nbsp;x2,
int&nbsp;y2)</PRE>
<DL>
<DD>Draws a line on the Canvas.
<P>
<DD><DL>
<DT><B>Parameters:</B><DD><CODE>x1</CODE> - x co-ordinate of start of line<DD><CODE>y1</CODE> - y co-ordinate of start of line<DD><CODE>x2</CODE> - x co-ordinate of end of line<DD><CODE>y2</CODE> - y co-ordinate of end of line</DL>
</DD>
</DL>
<HR>
<A NAME="drawString(java.lang.String, int, int)"><!-- --></A><H3>
drawString</H3>
<PRE>
public void <B>drawString</B>(java.lang.String&nbsp;text,
int&nbsp;x,
int&nbsp;y)</PRE>
<DL>
<DD>Draws a String on the Canvas.
<P>
<DD><DL>
<DT><B>Parameters:</B><DD><CODE>text</CODE> - the String to be displayed<DD><CODE>x</CODE> - x co-ordinate for text placement<DD><CODE>y</CODE> - y co-ordinate for text placement</DL>
</DD>
</DL>
<HR>
<A NAME="erase()"><!-- --></A><H3>
erase</H3>
<PRE>
public void <B>erase</B>()</PRE>
<DL>
<DD>Erase the whole canvas.
<P>
<DD><DL>
</DL>
</DD>
</DL>
<HR>
<A NAME="erase(java.awt.Shape)"><!-- --></A><H3>
erase</H3>
<PRE>
public void <B>erase</B>(java.awt.Shape&nbsp;shape)</PRE>
<DL>
<DD>Erase a given shape's interior on the screen.
<P>
<DD><DL>
<DT><B>Parameters:</B><DD><CODE>shape</CODE> - the shape object to be erased</DL>
</DD>
</DL>
<HR>
<A NAME="eraseCircle(int, int, int)"><!-- --></A><H3>
eraseCircle</H3>
<PRE>
public void <B>eraseCircle</B>(int&nbsp;xPos,
int&nbsp;yPos,
int&nbsp;diameter)</PRE>
<DL>
<DD>Erase the internal dimensions of the given circle. This is a
convenience method. A similar effect can be achieved with
the "erase" method.
<P>
<DD><DL>
</DL>
</DD>
</DL>
<HR>
<A NAME="eraseOutline(java.awt.Shape)"><!-- --></A><H3>
eraseOutline</H3>
<PRE>
public void <B>eraseOutline</B>(java.awt.Shape&nbsp;shape)</PRE>
<DL>
<DD>Erases a given shape's outline on the screen.
<P>
<DD><DL>
<DT><B>Parameters:</B><DD><CODE>shape</CODE> - the shape object to be erased</DL>
</DD>
</DL>
<HR>
<A NAME="eraseRectangle(int, int, int, int)"><!-- --></A><H3>
eraseRectangle</H3>
<PRE>
public void <B>eraseRectangle</B>(int&nbsp;xPos,
int&nbsp;yPos,
int&nbsp;width,
int&nbsp;height)</PRE>
<DL>
<DD>Erase the internal dimensions of the given rectangle. This is a
convenience method. A similar effect can be achieved with
the "erase" method.
<P>
<DD><DL>
</DL>
</DD>
</DL>
<HR>
<A NAME="eraseString(java.lang.String, int, int)"><!-- --></A><H3>
eraseString</H3>
<PRE>
public void <B>eraseString</B>(java.lang.String&nbsp;text,
int&nbsp;x,
int&nbsp;y)</PRE>
<DL>
<DD>Erases a String on the Canvas.
<P>
<DD><DL>
<DT><B>Parameters:</B><DD><CODE>text</CODE> - the String to be displayed<DD><CODE>x</CODE> - x co-ordinate for text placement<DD><CODE>y</CODE> - y co-ordinate for text placement</DL>
</DD>
</DL>
<HR>
<A NAME="fill(java.awt.Shape)"><!-- --></A><H3>
fill</H3>
<PRE>
public void <B>fill</B>(java.awt.Shape&nbsp;shape)</PRE>
<DL>
<DD>Fill the internal dimensions of a given shape with the current
foreground color of the canvas.
<P>
<DD><DL>
<DT><B>Parameters:</B><DD><CODE>shape</CODE> - the shape object to be filled</DL>
</DD>
</DL>
<HR>
<A NAME="fillCircle(int, int, int)"><!-- --></A><H3>
fillCircle</H3>
<PRE>
public void <B>fillCircle</B>(int&nbsp;xPos,
int&nbsp;yPos,
int&nbsp;diameter)</PRE>
<DL>
<DD>Fill the internal dimensions of the given circle with the current
foreground color of the canvas.
<P>
<DD><DL>
<DT><B>Parameters:</B><DD><CODE>xPos</CODE> - The x-coordinate of the circle center point<DD><CODE>yPos</CODE> - The y-coordinate of the circle center point<DD><CODE>diameter</CODE> - The diameter of the circle to be drawn</DL>
</DD>
</DL>
<HR>
<A NAME="fillRectangle(int, int, int, int)"><!-- --></A><H3>
fillRectangle</H3>
<PRE>
public void <B>fillRectangle</B>(int&nbsp;xPos,
int&nbsp;yPos,
int&nbsp;width,
int&nbsp;height)</PRE>
<DL>
<DD>Fill the internal dimensions of the given rectangle with the current
foreground color of the canvas. This is a convenience method. A similar
effect can be achieved with the "fill" method.
<P>
<DD><DL>
</DL>
</DD>
</DL>
<HR>
<A NAME="getBackgroundColor()"><!-- --></A><H3>
getBackgroundColor</H3>
<PRE>
public java.awt.Color <B>getBackgroundColor</B>()</PRE>
<DL>
<DD>Returns the current color of the background
<P>
<DD><DL>
<DT><B>Returns:</B><DD>the color of the background of the Canvas</DL>
</DD>
</DL>
<HR>
<A NAME="getFont()"><!-- --></A><H3>
getFont</H3>
<PRE>
public java.awt.Font <B>getFont</B>()</PRE>
<DL>
<DD>Returns the current font of the canvas.
<P>
<DD><DL>
<DT><B>Returns:</B><DD>the font currently in use</DL>
</DD>
</DL>
<HR>
<A NAME="getForegroundColor()"><!-- --></A><H3>
getForegroundColor</H3>
<PRE>
public java.awt.Color <B>getForegroundColor</B>()</PRE>
<DL>
<DD>Returns the current color of the foreground.
<P>
<DD><DL>
<DT><B>Returns:</B><DD>the color of the foreground of the Canvas</DL>
</DD>
</DL>
<HR>
<A NAME="getSize()"><!-- --></A><H3>
getSize</H3>
<PRE>
public java.awt.Dimension <B>getSize</B>()</PRE>
<DL>
<DD>Returns the size of the canvas.
<P>
<DD><DL>
<DT><B>Returns:</B><DD>The current dimension of the canvas</DL>
</DD>
</DL>
<HR>
<A NAME="isVisible()"><!-- --></A><H3>
isVisible</H3>
<PRE>
public boolean <B>isVisible</B>()</PRE>
<DL>
<DD>Provide information on visibility of the Canvas.
<P>
<DD><DL>
<DT><B>Returns:</B><DD>true if canvas is visible, false otherwise</DL>
</DD>
</DL>
<HR>
<A NAME="setBackgroundColor(java.awt.Color)"><!-- --></A><H3>
setBackgroundColor</H3>
<PRE>
public void <B>setBackgroundColor</B>(java.awt.Color&nbsp;newColor)</PRE>
<DL>
<DD>Sets the background color of the Canvas.
<P>
<DD><DL>
<DT><B>Parameters:</B><DD><CODE>newColor</CODE> - the new color for the background of the Canvas</DL>
</DD>
</DL>
<HR>
<A NAME="setFont(java.awt.Font)"><!-- --></A><H3>
setFont</H3>
<PRE>
public void <B>setFont</B>(java.awt.Font&nbsp;newFont)</PRE>
<DL>
<DD>changes the current Font used on the Canvas
<P>
<DD><DL>
<DT><B>Parameters:</B><DD><CODE>newFont</CODE> - new font to be used for String output</DL>
</DD>
</DL>
<HR>
<A NAME="setForegroundColor(java.awt.Color)"><!-- --></A><H3>
setForegroundColor</H3>
<PRE>
public void <B>setForegroundColor</B>(java.awt.Color&nbsp;newColor)</PRE>
<DL>
<DD>Sets the foreground color of the Canvas.
<P>
<DD><DL>
<DT><B>Parameters:</B><DD><CODE>newColor</CODE> - the new color for the foreground of the Canvas</DL>
</DD>
</DL>
<HR>
<A NAME="setSize(int, int)"><!-- --></A><H3>
setSize</H3>
<PRE>
public void <B>setSize</B>(int&nbsp;width,
int&nbsp;height)</PRE>
<DL>
<DD>Sets the size of the canvas.
<P>
<DD><DL>
<DT><B>Parameters:</B><DD><CODE>width</CODE> - new width<DD><CODE>height</CODE> - new height</DL>
</DD>
</DL>
<HR>
<A NAME="setVisible(boolean)"><!-- --></A><H3>
setVisible</H3>
<PRE>
public void <B>setVisible</B>(boolean&nbsp;visible)</PRE>
<DL>
<DD>Set the canvas visibility and brings canvas to the front of screen
when made visible. This method can also be used to bring an already
visible canvas to the front of other windows.
<P>
<DD><DL>
<DT><B>Parameters:</B><DD><CODE>visible</CODE> - boolean value representing the desired visibility of
the canvas (true or false)</DL>
</DD>
</DL>
<HR>
<A NAME="wait(int)"><!-- --></A><H3>
wait</H3>
<PRE>
public void <B>wait</B>(int&nbsp;milliseconds)</PRE>
<DL>
<DD>Waits for a specified number of milliseconds before finishing.
This provides an easy way to specify a small delay which can be
used when producing animations.
<P>
<DD><DL>
<DT><B>Parameters:</B><DD><CODE>milliseconds</CODE> - the number</DL>
</DD>
</DL>
<!-- ========= END OF CLASS DATA ========= -->
<HR>
<HR>
</BODY>
</HTML>

View File

@@ -0,0 +1,32 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<!--NewPage-->
<HTML>
<HEAD>
<!-- Generated by javadoc (build 1.8.0_31) on Mon Oct 05 10:02:33 BST 2015 -->
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
<TITLE>
All Classes
</TITLE>
<META NAME="date" CONTENT="2015-10-05">
<LINK REL ="stylesheet" TYPE="text/css" HREF="stylesheet.css" TITLE="Style">
</HEAD>
<BODY BGCOLOR="white">
<FONT size="+1" CLASS="FrameHeadingFont">
<B>All Classes</B></FONT>
<BR>
<TABLE BORDER="0" WIDTH="100%" SUMMARY="">
<TR>
<TD NOWRAP><FONT CLASS="FrameItemFont"><A HREF="Canvas.html" title="class in &lt;Unnamed&gt;" target="classFrame">Canvas</A>
<BR>
</FONT></TD>
</TR>
</TABLE>
</BODY>
</HTML>

View File

@@ -0,0 +1,32 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<!--NewPage-->
<HTML>
<HEAD>
<!-- Generated by javadoc (build 1.8.0_31) on Mon Oct 05 10:02:33 BST 2015 -->
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
<TITLE>
All Classes
</TITLE>
<META NAME="date" CONTENT="2015-10-05">
<LINK REL ="stylesheet" TYPE="text/css" HREF="stylesheet.css" TITLE="Style">
</HEAD>
<BODY BGCOLOR="white">
<FONT size="+1" CLASS="FrameHeadingFont">
<B>All Classes</B></FONT>
<BR>
<TABLE BORDER="0" WIDTH="100%" SUMMARY="">
<TR>
<TD NOWRAP><FONT CLASS="FrameItemFont"><A HREF="Canvas.html" title="class in &lt;Unnamed&gt;">Canvas</A>
<BR>
</FONT></TD>
</TR>
</TABLE>
</BODY>
</HTML>

View File

@@ -0,0 +1,45 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<!--NewPage-->
<HTML>
<HEAD>
<!-- Generated by javadoc (build 1.8.0_31) on Mon Oct 05 10:02:33 BST 2015 -->
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
<TITLE>
Constant Field Values
</TITLE>
<META NAME="date" CONTENT="2015-10-05">
<LINK REL ="stylesheet" TYPE="text/css" HREF="stylesheet.css" TITLE="Style">
<SCRIPT type="text/javascript">
function windowTitle()
{
if (location.href.indexOf('is-external=true') == -1) {
parent.document.title="Constant Field Values";
}
}
</SCRIPT>
<NOSCRIPT>
</NOSCRIPT>
</HEAD>
<BODY BGCOLOR="white" onload="windowTitle();">
<HR>
<HR>
<CENTER>
<H1>
Constant Field Values</H1>
</CENTER>
<HR SIZE="4" NOSHADE>
<B>Contents</B><UL>
</UL>
<HR>
<HR>
</BODY>
</HTML>

View File

@@ -0,0 +1,37 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">
<!--NewPage-->
<HTML>
<HEAD>
<!-- Generated by javadoc on Mon Oct 05 10:02:33 BST 2015-->
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
<TITLE>
Generated Documentation (Untitled)
</TITLE>
<SCRIPT type="text/javascript">
targetPage = "" + window.location.search;
if (targetPage != "" && targetPage != "undefined")
targetPage = targetPage.substring(1);
if (targetPage.indexOf(":") != -1)
targetPage = "undefined";
function loadFrames() {
if (targetPage != "" && targetPage != "undefined")
top.classFrame.location = top.targetPage;
}
</SCRIPT>
<NOSCRIPT>
</NOSCRIPT>
</HEAD>
<FRAMESET cols="20%,80%" title="" onLoad="top.loadFrames()">
<FRAME src="allclasses-frame.html" name="packageFrame" title="All classes and interfaces (except non-static nested types)">
<FRAME src="Canvas.html" name="classFrame" title="Package, class and interface descriptions" scrolling="yes">
<NOFRAMES>
<H2>
Frame Alert</H2>
<P>
This document is designed to be viewed using the frames feature. If you see this message, you are using a non-frame-capable web client.
<BR>
Link to<A HREF="Canvas.html">Non-frame version.</A>
</NOFRAMES>
</FRAMESET>
</HTML>

View File

@@ -0,0 +1,43 @@
Class documentation
<---- javadoc command: ---->
/Applications/BlueJ/BlueJ 3.1.5/BlueJ.app/Contents/Frameworks/jdk.framework/Versions/A/Contents/Home/bin/javadoc
-author
-version
-nodeprecated
-package
-noindex
-notree
-nohelp
-nonavbar
-source
1.8
-classpath
/Applications/BlueJ/BlueJ 3.1.5/BlueJ.app/Contents/Resources/Java/bluejcore.jar:/Applications/BlueJ/BlueJ 3.1.5/BlueJ.app/Contents/Resources/Java/junit-4.8.2.jar:/Applications/BlueJ/BlueJ 3.1.5/BlueJ.app/Contents/Resources/Java/userlib/pi4j-core.jar:/Applications/BlueJ/BlueJ 3.1.5/BlueJ.app/Contents/Resources/Java/userlib/pi4j-device.jar:/Applications/BlueJ/BlueJ 3.1.5/BlueJ.app/Contents/Resources/Java/userlib/pi4j-gpio-extension.jar:/Applications/BlueJ/BlueJ 3.1.5/BlueJ.app/Contents/Resources/Java/userlib/pi4j-service.jar:/Users/mik/Documents/Dropbox/ofwj/projects/chapter06/scribble
-d
/Users/mik/Documents/Dropbox/ofwj/projects/chapter06/scribble/doc
-encoding
UTF-8
-charset
UTF-8
-docletpath
/Applications/BlueJ/BlueJ 3.1.5/BlueJ.app/Contents/Resources/Java/bjdoclet.jar
-doclet
bluej.doclet.doclets.formats.html.HtmlDoclet
/Users/mik/Documents/Dropbox/ofwj/projects/chapter06/scribble/Canvas.java
<---- end of javadoc command ---->
Loading source file /Users/mik/Documents/Dropbox/ofwj/projects/chapter06/scribble/Canvas.java...
Constructing Javadoc information...
Standard Doclet version 1.8.0_31
Building tree for all the packages and classes...
Generating /Users/mik/Documents/Dropbox/ofwj/projects/chapter06/scribble/doc/Canvas.html...
/Users/mik/Documents/Dropbox/ofwj/projects/chapter06/scribble/Canvas.java:51: warning - @param argument "bgClour" is not a parameter name.
Generating /Users/mik/Documents/Dropbox/ofwj/projects/chapter06/scribble/doc/package-frame.html...
Generating /Users/mik/Documents/Dropbox/ofwj/projects/chapter06/scribble/doc/package-summary.html...
Generating /Users/mik/Documents/Dropbox/ofwj/projects/chapter06/scribble/doc/constant-values.html...
Building index for all the packages and classes...
Building index for all classes...
Generating /Users/mik/Documents/Dropbox/ofwj/projects/chapter06/scribble/doc/allclasses-frame.html...
Generating /Users/mik/Documents/Dropbox/ofwj/projects/chapter06/scribble/doc/allclasses-noframe.html...
Generating /Users/mik/Documents/Dropbox/ofwj/projects/chapter06/scribble/doc/index.html...
Generating /Users/mik/Documents/Dropbox/ofwj/projects/chapter06/scribble/doc/stylesheet.css...
1 warning

View File

@@ -0,0 +1,33 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<!--NewPage-->
<HTML>
<HEAD>
<!-- Generated by javadoc (build 1.8.0_31) on Mon Oct 05 10:02:33 BST 2015 -->
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
<TITLE>
&lt;Unnamed&gt;
</TITLE>
<META NAME="date" CONTENT="2015-10-05">
<LINK REL ="stylesheet" TYPE="text/css" HREF="stylesheet.css" TITLE="Style">
</HEAD>
<BODY BGCOLOR="white">
<FONT size="+1" CLASS="FrameTitleFont">
<A HREF="package-summary.html" target="classFrame">&lt;Unnamed&gt;</A></FONT>
<TABLE BORDER="0" WIDTH="100%" SUMMARY="">
<TR>
<TD NOWRAP><FONT size="+1" CLASS="FrameHeadingFont">
Classes</FONT>&nbsp;
<FONT CLASS="FrameItemFont">
<BR>
<A HREF="Canvas.html" title="class in &lt;Unnamed&gt;" target="classFrame">Canvas</A></FONT></TD>
</TR>
</TABLE>
</BODY>
</HTML>

View File

@@ -0,0 +1,47 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<!--NewPage-->
<HTML>
<HEAD>
<!-- Generated by javadoc (build 1.8.0_31) on Mon Oct 05 10:02:33 BST 2015 -->
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
<TITLE>
</TITLE>
<META NAME="date" CONTENT="2015-10-05">
<LINK REL ="stylesheet" TYPE="text/css" HREF="stylesheet.css" TITLE="Style">
</HEAD>
<BODY BGCOLOR="white">
<HR>
<HR>
<H2>
Package &lt;Unnamed&gt;
</H2>
<TABLE BORDER="1" WIDTH="100%" CELLPADDING="3" CELLSPACING="0" SUMMARY="">
<TR BGCOLOR="#CCCCFF" CLASS="TableHeadingColor">
<TH ALIGN="left" COLSPAN="2"><FONT SIZE="+2">
<B>Class Summary</B></FONT></TH>
</TR>
<TR BGCOLOR="white" CLASS="TableRowColor">
<TD WIDTH="15%"><B><A HREF="Canvas.html" title="class in &lt;Unnamed&gt;">Canvas</A></B></TD>
<TD>Class Canvas - a class to allow for simple graphical
drawing on a canvas.</TD>
</TR>
</TABLE>
&nbsp;
<P>
<DL>
</DL>
<HR>
<HR>
</BODY>
</HTML>

Binary file not shown.

After

Width:  |  Height:  |  Size: 57 B

View File

@@ -0,0 +1,29 @@
/* Javadoc style sheet */
/* Define colors, fonts and other style attributes here to override the defaults */
/* Page background color */
body { background-color: #FFFFFF; color:#000000 }
/* Headings */
h1 { font-size: 145% }
/* Table colors */
.TableHeadingColor { background: #CCCCFF; color:#000000 } /* Dark mauve */
.TableSubHeadingColor { background: #EEEEFF; color:#000000 } /* Light mauve */
.TableRowColor { background: #FFFFFF; color:#000000 } /* White */
/* Font used in left-hand frame lists */
.FrameTitleFont { font-size: 100%; font-family: Helvetica, Arial, sans-serif; color:#000000 }
.FrameHeadingFont { font-size: 90%; font-family: Helvetica, Arial, sans-serif; color:#000000 }
.FrameItemFont { font-size: 90%; font-family: Helvetica, Arial, sans-serif; color:#000000 }
/* Navigation bar fonts and colors */
.NavBarCell1 { background-color:#EEEEFF; color:#000000} /* Light mauve */
.NavBarCell1Rev { background-color:#00008B; color:#FFFFFF} /* Dark Blue */
.NavBarFont1 { font-family: Arial, Helvetica, sans-serif; color:#000000;color:#000000;}
.NavBarFont1Rev { font-family: Arial, Helvetica, sans-serif; color:#FFFFFF;color:#FFFFFF;}
.NavBarCell2 { font-family: Arial, Helvetica, sans-serif; background-color:#FFFFFF; color:#000000}
.NavBarCell3 { font-family: Arial, Helvetica, sans-serif; background-color:#FFFFFF; color:#000000}

View File

@@ -0,0 +1,64 @@
#BlueJ package file
dependency1.from=DrawDemo
dependency1.to=Canvas
dependency1.type=UsesDependency
dependency2.from=Pen
dependency2.to=Canvas
dependency2.type=UsesDependency
dependency3.from=DrawDemo
dependency3.to=Pen
dependency3.type=UsesDependency
objectbench.height=76
objectbench.width=756
package.editor.height=394
package.editor.width=648
package.editor.x=70
package.editor.y=80
package.numDependencies=3
package.numTargets=3
package.showExtends=true
package.showUses=true
project.charset=UTF-8
readme.editor.height=707
readme.editor.width=917
readme.editor.x=53
readme.editor.y=50
target1.editor.height=756
target1.editor.width=948
target1.editor.x=427
target1.editor.y=141
target1.height=60
target1.name=Canvas
target1.naviview.expanded=true
target1.showInterface=true
target1.type=ClassTarget
target1.typeParameters=
target1.width=100
target1.x=390
target1.y=230
target2.editor.height=700
target2.editor.width=900
target2.editor.x=92
target2.editor.y=56
target2.height=60
target2.name=Pen
target2.naviview.expanded=true
target2.showInterface=false
target2.type=ClassTarget
target2.typeParameters=
target2.width=100
target2.x=260
target2.y=140
target3.editor.height=713
target3.editor.width=1016
target3.editor.x=311
target3.editor.y=34
target3.height=60
target3.name=DrawDemo
target3.naviview.expanded=true
target3.showInterface=false
target3.type=ClassTarget
target3.typeParameters=
target3.width=110
target3.x=110
target3.y=50

View File

@@ -0,0 +1,8 @@
#BlueJ class context
comment0.params=
comment0.target=InputReader()
comment0.text=\n\ Create\ a\ new\ InputReader\ that\ reads\ text\ from\ the\ text\ terminal.\n
comment1.params=
comment1.target=java.util.HashSet\ getInput()
comment1.text=\n\ Read\ a\ line\ of\ text\ from\ standard\ input\ (the\ text\ terminal),\n\ and\ return\ it\ as\ a\ set\ of\ words.\n\n\ @return\ \ A\ set\ of\ Strings,\ where\ each\ String\ is\ one\ of\ the\ \n\ \ \ \ \ \ \ \ \ \ words\ typed\ by\ the\ user\n
numComments=2

View File

@@ -0,0 +1,45 @@
import java.util.HashSet;
import java.util.Scanner;
/**
* InputReader reads typed text input from the standard text terminal.
* The text typed by a user is then chopped into words, and a set of words
* is provided.
*
* @author Michael Kölling and David J. Barnes
* @version 1.0 (2016.02.29)
*/
public class InputReader
{
private Scanner reader;
/**
* Create a new InputReader that reads text from the text terminal.
*/
public InputReader()
{
reader = new Scanner(System.in);
}
/**
* Read a line of text from standard input (the text terminal),
* and return it as a set of words.
*
* @return A set of Strings, where each String is one of the
* words typed by the user
*/
public HashSet<String> getInput()
{
System.out.print("> "); // print prompt
String inputLine = reader.nextLine().trim().toLowerCase();
String[] wordArray = inputLine.split(" "); // split at spaces
// add words from array into hashset
HashSet<String> words = new HashSet<>();
for(String word : wordArray) {
words.add(word);
}
return words;
}
}

View File

@@ -0,0 +1,30 @@
TechSupport - the DodgySoft Technical support system.
This project is part of the material for the book
Objects First with Java - A Practical Introduction using BlueJ
Sixth edition
David J. Barnes and Michael Kölling
Pearson Education, 2016
This project is discussed in chapter 6.
This project implements a technical support system for customers of the
DodgySoft software company. Users can describe their software problems and
get advice instantly!
The idea is based on Eliza - a famous program described by Joseph Weizenbaum
in 1966. (Do a web search for "Eliza" and "Weizenbaum" if you want to know
more about this.)
In fact, it is much more primitive than Eliza. But that's enough to match the
quality of many software companies' technical support advice... ;-)
To start this program, create a SupportSystem object and execute the "start"
method.
Then start describing your problem by typing in the terminal window.
The purpose of this project is to demonstrate and study library classes, such
as ArrayList, HashMap, HashSet, and Random.

View File

@@ -0,0 +1,17 @@
#BlueJ class context
comment0.params=
comment0.target=Responder()
comment0.text=\n\ Construct\ a\ Responder\n
comment1.params=words
comment1.target=java.lang.String\ generateResponse(java.util.HashSet)
comment1.text=\n\ Generate\ a\ response\ from\ a\ given\ set\ of\ input\ words.\n\ \n\ @param\ words\ \ A\ set\ of\ words\ entered\ by\ the\ user\n\ @return\ \ \ \ \ \ \ A\ string\ that\ should\ be\ displayed\ as\ the\ response\n
comment2.params=
comment2.target=void\ fillResponseMap()
comment2.text=\n\ Enter\ all\ the\ known\ keywords\ and\ their\ associated\ responses\n\ into\ our\ response\ map.\n
comment3.params=
comment3.target=void\ fillDefaultResponses()
comment3.text=\n\ Build\ up\ a\ list\ of\ default\ responses\ from\ which\ we\ can\ pick\ one\n\ if\ we\ don't\ know\ what\ else\ to\ say.\n
comment4.params=
comment4.target=java.lang.String\ pickDefaultResponse()
comment4.text=\n\ Randomly\ select\ and\ return\ one\ of\ the\ default\ responses.\n\ @return\ \ \ \ \ A\ random\ default\ response\n
numComments=5

View File

@@ -0,0 +1,146 @@
import java.util.HashMap;
import java.util.HashSet;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Random;
/**
* The responder class represents a response generator object.
* It is used to generate an automatic response, based on specified input.
* Input is presented to the responder as a set of words, and based on those
* words the responder will generate a String that represents the response.
*
* Internally, the reponder uses a HashMap to associate words with response
* strings and a list of default responses. If any of the input words is found
* in the HashMap, the corresponding response is returned. If none of the input
* words is recognized, one of the default responses is randomly chosen.
*
* @author Michael Kölling and David J. Barnes
* @version 1.0 (2016.02.29)
*/
public class Responder
{
// Used to map key words to responses.
private HashMap<String, String> responseMap;
// Default responses to use if we don't recognise a word.
private ArrayList<String> defaultResponses;
private Random randomGenerator;
/**
* Construct a Responder
*/
public Responder()
{
responseMap = new HashMap<>();
defaultResponses = new ArrayList<>();
fillResponseMap();
fillDefaultResponses();
randomGenerator = new Random();
}
/**
* Generate a response from a given set of input words.
*
* @param words A set of words entered by the user
* @return A string that should be displayed as the response
*/
public String generateResponse(HashSet<String> words)
{
for (String word : words) {
String response = responseMap.get(word);
if(response != null) {
return response;
}
}
// If we get here, none of the words from the input line was recognized.
// In this case we pick one of our default responses (what we say when
// we cannot think of anything else to say...)
return pickDefaultResponse();
}
/**
* Enter all the known keywords and their associated responses
* into our response map.
*/
private void fillResponseMap()
{
responseMap.put("crash",
"Well, it never crashes on our system. It must have something\n" +
"to do with your system. Tell me more about your configuration.");
responseMap.put("crashes",
"Well, it never crashes on our system. It must have something\n" +
"to do with your system. Tell me more about your configuration.");
responseMap.put("slow",
"I think this has to do with your hardware. Upgrading your processor\n" +
"should solve all performance problems. Have you got a problem with\n" +
"our software?");
responseMap.put("performance",
"Performance was quite adequate in all our tests. Are you running\n" +
"any other processes in the background?");
responseMap.put("bug",
"Well, you know, all software has some bugs. But our software engineers\n" +
"are working very hard to fix them. Can you describe the problem a bit\n" +
"further?");
responseMap.put("buggy",
"Well, you know, all software has some bugs. But our software engineers\n" +
"are working very hard to fix them. Can you describe the problem a bit\n" +
"further?");
responseMap.put("windows",
"This is a known bug to do with the Windows operating system. Please\n" +
"report it to Microsoft. There is nothing we can do about this.");
responseMap.put("mac",
"This is a known bug to do with the Mac operating system. Please\n" +
"report it to Apple. There is nothing we can do about this.");
responseMap.put("expensive",
"The cost of our product is quite competitive. Have you looked around\n" +
"and really compared our features?");
responseMap.put("installation",
"The installation is really quite straight forward. We have tons of\n" +
"wizards that do all the work for you. Have you read the installation\n" +
"instructions?");
responseMap.put("memory",
"If you read the system requirements carefully, you will see that the\n" +
"specified memory requirements are 1.5 giga byte. You really should\n" +
"upgrade your memory. Anything else you want to know?");
responseMap.put("linux",
"We take Linux support very seriously. But there are some problems.\n" +
"Most have to do with incompatible glibc versions. Can you be a bit\n" +
"more precise?");
responseMap.put("bluej",
"Ahhh, BlueJ, yes. We tried to buy out those guys long ago, but\n" +
"they simply won't sell... Stubborn people they are. Nothing we can\n" +
"do about it, I'm afraid.");
}
/**
* Build up a list of default responses from which we can pick one
* if we don't know what else to say.
*/
private void fillDefaultResponses()
{
defaultResponses.add("That sounds odd. Could you describe that problem in more detail?");
defaultResponses.add("No other customer has ever complained about this before. \n" +
"What is your system configuration?");
defaultResponses.add("That sounds interesting. Tell me more...");
defaultResponses.add("I need a bit more information on that.");
defaultResponses.add("Have you checked that you do not have a dll conflict?");
defaultResponses.add("That is explained in the manual. Have you read the manual?");
defaultResponses.add("Your description is a bit wishy-washy. Have you got an expert\n" +
"there with you who could describe this more precisely?");
defaultResponses.add("That's not a bug, it's a feature!");
defaultResponses.add("Could you elaborate on that?");
}
/**
* Randomly select and return one of the default responses.
* @return A random default response
*/
private String pickDefaultResponse()
{
// Pick a random number for the index in the default response list.
// The number will be between 0 (inclusive) and the size of the list (exclusive).
int index = randomGenerator.nextInt(defaultResponses.size());
return defaultResponses.get(index);
}
}

View File

@@ -0,0 +1,14 @@
#BlueJ class context
comment0.params=
comment0.target=SupportSystem()
comment0.text=\n\ Creates\ a\ technical\ support\ system.\n
comment1.params=
comment1.target=void\ start()
comment1.text=\n\ Start\ the\ technical\ support\ system.\ This\ will\ print\ a\ welcome\ message\ and\ enter\n\ into\ a\ dialog\ with\ the\ user,\ until\ the\ user\ ends\ the\ dialog.\n
comment2.params=
comment2.target=void\ printWelcome()
comment2.text=\n\ Print\ a\ welcome\ message\ to\ the\ screen.\n
comment3.params=
comment3.target=void\ printGoodbye()
comment3.text=\n\ Print\ a\ good-bye\ message\ to\ the\ screen.\n
numComments=4

View File

@@ -0,0 +1,76 @@
import java.util.HashSet;
/**
* This class implements a technical support system. It is the top level class
* in this project. The support system communicates via text input/output
* in the text terminal.
*
* This class uses an object of class InputReader to read input from the user,
* and an object of class Responder to generate responses. It contains a loop
* that repeatedly reads input and generates output until the users wants to
* leave.
*
* @author Michael Kölling and David J. Barnes
* @version 1.0 (2016.02.29)
*/
public class SupportSystem
{
private InputReader reader;
private Responder responder;
private WordCounter counter;
/**
* Creates a technical support system.
*/
public SupportSystem()
{
reader = new InputReader();
responder = new Responder();
counter = new WordCounter();
}
/**
* Start the technical support system. This will print a welcome message and enter
* into a dialog with the user, until the user ends the dialog.
*/
public void start()
{
boolean finished = false;
printWelcome();
while(!finished) {
HashSet<String> input = reader.getInput();
if(input.contains("bye")) {
finished = true;
}
else {
counter.addWords(input);
String response = responder.generateResponse(input);
System.out.println(response);
}
}
printGoodbye();
}
/**
* Print a welcome message to the screen.
*/
private void printWelcome()
{
System.out.println("Welcome to the DodgySoft Technical Support System.");
System.out.println();
System.out.println("Please tell us about your problem.");
System.out.println("We will assist you with any problem you might have.");
System.out.println("Please type 'bye' to exit our system.");
}
/**
* Print a good-bye message to the screen.
*/
private void printGoodbye()
{
System.out.println("Nice talking to you. Bye...");
}
}

View File

@@ -0,0 +1,8 @@
#BlueJ class context
comment0.params=
comment0.target=WordCounter()
comment0.text=\n\ Create\ a\ WordCounter\n
comment1.params=input
comment1.target=void\ addWords(java.util.HashSet)
comment1.text=\n\ Update\ the\ usage\ count\ of\ all\ words\ in\ input.\n\ @param\ input\ A\ set\ of\ words\ entered\ by\ the\ user.\n
numComments=2

View File

@@ -0,0 +1,35 @@
import java.util.HashMap;
import java.util.HashSet;
/**
* Keep a record of how many times each word was
* entered by users.
*
* @author Michael Kölling and David J. Barnes
* @version 1.0 (2016.02.29)
*/
public class WordCounter
{
// Associate each word with a count.
private HashMap<String, Integer> counts;
/**
* Create a WordCounter
*/
public WordCounter()
{
counts = new HashMap<>();
}
/**
* Update the usage count of all words in input.
* @param input A set of words entered by the user.
*/
public void addWords(HashSet<String> input)
{
for(String word : input) {
int counter = counts.getOrDefault(word, 0);
counts.put(word, counter + 1);
}
}
}

View File

@@ -0,0 +1,156 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<!--NewPage-->
<HTML>
<HEAD>
<!-- Generated by javadoc (build 1.8.0_31) on Sun Oct 04 17:08:13 BST 2015 -->
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
<TITLE>
InputReader
</TITLE>
<META NAME="date" CONTENT="2015-10-04">
<LINK REL ="stylesheet" TYPE="text/css" HREF="stylesheet.css" TITLE="Style">
<SCRIPT type="text/javascript">
function windowTitle()
{
if (location.href.indexOf('is-external=true') == -1) {
parent.document.title="InputReader";
}
}
</SCRIPT>
<NOSCRIPT>
</NOSCRIPT>
</HEAD>
<BODY BGCOLOR="white" onload="windowTitle();">
<HR>
<HR>
<!-- ======== START OF CLASS DATA ======== -->
<H2>
Class InputReader</H2>
<PRE>
java.lang.Object
<IMG SRC="./resources/inherit.gif" ALT="extended by "><B>InputReader</B>
</PRE>
<HR>
<DL>
<DT><PRE>public class <B>InputReader</B><DT>extends java.lang.Object</DL>
</PRE>
<P>
InputReader reads typed text input from the standard text terminal.
The text typed by a user is then chopped into words, and a set of words
is provided.
<P>
<P>
<DL>
<DT><B>Version:</B></DT>
<DD>1.0 (2016.02.29)</DD>
<DT><B>Author:</B></DT>
<DD>Michael Kölling and David J. Barnes</DD>
</DL>
<HR>
<P>
<!-- ======== CONSTRUCTOR SUMMARY ======== -->
<A NAME="constructor_summary"><!-- --></A>
<TABLE BORDER="1" WIDTH="100%" CELLPADDING="3" CELLSPACING="0" SUMMARY="">
<TR BGCOLOR="#CCCCFF" CLASS="TableHeadingColor">
<TH ALIGN="left" COLSPAN="2"><FONT SIZE="+2">
<B>Constructor Summary</B></FONT></TH>
</TR>
<TR BGCOLOR="white" CLASS="TableRowColor">
<TD><CODE><B><A HREF="InputReader.html#InputReader()">InputReader</A></B>()</CODE>
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Create a new InputReader that reads text from the text terminal.</TD>
</TR>
</TABLE>
&nbsp;
<!-- ========== METHOD SUMMARY =========== -->
<A NAME="method_summary"><!-- --></A>
<TABLE BORDER="1" WIDTH="100%" CELLPADDING="3" CELLSPACING="0" SUMMARY="">
<TR BGCOLOR="#CCCCFF" CLASS="TableHeadingColor">
<TH ALIGN="left" COLSPAN="2"><FONT SIZE="+2">
<B>Method Summary</B></FONT></TH>
</TR>
<TR BGCOLOR="white" CLASS="TableRowColor">
<TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
<CODE>&nbsp;java.util.HashSet&lt;java.lang.String&gt;</CODE></FONT></TD>
<TD><CODE><B><A HREF="InputReader.html#getInput()">getInput</A></B>()</CODE>
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Read a line of text from standard input (the text terminal),
and return it as a set of words.</TD>
</TR>
</TABLE>
&nbsp;<A NAME="methods_inherited_from_class_java.lang.Object"><!-- --></A>
<TABLE BORDER="1" WIDTH="100%" CELLPADDING="3" CELLSPACING="0" SUMMARY="">
<TR BGCOLOR="#EEEEFF" CLASS="TableSubHeadingColor">
<TH ALIGN="left"><B>Methods inherited from class java.lang.Object</B></TH>
</TR>
<TR BGCOLOR="white" CLASS="TableRowColor">
<TD><CODE>clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait</CODE></TD>
</TR>
</TABLE>
&nbsp;
<P>
<!-- ========= CONSTRUCTOR DETAIL ======== -->
<A NAME="constructor_detail"><!-- --></A>
<TABLE BORDER="1" WIDTH="100%" CELLPADDING="3" CELLSPACING="0" SUMMARY="">
<TR BGCOLOR="#CCCCFF" CLASS="TableHeadingColor">
<TH ALIGN="left" COLSPAN="1"><FONT SIZE="+2">
<B>Constructor Detail</B></FONT></TH>
</TR>
</TABLE>
<A NAME="InputReader()"><!-- --></A><H3>
InputReader</H3>
<PRE>
public <B>InputReader</B>()</PRE>
<DL>
<DD>Create a new InputReader that reads text from the text terminal.
<P>
</DL>
<!-- ============ METHOD DETAIL ========== -->
<A NAME="method_detail"><!-- --></A>
<TABLE BORDER="1" WIDTH="100%" CELLPADDING="3" CELLSPACING="0" SUMMARY="">
<TR BGCOLOR="#CCCCFF" CLASS="TableHeadingColor">
<TH ALIGN="left" COLSPAN="1"><FONT SIZE="+2">
<B>Method Detail</B></FONT></TH>
</TR>
</TABLE>
<A NAME="getInput()"><!-- --></A><H3>
getInput</H3>
<PRE>
public java.util.HashSet&lt;java.lang.String&gt; <B>getInput</B>()</PRE>
<DL>
<DD>Read a line of text from standard input (the text terminal),
and return it as a set of words.
<P>
<DD><DL>
<DT><B>Returns:</B><DD>A set of Strings, where each String is one of the
words typed by the user</DL>
</DD>
</DL>
<!-- ========= END OF CLASS DATA ========= -->
<HR>
<HR>
</BODY>
</HTML>

View File

@@ -0,0 +1,32 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<!--NewPage-->
<HTML>
<HEAD>
<!-- Generated by javadoc (build 1.8.0_31) on Sun Oct 04 17:08:13 BST 2015 -->
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
<TITLE>
All Classes
</TITLE>
<META NAME="date" CONTENT="2015-10-04">
<LINK REL ="stylesheet" TYPE="text/css" HREF="stylesheet.css" TITLE="Style">
</HEAD>
<BODY BGCOLOR="white">
<FONT size="+1" CLASS="FrameHeadingFont">
<B>All Classes</B></FONT>
<BR>
<TABLE BORDER="0" WIDTH="100%" SUMMARY="">
<TR>
<TD NOWRAP><FONT CLASS="FrameItemFont"><A HREF="InputReader.html" title="class in &lt;Unnamed&gt;" target="classFrame">InputReader</A>
<BR>
</FONT></TD>
</TR>
</TABLE>
</BODY>
</HTML>

View File

@@ -0,0 +1,32 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<!--NewPage-->
<HTML>
<HEAD>
<!-- Generated by javadoc (build 1.8.0_31) on Sun Oct 04 17:08:13 BST 2015 -->
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
<TITLE>
All Classes
</TITLE>
<META NAME="date" CONTENT="2015-10-04">
<LINK REL ="stylesheet" TYPE="text/css" HREF="stylesheet.css" TITLE="Style">
</HEAD>
<BODY BGCOLOR="white">
<FONT size="+1" CLASS="FrameHeadingFont">
<B>All Classes</B></FONT>
<BR>
<TABLE BORDER="0" WIDTH="100%" SUMMARY="">
<TR>
<TD NOWRAP><FONT CLASS="FrameItemFont"><A HREF="InputReader.html" title="class in &lt;Unnamed&gt;">InputReader</A>
<BR>
</FONT></TD>
</TR>
</TABLE>
</BODY>
</HTML>

View File

@@ -0,0 +1,45 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<!--NewPage-->
<HTML>
<HEAD>
<!-- Generated by javadoc (build 1.8.0_31) on Sun Oct 04 17:08:13 BST 2015 -->
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
<TITLE>
Constant Field Values
</TITLE>
<META NAME="date" CONTENT="2015-10-04">
<LINK REL ="stylesheet" TYPE="text/css" HREF="stylesheet.css" TITLE="Style">
<SCRIPT type="text/javascript">
function windowTitle()
{
if (location.href.indexOf('is-external=true') == -1) {
parent.document.title="Constant Field Values";
}
}
</SCRIPT>
<NOSCRIPT>
</NOSCRIPT>
</HEAD>
<BODY BGCOLOR="white" onload="windowTitle();">
<HR>
<HR>
<CENTER>
<H1>
Constant Field Values</H1>
</CENTER>
<HR SIZE="4" NOSHADE>
<B>Contents</B><UL>
</UL>
<HR>
<HR>
</BODY>
</HTML>

View File

@@ -0,0 +1,37 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">
<!--NewPage-->
<HTML>
<HEAD>
<!-- Generated by javadoc on Sun Oct 04 17:08:13 BST 2015-->
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
<TITLE>
Generated Documentation (Untitled)
</TITLE>
<SCRIPT type="text/javascript">
targetPage = "" + window.location.search;
if (targetPage != "" && targetPage != "undefined")
targetPage = targetPage.substring(1);
if (targetPage.indexOf(":") != -1)
targetPage = "undefined";
function loadFrames() {
if (targetPage != "" && targetPage != "undefined")
top.classFrame.location = top.targetPage;
}
</SCRIPT>
<NOSCRIPT>
</NOSCRIPT>
</HEAD>
<FRAMESET cols="20%,80%" title="" onLoad="top.loadFrames()">
<FRAME src="allclasses-frame.html" name="packageFrame" title="All classes and interfaces (except non-static nested types)">
<FRAME src="InputReader.html" name="classFrame" title="Package, class and interface descriptions" scrolling="yes">
<NOFRAMES>
<H2>
Frame Alert</H2>
<P>
This document is designed to be viewed using the frames feature. If you see this message, you are using a non-frame-capable web client.
<BR>
Link to<A HREF="InputReader.html">Non-frame version.</A>
</NOFRAMES>
</FRAMESET>
</HTML>

View File

@@ -0,0 +1,41 @@
Class documentation
<---- javadoc command: ---->
/Applications/BlueJ/BlueJ 3.1.5/BlueJ.app/Contents/Frameworks/jdk.framework/Versions/A/Contents/Home/bin/javadoc
-author
-version
-nodeprecated
-package
-noindex
-notree
-nohelp
-nonavbar
-source
1.8
-classpath
/Applications/BlueJ/BlueJ 3.1.5/BlueJ.app/Contents/Resources/Java/bluejcore.jar:/Applications/BlueJ/BlueJ 3.1.5/BlueJ.app/Contents/Resources/Java/junit-4.8.2.jar:/Applications/BlueJ/BlueJ 3.1.5/BlueJ.app/Contents/Resources/Java/userlib/pi4j-core.jar:/Applications/BlueJ/BlueJ 3.1.5/BlueJ.app/Contents/Resources/Java/userlib/pi4j-device.jar:/Applications/BlueJ/BlueJ 3.1.5/BlueJ.app/Contents/Resources/Java/userlib/pi4j-gpio-extension.jar:/Applications/BlueJ/BlueJ 3.1.5/BlueJ.app/Contents/Resources/Java/userlib/pi4j-service.jar:/Users/mik/Documents/Dropbox/ofwj/projects/chapter06/tech-support-analysis
-d
/Users/mik/Documents/Dropbox/ofwj/projects/chapter06/tech-support-analysis/doc
-encoding
UTF-8
-charset
UTF-8
-docletpath
/Applications/BlueJ/BlueJ 3.1.5/BlueJ.app/Contents/Resources/Java/bjdoclet.jar
-doclet
bluej.doclet.doclets.formats.html.HtmlDoclet
/Users/mik/Documents/Dropbox/ofwj/projects/chapter06/tech-support-analysis/InputReader.java
<---- end of javadoc command ---->
Loading source file /Users/mik/Documents/Dropbox/ofwj/projects/chapter06/tech-support-analysis/InputReader.java...
Constructing Javadoc information...
Standard Doclet version 1.8.0_31
Building tree for all the packages and classes...
Generating /Users/mik/Documents/Dropbox/ofwj/projects/chapter06/tech-support-analysis/doc/InputReader.html...
Generating /Users/mik/Documents/Dropbox/ofwj/projects/chapter06/tech-support-analysis/doc/package-frame.html...
Generating /Users/mik/Documents/Dropbox/ofwj/projects/chapter06/tech-support-analysis/doc/package-summary.html...
Generating /Users/mik/Documents/Dropbox/ofwj/projects/chapter06/tech-support-analysis/doc/constant-values.html...
Building index for all the packages and classes...
Building index for all classes...
Generating /Users/mik/Documents/Dropbox/ofwj/projects/chapter06/tech-support-analysis/doc/allclasses-frame.html...
Generating /Users/mik/Documents/Dropbox/ofwj/projects/chapter06/tech-support-analysis/doc/allclasses-noframe.html...
Generating /Users/mik/Documents/Dropbox/ofwj/projects/chapter06/tech-support-analysis/doc/index.html...
Generating /Users/mik/Documents/Dropbox/ofwj/projects/chapter06/tech-support-analysis/doc/stylesheet.css...

View File

@@ -0,0 +1,33 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<!--NewPage-->
<HTML>
<HEAD>
<!-- Generated by javadoc (build 1.8.0_31) on Sun Oct 04 17:08:13 BST 2015 -->
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
<TITLE>
&lt;Unnamed&gt;
</TITLE>
<META NAME="date" CONTENT="2015-10-04">
<LINK REL ="stylesheet" TYPE="text/css" HREF="stylesheet.css" TITLE="Style">
</HEAD>
<BODY BGCOLOR="white">
<FONT size="+1" CLASS="FrameTitleFont">
<A HREF="package-summary.html" target="classFrame">&lt;Unnamed&gt;</A></FONT>
<TABLE BORDER="0" WIDTH="100%" SUMMARY="">
<TR>
<TD NOWRAP><FONT size="+1" CLASS="FrameHeadingFont">
Classes</FONT>&nbsp;
<FONT CLASS="FrameItemFont">
<BR>
<A HREF="InputReader.html" title="class in &lt;Unnamed&gt;" target="classFrame">InputReader</A></FONT></TD>
</TR>
</TABLE>
</BODY>
</HTML>

View File

@@ -0,0 +1,46 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<!--NewPage-->
<HTML>
<HEAD>
<!-- Generated by javadoc (build 1.8.0_31) on Sun Oct 04 17:08:13 BST 2015 -->
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
<TITLE>
</TITLE>
<META NAME="date" CONTENT="2015-10-04">
<LINK REL ="stylesheet" TYPE="text/css" HREF="stylesheet.css" TITLE="Style">
</HEAD>
<BODY BGCOLOR="white">
<HR>
<HR>
<H2>
Package &lt;Unnamed&gt;
</H2>
<TABLE BORDER="1" WIDTH="100%" CELLPADDING="3" CELLSPACING="0" SUMMARY="">
<TR BGCOLOR="#CCCCFF" CLASS="TableHeadingColor">
<TH ALIGN="left" COLSPAN="2"><FONT SIZE="+2">
<B>Class Summary</B></FONT></TH>
</TR>
<TR BGCOLOR="white" CLASS="TableRowColor">
<TD WIDTH="15%"><B><A HREF="InputReader.html" title="class in &lt;Unnamed&gt;">InputReader</A></B></TD>
<TD>InputReader reads typed text input from the standard text terminal.</TD>
</TR>
</TABLE>
&nbsp;
<P>
<DL>
</DL>
<HR>
<HR>
</BODY>
</HTML>

Binary file not shown.

After

Width:  |  Height:  |  Size: 57 B

View File

@@ -0,0 +1,29 @@
/* Javadoc style sheet */
/* Define colors, fonts and other style attributes here to override the defaults */
/* Page background color */
body { background-color: #FFFFFF; color:#000000 }
/* Headings */
h1 { font-size: 145% }
/* Table colors */
.TableHeadingColor { background: #CCCCFF; color:#000000 } /* Dark mauve */
.TableSubHeadingColor { background: #EEEEFF; color:#000000 } /* Light mauve */
.TableRowColor { background: #FFFFFF; color:#000000 } /* White */
/* Font used in left-hand frame lists */
.FrameTitleFont { font-size: 100%; font-family: Helvetica, Arial, sans-serif; color:#000000 }
.FrameHeadingFont { font-size: 90%; font-family: Helvetica, Arial, sans-serif; color:#000000 }
.FrameItemFont { font-size: 90%; font-family: Helvetica, Arial, sans-serif; color:#000000 }
/* Navigation bar fonts and colors */
.NavBarCell1 { background-color:#EEEEFF; color:#000000} /* Light mauve */
.NavBarCell1Rev { background-color:#00008B; color:#FFFFFF} /* Dark Blue */
.NavBarFont1 { font-family: Arial, Helvetica, sans-serif; color:#000000;color:#000000;}
.NavBarFont1Rev { font-family: Arial, Helvetica, sans-serif; color:#FFFFFF;color:#FFFFFF;}
.NavBarCell2 { font-family: Arial, Helvetica, sans-serif; background-color:#FFFFFF; color:#000000}
.NavBarCell3 { font-family: Arial, Helvetica, sans-serif; background-color:#FFFFFF; color:#000000}

View File

@@ -0,0 +1,77 @@
#BlueJ package file
dependency1.from=SupportSystem
dependency1.to=InputReader
dependency1.type=UsesDependency
dependency2.from=SupportSystem
dependency2.to=Responder
dependency2.type=UsesDependency
dependency3.from=SupportSystem
dependency3.to=WordCounter
dependency3.type=UsesDependency
objectbench.height=76
objectbench.width=756
package.editor.height=407
package.editor.width=648
package.editor.x=70
package.editor.y=80
package.numDependencies=3
package.numTargets=4
package.showExtends=true
package.showUses=true
project.charset=UTF-8
readme.editor.height=651
readme.editor.width=882
readme.editor.x=53
readme.editor.y=23
target1.editor.height=777
target1.editor.width=945
target1.editor.x=53
target1.editor.y=23
target1.height=60
target1.name=InputReader
target1.naviview.expanded=true
target1.showInterface=false
target1.type=ClassTarget
target1.typeParameters=
target1.width=110
target1.x=120
target1.y=210
target2.editor.height=737
target2.editor.width=890
target2.editor.x=127
target2.editor.y=23
target2.height=60
target2.name=WordCounter
target2.naviview.expanded=false
target2.showInterface=false
target2.type=ClassTarget
target2.typeParameters=
target2.width=120
target2.x=490
target2.y=140
target3.editor.height=737
target3.editor.width=972
target3.editor.x=53
target3.editor.y=48
target3.height=60
target3.name=Responder
target3.naviview.expanded=true
target3.showInterface=false
target3.type=ClassTarget
target3.typeParameters=
target3.width=100
target3.x=370
target3.y=210
target4.editor.height=755
target4.editor.width=860
target4.editor.x=147
target4.editor.y=27
target4.height=60
target4.name=SupportSystem
target4.naviview.expanded=true
target4.showInterface=false
target4.type=ClassTarget
target4.typeParameters=
target4.width=140
target4.x=230
target4.y=90

View File

@@ -0,0 +1,8 @@
#BlueJ class context
comment0.params=
comment0.target=InputReader()
comment0.text=\n\ Create\ a\ new\ InputReader\ that\ reads\ text\ from\ the\ text\ terminal.\n
comment1.params=
comment1.target=java.util.HashSet\ getInput()
comment1.text=\n\ Read\ a\ line\ of\ text\ from\ standard\ input\ (the\ text\ terminal),\n\ and\ return\ it\ as\ a\ set\ of\ words.\n\n\ @return\ \ A\ set\ of\ Strings,\ where\ each\ String\ is\ one\ of\ the\ \n\ \ \ \ \ \ \ \ \ \ words\ typed\ by\ the\ user\n
numComments=2

View File

@@ -0,0 +1,45 @@
import java.util.HashSet;
import java.util.Scanner;
/**
* InputReader reads typed text input from the standard text terminal.
* The text typed by a user is then chopped into words, and a set of words
* is provided.
*
* @author Michael Kölling and David J. Barnes
* @version 1.0 (2016.02.29)
*/
public class InputReader
{
private Scanner reader;
/**
* Create a new InputReader that reads text from the text terminal.
*/
public InputReader()
{
reader = new Scanner(System.in);
}
/**
* Read a line of text from standard input (the text terminal),
* and return it as a set of words.
*
* @return A set of Strings, where each String is one of the
* words typed by the user
*/
public HashSet<String> getInput()
{
System.out.print("> "); // print prompt
String inputLine = reader.nextLine().trim().toLowerCase();
String[] wordArray = inputLine.split(" "); // split at spaces
// add words from array into hashset
HashSet<String> words = new HashSet<>();
for(String word : wordArray) {
words.add(word);
}
return words;
}
}

View File

@@ -0,0 +1,30 @@
TechSupport - the DodgySoft Technical support system.
This project is part of the material for the book
Objects First with Java - A Practical Introduction using BlueJ
Sixth edition
David J. Barnes and Michael Kölling
Pearson Education, 2016
This project is discussed in chapter 6.
This project implements a technical support system for customers of the
DodgySoft software company. Users can describe their software problems and
get advice instantly!
The idea is based on Eliza - a famous program described by Joseph Weizenbaum
in 1966. (Do a web search for "Eliza" and "Weizenbaum" if you want to know
more about this.)
In fact, it is much more primitive than Eliza. But that's enough to match the
quality of many software companies' technical support advice... ;-)
To start this program, create a SupportSystem object and execute the "start"
method.
Then start describing your problem by typing in the terminal window.
The purpose of this project is to demonstrate and study library classes, such
as ArrayList, HashMap, HashSet, and Random.

View File

@@ -0,0 +1,16 @@
#BlueJ class context
comment0.params=
comment0.target=Responder()
comment0.text=\n\ Construct\ a\ Responder\n
comment1.params=words
comment1.target=java.lang.String\ generateResponse(java.util.HashSet)
comment2.params=
comment2.target=void\ fillResponseMap()
comment2.text=\n\ Enter\ all\ the\ known\ keywords\ and\ their\ associated\ responses\n\ into\ our\ response\ map.\n
comment3.params=
comment3.target=void\ fillDefaultResponses()
comment3.text=\n\ Build\ up\ a\ list\ of\ default\ responses\ from\ which\ we\ can\ pick\ one\n\ if\ we\ don't\ know\ what\ else\ to\ say.\n
comment4.params=
comment4.target=java.lang.String\ pickDefaultResponse()
comment4.text=\n\ Randomly\ select\ and\ return\ one\ of\ the\ default\ responses.\n\ @return\ \ \ \ \ A\ random\ default\ response\n
numComments=5

View File

@@ -0,0 +1,142 @@
import java.util.HashMap;
import java.util.HashSet;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Random;
/**
* The responder class represents a response generator object.
* It is used to generate an automatic response, based on specified input.
* Input is presented to the responder as a set of words, and based on those
* words the responder will generate a String that represents the response.
*
* Internally, the reponder uses a HashMap to associate words with response
* strings and a list of default responses. If any of the input words is found
* in the HashMap, the corresponding response is returned. If none of the input
* words is recognized, one of the default responses is randomly chosen.
*
* @author Michael Kölling and David J. Barnes
* @version 1.0 (2016.02.29)
*/
public class Responder
{
// Used to map key words to responses.
private HashMap<String, String> responseMap;
// Default responses to use if we don't recognise a word.
private ArrayList<String> defaultResponses;
private Random randomGenerator;
/**
* Construct a Responder
*/
public Responder()
{
responseMap = new HashMap<>();
defaultResponses = new ArrayList<>();
fillResponseMap();
fillDefaultResponses();
randomGenerator = new Random();
}
public String generateResponse(HashSet<String> words)
{
for (String word : words) {
String response = responseMap.get(word);
if(response != null) {
return response;
}
}
// If we get here, none of the words from the input line was recognized.
// In this case we pick one of our default responses (what we say when
// we cannot think of anything else to say...)
return pickDefaultResponse();
}
/**
* Enter all the known keywords and their associated responses
* into our response map.
*/
private void fillResponseMap()
{
responseMap.put("crash",
"Well, it never crashes on our system. It must have something\n" +
"to do with your system. Tell me more about your configuration.");
responseMap.put("crashes",
"Well, it never crashes on our system. It must have something\n" +
"to do with your system. Tell me more about your configuration.");
responseMap.put("slow",
"I think this has to do with your hardware. Upgrading your processor\n" +
"should solve all performance problems. Have you got a problem with\n" +
"our software?");
responseMap.put("performance",
"Performance was quite adequate in all our tests. Are you running\n" +
"any other processes in the background?");
responseMap.put("bug",
"Well, you know, all software has some bugs. But our software engineers\n" +
"are working very hard to fix them. Can you describe the problem a bit\n" +
"further?");
responseMap.put("buggy",
"Well, you know, all software has some bugs. But our software engineers\n" +
"are working very hard to fix them. Can you describe the problem a bit\n" +
"further?");
responseMap.put("windows",
"This is a known bug to do with the Windows operating system. Please\n" +
"report it to Microsoft. There is nothing we can do about this.");
responseMap.put("mac",
"This is a known bug to do with the Mac operating system. Please\n" +
"report it to Apple. There is nothing we can do about this.");
responseMap.put("expensive",
"The cost of our product is quite competitive. Have you looked around\n" +
"and really compared our features?");
responseMap.put("installation",
"The installation is really quite straight forward. We have tons of\n" +
"wizards that do all the work for you. Have you read the installation\n" +
"instructions?");
responseMap.put("memory",
"If you read the system requirements carefully, you will see that the\n" +
"specified memory requirements are 1.5 giga byte. You really should\n" +
"upgrade your memory. Anything else you want to know?");
responseMap.put("linux",
"We take Linux support very seriously. But there are some problems.\n" +
"Most have to do with incompatible glibc versions. Can you be a bit\n" +
"more precise?");
responseMap.put("bluej",
"Ahhh, BlueJ, yes. We tried to buy out those guys long ago, but\n" +
"they simply won't sell... Stubborn people they are. Nothing we can\n" +
"do about it, I'm afraid.");
}
/**
* Build up a list of default responses from which we can pick one
* if we don't know what else to say.
*/
private void fillDefaultResponses()
{
defaultResponses.add("That sounds odd. Could you describe that problem in more detail?");
defaultResponses.add("No other customer has ever complained about this before. \n" +
"What is your system configuration?");
defaultResponses.add("That sounds interesting. Tell me more...");
defaultResponses.add("I need a bit more information on that.");
defaultResponses.add("Have you checked that you do not have a dll conflict?");
defaultResponses.add("That is explained in the manual. Have you read the manual?");
defaultResponses.add("Your description is a bit wishy-washy. Have you got an expert\n" +
"there with you who could describe this more precisely?");
defaultResponses.add("That's not a bug, it's a feature!");
defaultResponses.add("Could you elaborate on that?");
}
/**
* Randomly select and return one of the default responses.
* @return A random default response
*/
private String pickDefaultResponse()
{
// Pick a random number for the index in the default response list.
// The number will be between 0 (inclusive) and the size of the list (exclusive).
int index = randomGenerator.nextInt(defaultResponses.size());
return defaultResponses.get(index);
}
}

View File

@@ -0,0 +1,13 @@
#BlueJ class context
comment0.params=
comment0.target=SupportSystem()
comment0.text=\n\ Creates\ a\ technical\ support\ system.\n
comment1.params=
comment1.target=void\ start()
comment2.params=
comment2.target=void\ printWelcome()
comment2.text=\n\ Print\ a\ welcome\ message\ to\ the\ screen.\n
comment3.params=
comment3.target=void\ printGoodbye()
comment3.text=\n\ Print\ a\ good-bye\ message\ to\ the\ screen.\n
numComments=4

View File

@@ -0,0 +1,70 @@
import java.util.HashSet;
/**
* This class implements a technical support system. It is the top level class
* in this project. The support system communicates via text input/output
* in the text terminal.
*
* This class uses an object of class InputReader to read input from the user,
* and an object of class Responder to generate responses. It contains a loop
* that repeatedly reads input and generates output until the users wants to
* leave.
*
* @author Michael Kölling and David J. Barnes
* @version 1.0 (2016.02.29)
*/
public class SupportSystem
{
private InputReader reader;
private Responder responder;
/**
* Creates a technical support system.
*/
public SupportSystem()
{
reader = new InputReader();
responder = new Responder();
}
public void start()
{
boolean finished = false;
printWelcome();
while(!finished) {
HashSet<String> input = reader.getInput();
if(input.contains("bye")) {
finished = true;
}
else {
String response = responder.generateResponse(input);
System.out.println(response);
}
}
printGoodbye();
}
/**
* Print a welcome message to the screen.
*/
private void printWelcome()
{
System.out.println("Welcome to the DodgySoft Technical Support System.");
System.out.println();
System.out.println("Please tell us about your problem.");
System.out.println("We will assist you with any problem you might have.");
System.out.println("Please type 'bye' to exit our system.");
}
/**
* Print a good-bye message to the screen.
*/
private void printGoodbye()
{
System.out.println("Nice talking to you. Bye...");
}
}

View File

@@ -0,0 +1,61 @@
#BlueJ package file
dependency1.from=SupportSystem
dependency1.to=InputReader
dependency1.type=UsesDependency
dependency2.from=SupportSystem
dependency2.to=Responder
dependency2.type=UsesDependency
objectbench.height=76
objectbench.width=756
package.editor.height=407
package.editor.width=648
package.editor.x=70
package.editor.y=80
package.numDependencies=2
package.numTargets=3
package.showExtends=true
package.showUses=true
project.charset=UTF-8
readme.editor.height=651
readme.editor.width=882
readme.editor.x=53
readme.editor.y=23
target1.editor.height=777
target1.editor.width=1189
target1.editor.x=53
target1.editor.y=23
target1.height=60
target1.name=InputReader
target1.naviview.expanded=false
target1.showInterface=false
target1.type=ClassTarget
target1.typeParameters=
target1.width=110
target1.x=90
target1.y=210
target2.editor.height=1049
target2.editor.width=1250
target2.editor.x=811
target2.editor.y=24
target2.height=60
target2.name=Responder
target2.naviview.expanded=true
target2.showInterface=false
target2.type=ClassTarget
target2.typeParameters=
target2.width=100
target2.x=400
target2.y=210
target3.editor.height=777
target3.editor.width=1189
target3.editor.x=66
target3.editor.y=125
target3.height=60
target3.name=SupportSystem
target3.naviview.expanded=true
target3.showInterface=false
target3.type=ClassTarget
target3.typeParameters=
target3.width=140
target3.x=230
target3.y=90

View File

@@ -0,0 +1,8 @@
#BlueJ class context
comment0.params=
comment0.target=InputReader()
comment0.text=\n\ Create\ a\ new\ InputReader\ that\ reads\ text\ from\ the\ text\ terminal.\n
comment1.params=
comment1.target=java.lang.String\ getInput()
comment1.text=\n\ Read\ a\ line\ of\ text\ from\ standard\ input\ (the\ text\ terminal),\n\ and\ return\ it\ as\ a\ String.\n\n\ @return\ \ A\ String\ typed\ by\ the\ user.\n
numComments=2

View File

@@ -0,0 +1,36 @@
import java.util.HashSet;
import java.util.Scanner;
/**
* InputReader reads typed text input from the standard text terminal.
* The text typed by a user is returned.
*
* @author Michael Kölling and David J. Barnes
* @version 0.1 (2016.02.29)
*/
public class InputReader
{
private Scanner reader;
/**
* Create a new InputReader that reads text from the text terminal.
*/
public InputReader()
{
reader = new Scanner(System.in);
}
/**
* Read a line of text from standard input (the text terminal),
* and return it as a String.
*
* @return A String typed by the user.
*/
public String getInput()
{
System.out.print("> "); // print prompt
String inputLine = reader.nextLine();
return inputLine;
}
}

View File

@@ -0,0 +1,31 @@
TechSupport - the DodgySoft Technical support system.
This project is part of the material for the book
Objects First with Java - A Practical Introduction using BlueJ
Sixth edition
David J. Barnes and Michael Kölling
Pearson Education, 2016
This project is discussed in chapter 6.
This project aims to implements a technical support system for customers of
the DodgySoft software company. Users can describe their software problems and
get advice instantly!
The idea is based on Eliza - a famous program described by Joseph Weizenbaum
in 1966. (Do a web search for "Eliza" and "Weizenbaum" if you want to know
more about this.)
In fact, it is much more primitive than Eliza. But that's enough to match the
quality of many software companies' technical support advice... ;-)
To start this program, create a SupportSystem object and execute the "start"
method.
Then start describing your problem by typing in the terminal window.
The purpose of this project is to demonstrate and study library classes, such
as ArrayList, HashMap, HashSet, and Random.
This project is only a first, rudimentary step towards the full solution.

View File

@@ -0,0 +1,8 @@
#BlueJ class context
comment0.params=
comment0.target=Responder()
comment0.text=\n\ Construct\ a\ Responder\ -\ nothing\ to\ do\n
comment1.params=
comment1.target=java.lang.String\ generateResponse()
comment1.text=\n\ Generate\ a\ response.\n\ @return\ \ \ A\ string\ that\ should\ be\ displayed\ as\ the\ response\n
numComments=2

View File

@@ -0,0 +1,25 @@
/**
* The responder class represents a response generator object.
* It is used to generate an automatic response to an input string.
*
* @author Michael Kölling and David J. Barnes
* @version 0.1 (2016.02.29)
*/
public class Responder
{
/**
* Construct a Responder - nothing to do
*/
public Responder()
{
}
/**
* Generate a response.
* @return A string that should be displayed as the response
*/
public String generateResponse()
{
return "That sounds interesting. Tell me more...";
}
}

View File

@@ -0,0 +1,14 @@
#BlueJ class context
comment0.params=
comment0.target=SupportSystem()
comment0.text=\n\ Creates\ a\ technical\ support\ system.\n
comment1.params=
comment1.target=void\ start()
comment1.text=\n\ Start\ the\ technical\ support\ system.\ This\ will\ print\ a\ welcome\n\ message\ and\ enter\ into\ a\ dialog\ with\ the\ user,\ until\ the\ user\n\ ends\ the\ dialog.\n
comment2.params=
comment2.target=void\ printWelcome()
comment2.text=\n\ Print\ a\ welcome\ message\ to\ the\ screen.\n
comment3.params=
comment3.target=void\ printGoodbye()
comment3.text=\n\ Print\ a\ good-bye\ message\ to\ the\ screen.\n
numComments=4

View File

@@ -0,0 +1,73 @@
/**
* This class implements a technical support system. It is the top level class
* in this project. The support system communicates via text input/output
* in the text terminal.
*
* This class uses an object of class InputReader to read input from the user,
* and an object of class Responder to generate responses. It contains a loop
* that repeatedly reads input and generates output until the users wants to
* leave.
*
* @author Michael Kölling and David J. Barnes
* @version 0.1 (2016.02.29)
*/
public class SupportSystem
{
private InputReader reader;
private Responder responder;
/**
* Creates a technical support system.
*/
public SupportSystem()
{
reader = new InputReader();
responder = new Responder();
}
/**
* Start the technical support system. This will print a welcome
* message and enter into a dialog with the user, until the user
* ends the dialog.
*/
public void start()
{
boolean finished = false;
printWelcome();
while(!finished) {
String input = reader.getInput();
if(input.startsWith("bye")) {
finished = true;
}
else {
String response = responder.generateResponse();
System.out.println(response);
}
}
printGoodbye();
}
/**
* Print a welcome message to the screen.
*/
private void printWelcome()
{
System.out.println("Welcome to the DodgySoft Technical Support System.");
System.out.println();
System.out.println("Please tell us about your problem.");
System.out.println("We will assist you with any problem you might have.");
System.out.println("Please type 'bye' to exit our system.");
}
/**
* Print a good-bye message to the screen.
*/
private void printGoodbye()
{
System.out.println("Nice talking to you. Bye...");
}
}

View File

@@ -0,0 +1,61 @@
#BlueJ package file
dependency1.from=SupportSystem
dependency1.to=InputReader
dependency1.type=UsesDependency
dependency2.from=SupportSystem
dependency2.to=Responder
dependency2.type=UsesDependency
objectbench.height=76
objectbench.width=766
package.editor.height=409
package.editor.width=658
package.editor.x=70
package.editor.y=80
package.numDependencies=2
package.numTargets=3
package.showExtends=true
package.showUses=true
project.charset=UTF-8
readme.editor.height=630
readme.editor.width=846
readme.editor.x=53
readme.editor.y=78
target1.editor.height=777
target1.editor.width=882
target1.editor.x=53
target1.editor.y=23
target1.height=60
target1.name=InputReader
target1.naviview.expanded=true
target1.showInterface=false
target1.type=ClassTarget
target1.typeParameters=
target1.width=110
target1.x=110
target1.y=200
target2.editor.height=746
target2.editor.width=860
target2.editor.x=53
target2.editor.y=54
target2.height=60
target2.name=Responder
target2.naviview.expanded=true
target2.showInterface=false
target2.type=ClassTarget
target2.typeParameters=
target2.width=100
target2.x=400
target2.y=200
target3.editor.height=660
target3.editor.width=841
target3.editor.x=49
target3.editor.y=23
target3.height=60
target3.name=SupportSystem
target3.naviview.expanded=true
target3.showInterface=false
target3.type=ClassTarget
target3.typeParameters=
target3.width=140
target3.x=240
target3.y=90

View File

@@ -0,0 +1,8 @@
#BlueJ class context
comment0.params=
comment0.target=InputReader()
comment0.text=\n\ Create\ a\ new\ InputReader\ that\ reads\ text\ from\ the\ text\ terminal.\n
comment1.params=
comment1.target=java.lang.String\ getInput()
comment1.text=\n\ Read\ a\ line\ of\ text\ from\ standard\ input\ (the\ text\ terminal),\n\ and\ return\ it\ as\ a\ String.\n\n\ @return\ \ A\ String\ typed\ by\ the\ user.\n
numComments=2

View File

@@ -0,0 +1,36 @@
import java.util.Scanner;
/**
* InputReader reads typed text input from the standard text terminal.
* The text typed by a user is then chopped into words, and a set of words
* is provided.
*
* @author Michael Kölling and David J. Barnes
* @version 0.2 (2016.02.29)
*/
public class InputReader
{
private Scanner reader;
/**
* Create a new InputReader that reads text from the text terminal.
*/
public InputReader()
{
reader = new Scanner(System.in);
}
/**
* Read a line of text from standard input (the text terminal),
* and return it as a String.
*
* @return A String typed by the user.
*/
public String getInput()
{
System.out.print("> "); // print prompt
String inputLine = reader.nextLine();
return inputLine;
}
}

View File

@@ -0,0 +1,21 @@
TechSupport - the DodgySoft Technical support system, version #2.
This project is part of the material for the book
Objects First with Java - A Practical Introduction using BlueJ
Sixth edition
David J. Barnes and Michael Kölling
Pearson Education, 2016
This project is discussed in chapter 6.
This project implements a second version of the technical support system
example. It is an intermediate version intended to demonstrate the first
few changes discussed in this book chapter.
To start this program, create a SupportSystem object and execute the "start"
method.
Then start describing your problem by typing in the terminal window.

View File

@@ -0,0 +1,11 @@
#BlueJ class context
comment0.params=
comment0.target=Responder()
comment0.text=\n\ Construct\ a\ Responder\n
comment1.params=
comment1.target=java.lang.String\ generateResponse()
comment1.text=\n\ Generate\ a\ response.\n\ \n\ @return\ \ A\ string\ that\ should\ be\ displayed\ as\ the\ response\n
comment2.params=
comment2.target=void\ fillResponses()
comment2.text=\n\ Build\ up\ a\ list\ of\ default\ responses\ from\ which\ we\ can\ pick\ one\n\ if\ we\ don't\ know\ what\ else\ to\ say.\n
numComments=3

View File

@@ -0,0 +1,62 @@
import java.util.ArrayList;
import java.util.Random;
/**
* The responder class represents a response generator object. It is used
* to generate an automatic response. This is the second version of this
* class. This time, we generate some random behavior by randomly selecting
* a phrase from a predefined list of responses.
*
* @author Michael Kölling and David J. Barnes
* @version 0.2 (2016.02.29)
*/
public class Responder
{
private Random randomGenerator;
private ArrayList<String> responses;
/**
* Construct a Responder
*/
public Responder()
{
randomGenerator = new Random();
responses = new ArrayList<>();
fillResponses();
}
/**
* Generate a response.
*
* @return A string that should be displayed as the response
*/
public String generateResponse()
{
// Pick a random number for the index in the default response
// list. The number will be between 0 (inclusive) and the size
// of the list (exclusive).
int index = randomGenerator.nextInt(responses.size());
return responses.get(index);
}
/**
* Build up a list of default responses from which we can pick one
* if we don't know what else to say.
*/
private void fillResponses()
{
responses.add("That sounds odd. Could you describe this in more detail?");
responses.add("No other customer has ever complained about this \n" +
"before. What is your system configuration?");
responses.add("I need a bit more information on that.");
responses.add("Have you checked that you do not have a dll conflict?");
responses.add("That is covered in the manual. Have you read the manual?");
responses.add("Your description is a bit wishy-washy. Have you got \n" +
"an expert there with you who could describe this better?");
responses.add("That's not a bug, it's a feature!");
responses.add("Could you elaborate on that?");
responses.add("Have you tried running the app on your phone?");
responses.add("I just checked StackOverflow - they don't know either.");
}
}

View File

@@ -0,0 +1,14 @@
#BlueJ class context
comment0.params=
comment0.target=SupportSystem()
comment0.text=\n\ Creates\ a\ technical\ support\ system.\n
comment1.params=
comment1.target=void\ start()
comment1.text=\n\ Start\ the\ technical\ support\ system.\ This\ will\ print\ a\ welcome\ message\ and\ enter\n\ into\ a\ dialog\ with\ the\ user,\ until\ the\ user\ ends\ the\ dialog.\n
comment2.params=
comment2.target=void\ printWelcome()
comment2.text=\n\ Print\ a\ welcome\ message\ to\ the\ screen.\n
comment3.params=
comment3.target=void\ printGoodbye()
comment3.text=\n\ Print\ a\ good-bye\ message\ to\ the\ screen.\n
numComments=4

View File

@@ -0,0 +1,71 @@
/**
* This class implements a technical support system. It is the top level class
* in this project. The support system communicates via text input/output
* in the text terminal.
*
* This class uses an object of class InputReader to read input from the user,
* and an object of class Responder to generate responses. It contains a loop
* that repeatedly reads input and generates output until the users wants to
* leave.
*
* @author Michael Kölling and David J. Barnes
* @version 0.2 (2016.02.29)
*/
public class SupportSystem
{
private InputReader reader;
private Responder responder;
/**
* Creates a technical support system.
*/
public SupportSystem()
{
reader = new InputReader();
responder = new Responder();
}
/**
* Start the technical support system. This will print a welcome message and enter
* into a dialog with the user, until the user ends the dialog.
*/
public void start()
{
boolean finished = false;
printWelcome();
while(!finished) {
String input = reader.getInput().trim().toLowerCase();
if(input.startsWith("bye")) {
finished = true;
}
else {
String response = responder.generateResponse();
System.out.println(response);
}
}
printGoodbye();
}
/**
* Print a welcome message to the screen.
*/
private void printWelcome()
{
System.out.println("Welcome to the DodgySoft Technical Support System.");
System.out.println();
System.out.println("Please tell us about your problem. We will assist you");
System.out.println("with any problem you might have. Please type 'bye'");
System.out.println("to exit our system.");
}
/**
* Print a good-bye message to the screen.
*/
private void printGoodbye()
{
System.out.println("Nice talking to you. Bye...");
}
}

View File

@@ -0,0 +1,61 @@
#BlueJ package file
dependency1.from=SupportSystem
dependency1.to=InputReader
dependency1.type=UsesDependency
dependency2.from=SupportSystem
dependency2.to=Responder
dependency2.type=UsesDependency
objectbench.height=76
objectbench.width=754
package.editor.height=406
package.editor.width=646
package.editor.x=70
package.editor.y=80
package.numDependencies=2
package.numTargets=3
package.showExtends=true
package.showUses=true
project.charset=UTF-8
readme.editor.height=651
readme.editor.width=823
readme.editor.x=53
readme.editor.y=23
target1.editor.height=762
target1.editor.width=908
target1.editor.x=53
target1.editor.y=60
target1.height=60
target1.name=InputReader
target1.naviview.expanded=true
target1.showInterface=false
target1.type=ClassTarget
target1.typeParameters=
target1.width=110
target1.x=130
target1.y=210
target2.editor.height=767
target2.editor.width=1248
target2.editor.x=32
target2.editor.y=23
target2.height=60
target2.name=Responder
target2.naviview.expanded=false
target2.showInterface=false
target2.type=ClassTarget
target2.typeParameters=
target2.width=100
target2.x=380
target2.y=210
target3.editor.height=657
target3.editor.width=921
target3.editor.x=53
target3.editor.y=23
target3.height=60
target3.name=SupportSystem
target3.naviview.expanded=true
target3.showInterface=false
target3.type=ClassTarget
target3.typeParameters=
target3.width=140
target3.x=240
target3.y=90