Results 1 to 4 of 4
Thread: Adding JPEG to JButton Child
- 03-27-2011, 07:26 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 2
- Rep Power
- 0
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 07:28 PM. Reason: code tags added
-
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 [code] above your pasted code and the tag [/code] below your pasted code like so:
Best of luck and welcome to the java-forums!!!Java Code:[code] // your code goes here // notice how the top and bottom tags are different [/code]
- 03-27-2011, 08:22 PM #3
Member
- Join Date
- Mar 2011
- Posts
- 2
- Rep Power
- 0
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
withJava Code:someButton.setText();
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.Java Code:someButton.setIcon(new ImageIcon("whitePawn.jpeg"));
-
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:
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:Java Code:System.out.println(System.getProperty("user.dir"));
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
-
Adding JButton to JFrame with background
By bzknight in forum AWT / SwingReplies: 1Last Post: 01-19-2011, 06:55 PM -
adding JList and JButton to a TabbedPane
By hariza in forum AWT / SwingReplies: 13Last Post: 10-06-2010, 01:46 AM -
Adding JButton to a JTable
By ting.at.net@hotmail.com in forum AWT / SwingReplies: 6Last Post: 05-26-2009, 03:37 AM -
adding Image to JButton
By mayhewj7 in forum New To JavaReplies: 3Last Post: 03-31-2009, 03:39 AM -
Problem on adding JButton on JPanel NEED HELP
By boisk in forum AWT / SwingReplies: 15Last Post: 03-15-2009, 02:27 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks