• Post Reply Bookmark Topic Watch Topic
  • New Topic

Drawing a rectangle in a JPanel  RSS feed

 
Toon Straete
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I registered today on this forum, but I've been reading here for some time already. There have been moments where I wanted to ask a question, but I always was able to find the solution myself after some research, but now I'm stuck. I'm a total beginner in Java, but not in programming in general (I have done many projects programming in C for microcontrollers). I'm interested in java because I would like to make some web applications, or even games, just for fun.

I'm using the Eclipse Indigo IDE, and I installed WindowBuilder Pro to create a GUI. I'm trying to draw a rectangle in a JPanel. It sort of works, but not completely.

My JPanel has a large size (bigger than 200x200 pixels). But I'm unable to draw a rectangle bigger than 10x10 pixels (or something of that size, could be 15x15).

If I draw a rectangle of 2x2 pixels, it works, but for some reason there is a limit on the size. (200x200 pixels ends up being a small square)

I can't seem to figure out where this limit comes from. It looks like there is a small 10x10 panel inside my JPanel.

While I'm very sure this problem occurs because my basic understanding of what I'm doing is seriously lacking, I really would like to find the solution to this.

 
Rob Spoor
Sheriff
Posts: 20903
81
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The JPanel panel has a FlowLayout. That uses the preferred size of its child components for determining how to display them. Your MyDrawing panel has no child components and no explicitly set preferred size, so the preferred size is 0x0. Give it a preferred size, or override the getPreferredSize() method, to give it a better preferred size.

Also, postpone adding panel to the content pane until after it's ready - after you've added drawing. Otherwise the latter may not show up until you force a repaint.
 
Campbell Ritchie
Sheriff
Posts: 53779
128
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch
I think this question would fit better into our GUIs forum, so I shall move it.
 
Toon Straete
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:The JPanel panel has a FlowLayout. That uses the preferred size of its child components for determining how to display them. Your MyDrawing panel has no child components and no explicitly set preferred size, so the preferred size is 0x0. Give it a preferred size, or override the getPreferredSize() method, to give it a better preferred size.

Also, postpone adding panel to the content pane until after it's ready - after you've added drawing. Otherwise the latter may not show up until you force a repaint.


Thanks. I knew there was nothing wrong with my code, I just didn't know about the behaviour of things.
I guess I need to read a book about this stuff, I got a feeling this won't be the first time something has a certain behaviour I can't figure out myself.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!