我发布了一些问题而没有任何回复。这个功课将在几天内完成,我不知道这有什么问题。我正在制作一个二十一点游戏,我的目标非常简单:我只想在屏幕上绘制一些卡片,以及一些告诉我得分是什么的标签。问题是三方面的,可能有相同的根本原因:
1)组件未正确绘制背景。调用setBackground
什么都不做,并且明确地绘制一个矩形是有效的,但随后被灰色透支,原因完全是让我失望。
2)是当我在标签上调用setText时,新文本被绘制在旧文本上,而不是替换它。
3)我有一个功能,每当用户按下“点击”按钮时,它会绘制一张新卡,它会为他们提供一张新卡。当卡片重新计算其位置时,旧的卡片将保留在屏幕上,因此您可以看到同一张卡片的多个副本。
这是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
答案 0 :(得分:2)
好的,问题是我没有将super.paintComponent(g)
行放在paintComponent(Graphics g)
的{{1}}函数中。所以,总是这样做! :)