Im building a blackjack game in java for school and I can't seem to figure out how to add the first four cards to the GPanel. The array of cards are shuffled and the strings in the array match the file name of the images. I can get the first card in the array to load but not the other three. Any help would be awesome.
我在java中为学校构建了一个blackjack游戏,我似乎不知道如何在GPanel中添加前四张卡。纸牌的数组被打乱,数组中的字符串与图像的文件名匹配。我可以在数组中得到第一张卡,而不是其他三个。任何帮助都是了不起的。
public void paintComponent(Graphics g) {
//System.out.println(gameInProgress);
if(gameInProgress == true) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
Image image = null;
int i;
currentPosition = 0;
for (i = 0; i < 4; i++) {
image = Toolkit.getDefaultToolkit().getImage("images/"+deck[currentPosition - i]+".png");
currentPosition++;
}
g2.drawImage(image, 115, 5, 80, (int) 106.67, this);
g2.finalize();
}
}
1 个解决方案
#1
0
You have 3 main issues in your code.
您的代码中有3个主要问题。
The first issue is caused by [currentPosition - i]
because the result will always equal 0, so the image/card at array index 0 will be the only one that will ever be drawn.
第一个问题是由[currentPosition - i]引起的,因为结果总是等于0,所以数组索引0中的图像/card将是惟一会被绘制出来的。
The second issue is that you only draw one image because g2.drawImage is only called after the for loop, instead it should be inside the for loop so that all 4 images/cards are drawn.
第二个问题是,你只画一个图像,因为g2。drawImage只在for循环之后调用,相反,它应该在for循环中,这样所有的4个图像/卡片就会被绘制出来。
The third issue is that you always draw your images in the same place, so even if you where drawing all 4 images you would only see the last one because it covers the previous ones.
第三个问题是,你总是在同一个地方画你的图像,所以即使你把所有4张图片都画在了上面,你只会看到最后一个,因为它覆盖了之前的图片。
Try this, it should print all 4 images (Assuming you have a 435x112 panel):
试试这个,它应该打印所有4张图片(假设你有一个435x112面板):
public void paintComponent(Graphics g) {
//System.out.println(gameInProgress);
if(gameInProgress == true) {
super.paintComponent(g);
//Add a couple new variables
int x = 115;
Graphics2D g2 = (Graphics2D) g;
Image image = null;
int i;
for (i = 0; i < 4; i++) {
image = Toolkit.getDefaultToolkit().getImage("images/"+deck[i]+".png");
//New location of g2.drawImage
//The X positions should change each time
g2.drawImage(image, x, 5, 80, 107, this);
//Change the X location of the next image
x = x + 80;
}
//Moved g2.drawImage from here to above the bracket
g2.finalize();
}
}
#1
0
You have 3 main issues in your code.
您的代码中有3个主要问题。
The first issue is caused by [currentPosition - i]
because the result will always equal 0, so the image/card at array index 0 will be the only one that will ever be drawn.
第一个问题是由[currentPosition - i]引起的,因为结果总是等于0,所以数组索引0中的图像/card将是惟一会被绘制出来的。
The second issue is that you only draw one image because g2.drawImage is only called after the for loop, instead it should be inside the for loop so that all 4 images/cards are drawn.
第二个问题是,你只画一个图像,因为g2。drawImage只在for循环之后调用,相反,它应该在for循环中,这样所有的4个图像/卡片就会被绘制出来。
The third issue is that you always draw your images in the same place, so even if you where drawing all 4 images you would only see the last one because it covers the previous ones.
第三个问题是,你总是在同一个地方画你的图像,所以即使你把所有4张图片都画在了上面,你只会看到最后一个,因为它覆盖了之前的图片。
Try this, it should print all 4 images (Assuming you have a 435x112 panel):
试试这个,它应该打印所有4张图片(假设你有一个435x112面板):
public void paintComponent(Graphics g) {
//System.out.println(gameInProgress);
if(gameInProgress == true) {
super.paintComponent(g);
//Add a couple new variables
int x = 115;
Graphics2D g2 = (Graphics2D) g;
Image image = null;
int i;
for (i = 0; i < 4; i++) {
image = Toolkit.getDefaultToolkit().getImage("images/"+deck[i]+".png");
//New location of g2.drawImage
//The X positions should change each time
g2.drawImage(image, x, 5, 80, 107, this);
//Change the X location of the next image
x = x + 80;
}
//Moved g2.drawImage from here to above the bracket
g2.finalize();
}
}