0

I am trying to draw a chessboard with setColor(c) and fillRect(x, y, dx, dy) methods of Graphics. I want it to be resizable according to the modifications of the window, but keeping the shape of a square. Additionally, I want it to be placed in the middle of the encompassing panel.

As it may be seen below, the last one is not the case. Apart from that everything resizes well.

what I got

  1. JFrame is split into two JPanels using GridBagLayout keeping the proportion 3:1. The upper cell uses fill = GridBagConstraints.BOTH. The corresponding upperPanel is thus stretched as red border shows.
  2. Then this upperPanel contains another JPanel boardPanel used solely for the chessboard itself. This part is definitely wrong here but I don't know how to fix it.

a. upperPanel defines the following layout in its constructor:

setLayout(new BorderLayout());
setBorder(BorderFactory.createLineBorder(Color.RED));
add(boardPanel, BorderLayout.CENTER);

I wanted to stretch the frame as far as possible within the grid cell.

b. boardPanel has then such layout:

setLayout(new BorderLayout());
setBorder(BorderFactory.createLineBorder(Color.BLUE));

Frankly speaking nothing else worked. The panel contains only graphics being drawn in paintComponent(Graphics g).

  1. setColor(c) and fillRect(x, y, dx, dy) is done in a loop within paintComponent(Graphics g) method of boardPanel, starting from x=0, y=0. The length of a chessboard cell edge is determined every time this method is called through calculations on getWidth() and getHeight() of an upperPanel (as boardPanel is a nested class in upperPanel).

Is there a way to make boardPanel adjust to what was drawn within it? So that the boardPanel is the size of an actual image of the board. I guess then layout of upperPanel would place it in the center of itself.

infoholic_anonymous
  • 969
  • 3
  • 12
  • 34
  • 1
    You mean something like [this](http://stackoverflow.com/questions/14635952/java-layout-proportions-creating-a-scalable-square-panel/14636828#14636828)? – MadProgrammer Nov 12 '14 at 09:06
  • 1
    for better help sooner post an SSCCE/MCVE, short, runnable, compilable, because attn screenshort talking about another issue – mKorbel Nov 12 '14 at 09:07
  • @MadProgrammer precisely, however I wondered (1) if it can be achieved a bit easier (for example withot custom layout) (2) if chessboard can be plainly painted instead of being constructed from buttons. Your solution is superb there but I was wondering if it's the only one or you just prepared an elastic one with greater functionality. – infoholic_anonymous Nov 12 '14 at 09:10
  • 1
    See also [Making a robust, resizable Swing Chess GUI](http://stackoverflow.com/q/21142686/418556). – Andrew Thompson Nov 12 '14 at 09:18
  • @mKorbel you mean that pure code would be better than description? The question seemed to me quite abstract. – infoholic_anonymous Nov 12 '14 at 09:23
  • @infoholic_anonymous question seemed to me quite abstract. == agree after link to code is posted by Andrew Thompson, otherwise isn't – mKorbel Nov 12 '14 at 09:25
  • Well, the layout is quite flexible, and having each cell as a component provides quite a bit of additional functionality (set the icon or add components to it), however, you could paint the cells manually through custom painting, but that would introduce quite a bit of work, not just through the painting, but the overall management of the game – MadProgrammer Nov 12 '14 at 09:25
  • @MadProgrammer but is there a way within the line of thinking I posted to make it work? To force the JPanel `a` containing the chessboard drawing to fit that chessboard and then to place it in the center of a JPanel `b`, where `b` contains [only] such containter? – infoholic_anonymous Nov 12 '14 at 09:32
  • *"To force the JPanel a containing the chessboard drawing to fit that chessboard and then to place it in the center of a JPanel b, where b contains [only] such containter?"* Given the linked source code (in my comment) does just that (while expanding the board to the largest square size available), I'm guessing you've not even glanced at the thread yet. :-/ – Andrew Thompson Nov 12 '14 at 10:02
  • If, you are manually painting the cheeseboard, then you should be able to calculate the x/y offsets that will center the border. Dropping the board inside multiple containers isn't really going to achieve anything, because the outer portions (width/height) of the component will still be larger than the size of the board itself... – MadProgrammer Nov 12 '14 at 11:57
  • @AndrewThompson your solution is great. I was just more interested in fixing my solution than finding new one. Excuse me for making my question so imprecise. I think I'd better delete this one and make another more detailed. Thanks for your time. – infoholic_anonymous Nov 14 '14 at 04:06
  • *"I think I'd better delete this one and make another more detailed."* Please [edit this question](http://stackoverflow.com/posts/26882972/edit) rather than ask a new one! – Andrew Thompson Nov 14 '14 at 04:08

0 Answers0