Java For Loop with ImageIcon - java

Can anyone help me solve this?
imagePanel = new JPanel();
label = new JLabel();
for (int i = 0; i < 9; i++)
{
label.setIcon(image[i]);
imagePanel.add(label);
}
When using this it gives me the errors:
Exception in thread "main" java.lang.NullPointerException...
at java.awt.Container.addImpl(Unknown Source)
at java.awt.Container.add(Unknown Source)
at javax.swing.JFrame.addImpl(Unknown Source)
at java.awt.Container.add(Unknown Source)
at assignment.Furniture_System.<init>(Furniture_System.java:55)
at assignment.Furniture_System.main(Furniture_System.java:113)
I suppose I should explain that I call this for loop in a void function that implements in a class constructor. That constructor is called in the main program.
And the variable array image is shown here:
private ImageIcon image[] =
{
new ImageIcon ("C:\\Users\\James\\Pictures\\Java\\1.jpg"),
new ImageIcon ("C:\\Users\\James\\Pictures\\Java\\2.jpg"),
new ImageIcon ("C:\\Users\\James\\Pictures\\Java\\3.jpg"),
new ImageIcon ("C:\\Users\\James\\Pictures\\Java\\4.jpg"),
new ImageIcon ("C:\\Users\\James\\Pictures\\Java\\5.jpg"),
new ImageIcon ("C:\\Users\\James\\Pictures\\Java\\6.jpg"),
new ImageIcon ("C:\\Users\\James\\Pictures\\Java\\7.jpg"),
new ImageIcon ("C:\\Users\\James\\Pictures\\Java\\8.jpg"),
new ImageIcon ("C:\\Users\\James\\Pictures\\Java\\9.jpg"),
new ImageIcon ("C:\\Users\\James\\Pictures\\Java\\10.jpg"),
};
All I am trying to do is cycle through the array, set and display the images as the cycle goes through.
Below is the whole program to make it easier to fix:
private ImageIcon image[] =
{
new ImageIcon ("C:\\Users\\James\\Pictures\\Java\\1.jpg"),
new ImageIcon ("C:\\Users\\James\\Pictures\\Java\\2.jpg"),
new ImageIcon ("C:\\Users\\James\\Pictures\\Java\\3.jpg"),
new ImageIcon ("C:\\Users\\James\\Pictures\\Java\\4.jpg"),
new ImageIcon ("C:\\Users\\James\\Pictures\\Java\\5.jpg"),
new ImageIcon ("C:\\Users\\James\\Pictures\\Java\\6.jpg"),
new ImageIcon ("C:\\Users\\James\\Pictures\\Java\\7.jpg"),
new ImageIcon ("C:\\Users\\James\\Pictures\\Java\\8.jpg"),
new ImageIcon ("C:\\Users\\James\\Pictures\\Java\\9.jpg"),
new ImageIcon ("C:\\Users\\James\\Pictures\\Java\\10.jpg")
};
private JLabel label;
private JLabel sofaImageLabel;
private JLabel armchairImageLabel;
private JLabel cDeskImageLabel;
private JLabel cTableImageLabel;
private JLabel tvStandImageLabel;
private JLabel cushionImageLabel;
private JLabel bedImageLabel;
private JLabel mattressImageLabel;
private JLabel duvetImageLabel;
private JLabel pillowImageLabel;
private JPanel buttonPanel;
private JPanel imagePanel;
private JButton button;
public Furniture_System()
{
setTitle("Furniture Management System");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
buildImagePanel();
// buildButtonPanel();
add(imagePanel, BorderLayout.WEST);
add(buttonPanel, BorderLayout.SOUTH);
pack();
setVisible(true);
}
private void buildImagePanel()
{
imagePanel = new JPanel();
for (int i = 0; i < 9; i++)
{
label = new JLabel();
label.setIcon(image[i]);
imagePanel.add(label);
}
}
private void buildButtonPanel()
{
buttonPanel = new JPanel();
button = new JButton("Get Image");
button.addActionListener(new chooseFileButton());
buttonPanel.add(button);
}
private class chooseFileButton implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
/* String filename = null;
JFileChooser fileChosen = new JFileChooser();
int status = fileChosen.showOpenDialog(null);
if (status == JFileChooser.APPROVE_OPTION)
{
File selectedFile = fileChosen.getSelectedFile();
filename = selectedFile.getPath();
JOptionPane.showMessageDialog(null, "You selected " + filename);
}
ImageIcon image = new ImageIcon(filename);
sofaImageLabel.setIcon(image);
sofaImageLabel.setText(null);
pack(); */
}
}
public static void main(String[] args)
{
new Furniture_System();
}
}

