62,612
社区成员
发帖
与我相关
我的任务
分享
public class GameInterface extends JFrame implements ActionListener {
JLabel lblBackground;
static JButton play01;
static JButton play02;
static JButton play03;
int index1;
int index2;
int score1;
int score2;
BufferedImage image1;
BufferedImage image2;
BufferedImage[] image;
boolean round1=true,round2=true,round3=true;
public GameInterface() {
// TODO Auto-generated constructor stub
//设置标题
setTitle("游戏主界面");
//位置居中
setLocation(200, 100);
//设置大小
setSize(1200, 500);
//设置退出方式
setDefaultCloseOperation(EXIT_ON_CLOSE);
//加载背景图片
ImageIcon imageIcon = new ImageIcon("src/image/游戏主界面.jpg");
lblBackground = new JLabel();
//设置图片大小
lblBackground.setBounds(0, 0, 1200, 500);
lblBackground.setLayout(null);
this.add(lblBackground);
play01 = new JButton("round 1");
play01.setBounds(500, 100, 150, 50);
lblBackground.add(play01);
play01.addActionListener(this);
play02 = new JButton("round 2");
play02.setBounds(500, 200, 150, 50);
lblBackground.add(play02);
play02.addActionListener(this);
play03 = new JButton("round 3");
play03.setBounds(500, 300, 150, 50);
lblBackground.add(play03);
play03.addActionListener(this);
for(int i=1;i<4;i++) {
image[i] = App.getImg("/image/0"+i+".jpg");
}
image1=image[1];
image2=image[1];
setLayout(null);
setVisible(true);
}
public static void main(String[] args) {
GameInterface g = new GameInterface();
}
public void judge() {
Random r = new Random();
index1 = r.nextInt(3) + 1;
index2 = r.nextInt(3) + 1;
image1 = image[index1];
image2 = image[index2];
repaint();
if (index1 == 1 && index2 == 1) {
JOptionPane.showMessageDialog(null, "此局平局");
}
if (index1 == 1 && index2 == 2) {
score1++;
JOptionPane.showMessageDialog(null, "此局机兄胜出");
}
if (index1 == 1 && index2 == 3) {
score2++;
JOptionPane.showMessageDialog(null, "此局魏兄胜出");
}
if (index1 == 2 && index2 == 1) {
score2++;
JOptionPane.showMessageDialog(null, "此局魏兄胜出");
}
if (index1 == 2 && index2 == 2) {
JOptionPane.showMessageDialog(null, "此局平局");
}
if (index1 == 2 && index2 == 3) {
score1++;
JOptionPane.showMessageDialog(null, "此局机兄胜出");
}
if (index1 == 3 && index2 == 1) {
score1++;
JOptionPane.showMessageDialog(null, "此局机兄胜出");
}
if (index1 == 3 && index2 == 2) {
score2++;
JOptionPane.showMessageDialog(null, "此局魏兄胜出");
}
if (index1 == 3 && index2 == 3) {
JOptionPane.showMessageDialog(null, "此局平局");
}
}
public void finalJudge() {
if (score1 > score2) {
JOptionPane.showMessageDialog(null, "结果为:机兄胜利");
} else if (score1 < score2) {
JOptionPane.showMessageDialog(null, "结果为:魏兄胜利");
} else {
JOptionPane.showMessageDialog(null, "结果为:平局");
}
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (e.getSource() == play01) {
if(round1) {
judge();
round1=false;
}
}
if (e.getSource() == play02) {
if(round2) {
judge();
round2=false;
}
}
if (e.getSource() == play03) {
if(round3) {
judge();
round3=false;
}
}
if(!round1&&!round2&&!round3) {
finalJudge();
round1=round2=round3=true;
score1=score2=0;
}
}
@Override
public void paint(Graphics g) {
// TODO Auto-generated method stub
super.paint(g);
g.setColor(Color.BLACK);
g.setFont(new Font("楷体", Font.BOLD, 30));
g.drawImage(image1, 0, 20, image1.getWidth(), image1.getHeight(), null);
g.drawImage(image2, 1030, 20, image2.getWidth(), image2.getHeight(), null);
g.drawString("机兄分数:" + score1, 250, 300);
g.drawString("魏兄分数:" + score2, 850, 300);
}
}
for(int i=1;i<4;i++) {
image[i] = App.getImg("/image/0"+i+".jpg");
}
image1=image[1];
image2=image[1];
在judge方法中修改成下面语句
image1=image[index1];
image2=image[index2];