Trouble displaying an image using GridBagLayout
hi
I am creating a GUI and have a number of labels and buttons within my JPanel. I have successfully added an image however it is not displaying in full. Only the top left corner is visible. I have constraints in place allowing me to position the image using gridx and gridy and have tried using gridHeight/gridWidth but it still does not sort the problem out. Here is my code.
Code:
public class GUI extends JFrame implements ActionListener{
int mode =0 ;
JFrame frame = new JFrame("interface");
JPanel panel;
public int run() {
frame = new JFrame("Interface");
frame.setVisible(true);
frame.setSize(800,700);
frame.setResizable(false);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
panel = new JPanel(new GridBagLayout());
frame.getContentPane().add(panel, BorderLayout.NORTH);
GridBagConstraints con = new GridBagConstraints();
con.gridx = 0;
con.gridy =0;
con.insets = new Insets(10,10,10,10);// spacing from item on left, right, up ,down
JLabel title = new JLabel("WELCOME TO BLACKJACK!");
Font tFont = new Font("serif",Font.BOLD,25);
title.setFont(tFont);
panel.add(title,con);
JLabel option = new JLabel("Please select a mode");
con.gridx = 0;
con.gridy = 3;
panel.add(option,con);
JButton button = new JButton ("Player");
button.setActionCommand("Button");
button.addActionListener(this);
con.gridx = 0;
con.gridy =4;
panel.add(button,con);
JButton button2 = new JButton ("Dealer");
button2.addActionListener(this);
con.gridx = 1;
con.gridy =4;
panel.add(button2,con);
CardImages s = new CardImages();
con.gridx = 0;
con.gridy = 6;
panel.add(s,con);
The image is passed by the following class:
Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class CardImages extends JPanel{
private ImageIcon image;
public CardImages() {
super();
}
public void paintComponent (Graphics g){
super.paintComponent(g);
image = new ImageIcon("QUEEN.jpg");
image.paintIcon(this,g, 0,0);
}
}
I have made a little progress. When i use
Code:
//con.fill = GridBagConstraints.HORIZONTAL;
the whole width of the image was displayed but only a strip at the top of the image.
Any suggestions??:=(:
Many Thanks