help drawing .gifs
I was wondering if someone would be so kind as to explain how to draw multiple .gifs, each one being offset by the width of the previous so they do not overlap? I know I have to use an ImageIcon, and add it to the JFrame.. but beyond that I am not sure. I got it to display one image, but if I use BorderLayout.CENTER for example, it will display them all in the same place, and I only end up seeing the last one. Any help is greatly appreciated.
dub stylee
This looks like a continuation of your other thread. If so, please put a message there
telling people to come here to post comments. Otherwise there will be a lot of
duplication, confusion, and unhappiness.
You say you got one image to display (so that's an advance on the other thread). In
order to display all 52 images
(1) Create a JPanel and give it a GridLayout - as you noticed a BorderLayout is not much use.
(2) Create JLabels from the ImageIcons and add them to the panel.
(3) Unless your images are really small wrap the panel in a JScrollPane
(4) Add the JScrollPane to the frame.
A couple of links that might be useful -
Sun's Tutorial contains a section on Swing
http://java.sun.com/docs/books/tutorial/uiswing/index.html
There's a lot of material here, and you will have to skip about a bit not always
understanding everything completely the first time you read it. But pay attention to
the parts that deal with JLabel (as a way of displaying an ImageIcon) and "Laying
out components within a container" which directly addresses your problem.
The Swing forum here http://forum.java.sun.com/forum.jspa?forumID=57
The best advice on Swing is to be obtained there. Posting some code is always a
good way of posing a question precisely. Not the whole thing - just a small example
of trying to display 52 images in a JFrame: probably about 20 lines.
Thanks a lot for your help. Changing it to a GridLayout rather than a BorderLayout fixed my problem. I had a BorderLayout for the original setup of the JFrame from some previous code I already had. I have been poking around the Swing tutorials, but I will look at the other forum you directed me to. Thanks again for your help.
dub stylee
Ok, I have one more question regarding this issue. I added a new method to the Deck class that will create the contentPane and display the images. But since main() is static, it doesn't want me to call createContentPane(). I could solve this by making everything static, but I know that is not the best way to accomplish this. Any suggestions?
public Container createContentPane() {
Deck deck = new Deck();
ImageIcon thisCardImage;
JLabel thisCard;
JPanel contentPane = new JPanel(new GridLayout());
contentPane.setOpaque(true);
for (int i = 0; i < 52; i++) {
thisCardImage = new ImageIcon(deck.cards + ".gif");
thisCard = new JLabel(thisCardImage);
contentPane.add(thisCard);
}
return contentPane;
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createContentPane();
}
});
}
Create an object of your class and do all the processing on that. Don't make anything static:public void run() {
new WhateverYourClassIsCalled().createContentPane();
}
Thanks, also, I was wondering how to make the layout wrap. The current setup just displays 52 cards all in a row. I would like it to wrap at the edge of a fixed-size window.dub stylee
Isn't there a way to tell the GridLayout that you want 4 rows and 13 columns? I'm sure there must be but since you're the one that needs to know, I will let you check out the API documentation.
Excellent. Sorry if this was a ridiculous question, but I have been searching the forums and the Swing tutorials without finding anything that would help me figure out my problem. Thanks again for your help, I now have a deck of cards that can be displayed using text or graphics! :) Now for the fun part of actually making the cards function.
dub stylee
So far, so good!But in future please remember that it is better to post Swing questions in the Swing forum.