Any component can have only one parent container, and not be reused.
So the code must be:
for (ImageIcon img : image)
{
JLabel label = new JLabel();
label.setIcon(img);
imagePanel.add(label);
}
Though I would look in Furniture_System.java at line 55 (in the constructor) what could be an access to null. Like above maybe imagePanel being null.

Related

Changing image when button pressed - Java

The gui has five buttons, and a default image. I would like the image to change when each of the buttons is pressed. I have tried this for 'pink' by updating the path but I am at loss as to what needs to be done each time.
public class PikminGui extends JFrame{
private JPanel buttons;
private JLabel title;
private JButton grow;
private JButton pink;
private JButton black;
private JButton blue;
private JButton red;
private JButton yellow;
private JFrame f;
private JLabel label;
private JPanel panel;
public String path;
public PikminGui() {
createGui();
}
public void createGui() {
path = "path-to-image1";
f = new JFrame();
panel = new JPanel(new BorderLayout());
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLocationRelativeTo(null);
f.setLocation(200,200);
f.setVisible(true);
title = new JLabel("PIKMIN MAKER", JLabel.CENTER);
panel.add(title,BorderLayout.NORTH);
grow = new JButton("Grow");
grow.setBorder(BorderFactory.createRaisedBevelBorder());
panel.add(grow,BorderLayout.WEST);
buttons = new JPanel();
pink = new JButton("Pink");
pink.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
path = "path-to-image2";
}
});
black = new JButton("Black");
yellow = new JButton("Yellow");
blue = new JButton("Blue");
red = new JButton("Red");
buttons.setLayout(new FlowLayout());
buttons.add(pink);
buttons.add(black);
buttons.add(yellow);
buttons.add(blue);
buttons.add(red);
panel.add(buttons,BorderLayout.SOUTH);
File file = new File(path);
BufferedImage image;
try {
image = ImageIO.read(file);
label = new JLabel(new ImageIcon(image));
panel.add(label, BorderLayout.CENTER );
f.getContentPane().add(panel);
f.pack();
f.add(panel);
} catch (IOException e) {
e.printStackTrace();
}
}
}

Trouble with switching different URL's in order to switch a GIF animation

