Java: Stage 4: draws all the cards which are currently in the cardsOnTable
array
I am currently doing an assignment, and am upto Stage 4. The instructions
given are
In the A1JPanel class complete the: private void drawTableCards(Graphics
g) { ... } helper method. This method draws all the cards which are
currently in the cardsOnTable array. Remember that some of the elements of
the cardsOnTable array are null. Use the howManyInEachRow array which
contains the number of cards which are currently in each row of the
cardsOnTable array.
I think that the problem may be somewhere else, most of this is just a
skeleton file. The problem could either be in the Card class which is used
for Stage 1 or the JPanel which is used for Stage 3/4. Stage 2 is not the
problem.
STAGE 1 - Card Class
import java.awt.*;
import javax.swing.*;
public class Card {
private int suit;
private int value;
private Rectangle cardArea;
private boolean isFaceUp;
private boolean isSelected;
public Card(int value, int suit) {
this.value = value;
this.suit = suit;
cardArea = new Rectangle(0,0,0,0);
isFaceUp = false;
isSelected = false;
}
//-------------------------------------------------------------------
//-------- Accessor and mutator methods -----------------------------
//-------------------------------------------------------------------
public void setIsFaceUp(boolean faceUp) {
isFaceUp = faceUp;
}
public boolean getIsFaceUp() {
return isFaceUp;
}
public boolean getIsSelected() {
return isSelected;
}
public void setIsSelected(boolean selected) {
isSelected = selected;
}
public void setCardArea(int x, int y, int w, int h) {
cardArea = new Rectangle(x, y, w, h);
}
//-------------------------------------------------------------------
//-------- Short methods for getting the centre point --------------
//-------- of the Card object, changing the position of -------------
//-------- the Card object and for comparing two Card objects: ------
//-------- (comparing suit and for comparing value). ----------------
//-------------------------------------------------------------------
public Rectangle getCardArea() {
return cardArea;
}
public void translate(int x, int y) {
cardArea.x = cardArea.x + x;
cardArea.y = cardArea.y + y;
}
public boolean isSameSuit(Card other) {
if(suit == other.suit){
return true;
}else{
return false;
}
}
public boolean hasSmallerValue(Card other) {
if(other.suit == 0){
return false;
}else if(suit == 0){
return true;
}else if(other.suit < suit){
return true;
}else{
return false;
}
}
public Point getCardCentrePt() {
int x1 = cardArea.x + (cardArea.width/2);
int y1 = cardArea.y + (cardArea.height/2);
Point cardCentrePt = new Point(x1, y1);
return cardCentrePt;
}
//-------------------------------------------------------------------
//-------- Returns true if the parameter Point object, --------------
//-------- pressPt, is inside the Card area. ------------------------
//-------------------------------------------------------------------
public boolean isInsideCardArea(Point pressPt) {
if(cardArea.contains(pressPt)){
return true;
}else{
return false;
}
}
//-------------------------------------------------------------------
//-------- Get String containing the card state ---------------------
//-------------------------------------------------------------------
public String getCardStatusInformation() {
String cardStatusInformation = new String(value + " " + suit + " "
+ cardArea.x +
" " + cardArea.y + " " +
isFaceUp + " " +
isSelected);
return cardStatusInformation;
}
STAGE 3/4 - Methods from JPanel
private void addNextColOfCards(ArrayList<Card> cardStack, Card[][]
cardsOnTable, int[] howManyInEachRow) {
for (int i = 0; i < NUMBER_ROWS; i++){
int cardStackSize = cardStack.size();
Card toPut = null;
int randomCard = (int)(Math.random()*cardStackSize - 1);
toPut = cardStack.get(randomCard);
cardStack.remove(randomCard);
cardsOnTable[i][howManyInEachRow[i]]=toPut;
toPut.setIsFaceUp(true);
howManyInEachRow[i] ++;
int row = i;
int col = howManyInEachRow[row];
setupIndividualCardPositionAndSize(toPut, row, col);
}
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
drawTableCards(g);
drawRestOfJPanelDisplay(g);
}
/*
Stage 4 (8 Marks)
*/
private void drawTableCards(Graphics g) {
//super.paintComponent(g);
for(int i = 0; i<cardsOnTable.length; i++){
for(int j = 0; j < howManyInEachRow[i]; j++){
Card draw = null;
cardsOnTable[i] [howManyInEachRow[i]] = draw;
if( draw != null){
draw.drawCard(g, this
);
No comments:
Post a Comment