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,29 @@
#BlueJ class context
comment0.params=
comment0.target=Canvas\ getCanvas()
comment0.text=\n\ Factory\ method\ to\ get\ the\ canvas\ singleton\ object.\n
comment1.params=title\ width\ height\ bgColor
comment1.target=Canvas(java.lang.String,\ int,\ int,\ java.awt.Color)
comment1.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\ bgColor\ the\ desired\ background\ color\ of\ the\ canvas\n
comment2.params=visible
comment2.target=void\ setVisible(boolean)
comment2.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
comment3.params=referenceObject\ color\ shape
comment3.target=void\ draw(java.lang.Object,\ java.lang.String,\ java.awt.Shape)
comment3.text=\n\ Draw\ a\ given\ shape\ onto\ the\ canvas.\n\ @param\ \ referenceObject\ \ an\ object\ to\ define\ identity\ for\ this\ shape\n\ @param\ \ color\ \ \ \ \ \ \ \ \ \ \ \ the\ color\ of\ the\ shape\n\ @param\ \ shape\ \ \ \ \ \ \ \ \ \ \ \ the\ shape\ object\ to\ be\ drawn\ on\ the\ canvas\n
comment4.params=referenceObject
comment4.target=void\ erase(java.lang.Object)
comment4.text=\n\ Erase\ a\ given\ shape's\ from\ the\ screen.\n\ @param\ \ referenceObject\ \ the\ shape\ object\ to\ be\ erased\ \n
comment5.params=colorString
comment5.target=void\ setForegroundColor(java.lang.String)
comment5.text=\n\ Set\ the\ foreground\ color\ of\ the\ Canvas.\n\ @param\ \ newColor\ \ \ the\ new\ color\ for\ the\ foreground\ of\ the\ Canvas\ \n
comment6.params=milliseconds
comment6.target=void\ wait(int)
comment6.text=\n\ Wait\ 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
comment7.params=
comment7.target=void\ redraw()
comment7.text=\n\ Redraw\ ell\ shapes\ currently\ on\ the\ Canvas.\n
comment8.params=
comment8.target=void\ erase()
comment8.text=\n\ Erase\ the\ whole\ canvas.\ (Does\ not\ repaint.)\n
numComments=9

View File

@@ -0,0 +1,230 @@
import javax.swing.*;
import java.awt.*;
import java.util.List;
import java.util.*;
/**
* Canvas is a class to allow for simple graphical drawing on a canvas.
* This is a modification of the general purpose Canvas, specially made for
* the BlueJ "shapes" example.
*
* @author: Bruce Quig
* @author: Michael Kölling (mik)
*
* @version 2016.02.29
*/
public class Canvas
{
// Note: The implementation of this class (specifically the handling of
// shape identity and colors) is slightly more complex than necessary. This
// is done on purpose to keep the interface and instance fields of the
// shape objects in this project clean and simple for educational purposes.
private static Canvas canvasSingleton;
/**
* Factory method to get the canvas singleton object.
*/
public static Canvas getCanvas()
{
if(canvasSingleton == null) {
canvasSingleton = new Canvas("BlueJ Picture Demo", 500, 300,
Color.white);
}
canvasSingleton.setVisible(true);
return canvasSingleton;
}
// ----- instance part -----
private JFrame frame;
private CanvasPane canvas;
private Graphics2D graphic;
private Color backgroundColor;
private Image canvasImage;
private List<Object> objects;
private HashMap<Object, ShapeDescription> shapes;
/**
* 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 bgColor the desired background color of the canvas
*/
private Canvas(String title, int width, int height, Color bgColor)
{
frame = new JFrame();
canvas = new CanvasPane();
frame.setContentPane(canvas);
frame.setTitle(title);
frame.setLocation(30, 30);
canvas.setPreferredSize(new Dimension(width, height));
backgroundColor = bgColor;
frame.pack();
objects = new ArrayList<Object>();
shapes = new HashMap<Object, ShapeDescription>();
}
/**
* 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(visible);
}
/**
* Draw a given shape onto the canvas.
* @param referenceObject an object to define identity for this shape
* @param color the color of the shape
* @param shape the shape object to be drawn on the canvas
*/
// Note: this is a slightly backwards way of maintaining the shape
// objects. It is carefully designed to keep the visible shape interfaces
// in this project clean and simple for educational purposes.
public void draw(Object referenceObject, String color, Shape shape)
{
objects.remove(referenceObject); // just in case it was already there
objects.add(referenceObject); // add at the end
shapes.put(referenceObject, new ShapeDescription(shape, color));
redraw();
}
/**
* Erase a given shape's from the screen.
* @param referenceObject the shape object to be erased
*/
public void erase(Object referenceObject)
{
objects.remove(referenceObject); // just in case it was already there
shapes.remove(referenceObject);
redraw();
}
/**
* Set the foreground color of the Canvas.
* @param newColor the new color for the foreground of the Canvas
*/
public void setForegroundColor(String colorString)
{
if(colorString.equals("red")) {
graphic.setColor(new Color(235, 25, 25));
}
else if(colorString.equals("black")) {
graphic.setColor(Color.black);
}
else if(colorString.equals("blue")) {
graphic.setColor(new Color(30, 75, 220));
}
else if(colorString.equals("yellow")) {
graphic.setColor(new Color(255, 230, 0));
}
else if(colorString.equals("green")) {
graphic.setColor(new Color(80, 160, 60));
}
else if(colorString.equals("magenta")) {
graphic.setColor(Color.magenta);
}
else if(colorString.equals("white")) {
graphic.setColor(Color.white);
}
else {
graphic.setColor(Color.black);
}
}
/**
* Wait 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 (Exception e)
{
// ignoring exception at the moment
}
}
/**
* Redraw ell shapes currently on the Canvas.
*/
private void redraw()
{
erase();
for(Object shape : objects) {
shapes.get(shape).draw(graphic);
}
canvas.repaint();
}
/**
* Erase the whole canvas. (Does not repaint.)
*/
private 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);
}
/************************************************************************
* 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);
}
}
/************************************************************************
* 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 ShapeDescription
{
private Shape shape;
private String colorString;
public ShapeDescription(Shape shape, String color)
{
this.shape = shape;
colorString = color;
}
public void draw(Graphics2D graphic)
{
setForegroundColor(colorString);
graphic.fill(shape);
}
}
}

View File

@@ -0,0 +1,47 @@
#BlueJ class context
comment0.params=
comment0.target=Circle()
comment0.text=\n\ Create\ a\ new\ circle\ at\ default\ position\ with\ default\ color.\n
comment1.params=
comment1.target=void\ makeVisible()
comment1.text=\n\ Make\ this\ circle\ visible.\ If\ it\ was\ already\ visible,\ do\ nothing.\n
comment10.params=distance
comment10.target=void\ slowMoveVertical(int)
comment10.text=\n\ Slowly\ move\ the\ circle\ vertically\ by\ 'distance'\ pixels.\n
comment11.params=newDiameter
comment11.target=void\ changeSize(int)
comment11.text=\n\ Change\ the\ size\ to\ the\ new\ size\ (in\ pixels).\ Size\ must\ be\ >\=\ 0.\n
comment12.params=newColor
comment12.target=void\ changeColor(java.lang.String)
comment12.text=\n\ Change\ the\ color.\ Valid\ colors\ are\ "red",\ "yellow",\ "blue",\ "green",\n\ "magenta"\ and\ "black".\n
comment13.params=
comment13.target=void\ draw()
comment13.text=\n\ Draw\ the\ circle\ with\ current\ specifications\ on\ screen.\n
comment14.params=
comment14.target=void\ erase()
comment14.text=\n\ Erase\ the\ circle\ on\ screen.\n
comment2.params=
comment2.target=void\ makeInvisible()
comment2.text=\n\ Make\ this\ circle\ invisible.\ If\ it\ was\ already\ invisible,\ do\ nothing.\n
comment3.params=
comment3.target=void\ moveRight()
comment3.text=\n\ Move\ the\ circle\ a\ few\ pixels\ to\ the\ right.\n
comment4.params=
comment4.target=void\ moveLeft()
comment4.text=\n\ Move\ the\ circle\ a\ few\ pixels\ to\ the\ left.\n
comment5.params=
comment5.target=void\ moveUp()
comment5.text=\n\ Move\ the\ circle\ a\ few\ pixels\ up.\n
comment6.params=
comment6.target=void\ moveDown()
comment6.text=\n\ Move\ the\ circle\ a\ few\ pixels\ down.\n
comment7.params=distance
comment7.target=void\ moveHorizontal(int)
comment7.text=\n\ Move\ the\ circle\ horizontally\ by\ 'distance'\ pixels.\n
comment8.params=distance
comment8.target=void\ moveVertical(int)
comment8.text=\n\ Move\ the\ circle\ vertically\ by\ 'distance'\ pixels.\n
comment9.params=distance
comment9.target=void\ slowMoveHorizontal(int)
comment9.text=\n\ Slowly\ move\ the\ circle\ horizontally\ by\ 'distance'\ pixels.\n
numComments=15

View File

@@ -0,0 +1,191 @@
import java.awt.*;
import java.awt.geom.*;
/**
* A circle that can be manipulated and that draws itself on a canvas.
*
* @author Michael Kölling and David J. Barnes
* @version 2016.02.29
*/
public class Circle
{
private int diameter;
private int xPosition;
private int yPosition;
private String color;
private boolean isVisible;
/**
* Create a new circle at default position with default color.
*/
public Circle()
{
diameter = 68;
xPosition = 230;
yPosition = 90;
color = "blue";
}
/**
* Make this circle visible. If it was already visible, do nothing.
*/
public void makeVisible()
{
isVisible = true;
draw();
}
/**
* Make this circle invisible. If it was already invisible, do nothing.
*/
public void makeInvisible()
{
erase();
isVisible = false;
}
/**
* Move the circle a few pixels to the right.
*/
public void moveRight()
{
moveHorizontal(20);
}
/**
* Move the circle a few pixels to the left.
*/
public void moveLeft()
{
moveHorizontal(-20);
}
/**
* Move the circle a few pixels up.
*/
public void moveUp()
{
moveVertical(-20);
}
/**
* Move the circle a few pixels down.
*/
public void moveDown()
{
moveVertical(20);
}
/**
* Move the circle horizontally by 'distance' pixels.
*/
public void moveHorizontal(int distance)
{
erase();
xPosition += distance;
draw();
}
/**
* Move the circle vertically by 'distance' pixels.
*/
public void moveVertical(int distance)
{
erase();
yPosition += distance;
draw();
}
/**
* Slowly move the circle horizontally by 'distance' pixels.
*/
public void slowMoveHorizontal(int distance)
{
int delta;
if(distance < 0)
{
delta = -1;
distance = -distance;
}
else
{
delta = 1;
}
for(int i = 0; i < distance; i++)
{
xPosition += delta;
draw();
}
}
/**
* Slowly move the circle vertically by 'distance' pixels.
*/
public void slowMoveVertical(int distance)
{
int delta;
if(distance < 0)
{
delta = -1;
distance = -distance;
}
else
{
delta = 1;
}
for(int i = 0; i < distance; i++)
{
yPosition += delta;
draw();
}
}
/**
* Change the size to the new size (in pixels). Size must be >= 0.
*/
public void changeSize(int newDiameter)
{
erase();
diameter = newDiameter;
draw();
}
/**
* Change the color. Valid colors are "red", "yellow", "blue", "green",
* "magenta" and "black".
*/
public void changeColor(String newColor)
{
color = newColor;
draw();
}
/**
* Draw the circle with current specifications on screen.
*/
private void draw()
{
if(isVisible) {
Canvas canvas = Canvas.getCanvas();
canvas.draw(this, color, new Ellipse2D.Double(xPosition, yPosition,
diameter, diameter));
canvas.wait(10);
}
}
/**
* Erase the circle on screen.
*/
private void erase()
{
if(isVisible) {
Canvas canvas = Canvas.getCanvas();
canvas.erase(this);
}
}
}