I'm creating a StreetFighter kind of game, and I'm having trouble changing the GIF animations when the game is won. I changed the URL when the JProgressBar is equal to 0, but it does nothing. If anyone can help fix this problem, thanks in advance.
public class Player1 {
static JButton playerPunch = new JButton("Punch");
static JButton playerKick = new JButton("Kick");
static JButton playerSpecial = new JButton("Special Attack");
static JButton playerNothing = new JButton("Do Nothing");
public static void runAll(){
PlayerPunch();
PlayerKick();
PlayerSpecial();
PlayerNothing();
endGame();
}
public static void endGame(){
if(HealthBar.PlayerHealth.getValue() == 0){
}
if(HealthBar.EnemyHealth.getValue() == 0){
Game.gifString = ("http://media.tumblr.com/tumblr_lgpely0wDJ1qg8wsi.gif");
}
}
and here's the 2nd part of another class including the URL itself.
static String gifString = new String("http://cdn.gifbay.com/2012/10/mortal_kombat_prom-3939.gif");
static JPanel playerPanel = new JPanel();
static JButton start = new JButton("Start");
static JButton exit = new JButton("Exit");
static JPanel menu = new Menu();
static JPanel bg = new JPanel();
static int state = 0;
public static void main(String[] args) throws MalformedURLException {
JPanel buttonPane = new JPanel();
final JPanel masterPanel = new JPanel(new BorderLayout());
final JFrame f = new JFrame("Mortal Kombat Game");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
URL url = new URL(gifString);
Icon icon = new ImageIcon(url);
final JLabel label = new JLabel(icon);
final JLabel title = new JLabel("Mortal Kombat Game");
f.pack();
f.setVisible(true);
f.setSize(498,400);
f.setLocationRelativeTo(null);

How to set JLabel in a specific area in JFrame?

I am trying to make a desktop application in Java Swing. I am trying to create image slider in frame and I got it. Now problem in that I want to set the specific area for imagelabel in that frame. How can I do this? I want to set imagelabel in left side. I am posting my snapshot which I am getting after running my program.
Here is my code
public class ImageSlider extends JPanel implements ActionListener {
private static final int MAX = 20;
private static final Font sans = new Font("SansSerif", Font.PLAIN, 16);
private static final Border border =
BorderFactory.createMatteBorder(4, 16, 4, 16, Color.BLUE);
private List<String> list = new ArrayList<String>(MAX);
private List<ImageIcon> cache = new ArrayList<ImageIcon>(MAX);
private JLabel imageLabel = new JLabel();
//label = new JLabel( image, SwingConstants.CENTER);
private JButton prevButton = new JButton();
private JButton nextButton = new JButton();
private JComboBox favorites;
public ImageSlider() {
this.setLayout(new BorderLayout());
list.add("c.jpg");
list.add("a0.png");
list.add("yellow.png");
list.add("a0.png");
list.add("c.jpg");
for (int i = 0; i < list.size(); i++) cache.add(i, null);
ImageIcon image = new ImageIcon("E:\\SOFTWARE\\TrainPIS\\res\\drawable\\a0.png");
JLabel titleLabel = new JLabel(image, SwingConstants.CENTER);
// titleLabel.setText("ImageSlider");
titleLabel.setHorizontalAlignment(JLabel.CENTER);
titleLabel.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 24));
titleLabel.setBorder(border);
this.add(titleLabel, BorderLayout.NORTH);
imageLabel.setIcon(getImage(0));
imageLabel.setAlignmentX(LEFT_ALIGNMENT);
imageLabel.setHorizontalAlignment(JLabel.CENTER);
imageLabel.setBorder(border);
this.add(imageLabel, BorderLayout.CENTER);
favorites = new JComboBox(
list.toArray(new String[list.size()]));
favorites.setActionCommand("favs");
favorites.addActionListener(this);
prevButton.setText("\u22b2Prev");
prevButton.setFont(sans);
prevButton.setActionCommand("prev");
prevButton.addActionListener(this);
nextButton.setText("Next\u22b3");
nextButton.setFont(sans);
nextButton.setActionCommand("next");
nextButton.addActionListener(this);
JPanel controlPanel = new JPanel();
controlPanel.add(prevButton);
controlPanel.add(favorites);
controlPanel.add(nextButton);
controlPanel.setBorder(border);
this.add(controlPanel, BorderLayout.SOUTH);
}
public void actionPerformed(ActionEvent ae) {
String cmd = ae.getActionCommand();
if ("favs".equals(cmd)) {
int index = favorites.getSelectedIndex();
ImageIcon image = getImage(index);
imageLabel.setIcon(image);
if (image != null) imageLabel.setText("");
else imageLabel.setText("Image not available.");
}
if ("prev".equals(cmd)) {
int index = favorites.getSelectedIndex() - 1;
if (index < 0) index = list.size() - 1;
favorites.setSelectedIndex(index);
}
if ("next".equals(cmd)) {
int index = favorites.getSelectedIndex() + 1;
if (index > list.size() - 1) index = 0;
favorites.setSelectedIndex(index);
}
}
public JButton getDefault() { return nextButton; }
// Return the (possibly cached) image having the given index.
private ImageIcon getImage(int index) {
ImageIcon image = cache.get(index);
if (image != null) return image;
String name = "/images/" + list.get(index);
URL url = ImageSlider.class.getResource(name);
if (url != null) {
image = new ImageIcon(url);
}
cache.set(index, image);
return image;
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ImageSlider go = new ImageSlider();
frame.add(go);
frame.setTitle("ImageSlider");
// frame.setSize(400, 300);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setUndecorated(true);
frame.setVisible(true);
go.getDefault().requestFocusInWindow();
}
});
}
}
How can I achieve my goal?
The easiest way to achieve this is to put the imageLabel into a JPanel with a FlowLayout. Then add that panel to the bigger BorderLayout.
So change:
this.add(imageLabel, BorderLayout.CENTER);
To something like:
JPanel imageConstrain = new JPanel(new FlowLayout(SwingConstants.LEFT));
imageConstrain.add(imageLabel);
this.add(imageConstrain, BorderLayout.CENTER);

