first commit
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
#BlueJ class context
|
||||
comment0.params=
|
||||
comment0.target=BallDemo()
|
||||
comment0.text=\n\ Create\ a\ BallDemo\ object.\ Creates\ a\ fresh\ canvas\ and\ makes\ it\ visible.\n
|
||||
comment1.params=
|
||||
comment1.target=void\ bounce()
|
||||
comment1.text=\n\ Simulate\ two\ bouncing\ balls\n
|
||||
numComments=2
|
||||
@@ -0,0 +1,54 @@
|
||||
import java.awt.Color;
|
||||
|
||||
/**
|
||||
* Class BallDemo - a short demonstration showing animation with the
|
||||
* Canvas class.
|
||||
*
|
||||
* @author Michael Kölling and David J. Barnes
|
||||
* @version 2016.02.29
|
||||
*/
|
||||
|
||||
public class BallDemo
|
||||
{
|
||||
private Canvas myCanvas;
|
||||
|
||||
/**
|
||||
* Create a BallDemo object. Creates a fresh canvas and makes it visible.
|
||||
*/
|
||||
public BallDemo()
|
||||
{
|
||||
myCanvas = new Canvas("Ball Demo", 600, 500);
|
||||
}
|
||||
|
||||
/**
|
||||
* Simulate two bouncing balls
|
||||
*/
|
||||
public void bounce()
|
||||
{
|
||||
int ground = 400; // position of the ground line
|
||||
|
||||
myCanvas.setVisible(true);
|
||||
|
||||
// draw the ground
|
||||
myCanvas.setForegroundColor(Color.BLACK);
|
||||
myCanvas.drawLine(50, ground, 550, ground);
|
||||
|
||||
// create and show the balls
|
||||
BouncingBall ball = new BouncingBall(50, 50, 16, Color.BLUE, ground, myCanvas);
|
||||
ball.draw();
|
||||
BouncingBall ball2 = new BouncingBall(70, 80, 20, Color.RED, ground, myCanvas);
|
||||
ball2.draw();
|
||||
|
||||
// make them bounce
|
||||
boolean finished = false;
|
||||
while (!finished) {
|
||||
myCanvas.wait(50); // small delay
|
||||
ball.move();
|
||||
ball2.move();
|
||||
// stop once ball has travelled a certain distance on x axis
|
||||
if(ball.getXPosition() >= 550 || ball2.getXPosition() >= 550) {
|
||||
finished = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
#BlueJ class context
|
||||
comment0.params=xPos\ yPos\ ballDiameter\ ballColor\ groundPos\ drawingCanvas
|
||||
comment0.target=BouncingBall(int,\ int,\ int,\ java.awt.Color,\ int,\ Canvas)
|
||||
comment0.text=\n\ Constructor\ for\ objects\ of\ class\ BouncingBall\n\n\ @param\ xPos\ \ the\ horizontal\ coordinate\ of\ the\ ball\n\ @param\ yPos\ \ the\ vertical\ coordinate\ of\ the\ ball\n\ @param\ ballDiameter\ \ the\ diameter\ (in\ pixels)\ of\ the\ ball\n\ @param\ ballColor\ \ the\ color\ of\ the\ ball\n\ @param\ groundPos\ \ the\ position\ of\ the\ ground\ (where\ the\ wall\ will\ bounce)\n\ @param\ drawingCanvas\ \ the\ canvas\ to\ draw\ this\ ball\ on\n
|
||||
comment1.params=
|
||||
comment1.target=void\ draw()
|
||||
comment1.text=\n\ Draw\ this\ ball\ at\ its\ current\ position\ onto\ the\ canvas.\n
|
||||
comment2.params=
|
||||
comment2.target=void\ erase()
|
||||
comment2.text=\n\ Erase\ this\ ball\ at\ its\ current\ position.\n
|
||||
comment3.params=
|
||||
comment3.target=void\ move()
|
||||
comment3.text=\n\ Move\ this\ ball\ according\ to\ its\ position\ and\ speed\ and\ redraw.\n
|
||||
comment4.params=
|
||||
comment4.target=int\ getXPosition()
|
||||
comment4.text=\n\ return\ the\ horizontal\ position\ of\ this\ ball\n
|
||||
comment5.params=
|
||||
comment5.target=int\ getYPosition()
|
||||
comment5.text=\n\ return\ the\ vertical\ position\ of\ this\ ball\n
|
||||
numComments=6
|
||||
@@ -0,0 +1,109 @@
|
||||
import java.awt.*;
|
||||
import java.awt.geom.*;
|
||||
|
||||
/**
|
||||
* Class BouncingBall - a graphical ball that observes the effect of gravity. The ball
|
||||
* has the ability to move. Details of movement are determined by the ball itself. It
|
||||
* will fall downwards, accelerating with time due to the effect of gravity, and bounce
|
||||
* upward again when hitting the ground.
|
||||
*
|
||||
* This movement can be initiated by repeated calls to the "move" method.
|
||||
*
|
||||
* @author Michael Kölling (mik)
|
||||
* @author David J. Barnes
|
||||
* @author Bruce Quig
|
||||
*
|
||||
* @version 2016.02.29
|
||||
*/
|
||||
|
||||
public class BouncingBall
|
||||
{
|
||||
private static final int GRAVITY = 3; // effect of gravity
|
||||
|
||||
private int ballDegradation = 2;
|
||||
private Ellipse2D.Double circle;
|
||||
private Color color;
|
||||
private int diameter;
|
||||
private int xPosition;
|
||||
private int yPosition;
|
||||
private final int groundPosition; // y position of ground
|
||||
private Canvas canvas;
|
||||
private int ySpeed = 1; // initial downward speed
|
||||
|
||||
/**
|
||||
* Constructor for objects of class BouncingBall
|
||||
*
|
||||
* @param xPos the horizontal coordinate of the ball
|
||||
* @param yPos the vertical coordinate of the ball
|
||||
* @param ballDiameter the diameter (in pixels) of the ball
|
||||
* @param ballColor the color of the ball
|
||||
* @param groundPos the position of the ground (where the wall will bounce)
|
||||
* @param drawingCanvas the canvas to draw this ball on
|
||||
*/
|
||||
public BouncingBall(int xPos, int yPos, int ballDiameter, Color ballColor,
|
||||
int groundPos, Canvas drawingCanvas)
|
||||
{
|
||||
xPosition = xPos;
|
||||
yPosition = yPos;
|
||||
color = ballColor;
|
||||
diameter = ballDiameter;
|
||||
groundPosition = groundPos;
|
||||
canvas = drawingCanvas;
|
||||
}
|
||||
|
||||
/**
|
||||
* Draw this ball at its current position onto the canvas.
|
||||
**/
|
||||
public void draw()
|
||||
{
|
||||
canvas.setForegroundColor(color);
|
||||
canvas.fillCircle(xPosition, yPosition, diameter);
|
||||
}
|
||||
|
||||
/**
|
||||
* Erase this ball at its current position.
|
||||
**/
|
||||
public void erase()
|
||||
{
|
||||
canvas.eraseCircle(xPosition, yPosition, diameter);
|
||||
}
|
||||
|
||||
/**
|
||||
* Move this ball according to its position and speed and redraw.
|
||||
**/
|
||||
public void move()
|
||||
{
|
||||
// remove from canvas at the current position
|
||||
erase();
|
||||
|
||||
// compute new position
|
||||
ySpeed += GRAVITY;
|
||||
yPosition += ySpeed;
|
||||
xPosition +=2;
|
||||
|
||||
// check if it has hit the ground
|
||||
if (yPosition >= (groundPosition - diameter) && ySpeed > 0) {
|
||||
yPosition = (int)(groundPosition - diameter);
|
||||
ySpeed = -ySpeed + ballDegradation;
|
||||
}
|
||||
|
||||
// draw again at new position
|
||||
draw();
|
||||
}
|
||||
|
||||
/**
|
||||
* return the horizontal position of this ball
|
||||
*/
|
||||
public int getXPosition()
|
||||
{
|
||||
return xPosition;
|
||||
}
|
||||
|
||||
/**
|
||||
* return the vertical position of this ball
|
||||
*/
|
||||
public int getYPosition()
|
||||
{
|
||||
return yPosition;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
#BlueJ class context
|
||||
comment0.params=title
|
||||
comment0.target=Canvas(java.lang.String)
|
||||
comment0.text=\n\ Create\ a\ Canvas\ with\ default\ height,\ width\ and\ background\ color\ \n\ (300,\ 300,\ white).\n\ @param\ title\ \ title\ to\ appear\ in\ Canvas\ Frame\ \ \ \ \ \n
|
||||
comment1.params=title\ width\ height
|
||||
comment1.target=Canvas(java.lang.String,\ int,\ int)
|
||||
comment1.text=\n\ Create\ a\ Canvas\ with\ default\ background\ color\ (white).\n\ @param\ title\ \ title\ to\ appear\ in\ Canvas\ Frame\n\ @param\ width\ \ the\ desired\ width\ for\ the\ canvas\n\ @param\ height\ \ the\ desired\ height\ for\ the\ canvas\n
|
||||
comment10.params=xPos\ yPos\ diameter
|
||||
comment10.target=void\ eraseCircle(int,\ int,\ int)
|
||||
comment10.text=\n\ Erase\ the\ internal\ dimensions\ of\ the\ given\ circle.\ This\ is\ a\ \n\ convenience\ method.\ A\ similar\ effect\ can\ be\ achieved\ with\n\ the\ "erase"\ method.\n
|
||||
comment11.params=xPos\ yPos\ width\ height
|
||||
comment11.target=void\ eraseRectangle(int,\ int,\ int,\ int)
|
||||
comment11.text=\n\ Erase\ the\ internal\ dimensions\ of\ the\ given\ rectangle.\ This\ is\ a\ \n\ convenience\ method.\ A\ similar\ effect\ can\ be\ achieved\ with\n\ the\ "erase"\ method.\n
|
||||
comment12.params=shape
|
||||
comment12.target=void\ erase(java.awt.Shape)
|
||||
comment12.text=\n\ Erase\ a\ given\ shape's\ interior\ on\ the\ screen.\n\ @param\ \ shape\ \ the\ shape\ object\ to\ be\ erased\ \n
|
||||
comment13.params=shape
|
||||
comment13.target=void\ eraseOutline(java.awt.Shape)
|
||||
comment13.text=\n\ Erases\ a\ given\ shape's\ outline\ on\ the\ screen.\n\ @param\ \ shape\ \ the\ shape\ object\ to\ be\ erased\ \n
|
||||
comment14.params=image\ x\ y
|
||||
comment14.target=boolean\ drawImage(java.awt.Image,\ int,\ int)
|
||||
comment14.text=\n\ Draws\ an\ image\ onto\ the\ canvas.\n\ @param\ \ image\ \ \ the\ Image\ object\ to\ be\ displayed\ \n\ @param\ \ x\ \ \ \ \ \ \ x\ co-ordinate\ for\ Image\ placement\ \n\ @param\ \ y\ \ \ \ \ \ \ y\ co-ordinate\ for\ Image\ placement\ \n\ @return\ \ returns\ boolean\ value\ representing\ whether\ the\ image\ was\ \n\ \ \ \ \ \ \ \ \ \ completely\ loaded\ \n
|
||||
comment15.params=text\ x\ y
|
||||
comment15.target=void\ drawString(java.lang.String,\ int,\ int)
|
||||
comment15.text=\n\ Draws\ a\ String\ on\ the\ Canvas.\n\ @param\ \ text\ \ \ the\ String\ to\ be\ displayed\ \n\ @param\ \ x\ \ \ \ \ \ x\ co-ordinate\ for\ text\ placement\ \n\ @param\ \ y\ \ \ \ \ \ y\ co-ordinate\ for\ text\ placement\n
|
||||
comment16.params=text\ x\ y
|
||||
comment16.target=void\ eraseString(java.lang.String,\ int,\ int)
|
||||
comment16.text=\n\ Erases\ a\ String\ on\ the\ Canvas.\n\ @param\ \ text\ \ \ \ \ the\ String\ to\ be\ displayed\ \n\ @param\ \ x\ \ \ \ \ \ \ \ x\ co-ordinate\ for\ text\ placement\ \n\ @param\ \ y\ \ \ \ \ \ \ \ y\ co-ordinate\ for\ text\ placement\n
|
||||
comment17.params=x1\ y1\ x2\ y2
|
||||
comment17.target=void\ drawLine(int,\ int,\ int,\ int)
|
||||
comment17.text=\n\ Draws\ a\ line\ on\ the\ Canvas.\n\ @param\ \ x1\ \ \ x\ co-ordinate\ of\ start\ of\ line\ \n\ @param\ \ y1\ \ \ y\ co-ordinate\ of\ start\ of\ line\ \n\ @param\ \ x2\ \ \ x\ co-ordinate\ of\ end\ of\ line\ \n\ @param\ \ y2\ \ \ y\ co-ordinate\ of\ end\ of\ line\ \n
|
||||
comment18.params=newColor
|
||||
comment18.target=void\ setForegroundColor(java.awt.Color)
|
||||
comment18.text=\n\ Sets\ the\ foreground\ color\ of\ the\ Canvas.\n\ @param\ \ newColor\ \ \ the\ new\ color\ for\ the\ foreground\ of\ the\ Canvas\ \n
|
||||
comment19.params=
|
||||
comment19.target=java.awt.Color\ getForegroundColor()
|
||||
comment19.text=\n\ Returns\ the\ current\ color\ of\ the\ foreground.\n\ @return\ \ \ the\ color\ of\ the\ foreground\ of\ the\ Canvas\ \n
|
||||
comment2.params=title\ width\ height\ bgColor
|
||||
comment2.target=Canvas(java.lang.String,\ int,\ int,\ java.awt.Color)
|
||||
comment2.text=\n\ Create\ a\ Canvas.\n\ @param\ title\ \ title\ to\ appear\ in\ Canvas\ Frame\n\ @param\ width\ \ the\ desired\ width\ for\ the\ canvas\n\ @param\ height\ \ the\ desired\ height\ for\ the\ canvas\n\ @param\ bgClour\ \ the\ desired\ background\ color\ of\ the\ canvas\n
|
||||
comment20.params=newColor
|
||||
comment20.target=void\ setBackgroundColor(java.awt.Color)
|
||||
comment20.text=\n\ Sets\ the\ background\ color\ of\ the\ Canvas.\n\ @param\ \ newColor\ \ \ the\ new\ color\ for\ the\ background\ of\ the\ Canvas\ \n
|
||||
comment21.params=
|
||||
comment21.target=java.awt.Color\ getBackgroundColor()
|
||||
comment21.text=\n\ Returns\ the\ current\ color\ of\ the\ background\n\ @return\ \ \ the\ color\ of\ the\ background\ of\ the\ Canvas\ \n
|
||||
comment22.params=newFont
|
||||
comment22.target=void\ setFont(java.awt.Font)
|
||||
comment22.text=\n\ changes\ the\ current\ Font\ used\ on\ the\ Canvas\n\ @param\ \ newFont\ \ \ new\ font\ to\ be\ used\ for\ String\ output\n
|
||||
comment23.params=
|
||||
comment23.target=java.awt.Font\ getFont()
|
||||
comment23.text=\n\ Returns\ the\ current\ font\ of\ the\ canvas.\n\ @return\ \ \ \ \ the\ font\ currently\ in\ use\n
|
||||
comment24.params=width\ height
|
||||
comment24.target=void\ setSize(int,\ int)
|
||||
comment24.text=\n\ Sets\ the\ size\ of\ the\ canvas.\n\ @param\ \ width\ \ \ \ new\ width\ \n\ @param\ \ height\ \ \ new\ height\ \n
|
||||
comment25.params=
|
||||
comment25.target=java.awt.Dimension\ getSize()
|
||||
comment25.text=\n\ Returns\ the\ size\ of\ the\ canvas.\n\ @return\ \ \ \ \ The\ current\ dimension\ of\ the\ canvas\n
|
||||
comment26.params=milliseconds
|
||||
comment26.target=void\ wait(int)
|
||||
comment26.text=\n\ Waits\ for\ a\ specified\ number\ of\ milliseconds\ before\ finishing.\n\ This\ provides\ an\ easy\ way\ to\ specify\ a\ small\ delay\ which\ can\ be\n\ used\ when\ producing\ animations.\n\ @param\ \ milliseconds\ \ the\ number\ \n
|
||||
comment3.params=visible
|
||||
comment3.target=void\ setVisible(boolean)
|
||||
comment3.text=\n\ Set\ the\ canvas\ visibility\ and\ brings\ canvas\ to\ the\ front\ of\ screen\n\ when\ made\ visible.\ This\ method\ can\ also\ be\ used\ to\ bring\ an\ already\n\ visible\ canvas\ to\ the\ front\ of\ other\ windows.\n\ @param\ visible\ \ boolean\ value\ representing\ the\ desired\ visibility\ of\n\ the\ canvas\ (true\ or\ false)\ \n
|
||||
comment4.params=
|
||||
comment4.target=boolean\ isVisible()
|
||||
comment4.text=\n\ Provide\ information\ on\ visibility\ of\ the\ Canvas.\n\ @return\ \ true\ if\ canvas\ is\ visible,\ false\ otherwise\n
|
||||
comment5.params=shape
|
||||
comment5.target=void\ draw(java.awt.Shape)
|
||||
comment5.text=\n\ Draw\ the\ outline\ of\ a\ given\ shape\ onto\ the\ canvas.\n\ @param\ \ shape\ \ the\ shape\ object\ to\ be\ drawn\ on\ the\ canvas\n
|
||||
comment6.params=shape
|
||||
comment6.target=void\ fill(java.awt.Shape)
|
||||
comment6.text=\n\ Fill\ the\ internal\ dimensions\ of\ a\ given\ shape\ with\ the\ current\ \n\ foreground\ color\ of\ the\ canvas.\n\ @param\ \ shape\ \ the\ shape\ object\ to\ be\ filled\ \n
|
||||
comment7.params=xPos\ yPos\ diameter
|
||||
comment7.target=void\ fillCircle(int,\ int,\ int)
|
||||
comment7.text=\n\ Fill\ the\ internal\ dimensions\ of\ the\ given\ circle\ with\ the\ current\ \n\ foreground\ color\ of\ the\ canvas.\n\ @param\ \ xPos\ \ The\ x-coordinate\ of\ the\ circle\ center\ point\n\ @param\ \ yPos\ \ The\ y-coordinate\ of\ the\ circle\ center\ point\n\ @param\ \ diameter\ \ The\ diameter\ of\ the\ circle\ to\ be\ drawn\n
|
||||
comment8.params=xPos\ yPos\ width\ height
|
||||
comment8.target=void\ fillRectangle(int,\ int,\ int,\ int)
|
||||
comment8.text=\n\ Fill\ the\ internal\ dimensions\ of\ the\ given\ rectangle\ with\ the\ current\ \n\ foreground\ color\ of\ the\ canvas.\ This\ is\ a\ convenience\ method.\ A\ similar\ \n\ effect\ can\ be\ achieved\ with\ the\ "fill"\ method.\n
|
||||
comment9.params=
|
||||
comment9.target=void\ erase()
|
||||
comment9.text=\n\ Erase\ the\ whole\ canvas.\n
|
||||
numComments=27
|
||||
@@ -0,0 +1,364 @@
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.geom.*;
|
||||
|
||||
/**
|
||||
* Class Canvas - a class to allow for simple graphical
|
||||
* drawing on a canvas.
|
||||
*
|
||||
* @author Michael Kölling (mik)
|
||||
* @author Bruce Quig
|
||||
*
|
||||
* @version 2016.02.29
|
||||
*/
|
||||
|
||||
public class Canvas
|
||||
{
|
||||
private JFrame frame;
|
||||
private CanvasPane canvas;
|
||||
private Graphics2D graphic;
|
||||
private Color backgroundColor;
|
||||
private Image canvasImage;
|
||||
|
||||
/**
|
||||
* Create a Canvas with default height, width and background color
|
||||
* (300, 300, white).
|
||||
* @param title title to appear in Canvas Frame
|
||||
*/
|
||||
public Canvas(String title)
|
||||
{
|
||||
this(title, 300, 300, Color.white);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a Canvas with default background color (white).
|
||||
* @param title title to appear in Canvas Frame
|
||||
* @param width the desired width for the canvas
|
||||
* @param height the desired height for the canvas
|
||||
*/
|
||||
public Canvas(String title, int width, int height)
|
||||
{
|
||||
this(title, width, height, Color.white);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a Canvas.
|
||||
* @param title title to appear in Canvas Frame
|
||||
* @param width the desired width for the canvas
|
||||
* @param height the desired height for the canvas
|
||||
* @param bgClour the desired background color of the canvas
|
||||
*/
|
||||
public Canvas(String title, int width, int height, Color bgColor)
|
||||
{
|
||||
frame = new JFrame();
|
||||
canvas = new CanvasPane();
|
||||
frame.setContentPane(canvas);
|
||||
frame.setTitle(title);
|
||||
canvas.setPreferredSize(new Dimension(width, height));
|
||||
backgroundColor = bgColor;
|
||||
frame.pack();
|
||||
setVisible(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the canvas visibility and brings canvas to the front of screen
|
||||
* when made visible. This method can also be used to bring an already
|
||||
* visible canvas to the front of other windows.
|
||||
* @param visible boolean value representing the desired visibility of
|
||||
* the canvas (true or false)
|
||||
*/
|
||||
public void setVisible(boolean visible)
|
||||
{
|
||||
if(graphic == null) {
|
||||
// first time: instantiate the offscreen image and fill it with
|
||||
// the background color
|
||||
Dimension size = canvas.getSize();
|
||||
canvasImage = canvas.createImage(size.width, size.height);
|
||||
graphic = (Graphics2D)canvasImage.getGraphics();
|
||||
graphic.setColor(backgroundColor);
|
||||
graphic.fillRect(0, 0, size.width, size.height);
|
||||
graphic.setColor(Color.black);
|
||||
}
|
||||
frame.setVisible(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Provide information on visibility of the Canvas.
|
||||
* @return true if canvas is visible, false otherwise
|
||||
*/
|
||||
public boolean isVisible()
|
||||
{
|
||||
return frame.isVisible();
|
||||
}
|
||||
|
||||
/**
|
||||
* Draw the outline of a given shape onto the canvas.
|
||||
* @param shape the shape object to be drawn on the canvas
|
||||
*/
|
||||
public void draw(Shape shape)
|
||||
{
|
||||
graphic.draw(shape);
|
||||
canvas.repaint();
|
||||
}
|
||||
|
||||
/**
|
||||
* Fill the internal dimensions of a given shape with the current
|
||||
* foreground color of the canvas.
|
||||
* @param shape the shape object to be filled
|
||||
*/
|
||||
public void fill(Shape shape)
|
||||
{
|
||||
graphic.fill(shape);
|
||||
canvas.repaint();
|
||||
}
|
||||
|
||||
/**
|
||||
* Fill the internal dimensions of the given circle with the current
|
||||
* foreground color of the canvas.
|
||||
* @param xPos The x-coordinate of the circle center point
|
||||
* @param yPos The y-coordinate of the circle center point
|
||||
* @param diameter The diameter of the circle to be drawn
|
||||
*/
|
||||
public void fillCircle(int xPos, int yPos, int diameter)
|
||||
{
|
||||
Ellipse2D.Double circle = new Ellipse2D.Double(xPos, yPos, diameter, diameter);
|
||||
fill(circle);
|
||||
}
|
||||
|
||||
/**
|
||||
* Fill the internal dimensions of the given rectangle with the current
|
||||
* foreground color of the canvas. This is a convenience method. A similar
|
||||
* effect can be achieved with the "fill" method.
|
||||
*/
|
||||
public void fillRectangle(int xPos, int yPos, int width, int height)
|
||||
{
|
||||
fill(new Rectangle(xPos, yPos, width, height));
|
||||
}
|
||||
|
||||
/**
|
||||
* Erase the whole canvas.
|
||||
*/
|
||||
public void erase()
|
||||
{
|
||||
Color original = graphic.getColor();
|
||||
graphic.setColor(backgroundColor);
|
||||
Dimension size = canvas.getSize();
|
||||
graphic.fill(new Rectangle(0, 0, size.width, size.height));
|
||||
graphic.setColor(original);
|
||||
canvas.repaint();
|
||||
}
|
||||
|
||||
/**
|
||||
* Erase the internal dimensions of the given circle. This is a
|
||||
* convenience method. A similar effect can be achieved with
|
||||
* the "erase" method.
|
||||
*/
|
||||
public void eraseCircle(int xPos, int yPos, int diameter)
|
||||
{
|
||||
Ellipse2D.Double circle = new Ellipse2D.Double(xPos, yPos, diameter, diameter);
|
||||
erase(circle);
|
||||
}
|
||||
|
||||
/**
|
||||
* Erase the internal dimensions of the given rectangle. This is a
|
||||
* convenience method. A similar effect can be achieved with
|
||||
* the "erase" method.
|
||||
*/
|
||||
public void eraseRectangle(int xPos, int yPos, int width, int height)
|
||||
{
|
||||
erase(new Rectangle(xPos, yPos, width, height));
|
||||
}
|
||||
|
||||
/**
|
||||
* Erase a given shape's interior on the screen.
|
||||
* @param shape the shape object to be erased
|
||||
*/
|
||||
public void erase(Shape shape)
|
||||
{
|
||||
Color original = graphic.getColor();
|
||||
graphic.setColor(backgroundColor);
|
||||
graphic.fill(shape); // erase by filling background color
|
||||
graphic.setColor(original);
|
||||
canvas.repaint();
|
||||
}
|
||||
|
||||
/**
|
||||
* Erases a given shape's outline on the screen.
|
||||
* @param shape the shape object to be erased
|
||||
*/
|
||||
public void eraseOutline(Shape shape)
|
||||
{
|
||||
Color original = graphic.getColor();
|
||||
graphic.setColor(backgroundColor);
|
||||
graphic.draw(shape); // erase by drawing background color
|
||||
graphic.setColor(original);
|
||||
canvas.repaint();
|
||||
}
|
||||
|
||||
/**
|
||||
* Draws an image onto the canvas.
|
||||
* @param image the Image object to be displayed
|
||||
* @param x x co-ordinate for Image placement
|
||||
* @param y y co-ordinate for Image placement
|
||||
* @return returns boolean value representing whether the image was
|
||||
* completely loaded
|
||||
*/
|
||||
public boolean drawImage(Image image, int x, int y)
|
||||
{
|
||||
boolean result = graphic.drawImage(image, x, y, null);
|
||||
canvas.repaint();
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Draws a String on the Canvas.
|
||||
* @param text the String to be displayed
|
||||
* @param x x co-ordinate for text placement
|
||||
* @param y y co-ordinate for text placement
|
||||
*/
|
||||
public void drawString(String text, int x, int y)
|
||||
{
|
||||
graphic.drawString(text, x, y);
|
||||
canvas.repaint();
|
||||
}
|
||||
|
||||
/**
|
||||
* Erases a String on the Canvas.
|
||||
* @param text the String to be displayed
|
||||
* @param x x co-ordinate for text placement
|
||||
* @param y y co-ordinate for text placement
|
||||
*/
|
||||
public void eraseString(String text, int x, int y)
|
||||
{
|
||||
Color original = graphic.getColor();
|
||||
graphic.setColor(backgroundColor);
|
||||
graphic.drawString(text, x, y);
|
||||
graphic.setColor(original);
|
||||
canvas.repaint();
|
||||
}
|
||||
|
||||
/**
|
||||
* Draws a line on the Canvas.
|
||||
* @param x1 x co-ordinate of start of line
|
||||
* @param y1 y co-ordinate of start of line
|
||||
* @param x2 x co-ordinate of end of line
|
||||
* @param y2 y co-ordinate of end of line
|
||||
*/
|
||||
public void drawLine(int x1, int y1, int x2, int y2)
|
||||
{
|
||||
graphic.drawLine(x1, y1, x2, y2);
|
||||
canvas.repaint();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the foreground color of the Canvas.
|
||||
* @param newColor the new color for the foreground of the Canvas
|
||||
*/
|
||||
public void setForegroundColor(Color newColor)
|
||||
{
|
||||
graphic.setColor(newColor);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the current color of the foreground.
|
||||
* @return the color of the foreground of the Canvas
|
||||
*/
|
||||
public Color getForegroundColor()
|
||||
{
|
||||
return graphic.getColor();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the background color of the Canvas.
|
||||
* @param newColor the new color for the background of the Canvas
|
||||
*/
|
||||
public void setBackgroundColor(Color newColor)
|
||||
{
|
||||
backgroundColor = newColor;
|
||||
graphic.setBackground(newColor);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the current color of the background
|
||||
* @return the color of the background of the Canvas
|
||||
*/
|
||||
public Color getBackgroundColor()
|
||||
{
|
||||
return backgroundColor;
|
||||
}
|
||||
|
||||
/**
|
||||
* changes the current Font used on the Canvas
|
||||
* @param newFont new font to be used for String output
|
||||
*/
|
||||
public void setFont(Font newFont)
|
||||
{
|
||||
graphic.setFont(newFont);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the current font of the canvas.
|
||||
* @return the font currently in use
|
||||
**/
|
||||
public Font getFont()
|
||||
{
|
||||
return graphic.getFont();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the size of the canvas.
|
||||
* @param width new width
|
||||
* @param height new height
|
||||
*/
|
||||
public void setSize(int width, int height)
|
||||
{
|
||||
canvas.setPreferredSize(new Dimension(width, height));
|
||||
Image oldImage = canvasImage;
|
||||
canvasImage = canvas.createImage(width, height);
|
||||
graphic = (Graphics2D)canvasImage.getGraphics();
|
||||
graphic.setColor(backgroundColor);
|
||||
graphic.fillRect(0, 0, width, height);
|
||||
graphic.drawImage(oldImage, 0, 0, null);
|
||||
frame.pack();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the size of the canvas.
|
||||
* @return The current dimension of the canvas
|
||||
*/
|
||||
public Dimension getSize()
|
||||
{
|
||||
return canvas.getSize();
|
||||
}
|
||||
|
||||
/**
|
||||
* Waits for a specified number of milliseconds before finishing.
|
||||
* This provides an easy way to specify a small delay which can be
|
||||
* used when producing animations.
|
||||
* @param milliseconds the number
|
||||
*/
|
||||
public void wait(int milliseconds)
|
||||
{
|
||||
try
|
||||
{
|
||||
Thread.sleep(milliseconds);
|
||||
}
|
||||
catch (InterruptedException e)
|
||||
{
|
||||
// ignoring exception at the moment
|
||||
}
|
||||
}
|
||||
|
||||
/************************************************************************
|
||||
* Inner class CanvasPane - the actual canvas component contained in the
|
||||
* Canvas frame. This is essentially a JPanel with added capability to
|
||||
* refresh the image drawn on it.
|
||||
*/
|
||||
private class CanvasPane extends JPanel
|
||||
{
|
||||
public void paint(Graphics g)
|
||||
{
|
||||
g.drawImage(canvasImage, 0, 0, null);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
Bouncing Balls - a demonstration of graphical output on a Canvas class.
|
||||
|
||||
This project is part of the material for the book
|
||||
|
||||
Objects First with Java - A Practical Introduction using BlueJ
|
||||
Sixth edition
|
||||
David J. Barnes and Michael Kölling
|
||||
Pearson Education, 2016
|
||||
|
||||
This project is discussed in chapter 6.
|
||||
|
||||
This project provides a short graphical demo in the BallDemo class.
|
||||
|
||||
This project provides scope for extension. The Canvas class does not
|
||||
usually need to be changed. It may be best to treat it as a library
|
||||
class (open the editor and switch to "documentation" view).
|
||||
|
||||
Other classes can be changed or added. The book contains suggestions
|
||||
for various exercises.
|
||||
@@ -0,0 +1,64 @@
|
||||
#BlueJ package file
|
||||
dependency1.from=BallDemo
|
||||
dependency1.to=Canvas
|
||||
dependency1.type=UsesDependency
|
||||
dependency2.from=BouncingBall
|
||||
dependency2.to=Canvas
|
||||
dependency2.type=UsesDependency
|
||||
dependency3.from=BallDemo
|
||||
dependency3.to=BouncingBall
|
||||
dependency3.type=UsesDependency
|
||||
objectbench.height=76
|
||||
objectbench.width=724
|
||||
package.editor.height=394
|
||||
package.editor.width=616
|
||||
package.editor.x=70
|
||||
package.editor.y=80
|
||||
package.numDependencies=3
|
||||
package.numTargets=3
|
||||
package.showExtends=true
|
||||
package.showUses=true
|
||||
project.charset=UTF-8
|
||||
readme.editor.height=707
|
||||
readme.editor.width=917
|
||||
readme.editor.x=65
|
||||
readme.editor.y=79
|
||||
target1.editor.height=714
|
||||
target1.editor.width=1075
|
||||
target1.editor.x=53
|
||||
target1.editor.y=42
|
||||
target1.height=60
|
||||
target1.name=BallDemo
|
||||
target1.naviview.expanded=true
|
||||
target1.showInterface=false
|
||||
target1.type=ClassTarget
|
||||
target1.typeParameters=
|
||||
target1.width=110
|
||||
target1.x=110
|
||||
target1.y=50
|
||||
target2.editor.height=776
|
||||
target2.editor.width=931
|
||||
target2.editor.x=78
|
||||
target2.editor.y=23
|
||||
target2.height=60
|
||||
target2.name=BouncingBall
|
||||
target2.naviview.expanded=true
|
||||
target2.showInterface=false
|
||||
target2.type=ClassTarget
|
||||
target2.typeParameters=
|
||||
target2.width=110
|
||||
target2.x=260
|
||||
target2.y=140
|
||||
target3.editor.height=756
|
||||
target3.editor.width=948
|
||||
target3.editor.x=53
|
||||
target3.editor.y=51
|
||||
target3.height=60
|
||||
target3.name=Canvas
|
||||
target3.naviview.expanded=true
|
||||
target3.showInterface=false
|
||||
target3.type=ClassTarget
|
||||
target3.typeParameters=
|
||||
target3.width=100
|
||||
target3.x=400
|
||||
target3.y=230
|
||||
Reference in New Issue
Block a user