Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Random "Slide Show-type" images

Status
Not open for further replies.

asiavoices

Programmer
Sep 20, 2001
90
CA
Hello all,

I need your help on this one. I've built a very simple application to show new images when a user clicks on a button (like a slide show).

Basically, when a user click on a button it will randomly give you the picture of playing card (Suit and Face)

The images are not stored in array but rather in a directory.

My problem is that when I click on the button, it does not show the random image. No compilation errors where encountered and to check, I've built a small routine to show the results (in text only) and it works. So, its just the images that are not showing up ....

Here's part of my code.



====================================


public class Playcards extends JFrame
implements ActionListener
{
.....

public Playcards()
{
...........
Container contentPane = getContentPane();
...........

hitButton = new JButton("HIT");
hitButton.addActionListener(this);
....................

JPanel buttonPanel = new JPanel();
buttonPanel.setBackground(new Color(204,255,204));
buttonPanel.setLayout(new GridLayout(1,6));

.............

imagePanel = new JPanel();
imagePanel.setLayout(new CardLayout(10,10));

imagePanel.add(new JButton(new ImageIcon("../../images/testimage.gif")), "test image");

...........

contentPane.setLayout(new BorderLayout());
contentPane.add(buttonPanel, BorderLayout.NORTH);
contentPane.add(imagePanel, BorderLayout.CENTER);

}


public void actionPerformed(ActionEvent buttonEvent)
{
Object buttonClicked = buttonEvent.getSource();

if (buttonClicked == hitButton)
{
imagePanel.add(new JButton(new ImageIcon("../../images/newImage.gif")), "New Test image");
validate();
repaint();

......

}

/* ---- This is my main() method ------- */

public static void main (String[] arg)
{

Playcards myApp = new Playcards();
myApp.pack();
myApp.setVisible(true);

}


}



Any ideas why I can't seem to have the picture show up?

Thanks,

Christopher




 
Hello all,

I figured out a way to have this work now just to let everyone know and to share it with all of you. Silly me!

Possible solution:

.....

icon = new ImageIcon("../images/testimage.gif");

imagePanel = new JLabel(icon);
imagePanel.setBackground(new Color(204,255,204));
imagePanel.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
imagePanel.setLayout(new CardLayout(0,0));

.....

public void actionPerformed(ActionEvent e)
{
....

if (e == hitButton)
{

ImageIcon newIcon = new ImageIcon("../images/newimage.gif");
imagePanel.setIcon(newIcon);
validate();
repaint();
....

}

Thank you to all those that have looked into my problem and try to come up with a solution.

Christopher


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top