Trouble Updating AbstractTableModel from another Class

I'm trying to update a JTable that pulls in data from an ArrayList. I have two frames in my program. The first frame is a JTable (AbstractTableModel) that displays the contents of the ArrayList. I click the "New" button on that frame to bring up the second window, which lets me add to the aforementioned ArrayList. When I click my "Save" button, the second window closes and the first is supposed to refresh with the new row. I don't have any syntactical errors in my code, and it looks conceptually right. I think the first place to start troubleshooting would be in the NoteCntl class. I'm under the impression that getNoteTableUI() should update the view with the new data when it's called, but I'm stumped as to what's going on. I'm new to the concept of Model View Controller, but I'd like to follow that as closely as possible.
Here is the Controller class:
public class NoteCntl {
private NoteTableModel theNoteTableModel = new NoteTableModel();;
private NoteTableUI theNoteTableUI;
private NoteDetailUI theNoteDetailUI;
public NoteCntl(){
theNoteTableUI = new NoteTableUI(this);
}
public NoteTableModel getNoteTableModel(){
return theNoteTableModel;
}
public void getNoteDetailUI(Note theNote){
if (theNoteDetailUI == null || theNote == null){
theNoteDetailUI = new NoteDetailUI(this,theNote);
}
else{
theNoteDetailUI.setVisible(true);
}
}
public NoteTableUI getNoteTableUI(){
theNoteTableModel.fireTableDataChanged(); //why doesn't this do anything?
theNoteTableUI.setVisible(true);
return theNoteTableUI;
}
public void deleteNote(int noteToDelete){
theNoteTableModel.removeRow(noteToDelete);
}
}
The First UI (Table):
public class NoteTableUI extends JFrame{
NoteTableModel noteModel;
NoteCntl theNoteCntl;
JPanel buttonPanel;
JPanel tablePanel;
JTable theNoteTable;
JScrollPane theScrollPane;
JButton backButton;
JButton deleteButton;
JButton editButton;
JButton newButton;
public NoteTableUI(NoteCntl theParentNoteCntl){
theNoteCntl = theParentNoteCntl;
this.initComponents();
this.setSize(400, 500);
this.setLocationRelativeTo(null);
this.setTitle("NoteTableUI");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public void initComponents(){
buttonPanel = new JPanel();
tablePanel = new JPanel();
backButton = new JButton("Back");
newButton = new JButton("New");
newButton.addActionListener(new newButtonListener());
editButton = new JButton("Edit");
deleteButton = new JButton("Delete");
deleteButton.addActionListener(new deleteButtonListener());
noteModel = theNoteCntl.getNoteTableModel();
theNoteTable = new JTable(theNoteCntl.getNoteTableModel());
theScrollPane = new JScrollPane(theNoteTable);
theNoteTable.setFillsViewportHeight(true);
tablePanel.add(theScrollPane);
buttonPanel.add(backButton);
buttonPanel.add(deleteButton);
buttonPanel.add(editButton);
buttonPanel.add(newButton);
this.getContentPane().add(buttonPanel, BorderLayout.NORTH);
this.getContentPane().add(tablePanel, BorderLayout.CENTER);
}
public class deleteButtonListener implements ActionListener{
public void actionPerformed(ActionEvent event){
int selectedRow = theNoteTable.getSelectedRow();
if (selectedRow == -1){
System.out.println("No row selected");
}
else{
noteModel.removeRow(selectedRow);
}
revalidate();
repaint();
}
}
public class newButtonListener implements ActionListener{
public void actionPerformed(ActionEvent event){
NoteTableUI.this.setVisible(false);
NoteTableUI.this.theNoteCntl.getNoteDetailUI(null);
/*
NoteDetailCntl theNoteDetailCntl = new NoteDetailCntl();
lastRow++;
long newRow = lastRow;
noteModel.addRow(newRow, 0, "", "");
revalidate();
repaint();
*/
}
}
The 2nd UI (Detail editor)
public class NoteDetailUI extends JFrame{
private final int FRAME_WIDTH = 700;
private final int FRAME_HEIGHT = 500;
private final int FIELD_WIDTH = 10;
JButton saveButton;
JButton backButton;
JTextField idField;
JTextField dateField;
JTextField nameField;
JTextField descriptionField;
JTextArea noteDetail;
JLabel idLabel;
JLabel dateLabel;
JLabel nameLabel;
JLabel descriptionLabel;
JPanel buttonPanel;
JPanel textFieldPanel;
JPanel textAreaPanel;
JPanel mainPanel;
NoteTableModel theNoteTableModel;
NoteDetailCntl theNoteDetailCntl;
NoteCntl theNoteCntl;
Note theCurrentNote;
public NoteDetailUI(){
this.initComponents();
this.setSize(FRAME_WIDTH,FRAME_HEIGHT);
this.setLocationRelativeTo(null);
this.setTitle("NoteDetailUI");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public NoteDetailUI(NoteCntl parentNoteCntl, Note theSelectedNote){
theNoteCntl = parentNoteCntl;
theCurrentNote = theSelectedNote;
this.initComponents();
this.setSize(400,500);
this.setLocationRelativeTo(null);
this.setTitle("Note");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public void initComponents(){
saveButton = new JButton("Save");
saveButton.addActionListener(new saveButtonListener());
backButton = new JButton("Back");
backButton.addActionListener(new backButtonListener());
idField = new JTextField(FIELD_WIDTH);
theNoteTableModel = new NoteTableModel();
idField.setText("10");
idField.setEditable(false);
dateField = new JTextField(FIELD_WIDTH);
dateField.setText("20131108");
nameField = new JTextField(FIELD_WIDTH);
nameField.setText("Untitled");
descriptionField = new JTextField(FIELD_WIDTH);
descriptionField.setText("not described");
idLabel = new JLabel("ID");
dateLabel = new JLabel("Date");
nameLabel = new JLabel("Name");
descriptionLabel = new JLabel("Description");
noteDetail = new JTextArea(25,60);
buttonPanel = new JPanel();
textFieldPanel = new JPanel();
textAreaPanel = new JPanel();
mainPanel = new JPanel(new BorderLayout());
buttonPanel.add(backButton);
buttonPanel.add(saveButton);
textFieldPanel.add(idLabel);
textFieldPanel.add(idField);
textFieldPanel.add(dateLabel);
textFieldPanel.add(dateField);
textFieldPanel.add(nameLabel);
textFieldPanel.add(nameField);
textFieldPanel.add(descriptionLabel);
textFieldPanel.add(descriptionField);
textAreaPanel.add(noteDetail);
mainPanel.add(buttonPanel, BorderLayout.SOUTH);
mainPanel.add(textFieldPanel, BorderLayout.NORTH);
mainPanel.add(textAreaPanel, BorderLayout.CENTER);
add(mainPanel);
}
public ArrayList<String> getNoteDetails(){
ArrayList<String> newData = new ArrayList<String>();
newData.add(idField.getText());
newData.add(dateField.getText());
newData.add(nameField.getText());
newData.add(descriptionField.getText());
return newData;
}
public class saveButtonListener implements ActionListener{
public void actionPerformed(ActionEvent event){
/*
* Access the noteTableData array in NoteTableModel
* Add the newData fields in order
*/
if(theCurrentNote == null){
int newNoteNumber = Integer.parseInt(NoteDetailUI.this.idField.getText());
int newNoteDate = Integer.parseInt(NoteDetailUI.this.dateField.getText());
String newNoteName = NoteDetailUI.this.nameField.getText();
String newNoteDescription = NoteDetailUI.this.descriptionField.getText();
NoteDetailUI.this.theCurrentNote = new EssayNote(newNoteNumber,newNoteDate,newNoteName,newNoteDescription);
NoteDetailUI.this.setVisible(false);
NoteDetailUI.this.dispose();
NoteDetailUI.this.theNoteCntl.getNoteTableUI();
}
else{
//if it's a current Note
}
//Refresh the JTable
}
}
public class backButtonListener implements ActionListener{
public void actionPerformed(ActionEvent event){
}
}
Thanks for all the help. I can provide the other classes if you want to just run the program and see what's happening, but I suspect it's either a problem with the fireTableDataChanged() call in the controller class or a problem with updating the contents of the ArrayList in the SaveButtonListener.

Resize JPanel to fit new JLabel icon

I'm trying to create an animation (spinning text) by repeatedly changing the icon on a JLabel. The issue is the images are not of the same size, and when they are bigger than the size of the first image, they are clipped.
One way around this is to setPreferredSize for the JLabel so that all images fit - but I imagine there must be a way dinamically resize the JPanel containing the JLabel?
In the code bellow I've also tried removing the JLabel alltogether, creating a new one and then adding the new one, but to the same effect.
public class AnimationPanelv2 extends JPanel{
private JButton start = new JButton("Start Animation");
private JLabel img = new JLabel();
private JTextField animSpeed = new JTextField(10);
private JTextField filePrefix = new JTextField(10);
private JTextField noOfImg = new JTextField(10);
private JTextField audioFile = new JTextField(10);
private Timer timer;
private AudioClip clip;
private ArrayList<ImageIcon> icon = new ArrayList<>();
private int step=0;
public AnimationPanelv2() {
//button is for starting the animation
start.addActionListener(new Animatie());
this.setLayout(new BorderLayout());
add(start, BorderLayout.NORTH);
//showing the label with the first frame
Class metaObj = this.getClass();
URL url = metaObj.getResource("/image/L1.gif");
img.setIcon(new ImageIcon(url));
// img.setPreferredSize(new Dimension(500,550));
add(img, BorderLayout.CENTER);
//control panel
JPanel controls = new JPanel(new GridLayout(4,2));
controls.setBorder(new TitledBorder("Enter information for animation"));
controls.add(new JLabel("Animation speed in ms"));
controls.add(animSpeed);
controls.add(new JLabel("Image file prefix"));
controls.add(filePrefix);
controls.add(new JLabel("Number of images"));
controls.add(noOfImg);
controls.add(new JLabel("Audio file"));
controls.add(audioFile);
//
add(controls, BorderLayout.SOUTH);
}
private class TimerAnimation implements ActionListener {
public void actionPerformed(ActionEvent e) {
remove(img);
img = new JLabel(icon.get(step++));
img.setVisible(true);
add(img, BorderLayout.CENTER);
// img.revalidate();
// img.repaint();
validate();
repaint();
updateUI();
if (step==Integer.parseInt(noOfImg.getText())) step=0;
}
}
private class Animatie implements ActionListener {
public void actionPerformed(ActionEvent e) {
//getting data from the text fields
int ms = Integer.parseInt(animSpeed.getText());
String s = filePrefix.getText();
int nr = Integer.parseInt(noOfImg.getText());
String audioFilePath = audioFile.getText();
// clip
Class metaObj = this.getClass();
URL url = metaObj.getResource("/audio/"+audioFilePath);
clip = Applet.newAudioClip(url);
//image loading
for (int i=1; i<=nr; i++){
url = metaObj.getResource("/image/"+s+i+".gif");
System.out.println("/image/"+s+i+".gif");
icon.add(new ImageIcon(url));
}
//timer
timer = new Timer(ms, new TimerAnimation());
timer.start();
clip.loop();
}
}
public static void main(String[] args) {
JFrame jf = new JFrame("This class test");
jf.setLocationRelativeTo(null);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.add(new AnimationPanelv2());
jf.pack();
jf.setVisible(true);
}
}
This whole panel will be used in an applet.
This is a screenshot: http://screencast.com/t/UmqQFZHJVy
The images that are supposed to be the frames, should be located in an /images/ sub-directory and if the user enters n for the number of frames and F for the image
prefix, then the files are F1, F2, and so on, to Fn (GIFs). The sound file should be in an /audio/ sub-directory, and the entire file name is given by the user.
You can try to create list of JLabels for each image, add them to a panel with CardLayout and swap cards.
Okay well a JLabel should size automatically to its given content, so to solve JPanel issue, simply override getPreferredSize() of JPanel containing the JLabel and return Dimensions according to the JLabel size.
public class MyPanel extends JPanel {
JLabel label=...;
#Override
public Dimension getPreferredSize() {
return new Dimension(label.getWidth(),label.getHeight());
}
}
also dont forget when you change Icon of JLabel call revalidate() and repaint() on JPanel instance in order for size changes to refelect.

Categories