Peter
Peter

Reputation: 449

GridBagLayout and drawing

I'm making a small game and at the beginning i want to have JCheckBox for choosing the language(after that they are few more of them for setting the game) and above that a jlabel with picture with name of the game OR draw an image there, the problem is that i dont know any other way how to center the panel with checkboxes then to use GridBagLayout and when i use this, i cannot draw anything to the frame, id like to also remove those grey lines around the checkboxes if its possible, appreciate any help, thanks.

This is my second question here and i cant add images yet so here is a link to the picture :

here is the picture

here is code for the frame

private GamePlan plan;
private JFrame frame;
private String language;
private JPanel panel;
private JCheckBox englishBox;
private JCheckBox germanBox;

public Settings(GamePlan plan){
    this.plan = plan;
    frame = new JFrame();
    frame.setSize(600, 500);
    frame.setLocation(200, 200);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(new GridBagLayout());
    frame.setResizable(false);
    frame.setVisible(true);
    panel = new JPanel(new GridLayout(2, 1));
    englishBox = new JCheckBox("English", false);
    germanBox = new JCheckBox("German", false);
    englishBox.addActionListener(new EnglishLanguage());
    germanBox.addActionListener(new GermanLanguage());
    panel.add(englishBox);
    panel.add(germanBox);
    englishBox.setOpaque(false);
    germanBox.setOpaque(false);
    panel.setOpaque(false);
    frame.add(panel);
    frame.getContentPane().setBackground(new Color(216,252,202));
}

Upvotes: 1

Views: 889

Answers (1)

Paul Samsotha
Paul Samsotha

Reputation: 208944

" the problem is that i dont know any other way how to center the panel with checkboxes then to use GridBagLayout and when i use this, i cannot draw anything to the frame"

I can't really tell what you're doing wrong without a complete example. I don't even see where you're trying to add the image. But don't try and draw on the frame. Draw on a JPanel instead.

Here is an example you may be able to gain some insight from.

enter image description here

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridBagLayout;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.TitledBorder;

public class ImageByDrawing {
    public ImageByDrawing() {
        ImagePanel imagePanel = new ImagePanel();
        imagePanel.setBorder(new TitledBorder("Drawn Image onto JPanel"));

        JCheckBox germanBox = new JCheckBox("German");
        germanBox.setOpaque(false);
        JCheckBox englishBox = new JCheckBox("English");
        englishBox.setOpaque(false);
        JPanel boxPanel = new JPanel();
        boxPanel.setBorder(new TitledBorder("JPanel with default FlowLayout"));
        boxPanel.setOpaque(false);
        boxPanel.add(germanBox);
        boxPanel.add(englishBox);

        JPanel centerPanel = new JPanel(new BorderLayout());
        centerPanel.add(imagePanel, BorderLayout.CENTER);
        centerPanel.add(boxPanel, BorderLayout.SOUTH);
        centerPanel.setBorder(new TitledBorder("JPanel with BorderLayout"));
        centerPanel.setOpaque(false);

        JPanel mainPanel = new JPanel(new GridBagLayout());
        mainPanel.add(centerPanel);
        mainPanel.setBorder(new TitledBorder("JPanel with GridBagLayout"));
        mainPanel.setBackground(new Color(216,252,202));

        JFrame frame = new JFrame();
        frame.add(mainPanel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.setSize(600, 600);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public class ImagePanel extends JPanel {
        BufferedImage img;
        int dWidth;
        int dHeight;
        public ImagePanel() {
            try {
                img = ImageIO.read(getClass().getResource("/resources/stackblack.jpg"));
                dWidth = img.getWidth();
                dHeight = img.getHeight();
            } catch (IOException ex) {
                Logger.getLogger(ImageByDrawing.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g); 
            g.drawImage(img, 0, 0, img.getWidth(), img.getHeight(), this);
        }
        @Override
        public Dimension getPreferredSize() {
            return (img == null) ? new Dimension(300, 300) : new Dimension(dWidth, dHeight);
        }
    }

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

Also I don't know why you prefer to draw the image. The same can be easily done with a JLabel and ImageIcon

enter image description here

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridBagLayout;
import javax.swing.ImageIcon;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.TitledBorder;

public class ImageByDrawing {

    public ImageByDrawing() {
        ImageIcon icon = new ImageIcon(getClass().getResource("/resources/stackblack.jpg"));
        JLabel label = new JLabel(icon);
        label.setBorder(new TitledBorder("JLabel with ImageIcon"));

        JCheckBox germanBox = new JCheckBox("German");
        germanBox.setOpaque(false);
        JCheckBox englishBox = new JCheckBox("English");
        englishBox.setOpaque(false);
        JPanel boxPanel = new JPanel();
        boxPanel.setBorder(new TitledBorder("JPanel with default FlowLayout"));
        boxPanel.setOpaque(false);
        boxPanel.add(germanBox);
        boxPanel.add(englishBox);

        JPanel centerPanel = new JPanel(new BorderLayout());
        centerPanel.add(label, BorderLayout.CENTER);
        centerPanel.add(boxPanel, BorderLayout.SOUTH);
        centerPanel.setBorder(new TitledBorder("JPanel with BorderLayout"));
        centerPanel.setOpaque(false);

        JPanel mainPanel = new JPanel(new GridBagLayout());
        mainPanel.add(centerPanel);
        mainPanel.setBorder(new TitledBorder("JPanel with GridBagLayout"));
        mainPanel.setBackground(new Color(216, 252, 202));

        JFrame frame = new JFrame();
        frame.add(mainPanel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.setSize(600, 600);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

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

The last part of your question, as @Jere pointed out you can use setFocusPainted for the check box germanBox.setFocusPainted(false);

Upvotes: 1

Related Questions