I’m trying to draw a simple rectangle whenever the computer returns rock. I’m not sure why this isn’t happening, I’m just stumped. I looked through my text box, went online, and performed a couple of searches on the forum and I can’t find an answer.
If I make a separate class to draw a rectangle, then just add it in a JPanel in my main class, shouldn’t it just pop up?
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;
public class GamePanel implements ActionListener{
private boolean isRunning = false;
int playersco = 0;
int compsco = 0;
int player;
int comp;
String playerroc, playersis, playerpap ;
String compprint, comproc, comppap, compsis;
JButton roc, pap, sis;
JPanel selectpanel, gamtex, compout;
JLabel playerscore;
JLabel compscore;
JLabel 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);
//compout.setBackground(Color.DARK_GRAY);
compchoice = new JLabel();
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);
//selectpanel.setBackground(Color.green);
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 ){
player = 1;
}else if(e.getSource() == pap){
player = 2;
}else if(e.getSource() == sis){
player = 3;
}
comprandom();
gamebrain();
}
public void comprandom(){
Random randomGenerator = new Random();
int randomInt = randomGenerator.nextInt(3);
if(randomInt == 1){
comp = 4;
compchoice.setText("Sicssors!");
}else if(randomInt == 2){
comp = 5 ;
compchoice.setText("Rock!");
RockComponent component = new RockComponent();
compout.add(component);
}else
{
comp = 6 ;
compchoice.setText("Paper!");
}
}
public void gamebrain()
{
if((player == 1 && comp == 4) ||( player == 3 && comp == 6)||( player == 2 && comp ==5 )){
playersco = playersco +1;
playerscore.setText(""+ playersco +" Player");
}else if( (player == 1 && comp == 5) ||(player == 2 && comp == 6) ||(player == 3 && comp == 4) ){
playerscore.setText(""+ playersco +" Player");
}
else{
compscore.setText(""+ compsco +" Computer");
compsco = compsco + 1;}
}
private void showgame(){
JFrame frame = new JFrame("Rock, Paper, Scissors");
frame.setContentPane(ContentPane());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(280, 190);
frame.setVisible(true);
}
public void run() {
showgame();
while(isRunning){
try{
Thread.sleep(20);
}catch(Exception e){}
}
}
public GamePanel(){
isRunning = true;
run();
}
public static void main(String[] args){
new GamePanel();
}
}
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import javax.swing.JComponent;
public class RockComponent extends JComponent{
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
Rectangle box = new Rectangle (5,10, 100,100);
g2.setColor(Color.blue);
g2.draw(box);
}
}
I want to thank everyone again. Thank all you for being so patient and respectful and providing such clear and well thought out answers. I can’t wait to get better at programming in Java so I can give back to the community.