我已经发布了几个问题,没有任何回应。这个作业几天后就要交了,我不知道这是怎么回事。我正在制作二十一点游戏,我的目标非常简单:我只想在屏幕上画一些牌,以及一些告诉我分数是多少的标签。问题有三个方面,并且可能具有相同的根本原因:
1) 组件没有正确绘制背景。调用setBackground
什么都不做,并且显式地绘制一个矩形有效,但随后被灰色透支,原因完全让我无法理解。
2)是当我在标签上调用 setText 时,新文本被绘制在旧文本上,而不是替换它。
3)我有一个功能,每次用户按下“Hit”按钮时都会绘制一张新卡,它会给他们一张新卡。当卡片重新计算其位置时,旧卡片会留在屏幕上,因此您会看到同一张卡片的多个副本。
这是绘制所有内容的 JPanel 子类中的代码:
package problem5.view;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import problem5.controller.BlackJackController;
import problem5.model.Card;
import problem5.model.Player;
public class CardPanel extends JPanel {
ArrayList<Card> crdCardsOnPanel;
private final int DEFAULT_PANEL_WIDTH = 600;
private int nCardHeight = 180;
private int nCardWidth = 135;
private int nPanelWidth;
private int nPanelHeight;
private int nDeckOffset;
private int nHeightOffset;
private int nSecondOffset;
private int nCardSpaceWidth;
private int nU;
private int nSecondPanelVerticalOffset;
private JButton btnHit;
private JButton btnStick;
private JLabel lblPlayerScore;
private JLabel lblDealerScore;
private BlackJackController bjc;
private int nDealerCardXCoords[];
private int nPlayerCardXCoords[];
public CardPanel(final BlackJackController bjc) {
this.bjc = bjc;
setLayout(null);
setBackground(Color.green);
//set button behavior
btnHit = new JButton("Hit");
btnHit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
bjc.dealCard(getPlayer());
}
});
btnStick = new JButton("Stick");
lblPlayerScore = new JLabel();
lblDealerScore = new JLabel();
resize(DEFAULT_PANEL_WIDTH);
System.out.printf("Bounds: %d, %d, %d, %d\n",
getBounds().x, getBounds().y, getBounds().width, getBounds().height);
add(btnHit);
add(btnStick);
add(lblDealerScore);
add(lblPlayerScore);
}
public void addCard(Card crdNew) {
crdCardsOnPanel.add(crdNew);
}
public void setController(BlackJackController bjc) {
this.bjc = bjc;
}
private BlackJackController getController(){
return this.bjc;
}
public void resize(int nNewWidth) {
nPanelWidth = nNewWidth;
nU = (int) (nNewWidth/36.0);
nPanelHeight = nU * 10;
nDeckOffset = nU;
nHeightOffset = nU;
nSecondOffset = nU;
nCardSpaceWidth = nU * 27;
nCardHeight = nU * 8;
nCardWidth = nU * 6;
nSecondPanelVerticalOffset = nU * 14;
//set bounds of buttons and labels
btnHit.setBounds(new Rectangle(nDeckOffset,
getPlayerCardViewRectangle().y,
nCardWidth, nCardHeight/2));
btnStick.setBounds(new Rectangle(nDeckOffset,
getPlayerCardViewRectangle().y + nCardHeight/2,
nCardWidth, nCardHeight/2));
lblPlayerScore.setBounds(new Rectangle(1*nU, 11*nU, 16*nU, 2*nU));
lblDealerScore.setBounds(new Rectangle(19*nU, 11*nU, 16*nU, 2*nU));
//calculate positions of dealer's and player's cards
calculateDealerCardXCoords();
calculatePlayerCardXCoords();
setPreferredSize(new Dimension(nPanelWidth, 24*nU));
setMinimumSize(new Dimension(nPanelWidth, 24*nU));
}
private void updateLabels() {
int nPlayerScore = getController().getPlayerScore();
int nDealerScore = getController().getDealerScore();
lblPlayerScore.setText(getPlayer().getName() + "'s Score: " + nPlayerScore);
lblDealerScore.setText("Dealer's Score: "+ nDealerScore);
}
public void resize() {
resize(getBounds().width);
}
public Player getPlayer() {
return getController().getPlayer();
}
public Player getDealer() {
return getController().getDealer();
}
private void calculateDealerCardXCoords() {
int nNumberOfCards = getDealer().getHand().getHandSize();
int xCoords[] = new int[nNumberOfCards];
int nOffset;
if (nNumberOfCards <= 4)
nOffset = 7*nU;
else
nOffset = nCardSpaceWidth/nNumberOfCards;
for (int i=0; i<nNumberOfCards; i++) {
xCoords[i] = i*nOffset + nDeckOffset;
}
nDealerCardXCoords = xCoords;
}
private void calculatePlayerCardXCoords() {
int nNumberOfCards = getPlayer().getHand().getHandSize();
int xCoords[] = new int[nNumberOfCards];
int nOffset;
if (nNumberOfCards <= 4)
nOffset = 7*nU;
else
nOffset = (nCardSpaceWidth-nCardWidth)/nNumberOfCards;
for (int i=0; i<nNumberOfCards; i++) {
xCoords[i] = i*nOffset;
}
nPlayerCardXCoords = xCoords;
}
public Rectangle getDealerCardViewRectangle() {
Rectangle rectReturn = new Rectangle();
Rectangle currentView = getBounds();
rectReturn.x = currentView.x + nDeckOffset;
rectReturn.y = currentView.y + nHeightOffset;
rectReturn.height = nCardHeight;
rectReturn.width = nCardSpaceWidth;
return rectReturn;
}
public Rectangle getPlayerCardViewRectangle() {
Rectangle rectReturn = new Rectangle();
Rectangle currentView = getBounds();
rectReturn.x = currentView.x + nDeckOffset + 8*nU;
rectReturn.y = currentView.y + nHeightOffset + nSecondPanelVerticalOffset;
rectReturn.height = nCardHeight;
rectReturn.width = nCardSpaceWidth;
return rectReturn;
}
public void updateScreen() {
resize();
updateLabels();
}
public void paintComponent(Graphics g)
{
//draw background
g.setColor(new Color(15,125,1));
g.drawRect(0, 0, 36*nU, 24*nU);
//draw dealer's cards
for (int i=0; i<nDealerCardXCoords.length; i++) {
g.drawImage(getDealer().getHand().getCards().get(i).getImage(),
nDealerCardXCoords[i] + getDealerCardViewRectangle().x, getDealerCardViewRectangle().y,
nCardWidth, nCardHeight, null, null);
}
//draw player's cards
for (int i=0; i<nPlayerCardXCoords.length; i++) {
g.drawImage(getPlayer().getHand().getCards().get(i).getImage(),
nPlayerCardXCoords[i] + getPlayerCardViewRectangle().x, getPlayerCardViewRectangle().y,
nCardWidth, nCardHeight, null, null);
}
// lblDealerScore.repaint();
// lblPlayerScore.repaint();
}
}
这是正在发生的事情的截图: http ://s10.postimage.org/3kokr17o9/Screen_Shot_2012_11_24_at_6_31_39_PM.png