Results 1 to 1 of 1
  1. #1
    treeface99 is offline Member
    Join Date
    Jan 2012
    Posts
    12
    Rep Power
    0

    Default 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.

    Java 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:

    Java 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

    Java 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
    Last edited by DarrylBurke; 03-16-2012 at 02:34 AM.

Similar Threads

  1. some swing components are not displaying
    By mDennis10 in forum AWT / Swing
    Replies: 6
    Last Post: 01-27-2012, 09:26 AM
  2. Problem with displaying an image in Java Swing
    By BackInSoftware in forum Advanced Java
    Replies: 10
    Last Post: 01-11-2011, 06:56 PM
  3. Getting and Displaying Swing Components From DataBase
    By vivekkumar in forum AWT / Swing
    Replies: 1
    Last Post: 07-27-2010, 03:32 PM
  4. Trouble Displaying Image
    By LankanSniper in forum Java 2D
    Replies: 3
    Last Post: 12-10-2009, 09:26 AM
  5. Java Swing GridBagLayout Problem
    By hemanthjava in forum AWT / Swing
    Replies: 1
    Last Post: 06-29-2008, 08:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •