E001 // 이벤트 대기중
E002 // [나]는 가위를 선택했고 [너]는 임의로 보를 선택 예정
E003 // 잠시후 타이머 활성화
E004 // [나]는 바위를 선택했고 [너]는 임의로 가위를 선택 예정
import javax.swing.*;
import rpsGame.RockPaperScissorsPanel;
public class RockPaperScissorsGUIMain
{
1 public static void main( String[] args ) {
final String imagePath = "C:\\Users\\user\\Downloads\\JAVA-main\\src\\rpsGame\\image\\";
JFrame frame = new JFrame( "가위바위보" );
2 frame.getContentPane().add( new RockPaperScissorsPanel( imagePath ) );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.pack();
frame.setVisible( true );
3 }
}
package rpsGame;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class RockPaperScissorsPanel extends JPanel implements ActionListener
{
protected final int SCISSORS = 0, ROCK = 1, PAPER = 2, QUESTIONMARK = 3;
protected final int BASE = 0, WIN = 1;
protected JLabel imgPlayerA, imgPlayerB, strPlayers;
protected JButton[] button;
protected ImageIcon[][] image;
protected Timer timer;
P1b public RockPaperScissorsPanel( final String imgPath ) {
image = new ImageIcon[4][2];
image[SCISSORS ][BASE] = new ImageIcon( new ImageIcon( imgPath + "scissors.gif" ).getImage().getScaledInstance( 200, 200, Image.SCALE_SMOOTH ) );
image[SCISSORS ][WIN] = new ImageIcon( new ImageIcon( imgPath + "scissors_win.gif" ).getImage().getScaledInstance( 200, 200, Image.SCALE_SMOOTH ) );
image[ROCK ][BASE] = new ImageIcon( new ImageIcon( imgPath + "rock.gif" ).getImage().getScaledInstance( 200, 200, Image.SCALE_SMOOTH ) );
image[ROCK ][WIN] = new ImageIcon( new ImageIcon( imgPath + "rock_win.gif" ).getImage().getScaledInstance( 200, 200, Image.SCALE_SMOOTH ) );
image[PAPER ][BASE] = new ImageIcon( new ImageIcon( imgPath + "paper.gif" ).getImage().getScaledInstance( 200, 200, Image.SCALE_SMOOTH ) );
image[PAPER ][WIN] = new ImageIcon( new ImageIcon( imgPath + "paper_win.gif" ).getImage().getScaledInstance( 200, 200, Image.SCALE_SMOOTH ) );
image[QUESTIONMARK][BASE] = new ImageIcon( new ImageIcon( imgPath + "questionmark.gif" ).getImage().getScaledInstance( 200, 200, Image.SCALE_SMOOTH ) );
image[QUESTIONMARK][WIN] = new ImageIcon( new ImageIcon( imgPath + "questionmark.gif" ).getImage().getScaledInstance( 200, 200, Image.SCALE_SMOOTH ) );
this.imgPlayerA = new JLabel( image[QUESTIONMARK][BASE] );
this.imgPlayerB = new JLabel( image[QUESTIONMARK][BASE] );
this.strPlayers = new JLabel( "<html><body>[너]<br>[나]</body></html>", JLabel.CENTER );
this.add( this.imgPlayerB );
this.add( this.strPlayers );
this.add( this.imgPlayerA );
button = new JButton[3];
button[SCISSORS] = new JButton( new ImageIcon( image[SCISSORS][BASE].getImage().getScaledInstance( 30, 30, Image.SCALE_SMOOTH ) ) );
button[ROCK ] = new JButton( new ImageIcon( image[ROCK ][BASE].getImage().getScaledInstance( 30, 30, Image.SCALE_SMOOTH ) ) );
button[PAPER ] = new JButton( new ImageIcon( image[PAPER ][BASE].getImage().getScaledInstance( 30, 30, Image.SCALE_SMOOTH ) ) );
button[SCISSORS].addActionListener( this );
button[ROCK ].addActionListener( this );
button[PAPER ].addActionListener( this );
this.add( new JLabel(" 가위 바위 보를 선택하세요 ") );
this.add( button[SCISSORS] );
this.add( button[ROCK] );
this.add( button[PAPER] );
this.setBackground( Color.cyan );
this.setPreferredSize( new Dimension(220, 530) );
this.timer = new Timer( 1000, new ActionListener() {
@Override
L1b public void actionPerformed( ActionEvent event ) {
L11 ready();
timer.stop();
L1e }
});
P1e }
P2b public void ready() {
imgPlayerA.setIcon( image[QUESTIONMARK][BASE] );
imgPlayerB.setIcon( image[QUESTIONMARK][BASE] );
button[0].setEnabled( true );
button[1].setEnabled( true );
button[2].setEnabled( true );
P2e }
@Override
L2b public void actionPerformed( ActionEvent event ) {
int playerA = select( event );
int playerB = (int)( Math.random() * 3 );
L21 show( playerA, playerB );
timer.start();
L2e }
P3b public void show( int playerA, int playerB ) {
int playerA_winOrLose = BASE, playerB_winOrLose = BASE;
if ( ( ( playerA == SCISSORS ) && ( playerB == PAPER ) )
|| ( ( playerA == ROCK ) && ( playerB == SCISSORS ) )
|| ( ( playerA == PAPER ) && ( playerB == ROCK ) ) ) {
playerA_winOrLose = WIN;
}
else if ( ( ( playerA == PAPER ) && ( playerB == SCISSORS ) )
|| ( ( playerA == SCISSORS ) && ( playerB == ROCK ) )
|| ( ( playerA == ROCK ) && ( playerB == PAPER ) ) ) {
playerB_winOrLose = WIN;
}
imgPlayerA.setIcon( image[ playerA ][ playerA_winOrLose ] );
imgPlayerB.setIcon( image[ playerB ][ playerB_winOrLose ] );
button[0].setEnabled( false );
button[1].setEnabled( false );
button[2].setEnabled( false );
P3e }
public int select( ActionEvent event ) {
if( event.getSource() == button[SCISSORS] )
return SCISSORS;
else if ( event.getSource() == button[ROCK] )
return ROCK;
else
return PAPER;
}
}