0

I have a JPanel subclass and overriding paintComponent(Graphics g) and calling g.drawRect(0, 0, 500, 75). Inside my main method I initialize it and call setBounds(50, 400, 500, 75) on it, but when it draws the rectangle it is only drawing something like this:

|________________

and missing the two other sides. Sorry I don't have the code, it's on a different computer. Any ideas?

Chris
  • 7,270
  • 19
  • 66
  • 110
  • 1
    Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556), along with layout padding & borders for [white space](http://stackoverflow.com/q/17874717/418556). – Andrew Thompson Nov 07 '13 at 01:26

1 Answers1

1

First, use;

g.drawRect(0, 0, 499, 74);

Second, don't use setBounds make use of appropriate layout managers and override getPreferredSize (and if required getMinimum/MaximumSize)

Third, don't rely on magic numbers...

g.drawRect(0, 0, getWidth() - 1, getHeight() - 1);
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366