View File

@@ -0,0 +1,47 @@
#BlueJ class context
comment0.params=
comment0.target=Person()
comment0.text=\n\ Create\ a\ new\ person\ at\ default\ position\ with\ default\ color.\n
comment1.params=
comment1.target=void\ makeVisible()
comment1.text=\n\ Make\ this\ person\ visible.\ If\ it\ was\ already\ visible,\ do\ nothing.\n
comment10.params=distance
comment10.target=void\ slowMoveVertical(int)
comment10.text=\n\ Slowly\ move\ the\ person\ vertically\ by\ 'distance'\ pixels.\n
comment11.params=newHeight\ newWidth
comment11.target=void\ changeSize(int,\ int)
comment11.text=\n\ Change\ the\ size\ to\ the\ new\ size\ (in\ pixels).\ Size\ must\ be\ >\=\ 0.\n
comment12.params=newColor
comment12.target=void\ changeColor(java.lang.String)
comment12.text=\n\ Change\ the\ color.\ Valid\ colors\ are\ "red",\ "yellow",\ "blue",\ "green",\n\ "magenta"\ and\ "black".\n
comment13.params=
comment13.target=void\ draw()
comment13.text=\n\ Draw\ the\ person\ with\ current\ specifications\ on\ screen.\n
comment14.params=
comment14.target=void\ erase()
comment14.text=\n\ Erase\ the\ person\ on\ screen.\n
comment2.params=
comment2.target=void\ makeInvisible()
comment2.text=\n\ Make\ this\ person\ invisible.\ If\ it\ was\ already\ invisible,\ do\ nothing.\n
comment3.params=
comment3.target=void\ moveRight()
comment3.text=\n\ Move\ the\ person\ a\ few\ pixels\ to\ the\ right.\n
comment4.params=
comment4.target=void\ moveLeft()
comment4.text=\n\ Move\ the\ person\ a\ few\ pixels\ to\ the\ left.\n
comment5.params=
comment5.target=void\ moveUp()
comment5.text=\n\ Move\ the\ person\ a\ few\ pixels\ up.\n
comment6.params=
comment6.target=void\ moveDown()
comment6.text=\n\ Move\ the\ person\ a\ few\ pixels\ down.\n
comment7.params=distance
comment7.target=void\ moveHorizontal(int)
comment7.text=\n\ Move\ the\ person\ horizontally\ by\ 'distance'\ pixels.\n
comment8.params=distance
comment8.target=void\ moveVertical(int)
comment8.text=\n\ Move\ the\ person\ vertically\ by\ 'distance'\ pixels.\n
comment9.params=distance
comment9.target=void\ slowMoveHorizontal(int)
comment9.text=\n\ Slowly\ move\ the\ person\ horizontally\ by\ 'distance'\ pixels.\n
numComments=15

View File

@@ -0,0 +1,206 @@
import java.awt.*;
/**
* A person that can be manipulated and that draws itself on a canvas.
*
* @author Michael Kölling and David J. Barnes
* @version 2016.02.29
*/
public class Person
{
private int height;
private int width;
private int xPosition;
private int yPosition;
private String color;
private boolean isVisible;
/**
* Create a new person at default position with default color.
*/
public Person()
{
height = 60;
width = 30;
xPosition = 280;
yPosition = 190;
color = "black";
isVisible = false;
}
/**
* Make this person visible. If it was already visible, do nothing.
*/
public void makeVisible()
{
isVisible = true;
draw();
}
/**
* Make this person invisible. If it was already invisible, do nothing.
*/
public void makeInvisible()
{
erase();
isVisible = false;
}
/**
* Move the person a few pixels to the right.
*/
public void moveRight()
{
moveHorizontal(20);
}
/**
* Move the person a few pixels to the left.
*/
public void moveLeft()
{
moveHorizontal(-20);
}
/**
* Move the person a few pixels up.
*/
public void moveUp()
{
moveVertical(-20);
}
/**
* Move the person a few pixels down.
*/
public void moveDown()
{
moveVertical(20);
}
/**
* Move the person horizontally by 'distance' pixels.
*/
public void moveHorizontal(int distance)
{
erase();
xPosition += distance;
draw();
}
/**
* Move the person vertically by 'distance' pixels.
*/
public void moveVertical(int distance)
{
erase();
yPosition += distance;
draw();
}
/**
* Slowly move the person horizontally by 'distance' pixels.
*/
public void slowMoveHorizontal(int distance)
{
int delta;
if(distance < 0)
{
delta = -1;
distance = -distance;
}
else
{
delta = 1;
}
for(int i = 0; i < distance; i++)
{
xPosition += delta;
draw();
}
}
/**
* Slowly move the person vertically by 'distance' pixels.
*/
public void slowMoveVertical(int distance)
{
int delta;
if(distance < 0)
{
delta = -1;
distance = -distance;
}
else
{
delta = 1;
}
for(int i = 0; i < distance; i++)
{
yPosition += delta;
draw();
}
}
/**
* Change the size to the new size (in pixels). Size must be >= 0.
*/
public void changeSize(int newHeight, int newWidth)
{
erase();
height = newHeight;
width = newWidth;
draw();
}
/**
* Change the color. Valid colors are "red", "yellow", "blue", "green",
* "magenta" and "black".
*/
public void changeColor(String newColor)
{
color = newColor;
draw();
}
/**
* Draw the person with current specifications on screen.
*/
private void draw()
{
int bh = (int)(height * 0.7); // body height
int hh = (height - bh) / 2; // half head height
int hw = width / 2; // half width
int x = xPosition;
int y = yPosition;
if(isVisible) {
Canvas canvas = Canvas.getCanvas();
int[] xpoints = { x-3, x-hw, x-hw, x-(int)(hw*0.2)-1, x-(int)(hw*0.2)-1, x-hw,
x-hw+(int)(hw*0.4)+1, x, x+hw-(int)(hw*0.4)-1, x+hw, x+(int)(hw*0.2)+1,
x+(int)(hw*0.2)+1, x+hw, x+hw, x+3, x+(int)(hw*0.6),
x+(int)(hw*0.6), x+3, x-3, x-(int)(hw*0.6), x-(int)(hw*0.6) };
int[] ypoints = { y, y+(int)(bh*0.2), y+(int)(bh*0.4), y+(int)(bh*0.2),
y+(int)(bh*0.5), y+bh, y+bh, y+(int)(bh*0.65), y+bh, y+bh,
y+(int)(bh*0.5), y+(int)(bh*0.2), y+(int)(bh*0.4), y+(int)(bh*0.2),
y, y-hh+3, y-hh-3, y-hh-hh, y-hh-hh, y-hh-3, y-hh+3 };
canvas.draw(this, color, new Polygon(xpoints, ypoints, 21));
canvas.wait(10);
}
}
/**
* Erase the person on screen.
*/
private void erase()
{
if(isVisible) {
Canvas canvas = Canvas.getCanvas();
canvas.erase(this);
}
}
}

View File

@@ -0,0 +1,43 @@
Project "figures"
Authors: Michael Kölling and David J. Barnes
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
It is discussed in chapter 1.
This is a very simple project to demonstrate some characteristics of
objects.
You can create various shapes, and you will see, if you do, that those
shapes are drawn on screen (in a window that we call the "canvas").
You can then manipulate these objects: change their position, size and
colour. Try it out: create a few different squares, triangles and circles.
This project is designed as a first example of object-oriented programming.
It illustrates a number of concepts:
- a Java project (application) is a collection of classes
- objects can be created from classes
- from any one class, many objects may be created
- objects have operations (methods)
- operations can have parameters
- parameters have types (at least String and int)
- objects hold data (fields)
- the operations and fields are common to all objects
- the values stored in the fields can be different for each object
The project also demonstrates
- BlueJ object creation
- interactive method invocation
- parameter passing
A good second project to look at after this is "picture", which adds a class
to those ones in this project. That class (named "Picture") uses the shapes
to draw a picture. It can be used to experiment with coding.

