Displaying Images On jButtons

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 10487 Views - Last Post: 10 December 2008 - 01:30 AM Rate Topic: -----

#1 mbarc44  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 30-November 08

Displaying Images On jButtons

Posted 30 November 2008 - 07:32 PM

hello, i'm working on 5x5 slide puzzle game and i'm having trouble displaying each image on each jbutton.

EDIT

ImageIcon images [] = {
					  new ImageIcon("Pictures/Img.0.0.JPG"),
					  new ImageIcon("Pictures/Img.0.1.JPG"),
					  new ImageIcon("Pictures/Img.0.2.JPG"),
					  new ImageIcon("Pictures/Img.0.3.JPG"),
					  new ImageIcon("Pictures/Img.0.4.JPG"),
					  .
					  .
					  new ImageIcon("Pictures/Img.4.4.JPG")};										  


for (int r = 0; r<5; r++){
			for (int c=0; c<5; c++){
				   JButton pic = new JButton(images[0]);
				   pic.addActionListener (this);			
				   one[r][c] = pic;			
				   t.add(pic);}


with this code only one image displays on every button.

This post has been edited by mbarc44: 01 December 2008 - 09:55 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Displaying Images On jButtons

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Displaying Images On jButtons

Posted 30 November 2008 - 07:41 PM

View Postmbarc44, on 30 Nov, 2008 - 06:32 PM, said:

hello, i'm working on 5x5 slide puzzle game and i'm having trouble displaying each image on each jbutton.so far i have created a grid and added the buttons(2d array of jbuttons).

ImageIcon images [] = {
					  new ImageIcon("Pictures/Img.0.0.JPG"),
					  new ImageIcon("Pictures/Img.0.1.JPG"),
					  new ImageIcon("Pictures/Img.0.2.JPG"),
					  new ImageIcon("Pictures/Img.0.3.JPG"),
					  new ImageIcon("Pictures/Img.0.4.JPG"),
					  .
					  .
					  new ImageIcon("Pictures/Img.4.4.JPG")};										  


 for (int r = 0; r<5; r++){
			for (int c=0; c<5; c++){ 
				   JButton pic = new JButton(images[0]);
				   pic.addActionListener (this);			
				   one[r][c] = pic;			
				   t.add(pic);} 


with this code only one image displays on every button.
any help would be appreciated.

you have a pic.setIcon(.....) missing
Was This Post Helpful? 0
  • +
  • -

#3 mbarc44  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 30-November 08

Re: Displaying Images On jButtons

Posted 30 November 2008 - 08:29 PM

I have tried pic.setIcon(.....) before and it didn't work.Every button had the same image like now.

Thank you for your reply.

This post has been edited by mbarc44: 30 November 2008 - 08:30 PM

Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Displaying Images On jButtons

Posted 30 November 2008 - 08:37 PM

View Postmbarc44, on 30 Nov, 2008 - 07:29 PM, said:

I have tried pic.setIcon(.....) before and it didn't work.Every button had the same image like now.

so yur code is wrong somewhere... post it
Was This Post Helpful? 0
  • +
  • -

#5 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • Joined: 14-September 07

Re: Displaying Images On jButtons

Posted 30 November 2008 - 09:02 PM

unless I'm mistaken the problem is here:

JButton pic = new JButton(images[0]);



You create the same picture each time. Always index 0 it looks like.

This post has been edited by KYA: 30 November 2008 - 09:02 PM

Was This Post Helpful? 0
  • +
  • -

#6 mbarc44  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 30-November 08

Re: Displaying Images On jButtons

Posted 30 November 2008 - 09:06 PM

View PostKYA, on 30 Nov, 2008 - 08:02 PM, said:

unless I'm mistaken the problem is here:

JButton pic = new JButton(images[0]);



You create the same picture each time. Always index 0 it looks like.

Yeah thats my problem.I'm kinda having hard time figuring out how to change that.
Was This Post Helpful? 0
  • +
  • -

#7 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • Joined: 14-September 07

Re: Displaying Images On jButtons

Posted 30 November 2008 - 09:25 PM

Use 'r' or 'c' to change it per row or column.
Was This Post Helpful? 0
  • +
  • -

#8 mbarc44  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 30-November 08

Re: Displaying Images On jButtons

Posted 01 December 2008 - 09:08 AM

I also tried that before.Its slight improvement.Only five images appeared.

Thanks.

This post has been edited by mbarc44: 01 December 2008 - 09:10 AM

Was This Post Helpful? 0
  • +
  • -

#9 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • Joined: 14-September 07

Re: Displaying Images On jButtons

Posted 01 December 2008 - 09:28 AM

Out of how many? Let's say we need the same picture for each row [the inverse applies equally well for columns]

We then use the value of 'r' to pick the index of the picture. That way it will not change until the outer for loop goes again.
Was This Post Helpful? 1
  • +
  • -

#10 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1939
  • View blog
  • Posts: 4,027
  • Joined: 11-December 07

Re: Displaying Images On jButtons

Posted 01 December 2008 - 09:37 AM

The easiest way round the problem is to store the images in a 5x5 array just like your buttons. Then use images[r][c] to retrieve the picture.

If you want to keep it as a 1D array then use images[(r*colCount)+c]. (where colCount is the number of columns - 5 in this case)

The first option is better because it'll make the program more readable and there's less chance of you (or me) getting the maths wrong.
Was This Post Helpful? 1
  • +
  • -

#11 mbarc44  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 30-November 08

Re: Displaying Images On jButtons

Posted 01 December 2008 - 09:49 AM

Thank you very much cfoley.Second option worked.I dont know how i didnt try that.Its so simple.

And thanks KYA.
Was This Post Helpful? 0
  • +
  • -

#12 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1939
  • View blog
  • Posts: 4,027
  • Joined: 11-December 07

Re: Displaying Images On jButtons

Posted 01 December 2008 - 09:51 AM

I guess I got my maths right then. :D

Glad it's sorted.
Was This Post Helpful? 0
  • +
  • -

#13 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • Joined: 14-September 07

Re: Displaying Images On jButtons

Posted 01 December 2008 - 10:05 AM

Posted Image

Maths ftw!
Was This Post Helpful? 0
  • +
  • -

#14 mbarc44  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 30-November 08

Re: Displaying Images On jButtons

Posted 02 December 2008 - 12:42 PM

I have hit another bump. Cant really figure out how to swap each image.
Basicially my program is somewhat supposed to look like this one(only with jbuttons and no animation).


Any ideas would be greatly appreciated.

This post has been edited by mbarc44: 02 December 2008 - 12:45 PM

Was This Post Helpful? 0
  • +
  • -

#15 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • Joined: 14-September 07

Re: Displaying Images On jButtons

Posted 02 December 2008 - 12:50 PM

//some sort of loop to do this
tempImage = imageArr[i];
imageArr[i] = imageYouWantToAssign;
imageYouWantToAssign = tempImage;



??
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2