Rectangle won't draw

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.

I might have overlooked it but you never seem to call the draw method in your main class. You just create the object.

How would I go about doing that?

Do I have to call the draw method when the computer returns rock, do I draw the method in my run, or my when I create my JFrames?

I’ve been messing with this for a while and I just can’t wrap my head around it, the little rectangle won’t show up anywhere. I know I’m missing something so simple, but I guess I’m thinking so hard I keep missing it.

Try overriding the paint method in the RockComponent class instead of the paintComponent method.

Is everything else getting painted instead of the rectangle when you get rock?

AFAIK Swing should call the paint methods for you unless you deliberately turn it off so it shouldn’t be a problem. Swing is a bit mysterious at times and at other times it’s outright flabbergasting!

Everything works as normal. All of my panels display appropriately, but this rectangle won’t show up no matter where I put it.

I haven’t tinkered with what you suggested, but I will when I get the chance.

Alright, drawing to a JFrame is a little bit more than just calling “Draw” and getting a picture onto the screen. You will need a place to draw the graphics into and one of the most common is called a “BufferedImage”. I am going to leave you with example code that gets a Rectangle to be drawn on a JFrame in blue.


import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;

import javax.swing.JComponent;
import javax.swing.JFrame;

public class box extends JComponent implements Runnable{
	
	private JFrame window;
	private BufferedImage bimg;
	
	public static void main(String arg[]){
		new box();
	}
	
	public box(){
		Thread looper = new Thread(this);
        looper.start();
		window = new JFrame("Rectangle Test");
		window.setSize(400, 400);
		window.add(this);
        window.validate();
        window.setVisible(true);
        window.pack();
        
	}
	
	@Override
    public Dimension getPreferredSize(){
        return new Dimension(400, 400);
    }
	
	@Override
    public void paintComponent(Graphics g){
        super.paintComponent(g);
        if(bimg == null)
        	bimg = (BufferedImage)createImage(400, 400);
        Graphics2D g2 = bimg.createGraphics();      
        g2.setColor(Color.BLUE);
        g2.fillRect(5, 10, 100, 100);//Edit here to change location
        
        g.drawImage(bimg, 0, 0, this);
    }

	@Override
    public final void run() {
        try{           
            while(true){
                Thread.sleep(10);
                repaint();
            }
        }catch(Exception e){
            System.exit(0);
        }
    }
}

As you can see, there is plenty of steps you need to take in order to draw a basic Rectangle within Swing. The fastest way to do it is to use the built in functions “fillRect” and “drawRect” within the Graphics, instead of making a Rectangle. Good luck.

Ok I think I found the problem. As I said, Swing is a mysterious beast and not straightforward to tame.

By default a JComponent doesn’t have a preferred size (default size = 0). And by default a JPanel uses a FlowLayout. A FlowLayout basically means that the JPanel will respect the size of its components. And because the JComponent is default 0 size you won’t see it.

So you can either change your JPanels to use a BorderLayout or override the getPreferredSize() method in your JComponent (RockComponent) and you should see your rectangle.

E.g:

compout = new JPanel(new BorderLayout());

or in your RockComponent class add:

public Dimension getPreferredSize() {
   return new Dimension(100,100);
}

[EDIT]: source http://stackoverflow.com/questions/3567190/displaying-a-jcomponent-inside-a-jpanel-on-a-jframe

Actually, since you set all your JPanels layouts to null adding the getPreferredSize() method to your RockComponent won’t work since you’re not using the JPanels default FlowLayout.

The only way you’ll see your rectangle by having a null LayoutManager is the same way you’re getting your other JComponents to show (I.e, your JLabels). Meaning that you will have to set its location and especially its size manually after you create the component.

So either get a LayoutManager to make your life easier or add these lines before adding your RockComponent to the compout JPanel:

          RockComponent component = new RockComponent();
          component.setLocation(0,0); // This line shouldn't be 100% necessary to show the rectangle
          component.setSize(100,100); // This is important.
          compout.add(component);

Here is an example of proper usage of components and AWT tools to render with Java2D, if it helps.