View File

@@ -0,0 +1,47 @@
#BlueJ class context
comment0.params=
comment0.target=Square()
comment0.text=\n\ Create\ a\ new\ square\ at\ default\ position\ with\ default\ color.\n
comment1.params=
comment1.target=void\ makeVisible()
comment1.text=\n\ Make\ this\ square\ visible.\ If\ it\ was\ already\ visible,\ do\ nothing.\n
comment10.params=distance
comment10.target=void\ slowMoveVertical(int)
comment10.text=\n\ Slowly\ move\ the\ square\ vertically\ by\ 'distance'\ pixels.\n
comment11.params=newSize
comment11.target=void\ changeSize(int)
comment11.text=\n\ Change\ the\ size\ to\ the\ new\ size\ (in\ pixels).\ Size\ must\ be\ >\=\ 0.\n
comment12.params=newColor
comment12.target=void\ changeColor(java.lang.String)
comment12.text=\n\ Change\ the\ color.\ Valid\ colors\ are\ "red",\ "yellow",\ "blue",\ "green",\n\ "magenta"\ and\ "black".\n
comment13.params=
comment13.target=void\ draw()
comment13.text=\n\ Draw\ the\ square\ with\ current\ specifications\ on\ screen.\n
comment14.params=
comment14.target=void\ erase()
comment14.text=\n\ Erase\ the\ square\ on\ screen.\n
comment2.params=
comment2.target=void\ makeInvisible()
comment2.text=\n\ Make\ this\ square\ invisible.\ If\ it\ was\ already\ invisible,\ do\ nothing.\n
comment3.params=
comment3.target=void\ moveRight()
comment3.text=\n\ Move\ the\ square\ a\ few\ pixels\ to\ the\ right.\n
comment4.params=
comment4.target=void\ moveLeft()
comment4.text=\n\ Move\ the\ square\ a\ few\ pixels\ to\ the\ left.\n
comment5.params=
comment5.target=void\ moveUp()
comment5.text=\n\ Move\ the\ square\ a\ few\ pixels\ up.\n
comment6.params=
comment6.target=void\ moveDown()
comment6.text=\n\ Move\ the\ square\ a\ few\ pixels\ down.\n
comment7.params=distance
comment7.target=void\ moveHorizontal(int)
comment7.text=\n\ Move\ the\ square\ horizontally\ by\ 'distance'\ pixels.\n
comment8.params=distance
comment8.target=void\ moveVertical(int)
comment8.text=\n\ Move\ the\ square\ vertically\ by\ 'distance'\ pixels.\n
comment9.params=distance
comment9.target=void\ slowMoveHorizontal(int)
comment9.text=\n\ Slowly\ move\ the\ square\ horizontally\ by\ 'distance'\ pixels.\n
numComments=15

View File

@@ -0,0 +1,191 @@
import java.awt.*;
/**
* A square that can be manipulated and that draws itself on a canvas.
*
* @author Michael Kölling and David J. Barnes
* @version 2016.02.29
*/
public class Square
{
private int size;
private int xPosition;
private int yPosition;
private String color;
private boolean isVisible;
/**
* Create a new square at default position with default color.
*/
public Square()
{
size = 60;
xPosition = 310;
yPosition = 120;
color = "red";
isVisible = false;
}
/**
* Make this square visible. If it was already visible, do nothing.
*/
public void makeVisible()
{
isVisible = true;
draw();
}
/**
* Make this square invisible. If it was already invisible, do nothing.
*/
public void makeInvisible()
{
erase();
isVisible = false;
}
/**
* Move the square a few pixels to the right.
*/
public void moveRight()
{
moveHorizontal(20);
}
/**
* Move the square a few pixels to the left.
*/
public void moveLeft()
{
moveHorizontal(-20);
}
/**
* Move the square a few pixels up.
*/
public void moveUp()
{
moveVertical(-20);
}
/**
* Move the square a few pixels down.
*/
public void moveDown()
{
moveVertical(20);
}
/**
* Move the square horizontally by 'distance' pixels.
*/
public void moveHorizontal(int distance)
{
erase();
xPosition += distance;
draw();
}
/**
* Move the square vertically by 'distance' pixels.
*/
public void moveVertical(int distance)
{
erase();
yPosition += distance;
draw();
}
/**
* Slowly move the square horizontally by 'distance' pixels.
*/
public void slowMoveHorizontal(int distance)
{
int delta;
if(distance < 0)
{
delta = -1;
distance = -distance;
}
else
{
delta = 1;
}
for(int i = 0; i < distance; i++)
{
xPosition += delta;
draw();
}
}
/**
* Slowly move the square vertically by 'distance' pixels.
*/
public void slowMoveVertical(int distance)
{
int delta;
if(distance < 0)
{
delta = -1;
distance = -distance;
}
else
{
delta = 1;
}
for(int i = 0; i < distance; i++)
{
yPosition += delta;
draw();
}
}
/**
* Change the size to the new size (in pixels). Size must be >= 0.
*/
public void changeSize(int newSize)
{
erase();
size = newSize;
draw();
}
/**
* Change the color. Valid colors are "red", "yellow", "blue", "green",
* "magenta" and "black".
*/
public void changeColor(String newColor)
{
color = newColor;
draw();
}
/**
* Draw the square with current specifications on screen.
*/
private void draw()
{
if(isVisible) {
Canvas canvas = Canvas.getCanvas();
canvas.draw(this, color,
new Rectangle(xPosition, yPosition, size, size));
canvas.wait(10);
}
}
/**
* Erase the square on screen.
*/
private void erase()
{
if(isVisible) {
Canvas canvas = Canvas.getCanvas();
canvas.erase(this);
}
}
}

View File

@@ -0,0 +1,47 @@
#BlueJ class context
comment0.params=
comment0.target=Triangle()
comment0.text=\n\ Create\ a\ new\ triangle\ at\ default\ position\ with\ default\ color.\n
comment1.params=
comment1.target=void\ makeVisible()
comment1.text=\n\ Make\ this\ triangle\ visible.\ If\ it\ was\ already\ visible,\ do\ nothing.\n
comment10.params=distance
comment10.target=void\ slowMoveVertical(int)
comment10.text=\n\ Slowly\ move\ the\ triangle\ vertically\ by\ 'distance'\ pixels.\n
comment11.params=newHeight\ newWidth
comment11.target=void\ changeSize(int,\ int)
comment11.text=\n\ Change\ the\ size\ to\ the\ new\ size\ (in\ pixels).\ Size\ must\ be\ >\=\ 0.\n
comment12.params=newColor
comment12.target=void\ changeColor(java.lang.String)
comment12.text=\n\ Change\ the\ color.\ Valid\ colors\ are\ "red",\ "yellow",\ "blue",\ "green",\n\ "magenta"\ and\ "black".\n
comment13.params=
comment13.target=void\ draw()
comment13.text=\n\ Draw\ the\ triangle\ with\ current\ specifications\ on\ screen.\n
comment14.params=
comment14.target=void\ erase()
comment14.text=\n\ Erase\ the\ triangle\ on\ screen.\n
comment2.params=
comment2.target=void\ makeInvisible()
comment2.text=\n\ Make\ this\ triangle\ invisible.\ If\ it\ was\ already\ invisible,\ do\ nothing.\n
comment3.params=
comment3.target=void\ moveRight()
comment3.text=\n\ Move\ the\ triangle\ a\ few\ pixels\ to\ the\ right.\n
comment4.params=
comment4.target=void\ moveLeft()
comment4.text=\n\ Move\ the\ triangle\ a\ few\ pixels\ to\ the\ left.\n
comment5.params=
comment5.target=void\ moveUp()
comment5.text=\n\ Move\ the\ triangle\ a\ few\ pixels\ up.\n
comment6.params=
comment6.target=void\ moveDown()
comment6.text=\n\ Move\ the\ triangle\ a\ few\ pixels\ down.\n
comment7.params=distance
comment7.target=void\ moveHorizontal(int)
comment7.text=\n\ Move\ the\ triangle\ horizontally\ by\ 'distance'\ pixels.\n
comment8.params=distance
comment8.target=void\ moveVertical(int)
comment8.text=\n\ Move\ the\ triangle\ vertically\ by\ 'distance'\ pixels.\n
comment9.params=distance
comment9.target=void\ slowMoveHorizontal(int)
comment9.text=\n\ Slowly\ move\ the\ triangle\ horizontally\ by\ 'distance'\ pixels.\n
numComments=15

View File

