Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: How to set (minimum) panel size for panel with a Graphics2D object only?

  1. #1
    Junior Member
    Join Date
    Jun 2014
    Posts
    25
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default How to set (minimum) panel size for panel with a Graphics2D object only?

    I am writing a program to solve Sudoku puzzles. I create the user interface using AWT/Swing.

    One panel of the main window (JFrame object) contains a Graphics2D component only (a grid for the sudoku puzzle). The problem is that this panel is too small for its contents when it is created. I have tried many things to make it bigger (like the setSize method of the panel), but to no avail. Who can tell me how I can make the panel with a single Graphics2D object larger?

    Below is the code for the main window, which is a JFrame object. I set its size to 400 x 400 to have ample room for all panels. I then create two panels: a sudoku panel that contains the sudoku grid and a button panel that contains two buttons. The button panel has a flow layout.

    I then create the main panel. This has a vertical BoxLayout. Finally I add the sudoku panel and the button panel to the main panel and the main panel to the frame.

    public class ViewController_Graphics extends JFrame {
        JButton solveButton = new JButton("Solve");
        JButton nakedSinglesButton = new JButton("Naked Singles");
     
            public ViewController_Graphics(SudokuModel model) {
            	super("Sudoku");
            	setSize(400,400);
              setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              setLookAndFeel();
              // create sudoku panel
              SudokuPanel sudokuPanel = new SudokuPanel();
              // create button panel
              JPanel buttonPanel = new JPanel();
              buttonPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
              buttonPanel.add(solveButton);
              buttonPanel.add(nakedSinglesButton);
              //create main panel
            	JPanel mainPanel = new JPanel();
            	mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
            	mainPanel.add(sudokuPanel);
            	mainPanel.add(buttonPanel);
            	// show frame
            	add(new JScrollPane(mainPanel));
              setVisible(true);
        }
    }

    Below is the code for the SudokuPanel class.

        class SudokuPanel extends JPanel {
            final int X_OFFSET = 10;
            final int Y_OFFSET = 10;
            final int WIDTH = 30;
            final int HEIGHT  = 30;
            final float LIGHTSTROKE = 1.0f;
            final float BOLDSTROKE = 2.0f;
     
            public void paintComponent(Graphics comp) {
                Graphics2D comp2D = (Graphics2D)comp;
                comp2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                comp2D.setColor(Color.black);
                for (int x = 0; x < 9; x++) {
                    for (int y = 0; y < 9; y++) {
                        int xPos = X_OFFSET + (x * WIDTH);
                        int yPos = Y_OFFSET + (y * HEIGHT);
                        comp2D.setStroke(new BasicStroke(LIGHTSTROKE, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_MITER));
                        comp2D.draw(new Rectangle2D.Double(xPos, yPos, WIDTH, HEIGHT));
                        if (((x % 3) == 0) && ((y % 3) == 0)) {
                            comp2D.setStroke(new BasicStroke(BOLDSTROKE, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_MITER));
                            comp2D.draw(new Rectangle2D.Double(xPos, yPos, WIDTH * 3, HEIGHT * 3));
                        }
                    }
                }
            }
     
        }

    When I run the program, the following Window shows up:



    As you can see, the button panel is exactly in the middle (and it remains in the middle, no matter how I resize the window). And there is not enough room for the sudoku panel.

    How can I make enough room for the sudoku panel when the window is created?


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: How to set (minimum) panel size for panel with a Graphics2D object only?

    The Component class has a setMinimumSize() function.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Jun 2014
    Posts
    25
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: How to set (minimum) panel size for panel with a Graphics2D object only?

    Thanks a lot! It works!

  4. #4
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: How to set (minimum) panel size for panel with a Graphics2D object only?

    You could also make your custom Panel overwrite its getPreferredSize() method, or use a layout that manages your components better.

  5. #5
    Junior Member
    Join Date
    Jun 2014
    Posts
    25
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: How to set (minimum) panel size for panel with a Graphics2D object only?

    Yes, I read that later. Which is the preferred method, override getPreferredSize() or use setMinimumSize() ?

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: How to set (minimum) panel size for panel with a Graphics2D object only?

    The preferred approach would be to use an appropriate LayoutManager and/or multiple containers to present the interface you desire.

  7. #7
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: How to set (minimum) panel size for panel with a Graphics2D object only?

    Its usually a bad habit to set a minimum size. Overwriting getPreferredSize is okay for your custom components but its not always possible, sometimes the preferred size is just the maximum.
    As Greg said, a good layout manager would be the better choice.

Similar Threads

  1. touch panel
    By alitehrani in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 16th, 2014, 07:34 AM
  2. How i can create a Panel in the same
    By websit in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 6th, 2013, 09:53 AM
  3. Help with bouncing on a panel
    By hellhunt in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 9th, 2012, 08:28 PM
  4. Panel Problems?
    By Dellick17 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 22nd, 2010, 01:31 PM
  5. Adding fixed size picture and button to panel
    By Javabeginner in forum AWT / Java Swing
    Replies: 10
    Last Post: August 23rd, 2010, 06:07 PM