Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (577)
games submitted by our members
Games in WIP (498)
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  
  Width and Height of a Jpanel  (Read 237 times)
0 Members and 1 Guest are viewing this topic.
Offline Beppe

Junior Member





« Posted 2013-08-13 23:00:10 »

Hi , i've a problem with the collision  with Panel borders ....
I post a  part of code for every class :
in main class:
1  
2  
3  
4  
 
GamePanel_1 g = new GamePanel_1();
add(g);
pack();

in Panel class:
1  
2  
3  
 
public static Dimension size= new Dimension(180,180);
setPreferredSize(size);

in a other class:
1  
2  
3  
4  
5  
if(x>160){
collide=true;
isrunning=false;
x=160;
}

160 is a number calculate like the width of the panel set with setpreferredsize minus the width of my image (20 pixel), the problem is that is like if the panel  haven't the size that I've set and so the collision don't work well
Offline Beppe

Junior Member





« Reply #1 - Posted 2013-08-14 19:18:07 »

Someone could help me ??
Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 114
Projects: 4
Exp: 3 years



« Reply #2 - Posted 2013-08-14 19:22:07 »

I don't quite understand your question...you have problems setting the size of the JPanel? What do you mean by "collision"?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Beppe

Junior Member





« Reply #3 - Posted 2013-08-14 19:37:19 »

No , I set the screen like 180X180 and i ve an image 20X20 pixel , I move the image on the screen ... when x of image is 160 = panelsize(180)-widthofobject(20) i set x=160 so in this way  the image should stay in the window... Instead  overstep the window...
Offline jonjava
« Reply #4 - Posted 2013-08-14 19:51:36 »

I don't see anything wrong with the code per se in regards to what you're wanting to do. Are you sure

1  
2  
3  
4  
5  
6  
if(x>160){
collide=true;
isrunning=false;
x=160;
System.out.println("Collision!!!"); // poor mans debugging
}


is being called?

Offline BurntPizza
« Reply #5 - Posted 2013-08-14 20:05:08 »

He's probably having a problem with the panel over-filling (for lack of a better term) the JFrame he is using. Run this code:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
public static void main(String[] a) {
   JFrame j = new JFrame();
   JPanel p = new JPanel() {
      @Override
      public void paint(Graphics g) {
         g.clearRect(0, 0, getWidth(), getHeight());
         g.setColor(Color.red);
         g.drawRect(0, 0, getWidth(), getHeight());
      }
   };
   p.setPreferredSize(new Dimension(180, 180));
   p.setBackground(Color.black);
   j.add(p);
   j.pack();
   j.setVisible(true);
}


No matter how you resize the window, you can never see the JPanel's right or bottom border.

So some reason they are 1 pixel out of bounds, and there are probably lots of fixes, but here is a quick one:
  • Set Panel prefferedSize(w + 1, h + 1)
  • Render up to getWidth() - 1 and getHeight() - 1

There is also probably some things you could do with a layout manager, but I haven't fiddled with Swing in ages, so I don't want to try and figure it out right now.
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.

xsi3rr4x (24 views)
2014-04-15 18:08:23

BurntPizza (19 views)
2014-04-15 03:46:01

UprightPath (33 views)
2014-04-14 17:39:50

UprightPath (17 views)
2014-04-14 17:35:47

Porlus (33 views)
2014-04-14 15:48:38

tom_mai78101 (59 views)
2014-04-10 04:04:31

BurntPizza (117 views)
2014-04-08 23:06:04

tom_mai78101 (217 views)
2014-04-05 13:34:39

trollwarrior1 (184 views)
2014-04-04 12:06:45

CJLetsGame (191 views)
2014-04-01 02:16:10
List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:05:20
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!