1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
| import java.awt.Color; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Random;
import javax.swing.*;
public class GamePanel implements ActionListener{ int playersco = 0; static int compsco = 0; String playerroc, playersis, playerpap, player; static String compprint, comproc, comppap, compsis, comp; JButton roc, pap, sis; JPanel selectpanel, gamtex, compout; JLabel playerscore, compscore, compchoice; public JPanel ContentPane(){ JPanel masterPanel = new JPanel(); masterPanel.setLayout(null); gamtex = new JPanel(); gamtex.setLayout(null); gamtex.setLocation(100, 10); gamtex.setSize(155,20); gamtex.setBackground(Color.yellow); masterPanel.add(gamtex); playerscore = new JLabel(""+ playersco +" Player"); playerscore.setLocation(0,0); playerscore.setSize(90,20); playerscore.setHorizontalAlignment(0); gamtex.add(playerscore); compscore = new JLabel(""+compsco+" Computer"); compscore.setLocation(20,0); compscore.setSize(190,20); compscore.setHorizontalAlignment(0); gamtex.add(compscore); compout = new JPanel(); compout.setLayout(null); compout.setLocation(130,35); compout.setSize(110,100); masterPanel.add(compout); compchoice = new JLabel(compprint); compchoice.setLocation(0,0); compchoice.setSize(85,20); compout.add(compchoice); selectpanel = new JPanel(); selectpanel.setLayout(null); selectpanel.setLocation(0,15); selectpanel.setSize(100,190); masterPanel.add(selectpanel); roc = new JButton("Rock"); roc.setLocation(10,5); roc.setSize(80,30); roc.addActionListener(this); selectpanel.add(roc); pap = new JButton("Paper"); pap.setLocation(10,45); pap.setSize(80,30); pap.addActionListener(this); selectpanel.add(pap); sis = new JButton("Sissors"); sis.setLocation(10,85); sis.setSize(80,30); sis.addActionListener(this); selectpanel.add(sis); masterPanel.setOpaque(true); return masterPanel; } public void actionPerformed(ActionEvent e){ if(e.getSource() == roc ){ playersco = playersco +1; player = playerroc; }else if(e.getSource() == pap){ playersco = playersco +1; player = playerpap; }else if(e.getSource() == sis){ playersco = playersco +1; player = playersis; } } public static void comprandom(){ Random randomGenerator = new Random(); int randomInt = randomGenerator.nextInt(3); if(randomInt == 1){ comp = compsis; compprint = "Sissors!"; compsco = compsco+1; }else if(randomInt == 2){ comp = comproc ; compprint = "Rock!"; compsco = compsco+1; }else { comp = comppap ; compprint = "Paper!"; compsco = compsco + 1; } }
private static void showgame(){ JFrame frame = new JFrame("Rock, Paper, Sissors"); GamePanel show = new GamePanel(); frame.setContentPane(show.ContentPane()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(280, 190); frame.setVisible(true); } public static void main(String[] args){ SwingUtilities.invokeLater(new Runnable() { public void run() { comprandom(); showgame(); } }); }
} |