@@ -0,0 +1,195 @@
import java.awt.*;
/**
* A triangle that can be manipulated and that draws itself on a canvas.
*
* @author Michael Kölling and David J. Barnes
* @version 2016.02.29
*/
public class Triangle
{
private int height;
private int width;
private int xPosition;
private int yPosition;
private String color;
private boolean isVisible;
/**
* Create a new triangle at default position with default color.
*/
public Triangle()
{
height = 60;
width = 70;
xPosition = 210;
yPosition = 140;
color = "green";
isVisible = false;
}
/**
* Make this triangle visible. If it was already visible, do nothing.
*/
public void makeVisible()
{
isVisible = true;
draw();
}
/**
* Make this triangle invisible. If it was already invisible, do nothing.
*/
public void makeInvisible()
{
erase();
isVisible = false;
}
/**
* Move the triangle a few pixels to the right.
*/
public void moveRight()
{
moveHorizontal(20);
}
/**
* Move the triangle a few pixels to the left.
*/
public void moveLeft()
{
moveHorizontal(-20);
}
/**
* Move the triangle a few pixels up.
*/
public void moveUp()
{
moveVertical(-20);
}
/**
* Move the triangle a few pixels down.
*/
public void moveDown()
{
moveVertical(20);
}
/**
* Move the triangle horizontally by 'distance' pixels.
*/
public void moveHorizontal(int distance)
{
erase();
xPosition += distance;
draw();
}
/**
* Move the triangle vertically by 'distance' pixels.
*/
public void moveVertical(int distance)
{
erase();
yPosition += distance;
draw();
}
/**
* Slowly move the triangle horizontally by 'distance' pixels.
*/
public void slowMoveHorizontal(int distance)
{
int delta;
if(distance < 0)
{
delta = -1;
distance = -distance;
}
else
{
delta = 1;
}
for(int i = 0; i < distance; i++)
{
xPosition += delta;
draw();
}
}
/**
* Slowly move the triangle vertically by 'distance' pixels.
*/
public void slowMoveVertical(int distance)
{
int delta;
if(distance < 0)
{
delta = -1;
distance = -distance;
}
else
{
delta = 1;
}
for(int i = 0; i < distance; i++)
{
yPosition += delta;
draw();
}
}
/**
* Change the size to the new size (in pixels). Size must be >= 0.
*/
public void changeSize(int newHeight, int newWidth)
{
erase();
height = newHeight;
width = newWidth;
draw();
}
/**
* Change the color. Valid colors are "red", "yellow", "blue", "green",
* "magenta" and "black".
*/
public void changeColor(String newColor)
{
color = newColor;
draw();
}
/**
* Draw the triangle with current specifications on screen.
*/
private void draw()
{
if(isVisible) {
Canvas canvas = Canvas.getCanvas();
int[] xpoints = { xPosition, xPosition + (width/2), xPosition - (width/2) };
int[] ypoints = { yPosition, yPosition + height, yPosition + height };
canvas.draw(this, color, new Polygon(xpoints, ypoints, 3));
canvas.wait(10);
}
}
/**
* Erase the triangle on screen.
*/
private void erase()
{
if(isVisible) {
Canvas canvas = Canvas.getCanvas();
canvas.erase(this);
}
}
}

View File

@@ -0,0 +1,93 @@
#BlueJ package file
dependency1.from=Triangle
dependency1.to=Canvas
dependency1.type=UsesDependency
dependency2.from=Circle
dependency2.to=Canvas
dependency2.type=UsesDependency
dependency3.from=Square
dependency3.to=Canvas
dependency3.type=UsesDependency
dependency4.from=Person
dependency4.to=Canvas
dependency4.type=UsesDependency
objectbench.height=76
objectbench.width=739
package.editor.height=380
package.editor.width=631
package.editor.x=70
package.editor.y=80
package.numDependencies=4
package.numTargets=5
package.showExtends=true
package.showUses=true
project.charset=UTF-8
readme.editor.height=661
readme.editor.width=861
readme.editor.x=53
readme.editor.y=23
target1.editor.height=811
target1.editor.width=907
target1.editor.x=205
target1.editor.y=46
target1.height=50
target1.name=Circle
target1.naviview.expanded=true
target1.showInterface=false
target1.type=ClassTarget
target1.typeParameters=
target1.width=80
target1.x=170
target1.y=160
target2.editor.height=784
target2.editor.width=934
target2.editor.x=53
target2.editor.y=23
target2.height=50
target2.name=Canvas
target2.naviview.expanded=true
target2.showInterface=false
target2.type=ClassTarget
target2.typeParameters=
target2.width=80
target2.x=50
target2.y=230
target3.editor.height=774
target3.editor.width=982
target3.editor.x=53
target3.editor.y=23
target3.height=50
target3.name=Triangle
target3.naviview.expanded=true
target3.showInterface=false
target3.type=ClassTarget
target3.typeParameters=
target3.width=80
target3.x=370
target3.y=80
target4.editor.height=774
target4.editor.width=986
target4.editor.x=244
target4.editor.y=23
target4.height=50
target4.name=Square
target4.naviview.expanded=true
target4.showInterface=false
target4.type=ClassTarget
target4.typeParameters=
target4.width=80
target4.x=270
target4.y=120
target5.editor.height=781
target5.editor.width=998
target5.editor.x=259
target5.editor.y=54
target5.height=50
target5.name=Person
target5.naviview.expanded=true
target5.showInterface=false
target5.type=ClassTarget
target5.typeParameters=
target5.width=80
target5.x=470
target5.y=40

View File

@@ -0,0 +1,29 @@
#BlueJ class context
comment0.params=
comment0.target=Canvas\ getCanvas()
comment0.text=\n\ Factory\ method\ to\ get\ the\ canvas\ singleton\ object.\n
comment1.params=title\ width\ height\ bgColor
comment1.target=Canvas(java.lang.String,\ int,\ int,\ java.awt.Color)
comment1.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\ bgColor\ the\ desired\ background\ color\ of\ the\ canvas\n
comment2.params=visible
comment2.target=void\ setVisible(boolean)
comment2.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
comment3.params=referenceObject\ color\ shape
comment3.target=void\ draw(java.lang.Object,\ java.lang.String,\ java.awt.Shape)
comment3.text=\n\ Draw\ a\ given\ shape\ onto\ the\ canvas.\n\ @param\ \ referenceObject\ \ an\ object\ to\ define\ identity\ for\ this\ shape\n\ @param\ \ color\ \ \ \ \ \ \ \ \ \ \ \ the\ color\ of\ the\ shape\n\ @param\ \ shape\ \ \ \ \ \ \ \ \ \ \ \ the\ shape\ object\ to\ be\ drawn\ on\ the\ canvas\n
comment4.params=referenceObject
comment4.target=void\ erase(java.lang.Object)
comment4.text=\n\ Erase\ a\ given\ shape's\ from\ the\ screen.\n\ @param\ \ referenceObject\ \ the\ shape\ object\ to\ be\ erased\ \n
comment5.params=colorString
comment5.target=void\ setForegroundColor(java.lang.String)
comment5.text=\n\ Set\ the\ foreground\ color\ of\ the\ Canvas.\n\ @param\ \ newColor\ \ \ the\ new\ color\ for\ the\ foreground\ of\ the\ Canvas\ \n
comment6.params=milliseconds
comment6.target=void\ wait(int)
comment6.text=\n\ Wait\ 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
comment7.params=
comment7.target=void\ redraw()
comment7.text=\n\ Redraw\ ell\ shapes\ currently\ on\ the\ Canvas.\n
comment8.params=
comment8.target=void\ erase()
comment8.text=\n\ Erase\ the\ whole\ canvas.\ (Does\ not\ repaint.)\n
numComments=9

View File

