Results 1 to 4 of 4
  1. #1
    rsieberg is offline Member
    Join Date
    Mar 2011
    Posts
    2
    Rep Power
    0

    Default Adding JPEG to JButton Child

    Hello,

    I am new to Swing and trying to improve my java by writing a chess program. I coded in the chess logic into an array of integers representing piece types. I then created a Swing GUI JFrame with an 8x8 grid layout of a class that extends JButton that handles mouse events. Currently, this grid is interactive and uses setText to display the piece type on the appropriate JButton child. I would like to display JPEG image files of chess piecess instead of setting text with integer valuesl; however, my code will not work. My images files are in the src directory so I just reference "whitePawn.jpeg". I would really appreciate help in getting this to run.

    Below are the relavent code snippets (here I intend for all of the pieces to display as White Pawns):


    Java Code:
    	public GUI(Board b)
    	{
    		repaint(b);
    	}
    
    	public void repaint(Board b)
    	{
    		Square[][] grid= new Square[8][8];
    		buttonBar.setLayout( new GridLayout(8,8) );
    		board = b;
    			
    		for(int i=0;i<8;i++)
    			for(int j=0;j<8;j++)
    			{
    				if(board.occupancy[i][j] == -1)
    				{
    					grid[i][j] = new Square(-1,i,j); 
    					grid[i][j].addMouseListener(new ButtonListener(i,j));
    					
    				}
    				else
    				{
    					switch(board.pieces.get(board.occupancy[i][j]).typeNumber)
    					{
    						case 1: grid[i][j] = new Square(1,i,j); grid[i][j].addMouseListener(new ButtonListener(i,j)); break;
    						case 2: grid[i][j] = new Square(2,i,j); grid[i][j].addMouseListener(new ButtonListener(i,j));	break;
    						case 3: grid[i][j] = new Square(3,i,j); grid[i][j].addMouseListener(new ButtonListener(i,j)); break;
    						case 4: grid[i][j] = new Square(4,i,j); grid[i][j].addMouseListener(new ButtonListener(i,j));	break;
    						case 5: grid[i][j] = new Square(5,i,j); grid[i][j].addMouseListener(new ButtonListener(i,j));	break;
    						case 6: grid[i][j] = new Square(6,i,j); grid[i][j].addMouseListener(new ButtonListener(i,j));	break;
    					}
    				}              
    				if((i+j)%2==1)
    					grid[i][j].setBackground(Color.BLACK);
    				else
    					grid[i][j].setBackground(Color.WHITE);
    				buttonBar.add(grid[i][j]);
    			}
    
    		buttonBar.setSize(600,550);
    		buttonBar.setVisible(true);
    	
    	}


    Java Code:
    import javax.swing.*;
    public class Square extends JButton
    {
    	ImageIcon img1;
    	
    	int type, rank, file;
    	Square(int a, int b, int c)
    	{
    		img1 = new ImageIcon("whitePawn.jpeg");
    		type = a;
    		rank = b;
    		file = c;
    		this.setIcon(img1);
    	}
    	
    	public void repaint()
    	{
    		setText(Integer.toString(type));
    		this.setIcon(img1);
    	}
    }
    Last edited by Fubarable; 03-27-2011 at 08:28 PM. Reason: code tags added

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Don't override repaint() unless you really really need to do this and know exactly what you are doing. Instead why not simply call the setIcon(...) method of your buttons when you need to change an image?


    Also, when posting code here, please use code tags so that your code will retain its formatting and thus will be readable -- after all, your goal is to get as many people to read your post and understand your code as possible, right?

    To do this, highlight your pasted code (please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code) and then press the code button, and your code will have tags.

    Another way to do this is to manually place the tags into your code by placing the tag [cod&#101;] above your pasted code and the tag [/cod&#101;] below your pasted code like so:

    Java Code:
    [cod&#101;]
      // your code goes here
      // notice how the top and bottom tags are different
    [/cod&#101;]
    Best of luck and welcome to the java-forums!!!

  3. #3
    rsieberg is offline Member
    Join Date
    Mar 2011
    Posts
    2
    Rep Power
    0

    Default

    Thank you for your help and quick response Fubarable, but I still cannot seem to get it to work. I removed all of the repaint methods and still have the interactive GUI board working with integers for set string.

    But if I replace

    Java Code:
     someButton.setText();
    with

    Java Code:
    someButton.setIcon(new ImageIcon("whitePawn.jpeg"));
    I cannot produce an image on my JButton child class. I thought it might be the placement of my jpeg files. I have them in my src folder along with the class files.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Currently your program is looking in the user directory to try to find the image files. To see where this is, place this line in your code:

    Java Code:
    System.out.println(System.getProperty("user.dir"));
    Are you using packages? If not, you should be. If your images are with the class files, then you can probably get them as resources. I'd give my classes responsible for setting the icons some private ImageIcon variables for each chess piece, that way you don't have to re-load the images each time an image is needed:

    e.g.,
    Java Code:
    class ChessGui  {
       private ImageIcon whitePawnIcon;
       private ImageIcon whiteBishopIcon;
       private ImageIcon whiteKnightIcon;
       // ... etc ...

    and then load the icons in the class's constructor, again using resources,

    Java Code:
       public ChessGui () {
          try {
             BufferedImage whitePawnImage = ImageIO.read(getClass().getResourceAsStream("whitePawn.jpg"));
             whitePawnIcon = new ImageIcon(whitePawnImage);
             BufferedImage whiteBishopImage = ImageIO.read(getClass().getResourceAsStream("whiteBishop.jpg"));
             whiteBishopIcon = new ImageIcon(whiteBishopImage);
             BufferedImage whiteKnightImage = ImageIO.read(getClass().getResourceAsStream("whiteKnight.jpg"));
             whiteKnightIcon = new ImageIcon(whiteKnightImage);
             //... etc ...
          } catch (IOException e1) {
             e1.printStackTrace();
          }

Similar Threads

  1. Adding JButton to JFrame with background
    By bzknight in forum AWT / Swing
    Replies: 1
    Last Post: 01-19-2011, 07:55 PM
  2. adding JList and JButton to a TabbedPane
    By hariza in forum AWT / Swing
    Replies: 13
    Last Post: 10-06-2010, 02:46 AM
  3. Adding JButton to a JTable
    By ting.at.net@hotmail.com in forum AWT / Swing
    Replies: 6
    Last Post: 05-26-2009, 04:37 AM
  4. adding Image to JButton
    By mayhewj7 in forum New To Java
    Replies: 3
    Last Post: 03-31-2009, 04:39 AM
  5. Problem on adding JButton on JPanel NEED HELP
    By boisk in forum AWT / Swing
    Replies: 15
    Last Post: 03-15-2009, 03:27 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
  •