package ui;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.util.Random;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
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;
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(imageIcon);
//設定圖片大小
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);
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=App.getImg("/image/0"+index1+".jpg");
image2=App.getImg("/image/0"+index2+".jpg");
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) {
judge();
}
if(e.getSource()==play02) {
judge();
}
if(e.getSource()==play03) {
judge();
finalJudge();
}
}
@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);
}
}
能夠運行出來,但是一直有很多警示
uj5u.com熱心網友回復:
image1和image2 沒有初始化先設定一個BufferImage陣列,變數名為image
先把3張圖片在類構造器全部加入 image這個陣列
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];
uj5u.com熱心網友回復:
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);
}
}
uj5u.com熱心網友回復:
哇??,好感動啊,謝謝您
uj5u.com熱心網友回復:
這么感動,得好好請人家吃頓飯,看場電影uj5u.com熱心網友回復:
大神,我想再請教您一下,麻煩看一下私信,我只能發三條,你幫了我這么多,我能做些什么不
uj5u.com熱心網友回復:
啊,哈哈哈,大概率都不在一個城市,哈哈哈
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/50848.html
標籤:Java SE