@@ -0,0 +1,230 @@
import javax.swing.*;
import java.awt.*;
import java.util.List;
import java.util.*;
/**
* Canvas is a class to allow for simple graphical drawing on a canvas.
* This is a modification of the general purpose Canvas, specially made for
* the BlueJ "shapes" example.
*
* @author: Bruce Quig
* @author: Michael K<>lling (mik)
*
* @version 2016.02.29
*/
public class Canvas
{
// Note: The implementation of this class (specifically the handling of
// shape identity and colors) is slightly more complex than necessary. This
// is done on purpose to keep the interface and instance fields of the
// shape objects in this project clean and simple for educational purposes.
private static Canvas canvasSingleton;
/**
* Factory method to get the canvas singleton object.
*/
public static Canvas getCanvas()
{
if(canvasSingleton == null) {
canvasSingleton = new Canvas("BlueJ Picture Demo", 500, 300,
Color.white);
}
canvasSingleton.setVisible(true);
return canvasSingleton;
}
// ----- instance part -----
private JFrame frame;
private CanvasPane canvas;
private Graphics2D graphic;
private Color backgroundColor;
private Image canvasImage;
private List<Object> objects;
private HashMap<Object, ShapeDescription> shapes;
/**
* 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 bgColor the desired background color of the canvas
*/
private Canvas(String title, int width, int height, Color bgColor)
{
frame = new JFrame();
canvas = new CanvasPane();
frame.setContentPane(canvas);
frame.setTitle(title);
frame.setLocation(30, 30);
canvas.setPreferredSize(new Dimension(width, height));
backgroundColor = bgColor;
frame.pack();
objects = new ArrayList<Object>();
shapes = new HashMap<Object, ShapeDescription>();
}
/**
* 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(visible);
}
/**
* Draw a given shape onto the canvas.
* @param referenceObject an object to define identity for this shape
* @param color the color of the shape
* @param shape the shape object to be drawn on the canvas
*/
// Note: this is a slightly backwards way of maintaining the shape
// objects. It is carefully designed to keep the visible shape interfaces
// in this project clean and simple for educational purposes.
public void draw(Object referenceObject, String color, Shape shape)
{
objects.remove(referenceObject); // just in case it was already there
objects.add(referenceObject); // add at the end
shapes.put(referenceObject, new ShapeDescription(shape, color));
redraw();
}
/**
* Erase a given shape's from the screen.
* @param referenceObject the shape object to be erased
*/
public void erase(Object referenceObject)
{
objects.remove(referenceObject); // just in case it was already there
shapes.remove(referenceObject);
redraw();
}
/**
* Set the foreground color of the Canvas.
* @param newColor the new color for the foreground of the Canvas
*/
public void setForegroundColor(String colorString)
{
if(colorString.equals("red")) {
graphic.setColor(new Color(235, 25, 25));
}
else if(colorString.equals("black")) {
graphic.setColor(Color.black);
}
else if(colorString.equals("blue")) {
graphic.setColor(new Color(30, 75, 220));
}
else if(colorString.equals("yellow")) {
graphic.setColor(new Color(255, 230, 0));
}
else if(colorString.equals("green")) {
graphic.setColor(new Color(80, 160, 60));
}
else if(colorString.equals("magenta")) {
graphic.setColor(Color.magenta);
}
else if(colorString.equals("white")) {
graphic.setColor(Color.white);
}
else {
graphic.setColor(Color.black);
}
}
/**
* Wait 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 (Exception e)
{
// ignoring exception at the moment
}
}
/**
* Redraw ell shapes currently on the Canvas.
*/
private void redraw()
{
erase();
for(Object shape : objects) {
shapes.get(shape).draw(graphic);
}
canvas.repaint();
}
/**
* Erase the whole canvas. (Does not repaint.)
*/
private 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);
}
/************************************************************************
* 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);
}
}
/************************************************************************
* 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 ShapeDescription
{
private Shape shape;
private String colorString;
public ShapeDescription(Shape shape, String color)
{
this.shape = shape;
colorString = color;
}
public void draw(Graphics2D graphic)
{
setForegroundColor(colorString);
graphic.fill(shape);
}
}
}

View File

@@ -0,0 +1,47 @@
#BlueJ class context
comment0.params=
comment0.target=Circle()
comment0.text=\n\ Create\ a\ new\ circle\ at\ default\ position\ with\ default\ color.\n
comment1.params=
comment1.target=void\ makeVisible()
comment1.text=\n\ Make\ this\ circle\ visible.\ If\ it\ was\ already\ visible,\ do\ nothing.\n
comment10.params=distance
comment10.target=void\ slowMoveVertical(int)
comment10.text=\n\ Slowly\ move\ the\ circle\ vertically\ by\ 'distance'\ pixels.\n
comment11.params=newDiameter
comment11.target=void\ changeSize(int)
comment11.text=\n\ Change\ the\ size\ to\ the\ new\ size\ (in\ pixels).\ Size\ must\ be\ >\=\ 0.\n
comment12.params=newColor
comment12.target=void\ changeColor(java.lang.String)
comment12.text=\n\ Change\ the\ color.\ Valid\ colors\ are\ "red",\ "yellow",\ "blue",\ "green",\n\ "magenta"\ and\ "black".\n
comment13.params=
comment13.target=void\ draw()
comment13.text=\n\ Draw\ the\ circle\ with\ current\ specifications\ on\ screen.\n
comment14.params=
comment14.target=void\ erase()
comment14.text=\n\ Erase\ the\ circle\ on\ screen.\n
comment2.params=
comment2.target=void\ makeInvisible()
comment2.text=\n\ Make\ this\ circle\ invisible.\ If\ it\ was\ already\ invisible,\ do\ nothing.\n
comment3.params=
comment3.target=void\ moveRight()
comment3.text=\n\ Move\ the\ circle\ a\ few\ pixels\ to\ the\ right.\n
comment4.params=
comment4.target=void\ moveLeft()
comment4.text=\n\ Move\ the\ circle\ a\ few\ pixels\ to\ the\ left.\n
comment5.params=
comment5.target=void\ moveUp()
comment5.text=\n\ Move\ the\ circle\ a\ few\ pixels\ up.\n
comment6.params=
comment6.target=void\ moveDown()
comment6.text=\n\ Move\ the\ circle\ a\ few\ pixels\ down.\n
comment7.params=distance
comment7.target=void\ moveHorizontal(int)
comment7.text=\n\ Move\ the\ circle\ horizontally\ by\ 'distance'\ pixels.\n
comment8.params=distance
comment8.target=void\ moveVertical(int)
comment8.text=\n\ Move\ the\ circle\ vertically\ by\ 'distance'\ pixels.\n
comment9.params=distance
comment9.target=void\ slowMoveHorizontal(int)
comment9.text=\n\ Slowly\ move\ the\ circle\ horizontally\ by\ 'distance'\ pixels.\n
numComments=15

View File

@@ -0,0 +1,191 @@
import java.awt.*;
import java.awt.geom.*;
/**
* A circle that can be manipulated and that draws itself on a canvas.
*
* @author Michael K<>lling and David J. Barnes
* @version 2016.02.29
*/
public class Circle
{
private int diameter;
private int xPosition;
private int yPosition;
private String color;
private boolean isVisible;
/**
* Create a new circle at default position with default color.
*/
public Circle()
{
diameter = 68;
xPosition = 230;
yPosition = 90;
color = "blue";
}
/**
* Make this circle visible. If it was already visible, do nothing.
*/
public void makeVisible()
{
isVisible = true;
draw();
}
/**
* Make this circle invisible. If it was already invisible, do nothing.
*/
public void makeInvisible()
{
erase();
isVisible = false;
}
/**
* Move the circle a few pixels to the right.
*/
public void moveRight()
{
moveHorizontal(20);
}
/**
* Move the circle a few pixels to the left.
*/
public void moveLeft()
{
moveHorizontal(-20);
}
/**
* Move the circle a few pixels up.
*/
public void moveUp()
{
moveVertical(-20);
}
/**
* Move the circle a few pixels down.
*/
public void moveDown()
{
moveVertical(20);
}
/**
* Move the circle horizontally by 'distance' pixels.
*/
public void moveHorizontal(int distance)
{
erase();
xPosition += distance;
draw();
}
/**
* Move the circle vertically by 'distance' pixels.
*/
public void moveVertical(int distance)
{
erase();
yPosition += distance;
draw();
}
/**
* Slowly move the circle horizontally by 'distance' pixels.
*/
public void slowMoveHorizontal(int distance)
{
int delta;
if(distance < 0)
{
delta = -1;
distance = -distance;
}
else
{
delta = 1;
}
for(int i = 0; i < distance; i++)
{
xPosition += delta;
draw();
}
}
/**
* Slowly move the circle vertically by 'distance' pixels.
*/
public void slowMoveVertical(int distance)
{
int delta;
if(distance < 0)
{
delta = -1;
distance = -distance;
}
else
{
delta = 1;
}
for(int i = 0; i < distance; i++)
{
yPosition += delta;
draw();
}
}
/**
* Change the size to the new size (in pixels). Size must be >= 0.
*/
public void changeSize(int newDiameter)
{
erase();
diameter = newDiameter;
draw();
}
/**
* Change the color. Valid colors are "red", "yellow", "blue", "green",
* "magenta" and "black".
*/
public void changeColor(String newColor)
{
color = newColor;
draw();
}
/**
* Draw the circle with current specifications on screen.
*/
private void draw()
{
if(isVisible) {
Canvas canvas = Canvas.getCanvas();
canvas.draw(this, color, new Ellipse2D.Double(xPosition, yPosition,
diameter, diameter));
canvas.wait(10);
}
}
/**
* Erase the circle on screen.
*/
private void erase()
{
if(isVisible) {
Canvas canvas = Canvas.getCanvas();
canvas.erase(this);
}
}
}

View File

@@ -0,0 +1,47 @@
#BlueJ class context
comment0.params=
comment0.target=Person()
comment0.text=\n\ Create\ a\ new\ person\ at\ default\ position\ with\ default\ color.\n
comment1.params=
comment1.target=void\ makeVisible()
comment1.text=\n\ Make\ this\ person\ visible.\ If\ it\ was\ already\ visible,\ do\ nothing.\n
comment10.params=distance
comment10.target=void\ slowMoveVertical(int)
comment10.text=\n\ Slowly\ move\ the\ person\ vertically\ by\ 'distance'\ pixels.\n
comment11.params=newHeight\ newWidth
comment11.target=void\ changeSize(int,\ int)
comment11.text=\n\ Change\ the\ size\ to\ the\ new\ size\ (in\ pixels).\ Size\ must\ be\ >\=\ 0.\n
comment12.params=newColor
comment12.target=void\ changeColor(java.lang.String)
comment12.text=\n\ Change\ the\ color.\ Valid\ colors\ are\ "red",\ "yellow",\ "blue",\ "green",\n\ "magenta"\ and\ "black".\n
comment13.params=
comment13.target=void\ draw()
comment13.text=\n\ Draw\ the\ person\ with\ current\ specifications\ on\ screen.\n
comment14.params=
comment14.target=void\ erase()
comment14.text=\n\ Erase\ the\ person\ on\ screen.\n
comment2.params=
comment2.target=void\ makeInvisible()
comment2.text=\n\ Make\ this\ person\ invisible.\ If\ it\ was\ already\ invisible,\ do\ nothing.\n
comment3.params=
comment3.target=void\ moveRight()
comment3.text=\n\ Move\ the\ person\ a\ few\ pixels\ to\ the\ right.\n
comment4.params=
comment4.target=void\ moveLeft()
comment4.text=\n\ Move\ the\ person\ a\ few\ pixels\ to\ the\ left.\n
comment5.params=
comment5.target=void\ moveUp()
comment5.text=\n\ Move\ the\ person\ a\ few\ pixels\ up.\n
comment6.params=
comment6.target=void\ moveDown()
comment6.text=\n\ Move\ the\ person\ a\ few\ pixels\ down.\n
comment7.params=distance
comment7.target=void\ moveHorizontal(int)
comment7.text=\n\ Move\ the\ person\ horizontally\ by\ 'distance'\ pixels.\n
comment8.params=distance
comment8.target=void\ moveVertical(int)
comment8.text=\n\ Move\ the\ person\ vertically\ by\ 'distance'\ pixels.\n
comment9.params=distance
comment9.target=void\ slowMoveHorizontal(int)
comment9.text=\n\ Slowly\ move\ the\ person\ horizontally\ by\ 'distance'\ pixels.\n
numComments=15

View File

