ブラックジャックゲームを作っています。すべてが完了し、ゲームを最初からやり直すためのリセット ボタンを作成したいと考えました。ActionListener で all() を削除しましたが、Board() が再び開始されたときに、JButton の描画または折りたたみが追加されません。PS を助けてください Cards() コンストラクターはすべて私のカード画像ファイルです。
import javax.swing.*;
import java.io.*;
import java.util.ArrayList;
import javax.imageio.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
public class Board extends JPanel implements ActionListener
{
Cards deck=new Cards();
static ArrayList<BufferedImage> pcards=new ArrayList<BufferedImage>();
static ArrayList<BufferedImage> ccards=new ArrayList<BufferedImage>();
static BufferedImage back;
static int w=1280/2;
static int h=760/2;
static ArrayList<String> p=new ArrayList<String>();
static ArrayList<String> com=new ArrayList<String>();
static Boolean last=true;
static String end="";
public Board()
{
setLayout(null);
Color green=new Color(00,55,00);
setBackground(green);
try
{
String c="Deck\\"+deck.draw();
p.add(c);
pcards.add(ImageIO.read(this.getClass().getResource(c)));
c="Deck\\"+deck.draw();
p.add(c);
pcards.add(ImageIO.read(this.getClass().getResource(c)));
c="Deck\\"+deck.draw();
com.add(c);
ccards.add(ImageIO.read(this.getClass().getResource(c)));
c="Deck\\"+deck.draw();
com.add(c);
ccards.add(ImageIO.read(this.getClass().getResource(c)));
back=(ImageIO.read(this.getClass().getResource("Deck\\b1fv.GIF")));
}
catch (IOException e)
{
//Not handled.
}
int cp=Integer.parseInt(Score(com));
while(cp<17)
{
try
{
String c="Deck\\"+deck.draw();
com.add(c);
ccards.add(ImageIO.read(this.getClass().getResource(c)));
}
catch (IOException e)
{
//Not handled.
}
cp=Integer.parseInt(Score(com));
}
JButton draw=new JButton("Draw");
draw.setBounds(w-120,h-30,90,40);
draw.setFont(new Font("Arabic", Font.PLAIN, 20));
draw.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent a)
{
int pp=Integer.parseInt(Score(p));
if(pp>21||end.length()>0)
{
last=false;
repaint();
}
else
{
try
{
String c="Deck\\"+deck.draw();
p.add(c);
pcards.add(ImageIO.read(this.getClass().getResource(c)));
repaint();
}
catch (IOException e)
{
//Not handled.
}
}}
});
add(draw);
JButton fold=new JButton("Fold");
fold.setBounds(w+40,h-30,90,40);
fold.setFont(new Font("Arabic", Font.PLAIN, 20));
fold.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent a)
{
int pp=Integer.parseInt(Score(p));
int cp=Integer.parseInt(Score(com));
end=End(pp,cp);
last=false;
repaint();
}
});
add(fold);
}
public void paint(Graphics g)
{
super.paint(g);
g.setFont(new Font("Arabic", Font.PLAIN, 50));
g.setColor(Color.black);
int pp=Integer.parseInt(Score(p));
int cp=Integer.parseInt(Score(com));
if(pp>21)
{
end=End(pp,cp);
last=false;
}
int i=0;
for(BufferedImage temp:pcards)
{
g.drawImage(temp, (w-120)+i, (h+(h/2))-80,100,170, null);
i+=60;
}
i=0;
if(last)
{
for(BufferedImage temp:ccards)
{
g.drawImage(back, (w-120)+i, (h-(h/2))-100,100,170, null);
i+=60;
}
g.drawString("Score:",(w-(w/2))-90,h-40);
g.drawString("???", (w-(w/2))-70, h+40);
}
else
{
for(BufferedImage temp:ccards)
{
g.drawImage(temp, (w-120)+i, (h-(h/2))-100,100,170, null);
i+=60;
}
g.drawString("Score:",w-(w/2)-90,h-40);
g.drawString(Score(com), (w-(w/2))-50, h+40);
}
g.drawString("Score:",w+(w/2)-50,h-40);
g.drawString(Score(p), (w+(w/2)), h+40);
int j=end.length();
g.setFont(new Font("Arabic", Font.PLAIN, 70));
Color l=new Color(66,153,204);
g.setColor(l);
g.drawString(end,(w-(j*20))+20,h-(h/2));
if(end.length()>0)
{
g.setFont(new Font("Arabic", Font.PLAIN, 30));
g.setColor(Color.black);
g.drawString("Play Again?",(w-(w/2))-160,h+(h/2));
JButton reset=new JButton("Yes");
reset.setBounds((w-(w/2))-120,(h+(h/2))+30,90,40);
reset.setFont(new Font("Arabic", Font.PLAIN, 20));
reset.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
last=true;
end="";
pcards.clear();ccards.clear();p.clear();com.clear();
repaint();
removeAll();
new Board();
}
});
add(reset);
}
}
public void actionPerformed(ActionEvent e)
{
}
public static String End(int x,int y)
{
if(x>21 && y<=21)
end="You Busted! Computer Won!";
else if(x<=21 && y>21)
end="You Won! Computer Busted!";
else if(x>21 && y>21)
end="Both Busted! Draw!";
else if(x>y)
end="You Win!";
else if(x<y)
end="You Lost!";
else
end="You and Computer Tied! Draw!";
return end;
}
public static String Score(ArrayList<String> x)
{
int sum=0;
for(int i=0; i<x.size(); i++)
{
String z=x.get(i);
if(z.charAt(6)=='2')
sum+=2;
if(z.charAt(6)=='3')
sum+=3;
if(z.charAt(6)=='4')
sum+=4;
if(z.charAt(6)=='5')
sum+=5;
if(z.charAt(6)=='6')
sum+=6;
if(z.charAt(6)=='7')
sum+=7;
if(z.charAt(6)=='8')
sum+=8;
if(z.charAt(6)=='9')
sum+=9;
if(z.charAt(6)=='1')
sum+=10;
if(z.charAt(6)=='j')
sum+=10;
if(z.charAt(6)=='q')
sum+=10;
if(z.charAt(6)=='k')
sum+=10;
if(z.charAt(6)=='a')
{
if(sum<=10)
sum+=11;
else
sum+=1;
}
}
String score=""+sum;
return score;
}
}
public class BlackJack extends JFrame
{
public BlackJack()
{
add(new Board());
System.out.println("Starting BlackJack...");
setTitle("Blackjack");
setSize(getToolkit().getScreenSize().width, getToolkit().getScreenSize().height-40);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setResizable(false);
setVisible(true);
}
public static void main(String args[])
{
new BlackJack();
}
}