2

i am trying to add 2 different panels in a Frame. one panel adds few buttons in the frame. others frame will add a chess board into the frame. i am confused, how to draw this board on a panel. my Frame will have a board on the top and buttons at the bottom. Moreover, let me know if i am somewhere wrong in the given code can anybody help me? my Code is

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Test {

    private JFrame main;
    private JPanel board;
    private JPanel buttons;
    private JButton add;
    private JButton delete;

    public Test()
    {
        main=new JFrame();
        board=new JPanel();
        buttons=new JPanel();
        add=new JButton("Add");
        delete=new JButton("Delete");
        init();
        addButtons();
    }
    public void init()
    {
        main.setSize(700,700);
        main.setVisible(true);
        main.setDefaultCloseOperation(main.EXIT_ON_CLOSE);
    }
    public void addButtons()
    {
        buttons.setSize(700,40);
        buttons.setLayout(new FlowLayout());
        buttons.add(add);
        buttons.add(delete);
        main.add(buttons,BorderLayout.SOUTH);

    }
    public void addBoxes()
    {
        // what should be my code here...??
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        new Test();

    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • What's the actual problem? What doesn't work about this? – Paul Samsotha Jan 14 '14 at 19:08
  • i dont know to paint chess board on my Jpane board and add it in my frame –  Jan 14 '14 at 19:09
  • have a look into http://www.edu4java.com/en/game/game1.html for info on how to use JPanels. – kajacx Jan 14 '14 at 19:17
  • See also this [example](http://stackoverflow.com/a/2562685/230513) and [variation](http://stackoverflow.com/a/2563350/230513). – trashgod Jan 14 '14 at 19:19
  • There are. Any ways to achieve this, for [example](http://stackoverflow.com/questions/15891250/creating-a-multicolored-board/15891779#15891779), [example](http://stackoverflow.com/questions/15870608/creating-a-draw-rectangle-filled-with-black-color-function-in-java-for-a-grid/15870637#15870637), [example](http://stackoverflow.com/questions/13448535/jbuttons-on-gridlayout-minesweeper/13448715#13448715) – MadProgrammer Jan 14 '14 at 19:43

1 Answers1

5
  1. You need a component to paint on, like a JPanel.
  2. You need to @Override its paintComponent method
  3. You can use a loop to paint using Graphics context
  4. Use a flag to alternate between colors.

Take a look at some Painting Graphics tutorials

In the mean time, give this a whirl

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class Board extends JPanel {

    private static final int DIM_WIDTH = 640;
    private static final int DIM_HEIGHT = 640;
    private static final int SQ_SIZE = 80;

    boolean black = true;

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        for (int i = 0; i < DIM_HEIGHT; i += SQ_SIZE) {
            if (black) {
                black = false;
            } else {
                black = true;
            }
            for (int j = 0; j < DIM_WIDTH; j += SQ_SIZE) {
                if (black) {
                    g.setColor(Color.WHITE);
                    g.fillRect(j, i, SQ_SIZE, SQ_SIZE);
                    black = false;
                } else {
                    g.setColor(Color.BLACK);
                    g.fillRect(j, i, SQ_SIZE, SQ_SIZE);
                    black = true;
                }
            }
        }
    }

    public static void createAndShowGui() {
        JFrame frame = new JFrame();
        frame.add(new Board());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationByPlatform(true);
        frame.pack();
        frame.setVisible(true);

    }

    public Dimension getPreferredSize() {
        return new Dimension(DIM_WIDTH, DIM_HEIGHT);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGui();
            }
        });
    }
}

enter image description here

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • What if i had to add a another panel with two buttons in it just under my chess board? –  Jan 14 '14 at 19:38
  • Take out the `main` method and the `createAndshowGui` method. Then the class is just a regular panel. You can add if the a `JFrame` of your own class. Then just add a separate panel below it, with the buttons. – Paul Samsotha Jan 14 '14 at 19:40
  • 1
    This could also be achieved with the use of GridLayout and JPanel, which would reduce the complexity of the management and make it easier to place pieces on the board for [example](http://stackoverflow.com/questions/12886200/sudoku-board-using-jpanels-in-java/12888426#12888426) – MadProgrammer Jan 14 '14 at 19:42
  • can u please explain what does these statements do?? frame.setLocationByPlatform(true); frame.pack(); –  Jan 14 '14 at 19:44
  • `.pack()` just maintains the preferred sizes of the components in the frame, to a minimal size, and `.setLocationbyPlatform()` just sets the location of the frame. You can also set it to `null` to center it. Also you may want to look at @MadProgrammer link. His idea seems more reasonable. – Paul Samsotha Jan 14 '14 at 19:46