@@ -0,0 +1,206 @@
import java.awt.*;
/**
* A person that can be manipulated and that draws itself on a canvas.
*
* @author Michael K<>lling and David J. Barnes
* @version 2016.02.29
*/
public class Person
{
private int height;
private int width;
private int xPosition;
private int yPosition;
private String color;
private boolean isVisible;
/**
* Create a new person at default position with default color.
*/
public Person()
{
height = 60;
width = 30;
xPosition = 280;
yPosition = 190;
color = "black";
isVisible = false;
}
/**
* Make this person visible. If it was already visible, do nothing.
*/
public void makeVisible()
{
isVisible = true;
draw();
}
/**
* Make this person invisible. If it was already invisible, do nothing.
*/
public void makeInvisible()
{
erase();
isVisible = false;
}
/**
* Move the person a few pixels to the right.
*/
public void moveRight()
{
moveHorizontal(20);
}
/**
* Move the person a few pixels to the left.
*/
public void moveLeft()
{
moveHorizontal(-20);
}
/**
* Move the person a few pixels up.
*/
public void moveUp()
{
moveVertical(-20);
}
/**
* Move the person a few pixels down.
*/
public void moveDown()
{
moveVertical(20);
}
/**
* Move the person horizontally by 'distance' pixels.
*/
public void moveHorizontal(int distance)
{
erase();
xPosition += distance;
draw();
}
/**
* Move the person vertically by 'distance' pixels.
*/
public void moveVertical(int distance)
{
erase();
yPosition += distance;
draw();
}
/**
* Slowly move the person horizontally by 'distance' pixels.
*/
public void slowMoveHorizontal(int distance)
{
int delta;
if(distance < 0)
{
delta = -1;
distance = -distance;
}
else
{
delta = 1;
}
for(int i = 0; i < distance; i++)
{
xPosition += delta;
draw();
}
}
/**
* Slowly move the person vertically by 'distance' pixels.
*/
public void slowMoveVertical(int distance)
{
int delta;
if(distance < 0)
{
delta = -1;
distance = -distance;
}
else
{
delta = 1;
}
for(int i = 0; i < distance; i++)
{
yPosition += delta;
draw();
}
}
/**
* Change the size to the new size (in pixels). Size must be >= 0.
*/
public void changeSize(int newHeight, int newWidth)
{
erase();
height = newHeight;
width = newWidth;
draw();
}
/**
* Change the color. Valid colors are "red", "yellow", "blue", "green",
* "magenta" and "black".
*/
public void changeColor(String newColor)
{
color = newColor;
draw();
}
/**
* Draw the person with current specifications on screen.
*/
private void draw()
{
int bh = (int)(height * 0.7); // body height
int hh = (height - bh) / 2; // half head height
int hw = width / 2; // half width
int x = xPosition;
int y = yPosition;
if(isVisible) {
Canvas canvas = Canvas.getCanvas();
int[] xpoints = { x-3, x-hw, x-hw, x-(int)(hw*0.2)-1, x-(int)(hw*0.2)-1, x-hw,
x-hw+(int)(hw*0.4)+1, x, x+hw-(int)(hw*0.4)-1, x+hw, x+(int)(hw*0.2)+1,
x+(int)(hw*0.2)+1, x+hw, x+hw, x+3, x+(int)(hw*0.6),
x+(int)(hw*0.6), x+3, x-3, x-(int)(hw*0.6), x-(int)(hw*0.6) };
int[] ypoints = { y, y+(int)(bh*0.2), y+(int)(bh*0.4), y+(int)(bh*0.2),
y+(int)(bh*0.5), y+bh, y+bh, y+(int)(bh*0.65), y+bh, y+bh,
y+(int)(bh*0.5), y+(int)(bh*0.2), y+(int)(bh*0.4), y+(int)(bh*0.2),
y, y-hh+3, y-hh-3, y-hh-hh, y-hh-hh, y-hh-3, y-hh+3 };
canvas.draw(this, color, new Polygon(xpoints, ypoints, 21));
canvas.wait(10);
}
}
/**
* Erase the person on screen.
*/
private void erase()
{
if(isVisible) {
Canvas canvas = Canvas.getCanvas();
canvas.erase(this);
}
}
}

View File

@@ -0,0 +1,14 @@
#BlueJ class context
comment0.params=
comment0.target=Picture()
comment0.text=\n\ Constructor\ for\ objects\ of\ class\ Picture\n
comment1.params=
comment1.target=void\ draw()
comment1.text=\n\ Draw\ this\ picture.\n
comment2.params=
comment2.target=void\ setBlackAndWhite()
comment2.text=\n\ Change\ this\ picture\ to\ black/white\ display\n
comment3.params=
comment3.target=void\ setColor()
comment3.text=\n\ Change\ this\ picture\ to\ use\ color\ display\n
numComments=4

View File

@@ -0,0 +1,94 @@
/**
* This class represents a simple picture. You can draw the picture using
* the draw method. But wait, there's more: being an electronic picture, it
* can be changed. You can set it to black-and-white display and back to
* colors (only after it's been drawn, of course).
*
* This class was written as an early example for teaching Java with BlueJ.
*
* @author Michael K<>lling and David J. Barnes
* @version 2016.02.29
*/
public class Picture
{
private Square wall;
private Square window;
private Triangle roof;
private Circle sun;
private Circle sun2;
private boolean drawn;
/**
* Constructor for objects of class Picture
*/
public Picture()
{
wall = new Square();
window = new Square();
roof = new Triangle();
sun = new Circle();
sun2 = new Circle();
drawn = false;
}
/**
* Draw this picture.
*/
public void draw()
{
if(!drawn) {
wall.moveHorizontal(-140);
wall.moveVertical(20);
wall.changeSize(120);
wall.makeVisible();
window.changeColor("black");
window.moveHorizontal(-120);
window.moveVertical(40);
window.changeSize(40);
window.makeVisible();
roof.changeSize(60, 180);
roof.moveHorizontal(20);
roof.moveVertical(-60);
roof.makeVisible();
sun.changeColor("blue");
sun.moveHorizontal(100);
sun.moveVertical(-40);
sun.changeSize(80);
sun.makeVisible();
sun2.changeColor("yellow");
sun2.moveHorizontal(30);
sun2.moveVertical(-35);
sun2.changeSize(50);
sun2.makeVisible();
drawn = true;
}
}
/**
* Change this picture to black/white display
*/
public void setBlackAndWhite()
{
wall.changeColor("black");
window.changeColor("white");
roof.changeColor("black");
sun.changeColor("black");
sun2.changeColor("black");
}
/**
* Change this picture to use color display
*/
public void setColor()
{
wall.changeColor("red");
window.changeColor("black");
roof.changeColor("green");
sun.changeColor("yellow");
sun2.changeColor("green");
}
}

View File

@@ -0,0 +1,19 @@
Project "house"
Authors: Michael K<>lling and David J. Barnes
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
It is discussed in chapter 1.
This is a very simple project to demonstrate some characteristics of
objects.
This project is often used after studying the "shapes" project. It
adds a class to those ones in "shapes". That class (named "Picture")
uses the shapes to draw a picture. It can be used to experiment with
modifying source code.

View File

@@ -0,0 +1,47 @@
#BlueJ class context
comment0.params=
comment0.target=Square()
comment0.text=\n\ Create\ a\ new\ square\ at\ default\ position\ with\ default\ color.\n
comment1.params=
comment1.target=void\ makeVisible()
comment1.text=\n\ Make\ this\ square\ visible.\ If\ it\ was\ already\ visible,\ do\ nothing.\n
comment10.params=distance
comment10.target=void\ slowMoveVertical(int)
comment10.text=\n\ Slowly\ move\ the\ square\ vertically\ by\ 'distance'\ pixels.\n
comment11.params=newSize
comment11.target=void\ changeSize(int)
comment11.text=\n\ Change\ the\ size\ to\ the\ new\ size\ (in\ pixels).\ Size\ must\ be\ >\=\ 0.\n
comment12.params=newColor
comment12.target=void\ changeColor(java.lang.String)
comment12.text=\n\ Change\ the\ color.\ Valid\ colors\ are\ "red",\ "yellow",\ "blue",\ "green",\n\ "magenta"\ and\ "black".\n
comment13.params=
comment13.target=void\ draw()
comment13.text=\n\ Draw\ the\ square\ with\ current\ specifications\ on\ screen.\n
comment14.params=
comment14.target=void\ erase()
comment14.text=\n\ Erase\ the\ square\ on\ screen.\n
comment2.params=
comment2.target=void\ makeInvisible()
comment2.text=\n\ Make\ this\ square\ invisible.\ If\ it\ was\ already\ invisible,\ do\ nothing.\n
comment3.params=
comment3.target=void\ moveRight()
comment3.text=\n\ Move\ the\ square\ a\ few\ pixels\ to\ the\ right.\n
comment4.params=
comment4.target=void\ moveLeft()
comment4.text=\n\ Move\ the\ square\ a\ few\ pixels\ to\ the\ left.\n
comment5.params=
comment5.target=void\ moveUp()
comment5.text=\n\ Move\ the\ square\ a\ few\ pixels\ up.\n
comment6.params=
comment6.target=void\ moveDown()
comment6.text=\n\ Move\ the\ square\ a\ few\ pixels\ down.\n
comment7.params=distance
comment7.target=void\ moveHorizontal(int)
comment7.text=\n\ Move\ the\ square\ horizontally\ by\ 'distance'\ pixels.\n
comment8.params=distance
comment8.target=void\ moveVertical(int)
comment8.text=\n\ Move\ the\ square\ vertically\ by\ 'distance'\ pixels.\n
comment9.params=distance
comment9.target=void\ slowMoveHorizontal(int)
comment9.text=\n\ Slowly\ move\ the\ square\ horizontally\ by\ 'distance'\ pixels.\n
numComments=15

View File

