Correction for Assignment 1 for DESC 391 Print E-mail
Friday, 26 September 2008 12:59

Correction for Assignment 1 for DESC 391

There was a bug in the code originally given for assignment 1. (If you have not downloaded the file before 3:15 pm Friday September 26, then don't worry; the current file has the correct code.) The screen did not refresh when new cards are drawn, leaving the old card shapes in the background. Below is the fix, which consists of adding one method to class Canvas, and calling it from the right place in class Card. In classes Canvas and Card, add the code highlighted in orange in the appropriate places. You do not need to change any other code to apply this fix.

 

In class Canvas

     /**
* Redraw all 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);
}

public void reset()
{
if(graphic != null)
{
erase();
objects.clear();
shapes.clear();
canvas.repaint();
}
}



/************************************************************************
* 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.
*/

 

In class Card

    /**
* Draw default playing cards. Defaults are convenient for testing.
*/
public void drawCards()
{
drawCards( "10", "H" ); // arbitrary values
}

/**
* Draw playing cards. Edit this code to specify the drawing.
* @param rank Rank of the card (A, 2 to 10, J, Q, K)
* @param suit Suit of the card (S for spades, C for clubs, H for hearts, D for diamonds)
*/
public void drawCards( String rank, String suit )
{
// Set the image for the face ranks
if ( rank == "J" )
canvas.setFaceCardImage("images/jack.png");
else if ( rank == "Q" )
canvas.setFaceCardImage("images/queen.png");
else if ( rank == "K" )
canvas.setFaceCardImage("images/king.png");

canvas.setCardRank( rank );
canvas.setCardSuit( suit );

canvas.reset();
canvas.setVisible(true);

 

 

Last Updated on Friday, 26 September 2008 13:55