Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (476)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (531)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Rectangle won't draw  (Read 1043 times)
0 Members and 1 Guest are viewing this topic.
Offline Nicky-G-

Senior Newbie





« Posted 2012-08-16 19:50:11 »

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?

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  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182  
183  
184  
185  
186  
187  
188  
189  
190  
191  
192  
193  
194  
195  
196  
197  
198  
199  
200  
201  
202  
203  
204  
205  
206  
207  
208  
209  
210  
211  
212  
213  
214  
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();
       }
             
   }
       
   
         
   
   

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
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.
Offline Gjallar

JGO Coder


Medals: 13
Projects: 1


Follower of Nurgle


« Reply #1 - Posted 2012-08-16 22:59:12 »

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

Senior Newbie





« Reply #2 - Posted 2012-08-16 23:07:53 »

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?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Nicky-G-

Senior Newbie





« Reply #3 - Posted 2012-08-19 00:37:51 »

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.
Offline jonjava
« Reply #4 - Posted 2012-08-19 01:23:47 »

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?

Offline jonjava
« Reply #5 - Posted 2012-08-19 01:33:57 »

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

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!

Offline Nicky-G-

Senior Newbie





« Reply #6 - Posted 2012-08-19 18:50:06 »

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.
Offline ctomni231

JGO Wizard


Medals: 98
Projects: 1
Exp: 7 years


Not a glitch. Just have a lil' pixelexia...


« Reply #7 - Posted 2012-08-19 21:52:57 »

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.

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  
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.

Offline jonjava
« Reply #8 - Posted 2012-08-19 23:15:08 »

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:
1  
compout = new JPanel(new BorderLayout());


or in your RockComponent class add:
1  
2  
3  
public Dimension getPreferredSize() {
   return new Dimension(100,100);
}


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

Offline jonjava
« Reply #9 - Posted 2012-08-19 23:37:02 »

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:

1  
2  
3  
4  
          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);

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ra4king

JGO Kernel


Medals: 336
Projects: 2
Exp: 5 years


I'm the King!


« Reply #10 - Posted 2012-08-21 02:11:26 »

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

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

pw (13 views)
2014-07-24 01:59:36

Riven (13 views)
2014-07-23 21:16:32

Riven (13 views)
2014-07-23 21:07:15

Riven (14 views)
2014-07-23 20:56:16

ctomni231 (43 views)
2014-07-18 06:55:21

Zero Volt (39 views)
2014-07-17 23:47:54

danieldean (32 views)
2014-07-17 23:41:23

MustardPeter (35 views)
2014-07-16 23:30:00

Cero (50 views)
2014-07-16 00:42:17

Riven (50 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!