@@ -0,0 +1,191 @@
import java.awt.*;
/**
* A square that can be manipulated and that draws itself on a canvas.
*
* @author Michael K<>lling and David J. Barnes
* @version 2016.02.29
*/
public class Square
{
private int size;
private int xPosition;
private int yPosition;
private String color;
private boolean isVisible;
/**
* Create a new square at default position with default color.
*/
public Square()
{
size = 60;
xPosition = 310;
yPosition = 120;
color = "red";
isVisible = false;
}
/**
* Make this square visible. If it was already visible, do nothing.
*/
public void makeVisible()
{
isVisible = true;
draw();
}
/**
* Make this square invisible. If it was already invisible, do nothing.
*/
public void makeInvisible()
{
erase();
isVisible = false;
}
/**
* Move the square a few pixels to the right.
*/
public void moveRight()
{
moveHorizontal(20);
}
/**
* Move the square a few pixels to the left.
*/
public void moveLeft()
{
moveHorizontal(-20);
}
/**
* Move the square a few pixels up.
*/
public void moveUp()
{
moveVertical(-20);
}
/**
* Move the square a few pixels down.
*/
public void moveDown()
{
moveVertical(20);
}
/**
* Move the square horizontally by 'distance' pixels.
*/
public void moveHorizontal(int distance)
{
erase();
xPosition += distance;
draw();
}
/**
* Move the square vertically by 'distance' pixels.
*/
public void moveVertical(int distance)
{
erase();
yPosition += distance;
draw();
}
/**
* Slowly move the square horizontally by 'distance' pixels.
*/
public void slowMoveHorizontal(int distance)
{
int delta;
if(distance < 0)
{
delta = -1;
distance = -distance;
}
else
{
delta = 1;
}
for(int i = 0; i < distance; i++)
{
xPosition += delta;
draw();
}
}
/**
* Slowly move the square vertically by 'distance' pixels.
*/
public void slowMoveVertical(int distance)
{
int delta;
if(distance < 0)
{
delta = -1;
distance = -distance;
}
else
{
delta = 1;
}
for(int i = 0; i < distance; i++)
{
yPosition += delta;
draw();
}
}
/**
* Change the size to the new size (in pixels). Size must be >= 0.
*/
public void changeSize(int newSize)
{
erase();
size = newSize;
draw();
}
/**
* Change the color. Valid colors are "red", "yellow", "blue", "green",
* "magenta" and "black".
*/
public void changeColor(String newColor)
{
color = newColor;
draw();
}
/**
* Draw the square with current specifications on screen.
*/
private void draw()
{
if(isVisible) {
Canvas canvas = Canvas.getCanvas();
canvas.draw(this, color,
new Rectangle(xPosition, yPosition, size, size));
canvas.wait(10);
}
}
/**
* Erase the square on screen.
*/
private void erase()
{
if(isVisible) {
Canvas canvas = Canvas.getCanvas();
canvas.erase(this);
}
}
}

View File

@@ -0,0 +1,47 @@
#BlueJ class context
comment0.params=
comment0.target=Triangle()
comment0.text=\n\ Create\ a\ new\ triangle\ at\ default\ position\ with\ default\ color.\n
comment1.params=
comment1.target=void\ makeVisible()
comment1.text=\n\ Make\ this\ triangle\ visible.\ If\ it\ was\ already\ visible,\ do\ nothing.\n
comment10.params=distance
comment10.target=void\ slowMoveVertical(int)
comment10.text=\n\ Slowly\ move\ the\ triangle\ vertically\ by\ 'distance'\ pixels.\n
comment11.params=newHeight\ newWidth
comment11.target=void\ changeSize(int,\ int)
comment11.text=\n\ Change\ the\ size\ to\ the\ new\ size\ (in\ pixels).\ Size\ must\ be\ >\=\ 0.\n
comment12.params=newColor
comment12.target=void\ changeColor(java.lang.String)
comment12.text=\n\ Change\ the\ color.\ Valid\ colors\ are\ "red",\ "yellow",\ "blue",\ "green",\n\ "magenta"\ and\ "black".\n
comment13.params=
comment13.target=void\ draw()
comment13.text=\n\ Draw\ the\ triangle\ with\ current\ specifications\ on\ screen.\n
comment14.params=
comment14.target=void\ erase()
comment14.text=\n\ Erase\ the\ triangle\ on\ screen.\n
comment2.params=
comment2.target=void\ makeInvisible()
comment2.text=\n\ Make\ this\ triangle\ invisible.\ If\ it\ was\ already\ invisible,\ do\ nothing.\n
comment3.params=
comment3.target=void\ moveRight()
comment3.text=\n\ Move\ the\ triangle\ a\ few\ pixels\ to\ the\ right.\n
comment4.params=
comment4.target=void\ moveLeft()
comment4.text=\n\ Move\ the\ triangle\ a\ few\ pixels\ to\ the\ left.\n
comment5.params=
comment5.target=void\ moveUp()
comment5.text=\n\ Move\ the\ triangle\ a\ few\ pixels\ up.\n
comment6.params=
comment6.target=void\ moveDown()
comment6.text=\n\ Move\ the\ triangle\ a\ few\ pixels\ down.\n
comment7.params=distance
comment7.target=void\ moveHorizontal(int)
comment7.text=\n\ Move\ the\ triangle\ horizontally\ by\ 'distance'\ pixels.\n
comment8.params=distance
comment8.target=void\ moveVertical(int)
comment8.text=\n\ Move\ the\ triangle\ vertically\ by\ 'distance'\ pixels.\n
comment9.params=distance
comment9.target=void\ slowMoveHorizontal(int)
comment9.text=\n\ Slowly\ move\ the\ triangle\ horizontally\ by\ 'distance'\ pixels.\n
numComments=15

View File

@@ -0,0 +1,195 @@
import java.awt.*;
/**
* A triangle that can be manipulated and that draws itself on a canvas.
*
* @author Michael K<>lling and David J. Barnes
* @version 2016.02.29
*/
public class Triangle
{
private int height;
private int width;
private int xPosition;
private int yPosition;
private String color;
private boolean isVisible;
/**
* Create a new triangle at default position with default color.
*/
public Triangle()
{
height = 60;
width = 70;
xPosition = 210;
yPosition = 140;
color = "green";
isVisible = false;
}
/**
* Make this triangle visible. If it was already visible, do nothing.
*/
public void makeVisible()
{
isVisible = true;
draw();
}
/**
* Make this triangle invisible. If it was already invisible, do nothing.
*/
public void makeInvisible()
{
erase();
isVisible = false;
}
/**
* Move the triangle a few pixels to the right.
*/
public void moveRight()
{
moveHorizontal(20);
}
/**
* Move the triangle a few pixels to the left.
*/
public void moveLeft()
{
moveHorizontal(-20);
}
/**
* Move the triangle a few pixels up.
*/
public void moveUp()
{
moveVertical(-20);
}
/**
* Move the triangle a few pixels down.
*/
public void moveDown()
{
moveVertical(20);
}
/**
* Move the triangle horizontally by 'distance' pixels.
*/
public void moveHorizontal(int distance)
{
erase();
xPosition += distance;
draw();
}
/**
* Move the triangle vertically by 'distance' pixels.
*/
public void moveVertical(int distance)
{
erase();
yPosition += distance;
draw();
}
/**
* Slowly move the triangle horizontally by 'distance' pixels.
*/
public void slowMoveHorizontal(int distance)
{
int delta;
if(distance < 0)
{
delta = -1;
distance = -distance;
}
else
{
delta = 1;
}
for(int i = 0; i < distance; i++)
{
xPosition += delta;
draw();
}
}
/**
* Slowly move the triangle vertically by 'distance' pixels.
*/
public void slowMoveVertical(int distance)
{
int delta;
if(distance < 0)
{
delta = -1;
distance = -distance;
}
else
{
delta = 1;
}
for(int i = 0; i < distance; i++)
{
yPosition += delta;
draw();
}
}
/**
* Change the size to the new size (in pixels). Size must be >= 0.
*/
public void changeSize(int newHeight, int newWidth)
{
erase();
height = newHeight;
width = newWidth;
draw();
}
/**
* Change the color. Valid colors are "red", "yellow", "blue", "green",
* "magenta" and "black".
*/
public void changeColor(String newColor)
{
color = newColor;
draw();
}
/**
* Draw the triangle with current specifications on screen.
*/
private void draw()
{
if(isVisible) {
Canvas canvas = Canvas.getCanvas();
int[] xpoints = { xPosition, xPosition + (width/2), xPosition - (width/2) };
int[] ypoints = { yPosition, yPosition + height, yPosition + height };
canvas.draw(this, color, new Polygon(xpoints, ypoints, 3));
canvas.wait(10);
}
}
/**
* Erase the triangle on screen.
*/
private void erase()
{
if(isVisible) {
Canvas canvas = Canvas.getCanvas();
canvas.erase(this);
}
}
}

View File

