first commit
This commit is contained in:
BIN
Semester 1/Programming 1/Java/examples/shapes/Canvas$1.class
Normal file
BIN
Semester 1/Programming 1/Java/examples/shapes/Canvas$1.class
Normal file
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
Semester 1/Programming 1/Java/examples/shapes/Canvas.class
Normal file
BIN
Semester 1/Programming 1/Java/examples/shapes/Canvas.class
Normal file
Binary file not shown.
26
Semester 1/Programming 1/Java/examples/shapes/Canvas.ctxt
Normal file
26
Semester 1/Programming 1/Java/examples/shapes/Canvas.ctxt
Normal file
@@ -0,0 +1,26 @@
|
||||
#BlueJ class context
|
||||
comment0.target=Canvas\ getCanvas()
|
||||
comment0.text=\nFactory\ method\ to\ get\ the\ canvas\ singleton\ object.\n\n
|
||||
comment1.params=title\ width\ height\ bgColour
|
||||
comment1.target=Canvas(String,\ int,\ int,\ Color)
|
||||
comment1.text=\nCreate\ 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\ colour\ of\ the\ canvas\n\n
|
||||
comment2.params=visible
|
||||
comment2.target=void\ setVisible(boolean)
|
||||
comment2.text=\nSet\ the\ canvas\ visibility\ and\ brings\ canvas\ to\ the\ front\ of\ screen\nwhen\ made\ visible.\ This\ method\ can\ also\ be\ used\ to\ bring\ an\ already\nvisible\ canvas\ to\ the\ front\ of\ other\ windows.\n@param\ visible\ \ boolean\ value\ representing\ the\ desired\ visibility\ of\nthe\ canvas\ (true\ or\ false)\ \n\n
|
||||
comment3.params=referenceObject\ color\ shape
|
||||
comment3.target=void\ draw(Object,\ String,\ Shape)
|
||||
comment3.text=\nDraw\ 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\n
|
||||
comment4.params=referenceObject
|
||||
comment4.target=void\ erase(Object)
|
||||
comment4.text=\nErase\ a\ given\ shape's\ from\ the\ screen.\n@param\ \ referenceObject\ \ the\ shape\ object\ to\ be\ erased\ \n\n
|
||||
comment5.params=colorString
|
||||
comment5.target=void\ setForegroundColor(String)
|
||||
comment5.text=\nSet\ the\ foreground\ colour\ of\ the\ Canvas.\n@param\ \ newColour\ \ \ the\ new\ colour\ for\ the\ foreground\ of\ the\ Canvas\ \n\n
|
||||
comment6.params=milliseconds
|
||||
comment6.target=void\ wait(int)
|
||||
comment6.text=\nWait\ for\ a\ specified\ number\ of\ milliseconds\ before\ finishing.\nThis\ provides\ an\ easy\ way\ to\ specify\ a\ small\ delay\ which\ can\ be\nused\ when\ producing\ animations.\n@param\ \ milliseconds\ \ the\ number\ \n\n
|
||||
comment7.target=void\ redraw()
|
||||
comment7.text=\nRedraw\ ell\ shapes\ currently\ on\ the\ Canvas.\n\n
|
||||
comment8.target=void\ erase()
|
||||
comment8.text=\nErase\ the\ whole\ canvas.\ (Does\ not\ repaint.)\n\n
|
||||
numComments=9
|
||||
221
Semester 1/Programming 1/Java/examples/shapes/Canvas.java
Normal file
221
Semester 1/Programming 1/Java/examples/shapes/Canvas.java
Normal file
@@ -0,0 +1,221 @@
|
||||
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 Kolling (mik)
|
||||
*
|
||||
* @version: 1.6 (shapes)
|
||||
*/
|
||||
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 Shapes Demo", 300, 300,
|
||||
Color.white);
|
||||
}
|
||||
canvasSingleton.setVisible(true);
|
||||
return canvasSingleton;
|
||||
}
|
||||
|
||||
// ----- instance part -----
|
||||
|
||||
private JFrame frame;
|
||||
private CanvasPane canvas;
|
||||
private Graphics2D graphic;
|
||||
private Color backgroundColour;
|
||||
private Image canvasImage;
|
||||
private List objects;
|
||||
private HashMap 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 bgClour the desired background colour of the canvas
|
||||
*/
|
||||
private Canvas(String title, int width, int height, Color bgColour)
|
||||
{
|
||||
frame = new JFrame();
|
||||
canvas = new CanvasPane();
|
||||
frame.setContentPane(canvas);
|
||||
frame.setTitle(title);
|
||||
canvas.setPreferredSize(new Dimension(width, height));
|
||||
backgroundColour = bgColour;
|
||||
frame.pack();
|
||||
objects = new ArrayList();
|
||||
shapes = new HashMap();
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 colour
|
||||
Dimension size = canvas.getSize();
|
||||
canvasImage = canvas.createImage(size.width, size.height);
|
||||
graphic = (Graphics2D)canvasImage.getGraphics();
|
||||
graphic.setColor(backgroundColour);
|
||||
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 colour of the Canvas.
|
||||
* @param newColour the new colour for the foreground of the Canvas
|
||||
*/
|
||||
public void setForegroundColor(String colorString)
|
||||
{
|
||||
if(colorString.equals("red"))
|
||||
graphic.setColor(Color.red);
|
||||
else if(colorString.equals("black"))
|
||||
graphic.setColor(Color.black);
|
||||
else if(colorString.equals("blue"))
|
||||
graphic.setColor(Color.blue);
|
||||
else if(colorString.equals("yellow"))
|
||||
graphic.setColor(Color.yellow);
|
||||
else if(colorString.equals("green"))
|
||||
graphic.setColor(Color.green);
|
||||
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(Iterator i=objects.iterator(); i.hasNext(); ) {
|
||||
((ShapeDescription)shapes.get(i.next())).draw(graphic);
|
||||
}
|
||||
canvas.repaint();
|
||||
}
|
||||
|
||||
/**
|
||||
* Erase the whole canvas. (Does not repaint.)
|
||||
*/
|
||||
private void erase()
|
||||
{
|
||||
Color original = graphic.getColor();
|
||||
graphic.setColor(backgroundColour);
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
BIN
Semester 1/Programming 1/Java/examples/shapes/Circle.class
Normal file
BIN
Semester 1/Programming 1/Java/examples/shapes/Circle.class
Normal file
Binary file not shown.
44
Semester 1/Programming 1/Java/examples/shapes/Circle.ctxt
Normal file
44
Semester 1/Programming 1/Java/examples/shapes/Circle.ctxt
Normal file
@@ -0,0 +1,44 @@
|
||||
#BlueJ class context
|
||||
comment0.target=Circle()
|
||||
comment0.text=\nCreate\ a\ new\ circle\ at\ default\ position\ with\ default\ color.\n\n
|
||||
comment1.params=x\ y
|
||||
comment1.target=Circle(int,\ int)
|
||||
comment10.params=distance
|
||||
comment10.target=void\ moveHorizontal(int)
|
||||
comment10.text=\nMove\ the\ circle\ horizontally\ by\ 'distance'\ pixels.\n\n
|
||||
comment11.params=distance
|
||||
comment11.target=void\ moveVertical(int)
|
||||
comment11.text=\nMove\ the\ circle\ vertically\ by\ 'distance'\ pixels.\n\n
|
||||
comment12.params=distance
|
||||
comment12.target=void\ slowMoveHorizontal(int)
|
||||
comment12.text=\nSlowly\ move\ the\ circle\ horizontally\ by\ 'distance'\ pixels.\n\n
|
||||
comment13.params=distance
|
||||
comment13.target=void\ slowMoveVertical(int)
|
||||
comment13.text=\nSlowly\ move\ the\ circle\ vertically\ by\ 'distance'\ pixels.\n\n
|
||||
comment14.params=newDiameter
|
||||
comment14.target=void\ changeSize(int)
|
||||
comment14.text=\nChange\ the\ size\ to\ the\ new\ size\ (in\ pixels).\ Size\ must\ be\ >\=\ 0.\n\n
|
||||
comment15.params=newColor
|
||||
comment15.target=void\ changeColor(String)
|
||||
comment15.text=\nChange\ the\ color.\ Valid\ colors\ are\ "red",\ "yellow",\ "blue",\ "green",\n"magenta"\ and\ "black".\n\n
|
||||
comment16.target=void\ draw()
|
||||
comment16.text=Draw\ the\ circle\ with\ current\ specifications\ on\ screen.\n\n
|
||||
comment17.target=void\ erase()
|
||||
comment17.text=Erase\ the\ circle\ on\ screen.\n\n
|
||||
comment2.params=x\ y\ col
|
||||
comment2.target=Circle(int,\ int,\ String)
|
||||
comment3.params=x\ y\ col\ d
|
||||
comment3.target=Circle(int,\ int,\ String,\ int)
|
||||
comment4.target=void\ makeVisible()
|
||||
comment4.text=\nMake\ this\ circle\ visible.\ If\ it\ was\ already\ visible,\ do\ nothing.\n\n
|
||||
comment5.target=void\ makeInvisible()
|
||||
comment5.text=\nMake\ this\ circle\ invisible.\ If\ it\ was\ already\ invisible,\ do\ nothing.\n\n
|
||||
comment6.target=void\ moveRight()
|
||||
comment6.text=\nMove\ the\ circle\ a\ few\ pixels\ to\ the\ right.\n\n
|
||||
comment7.target=void\ moveLeft()
|
||||
comment7.text=\nMove\ the\ circle\ a\ few\ pixels\ to\ the\ left.\n\n
|
||||
comment8.target=void\ moveUp()
|
||||
comment8.text=\nMove\ the\ circle\ a\ few\ pixels\ up.\n\n
|
||||
comment9.target=void\ moveDown()
|
||||
comment9.text=\nMove\ the\ circle\ a\ few\ pixels\ down.\n\n
|
||||
numComments=18
|
||||
211
Semester 1/Programming 1/Java/examples/shapes/Circle.java
Normal file
211
Semester 1/Programming 1/Java/examples/shapes/Circle.java
Normal file
@@ -0,0 +1,211 @@
|
||||
import java.awt.*;
|
||||
import java.awt.geom.*;
|
||||
|
||||
/**
|
||||
* A circle that can be manipulated and that draws itself on a canvas.
|
||||
*
|
||||
* @author Michael Kolling and David J. Barnes
|
||||
* @version 1.0 (15 July 2000)
|
||||
*/
|
||||
|
||||
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 = 30;
|
||||
xPosition = 20;
|
||||
yPosition = 60;
|
||||
color = "blue";
|
||||
isVisible = false;
|
||||
}
|
||||
|
||||
public Circle(int x, int y){
|
||||
diameter = 30;
|
||||
xPosition = x;
|
||||
yPosition = y;
|
||||
color = "blue";
|
||||
}
|
||||
|
||||
public Circle(int x, int y, String col){
|
||||
this(x,y,col,20);
|
||||
}
|
||||
public Circle(int x, int y, String col, int d){
|
||||
diameter = d;
|
||||
xPosition = x;
|
||||
yPosition = y;
|
||||
color = "blue";
|
||||
makeVisible();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
44
Semester 1/Programming 1/Java/examples/shapes/README.TXT
Normal file
44
Semester 1/Programming 1/Java/examples/shapes/README.TXT
Normal file
@@ -0,0 +1,44 @@
|
||||
Project "shapes"
|
||||
Authors: Michael Kolling and David J. Barnes
|
||||
|
||||
This project is part of the material for the book
|
||||
|
||||
Objects First with Java - A Practical Introduction using BlueJ
|
||||
David J. Barnes and Michael Kolling
|
||||
Pearson Education, 2002
|
||||
|
||||
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.
|
||||
|
||||
Michael Kolling, July 2000
|
||||
BIN
Semester 1/Programming 1/Java/examples/shapes/Square.class
Normal file
BIN
Semester 1/Programming 1/Java/examples/shapes/Square.class
Normal file
Binary file not shown.
38
Semester 1/Programming 1/Java/examples/shapes/Square.ctxt
Normal file
38
Semester 1/Programming 1/Java/examples/shapes/Square.ctxt
Normal file
@@ -0,0 +1,38 @@
|
||||
#BlueJ class context
|
||||
comment0.target=Square()
|
||||
comment0.text=\nCreate\ a\ new\ square\ at\ default\ position\ with\ default\ color.\n\n
|
||||
comment1.target=void\ makeVisible()
|
||||
comment1.text=\nMake\ this\ square\ visible.\ If\ it\ was\ already\ visible,\ do\ nothing.\n\n
|
||||
comment10.params=distance
|
||||
comment10.target=void\ slowMoveVertical(int)
|
||||
comment10.text=\nSlowly\ move\ the\ square\ vertically\ by\ 'distance'\ pixels.\n\n
|
||||
comment11.params=newSize
|
||||
comment11.target=void\ changeSize(int)
|
||||
comment11.text=\nChange\ the\ size\ to\ the\ new\ size\ (in\ pixels).\ Size\ must\ be\ >\=\ 0.\n\n
|
||||
comment12.params=newColor
|
||||
comment12.target=void\ changeColor(String)
|
||||
comment12.text=\nChange\ the\ color.\ Valid\ colors\ are\ "red",\ "yellow",\ "blue",\ "green",\n"magenta"\ and\ "black".\n\n
|
||||
comment13.target=void\ draw()
|
||||
comment13.text=Draw\ the\ square\ with\ current\ specifications\ on\ screen.\n\n
|
||||
comment14.target=void\ erase()
|
||||
comment14.text=Erase\ the\ square\ on\ screen.\n\n
|
||||
comment2.target=void\ makeInvisible()
|
||||
comment2.text=\nMake\ this\ square\ invisible.\ If\ it\ was\ already\ invisible,\ do\ nothing.\n\n
|
||||
comment3.target=void\ moveRight()
|
||||
comment3.text=\nMove\ the\ square\ a\ few\ pixels\ to\ the\ right.\n\n
|
||||
comment4.target=void\ moveLeft()
|
||||
comment4.text=\nMove\ the\ square\ a\ few\ pixels\ to\ the\ left.\n\n
|
||||
comment5.target=void\ moveUp()
|
||||
comment5.text=\nMove\ the\ square\ a\ few\ pixels\ up.\n\n
|
||||
comment6.target=void\ moveDown()
|
||||
comment6.text=\nMove\ the\ square\ a\ few\ pixels\ down.\n\n
|
||||
comment7.params=distance
|
||||
comment7.target=void\ moveHorizontal(int)
|
||||
comment7.text=\nMove\ the\ square\ horizontally\ by\ 'distance'\ pixels.\n\n
|
||||
comment8.params=distance
|
||||
comment8.target=void\ moveVertical(int)
|
||||
comment8.text=\nMove\ the\ square\ vertically\ by\ 'distance'\ pixels.\n\n
|
||||
comment9.params=distance
|
||||
comment9.target=void\ slowMoveHorizontal(int)
|
||||
comment9.text=\nSlowly\ move\ the\ square\ horizontally\ by\ 'distance'\ pixels.\n\n
|
||||
numComments=15
|
||||
191
Semester 1/Programming 1/Java/examples/shapes/Square.java
Normal file
191
Semester 1/Programming 1/Java/examples/shapes/Square.java
Normal file
@@ -0,0 +1,191 @@
|
||||
import java.awt.*;
|
||||
|
||||
/**
|
||||
* A square that can be manipulated and that draws itself on a canvas.
|
||||
*
|
||||
* @author Michael Kolling and David J. Barnes
|
||||
* @version 1.0 (15 July 2000)
|
||||
*/
|
||||
|
||||
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 = 30;
|
||||
xPosition = 60;
|
||||
yPosition = 50;
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
BIN
Semester 1/Programming 1/Java/examples/shapes/Triangle.class
Normal file
BIN
Semester 1/Programming 1/Java/examples/shapes/Triangle.class
Normal file
Binary file not shown.
38
Semester 1/Programming 1/Java/examples/shapes/Triangle.ctxt
Normal file
38
Semester 1/Programming 1/Java/examples/shapes/Triangle.ctxt
Normal file
@@ -0,0 +1,38 @@
|
||||
#BlueJ class context
|
||||
comment0.target=Triangle()
|
||||
comment0.text=\nCreate\ a\ new\ triangle\ at\ default\ position\ with\ default\ color.\n\n
|
||||
comment1.target=void\ makeVisible()
|
||||
comment1.text=\nMake\ this\ triangle\ visible.\ If\ it\ was\ already\ visible,\ do\ nothing.\n\n
|
||||
comment10.params=distance
|
||||
comment10.target=void\ slowMoveVertical(int)
|
||||
comment10.text=\nSlowly\ move\ the\ triangle\ vertically\ by\ 'distance'\ pixels.\n\n
|
||||
comment11.params=newHeight\ newWidth
|
||||
comment11.target=void\ changeSize(int,\ int)
|
||||
comment11.text=\nChange\ the\ size\ to\ the\ new\ size\ (in\ pixels).\ Size\ must\ be\ >\=\ 0.\n\n
|
||||
comment12.params=newColor
|
||||
comment12.target=void\ changeColor(String)
|
||||
comment12.text=\nChange\ the\ color.\ Valid\ colors\ are\ "red",\ "yellow",\ "blue",\ "green",\n"magenta"\ and\ "black".\n\n
|
||||
comment13.target=void\ draw()
|
||||
comment13.text=Draw\ the\ triangle\ with\ current\ specifications\ on\ screen.\n\n
|
||||
comment14.target=void\ erase()
|
||||
comment14.text=Erase\ the\ triangle\ on\ screen.\n\n
|
||||
comment2.target=void\ makeInvisible()
|
||||
comment2.text=\nMake\ this\ triangle\ invisible.\ If\ it\ was\ already\ invisible,\ do\ nothing.\n\n
|
||||
comment3.target=void\ moveRight()
|
||||
comment3.text=\nMove\ the\ triangle\ a\ few\ pixels\ to\ the\ right.\n\n
|
||||
comment4.target=void\ moveLeft()
|
||||
comment4.text=\nMove\ the\ triangle\ a\ few\ pixels\ to\ the\ left.\n\n
|
||||
comment5.target=void\ moveUp()
|
||||
comment5.text=\nMove\ the\ triangle\ a\ few\ pixels\ up.\n\n
|
||||
comment6.target=void\ moveDown()
|
||||
comment6.text=\nMove\ the\ triangle\ a\ few\ pixels\ down.\n\n
|
||||
comment7.params=distance
|
||||
comment7.target=void\ moveHorizontal(int)
|
||||
comment7.text=\nMove\ the\ triangle\ horizontally\ by\ 'distance'\ pixels.\n\n
|
||||
comment8.params=distance
|
||||
comment8.target=void\ moveVertical(int)
|
||||
comment8.text=\nMove\ the\ triangle\ vertically\ by\ 'distance'\ pixels.\n\n
|
||||
comment9.params=distance
|
||||
comment9.target=void\ slowMoveHorizontal(int)
|
||||
comment9.text=\nSlowly\ move\ the\ triangle\ horizontally\ by\ 'distance'\ pixels.\n\n
|
||||
numComments=15
|
||||
195
Semester 1/Programming 1/Java/examples/shapes/Triangle.java
Normal file
195
Semester 1/Programming 1/Java/examples/shapes/Triangle.java
Normal file
@@ -0,0 +1,195 @@
|
||||
import java.awt.*;
|
||||
|
||||
/**
|
||||
* A triangle that can be manipulated and that draws itself on a canvas.
|
||||
*
|
||||
* @author Michael Kolling and David J. Barnes
|
||||
* @version 1.0 (15 July 2000)
|
||||
*/
|
||||
|
||||
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 = 30;
|
||||
width = 40;
|
||||
xPosition = 50;
|
||||
yPosition = 15;
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
61
Semester 1/Programming 1/Java/examples/shapes/bluej.pkg
Normal file
61
Semester 1/Programming 1/Java/examples/shapes/bluej.pkg
Normal file
@@ -0,0 +1,61 @@
|
||||
#BlueJ package file
|
||||
dependency1.from=Triangle
|
||||
dependency1.to=Canvas
|
||||
dependency1.type=UsesDependency
|
||||
dependency2.from=Square
|
||||
dependency2.to=Canvas
|
||||
dependency2.type=UsesDependency
|
||||
dependency3.from=Circle
|
||||
dependency3.to=Canvas
|
||||
dependency3.type=UsesDependency
|
||||
objectbench.height=76
|
||||
objectbench.width=940
|
||||
package.editor.height=874
|
||||
package.editor.width=814
|
||||
package.editor.x=0
|
||||
package.editor.y=31
|
||||
package.numDependencies=3
|
||||
package.numTargets=4
|
||||
package.showExtends=true
|
||||
package.showUses=true
|
||||
project.charset=UTF-8
|
||||
target1.editor.height=521
|
||||
target1.editor.width=626
|
||||
target1.editor.x=0
|
||||
target1.editor.y=0
|
||||
target1.height=40
|
||||
target1.name=Circle
|
||||
target1.showInterface=false
|
||||
target1.type=ClassTarget
|
||||
target1.typeParameters=
|
||||
target1.width=70
|
||||
target1.x=100
|
||||
target1.y=120
|
||||
target2.editor.height=521
|
||||
target2.editor.width=626
|
||||
target2.editor.x=0
|
||||
target2.editor.y=0
|
||||
target2.height=40
|
||||
target2.name=Canvas
|
||||
target2.showInterface=false
|
||||
target2.type=ClassTarget
|
||||
target2.typeParameters=
|
||||
target2.width=70
|
||||
target2.x=20
|
||||
target2.y=180
|
||||
target3.height=40
|
||||
target3.name=Triangle
|
||||
target3.showInterface=false
|
||||
target3.type=ClassTarget
|
||||
target3.typeParameters=
|
||||
target3.width=70
|
||||
target3.x=260
|
||||
target3.y=60
|
||||
target4.height=40
|
||||
target4.name=Square
|
||||
target4.showInterface=false
|
||||
target4.type=ClassTarget
|
||||
target4.typeParameters=
|
||||
target4.width=70
|
||||
target4.x=180
|
||||
target4.y=90
|
||||
61
Semester 1/Programming 1/Java/examples/shapes/package.bluej
Normal file
61
Semester 1/Programming 1/Java/examples/shapes/package.bluej
Normal file
@@ -0,0 +1,61 @@
|
||||
#BlueJ package file
|
||||
dependency1.from=Triangle
|
||||
dependency1.to=Canvas
|
||||
dependency1.type=UsesDependency
|
||||
dependency2.from=Square
|
||||
dependency2.to=Canvas
|
||||
dependency2.type=UsesDependency
|
||||
dependency3.from=Circle
|
||||
dependency3.to=Canvas
|
||||
dependency3.type=UsesDependency
|
||||
objectbench.height=76
|
||||
objectbench.width=940
|
||||
package.editor.height=874
|
||||
package.editor.width=814
|
||||
package.editor.x=0
|
||||
package.editor.y=31
|
||||
package.numDependencies=3
|
||||
package.numTargets=4
|
||||
package.showExtends=true
|
||||
package.showUses=true
|
||||
project.charset=UTF-8
|
||||
target1.editor.height=521
|
||||
target1.editor.width=626
|
||||
target1.editor.x=0
|
||||
target1.editor.y=0
|
||||
target1.height=40
|
||||
target1.name=Circle
|
||||
target1.showInterface=false
|
||||
target1.type=ClassTarget
|
||||
target1.typeParameters=
|
||||
target1.width=70
|
||||
target1.x=100
|
||||
target1.y=120
|
||||
target2.editor.height=521
|
||||
target2.editor.width=626
|
||||
target2.editor.x=0
|
||||
target2.editor.y=0
|
||||
target2.height=40
|
||||
target2.name=Canvas
|
||||
target2.showInterface=false
|
||||
target2.type=ClassTarget
|
||||
target2.typeParameters=
|
||||
target2.width=70
|
||||
target2.x=20
|
||||
target2.y=180
|
||||
target3.height=40
|
||||
target3.name=Triangle
|
||||
target3.showInterface=false
|
||||
target3.type=ClassTarget
|
||||
target3.typeParameters=
|
||||
target3.width=70
|
||||
target3.x=260
|
||||
target3.y=60
|
||||
target4.height=40
|
||||
target4.name=Square
|
||||
target4.showInterface=false
|
||||
target4.type=ClassTarget
|
||||
target4.typeParameters=
|
||||
target4.width=70
|
||||
target4.x=180
|
||||
target4.y=90
|
||||
Reference in New Issue
Block a user