@@ -0,0 +1,115 @@
#BlueJ package file
dependency1.from=Triangle
dependency1.to=Canvas
dependency1.type=UsesDependency
dependency2.from=Picture
dependency2.to=Square
dependency2.type=UsesDependency
dependency3.from=Picture
dependency3.to=Triangle
dependency3.type=UsesDependency
dependency4.from=Picture
dependency4.to=Circle
dependency4.type=UsesDependency
dependency5.from=Circle
dependency5.to=Canvas
dependency5.type=UsesDependency
dependency6.from=Square
dependency6.to=Canvas
dependency6.type=UsesDependency
dependency7.from=Person
dependency7.to=Canvas
dependency7.type=UsesDependency
objectbench.height=76
objectbench.width=1069
package.editor.height=640
package.editor.width=943
package.editor.x=20
package.editor.y=51
package.numDependencies=7
package.numTargets=6
package.showExtends=true
package.showUses=true
project.charset=x-MacRoman
readme.editor.height=557
readme.editor.width=826
readme.editor.x=53
readme.editor.y=23
target1.editor.height=717
target1.editor.width=900
target1.editor.x=53
target1.editor.y=60
target1.height=50
target1.name=Circle
target1.naviview.expanded=true
target1.showInterface=false
target1.type=ClassTarget
target1.typeParameters=
target1.width=80
target1.x=150
target1.y=230
target2.editor.height=774
target2.editor.width=857
target2.editor.x=47
target2.editor.y=38
target2.height=50
target2.name=Picture
target2.naviview.expanded=true
target2.showInterface=false
target2.type=ClassTarget
target2.typeParameters=
target2.width=80
target2.x=70
target2.y=70
target3.editor.height=781
target3.editor.width=898
target3.editor.x=53
target3.editor.y=51
target3.height=50
target3.name=Canvas
target3.naviview.expanded=true
target3.showInterface=false
target3.type=ClassTarget
target3.typeParameters=
target3.width=80
target3.x=50
target3.y=310
target4.editor.height=727
target4.editor.width=936
target4.editor.x=53
target4.editor.y=60
target4.height=50
target4.name=Triangle
target4.naviview.expanded=true
target4.showInterface=false
target4.type=ClassTarget
target4.typeParameters=
target4.width=80
target4.x=350
target4.y=150
target5.editor.height=722
target5.editor.width=879
target5.editor.x=53
target5.editor.y=60
target5.height=50
target5.name=Square
target5.naviview.expanded=true
target5.showInterface=false
target5.type=ClassTarget
target5.typeParameters=
target5.width=80
target5.x=250
target5.y=190
target6.editor.height=697
target6.editor.width=988
target6.editor.x=53
target6.editor.y=90
target6.height=50
target6.name=Person
target6.naviview.expanded=true
target6.showInterface=false
target6.type=ClassTarget
target6.typeParameters=
target6.width=80
target6.x=450
target6.y=120

View File

@@ -0,0 +1,23 @@
#BlueJ class context
comment0.params=maxNumberOfStudents
comment0.target=LabClass(int)
comment0.text=\n\ Create\ a\ LabClass\ with\ a\ maximum\ number\ of\ enrolments.\ All\ other\ details\n\ are\ set\ to\ default\ values.\n
comment1.params=newStudent
comment1.target=void\ enrollStudent(Student)
comment1.text=\n\ Add\ a\ student\ to\ this\ LabClass.\n
comment2.params=
comment2.target=int\ numberOfStudents()
comment2.text=\n\ Return\ the\ number\ of\ students\ currently\ enrolled\ in\ this\ LabClass.\n
comment3.params=roomNumber
comment3.target=void\ setRoom(java.lang.String)
comment3.text=\n\ Set\ the\ room\ number\ for\ this\ LabClass.\n
comment4.params=timeAndDayString
comment4.target=void\ setTime(java.lang.String)
comment4.text=\n\ Set\ the\ time\ for\ this\ LabClass.\ The\ parameter\ should\ define\ the\ day\n\ and\ the\ time\ of\ day,\ such\ as\ "Friday,\ 10am".\n
comment5.params=instructorName
comment5.target=void\ setInstructor(java.lang.String)
comment5.text=\n\ Set\ the\ name\ of\ the\ instructor\ for\ this\ LabClass.\n
comment6.params=
comment6.target=void\ printList()
comment6.text=\n\ Print\ out\ a\ class\ list\ with\ other\ LabClass\ details\ to\ the\ standard\n\ terminal.\n
numComments=7

View File

@@ -0,0 +1,91 @@
import java.util.*;
/**
* The LabClass class represents an enrolment list for one lab class. It stores
* the time, room and participants of the lab, as well as the instructor's name.
*
* @author Michael Kölling and David Barnes
* @version 2016.02.29
*/
public class LabClass
{
private String instructor;
private String room;
private String timeAndDay;
private ArrayList<Student> students;
private int capacity;
/**
* Create a LabClass with a maximum number of enrolments. All other details
* are set to default values.
*/
public LabClass(int maxNumberOfStudents)
{
instructor = "unknown";
room = "unknown";
timeAndDay = "unknown";
students = new ArrayList<Student>();
capacity = maxNumberOfStudents;
}
/**
* Add a student to this LabClass.
*/
public void enrollStudent(Student newStudent)
{
if(students.size() == capacity) {
System.out.println("The class is full, you cannot enrol.");
}
else {
students.add(newStudent);
}
}
/**
* Return the number of students currently enrolled in this LabClass.
*/
public int numberOfStudents()
{
return students.size();
}
/**
* Set the room number for this LabClass.
*/
public void setRoom(String roomNumber)
{
room = roomNumber;
}
/**
* Set the time for this LabClass. The parameter should define the day
* and the time of day, such as "Friday, 10am".
*/
public void setTime(String timeAndDayString)
{
timeAndDay = timeAndDayString;
}
/**
* Set the name of the instructor for this LabClass.
*/
public void setInstructor(String instructorName)
{
instructor = instructorName;
}
/**
* Print out a class list with other LabClass details to the standard
* terminal.
*/
public void printList()
{
System.out.println("Lab class " + timeAndDay);
System.out.println("Instructor: " + instructor + " Room: " + room);
System.out.println("Class list:");
for(Student student : students) {
student.print();
}
System.out.println("Number of students: " + numberOfStudents());
}
}

View File

@@ -0,0 +1,18 @@
Project: lab-classes
Authors: David J. Barnes and Michael Kölling
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
It is discussed in chapter 1.
The purpose of this project is to illustrate object interaction and to examine
some simple code. See chapters one and three of the book.
To use this project, create some Student objects, then create a LabClass object.
Add the students to the class using the "enrolStudent" method. Print a class
list.

View File

@@ -0,0 +1,26 @@
#BlueJ class context
comment0.params=fullName\ studentID
comment0.target=Student(java.lang.String,\ java.lang.String)
comment0.text=\n\ Create\ a\ new\ student\ with\ a\ given\ name\ and\ ID\ number.\n
comment1.params=
comment1.target=java.lang.String\ getName()
comment1.text=\n\ Return\ the\ full\ name\ of\ this\ student.\n
comment2.params=replacementName
comment2.target=void\ changeName(java.lang.String)
comment2.text=\n\ Set\ a\ new\ name\ for\ this\ student.\n
comment3.params=
comment3.target=java.lang.String\ getStudentID()
comment3.text=\n\ Return\ the\ student\ ID\ of\ this\ student.\n
comment4.params=additionalPoints
comment4.target=void\ addCredits(int)
comment4.text=\n\ Add\ some\ credit\ points\ to\ the\ student's\ accumulated\ credits.\n
comment5.params=
comment5.target=int\ getCredits()
comment5.text=\n\ Return\ the\ number\ of\ credit\ points\ this\ student\ has\ accumulated.\n
comment6.params=
comment6.target=java.lang.String\ getLoginName()
comment6.text=\n\ Return\ the\ login\ name\ of\ this\ student.\ The\ login\ name\ is\ a\ combination\n\ of\ the\ first\ four\ characters\ of\ the\ student's\ name\ and\ the\ first\ three\n\ characters\ of\ the\ student's\ ID\ number.\n
comment7.params=
comment7.target=void\ print()
comment7.text=\n\ Print\ the\ student's\ name\ and\ ID\ number\ to\ the\ output\ terminal.\n
numComments=8

View File

@@ -0,0 +1,85 @@
/**
* The Student class represents a student in a student administration system.
* It holds the student details relevant in our context.
*
* @author Michael Kölling and David Barnes
* @version 2016.02.29
*/
public class Student
{
// the student's full name
private String name;
// the student ID
private String id;
// the amount of credits for study taken so far
private int credits;
/**
* Create a new student with a given name and ID number.
*/
public Student(String fullName, String studentID)
{
name = fullName;
id = studentID;
credits = 0;
}
/**
* Return the full name of this student.
*/
public String getName()
{
return name;
}
/**
* Set a new name for this student.
*/
public void changeName(String replacementName)
{
name = replacementName;
}
/**
* Return the student ID of this student.
*/
public String getStudentID()
{
return id;
}
/**
* Add some credit points to the student's accumulated credits.
*/
public void addCredits(int additionalPoints)
{
credits += additionalPoints;
}
/**
* Return the number of credit points this student has accumulated.
*/
public int getCredits()
{
return credits;
}
/**
* Return the login name of this student. The login name is a combination
* of the first four characters of the student's name and the first three
* characters of the student's ID number.
*/
public String getLoginName()
{
return name.substring(0,4) + id.substring(0,3);
}
/**
* Print the student's name and ID number to the output terminal.
*/
public void print()
{
System.out.println(name + ", student ID: " + id + ", credits: " + credits);
}
}

View File

@@ -0,0 +1,45 @@
#BlueJ package file
dependency1.from=LabClass
dependency1.to=Student
dependency1.type=UsesDependency
objectbench.height=100
objectbench.width=439
package.editor.height=358
package.editor.width=636
package.editor.x=70
package.editor.y=80
package.numDependencies=1
package.numTargets=2
package.showExtends=true
package.showUses=true
project.charset=UTF-8
readme.editor.height=607
readme.editor.width=815
readme.editor.x=53
readme.editor.y=45
target1.editor.height=730
target1.editor.width=826
target1.editor.x=53
target1.editor.y=60
target1.height=60
target1.name=LabClass
target1.naviview.expanded=true
target1.showInterface=false
target1.type=ClassTarget
target1.typeParameters=
target1.width=100
target1.x=140
target1.y=90
target2.editor.height=777
target2.editor.width=882
target2.editor.x=53
target2.editor.y=23
target2.height=60
target2.name=Student
target2.naviview.expanded=true
target2.showInterface=false
target2.type=ClassTarget
target2.typeParameters=
target2.width=100
target2.x=280
target2.y=190