Link to home
Create AccountLog in
Avatar of saturnian
saturnianFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Connect classes for Applet animation.

Hi have posted a previous message about how to connect classes.I got it working with help.Now a have an animation that needs to be put in motion by clicking in one button.This button is called "PE Content".But there's so many classes that is complicated to me to put them in the correct place.The first part of the code is the simulation and the second part is the animation so I wanna be able to, when the user clicks on  "PE Content" button the animation will take place on other window.
Anyone to help me?
Thanks
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.imageio.ImageIO;
import javax.swing.*;
 
public class SystolicSimulation extends JFrame {
        private Image[] images;
        private ImageIcon icon;
        private int imageIndex;
 
        private void loadImages() {
                try {
                        images = new Image[4];
                         images[0] = new ImageIcon("FirstClockCycleGeneral.GIF").getImage();
                         images[1] = new ImageIcon("SecondClockCycleGeneral.GIF").getImage();
                         images[2] = new ImageIcon("ThirdClockCycleGeneral.GIF").getImage();
                         images[3] = new ImageIcon("FourthClockCycleGeneral.GIF").getImage();
                         
                } catch (Exception e) {
                        e.printStackTrace();
                }
        }
 
      private void setGui() {
		try {
			loadImages();
			setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			icon = new ImageIcon(images[imageIndex]);
			getContentPane()
				.add(new JScrollPane(new JLabel(icon)), BorderLayout.CENTER);
 
			JPanel p = new JPanel();
			JButton b = new JButton(new Simulation("Next image"));
			JButton c = new JButton(new Simulation("Numeric Version"));
			JButton d = new JButton(new Simulation("PE Content"));
			d.addActionListener(new PEShower());
			p.add(b);
			p.add(c);
			p.add(d);
			getContentPane().add(p, BorderLayout.SOUTH);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
 private class PEShower implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			JOptionPane.showMessageDialog(SystolicSimulation.this, new PEImage("PE.jpg"));
		}
	}
        
 public class PEImage extends JPanel {
        JScrollPane scrollPane;
        ImageIcon icon;
 
        public PEImage(String iconPath) {
                icon = new ImageIcon(iconPath);
                setLayout(new BorderLayout());
 
                JPanel panel = new JPanel() {
                                public void paintComponent(Graphics g) {
                                        g.drawImage(icon.getImage(), 0, 0, null);
 
                                        setOpaque(false);
                                        super.paintComponent(g);
                                }
                        };
 
                scrollPane = new JScrollPane(panel);
                add(scrollPane);
                setPreferredSize(new Dimension(icon.getImage().getWidth(this),
                                icon.getImage().getHeight(this)));
        }
 
   
           
 }
 
        public static void main(String[] args) {
                try {
                        SwingUtilities.invokeAndWait(new Runnable() {
                                        public void run() {
                                                SystolicSimulation f = new SystolicSimulation();
                                                f.setGui();
                                                f.pack();
                                                f.setVisible(true);
                                        }
                                });
                } catch (Exception e) {
                        e.printStackTrace();
                }
        }
 
        private class Simulation extends AbstractAction {
                public Simulation(String name) {
                        super(name);
                }
 
                public void actionPerformed(ActionEvent e) {
                        int ix = ++imageIndex % images.length;
                        icon.setImage(images[ix]);
                        validate();
                        repaint();
                }
        }
}
 
 
 
 
 
 
 
import java.awt.*;
import javax.swing.*;
import java.applet.*;
    
    public class SystolicAnimation extends JComponent implements Runnable {
        Image[] images = new Image[4];
        int frame = 0;
    
        public void paint(Graphics g) {
            Image image = images[frame];
            if (image != null) {
                // Draw the current image
                int x = 0;
                int y = 0;
                g.drawImage(image, x, y, this);
            }
        }
    
        public void run() {
            // Load the array of images
            images[0] = new ImageIcon("FirstClockCycle.GIF").getImage();
            images[1] = new ImageIcon("SecondClockCycle.GIF").getImage();
            images[2] = new ImageIcon("ThirdClockCycle.GIF").getImage();
            images[3] = new ImageIcon("FourthClockCycle.GIF").getImage();
    
            // Display each image for 1 second
            int delay = 4000;    // 1 second
    
            try {
                while (true) {
                    // Move to the next image
                    frame = (frame+1)%images.length;
    
                    // Causes the paint() method to be called
                    repaint();
    
                    // Wait
                    Thread.sleep(delay);
                }
            } catch (Exception e) {
            }
        }
    
        public static void main(String[] args) {
            SystolicAnimation app = new SystolicAnimation();
    
            // Display the animation in a frame
            JFrame frame = new JFrame();
            frame.getContentPane().add(app);
            frame.setSize(600, 500);
            frame.setVisible(true);
    
            (new Thread(app)).start();
        }
    }

Open in new window

Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Does the animation actually work?
Avatar of saturnian

ASKER

>>>>>Does the animation actually work?

Yes.Actually is looping with 4 images.Is not a real animation.But is enough for my objectives.I just want to connect the classes.Thanks
I've changed it a bit. Incorporate it exactly as the previous one was done. You'll need to make a new private class. Call it AnimationShower. You really must do more of this yourself ;-)

You can get rid of the main method
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
 
 
public class SystolicAnimation extends JPanel implements ActionListener {
	Image[] images = new Image[4];
	int frame = 0;
 
	// Display each image for 1 second
	int DELAY = 1000; // 1 second
	Timer timer;
 
	public SystolicAnimation() {
		loadImages();
	}
 
	public void start() {
		new Timer(DELAY, this).start();
	}
 
	public void actionPerformed(ActionEvent e) {
		// Move to the next image
		frame = (frame + 1) % images.length;
 
		// Causes the paint() method to be called
		repaint();
	}
 
	public void paintComponent(Graphics g) {
		super.paintComponent(g);
 
		Image image = images[frame];
 
		if (image != null) {
			// Draw the current image
			int x = 0;
			int y = 0;
			g.drawImage(image, x, y, this);
		}
	}
 
	private void loadImages() {
		// Load the array of images
		images[0] = new ImageIcon("FirstClockCycle.GIF").getImage();
		images[1] = new ImageIcon("SecondClockCycle.GIF").getImage();
		images[2] = new ImageIcon("ThirdClockCycle.GIF").getImage();
		images[3] = new ImageIcon("FourthClockCycle.GIF").getImage();
 
		int width = 0;
		int height = 0;
 
		for (int i = 0; i < images.length; i++) {
			width = Math.max(width, images[i].getWidth(this));
			height = Math.max(height, images[i].getHeight(this));
		}
		setPreferredSize(new Dimension(width, height));
	}
 
	public static void main(String[] args) {
		SystolicAnimation app = new SystolicAnimation();
		app.start();
		JOptionPane.showMessageDialog(null, app);
	}
}

Open in new window

>>>>>I've changed it a bit. Incorporate it exactly as the previous one was done. You'll need to make a new private class. Call it AnimationShower.
You can get rid of the main method

Is giving me a error about the "reference to Timer is ambiguous".I Incorporated this code and added a private class but probably doesn't work, speially because that error.

>>>>You really must do more of this yourself ;-)

I know, I have to practice a lot, I'm still a newbie :$
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.imageio.ImageIO;
import javax.swing.*;
 
public class SystolicSimulation extends JFrame {
        private Image[] images;
        private ImageIcon icon;
        private int imageIndex;
 
        private void loadImages() {
                try {
                        images = new Image[4];
                         images[0] = new ImageIcon("FirstClockCycleGeneral.GIF").getImage();
                         images[1] = new ImageIcon("SecondClockCycleGeneral.GIF").getImage();
                         images[2] = new ImageIcon("ThirdClockCycleGeneral.GIF").getImage();
                         images[3] = new ImageIcon("FourthClockCycleGeneral.GIF").getImage();
                         
                } catch (Exception e) {
                        e.printStackTrace();
                }
        }
 
      private void setGui() {
		try {
			loadImages();
			setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			icon = new ImageIcon(images[imageIndex]);
			getContentPane()
				.add(new JScrollPane(new JLabel(icon)), BorderLayout.CENTER);
 
			JPanel p = new JPanel();
			JButton b = new JButton(new Simulation("Next image"));
			JButton c = new JButton(new Simulation("Numeric Version"));
			JButton d = new JButton(new Simulation("PE Content"));
			d.addActionListener(new PEShower());
			p.add(b);
			p.add(c);
			p.add(d);
			getContentPane().add(p, BorderLayout.SOUTH);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
private class PEShower implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			JOptionPane.showMessageDialog(SystolicSimulation.this, new PEImage("PE.jpg"));
		}
	}
public class SystolicAnimation extends JPanel implements ActionListener {
        Image[] images = new Image[4];
        int frame = 0;
 
        // Display each image for 1 second
        int DELAY = 1000; // 1 second
        Timer timer;
 
        public SystolicAnimation() {
                loadImages();
        }
 
        public void start() {
                new Timer(DELAY, this).start();
        }
 
        public void actionPerformed(ActionEvent e) {
                // Move to the next image
                frame = (frame + 1) % images.length;
 
                // Causes the paint() method to be called
                repaint();
        }
 
        public void paintComponent(Graphics g) {
                super.paintComponent(g);
 
                Image image = images[frame];
 
                if (image != null) {
                        // Draw the current image
                        int x = 0;
                        int y = 0;
                        g.drawImage(image, x, y, this);
                }
        }
 
        private void loadImages() {
                // Load the array of images
                images[0] = new ImageIcon("FirstClockCycle.GIF").getImage();
                images[1] = new ImageIcon("SecondClockCycle.GIF").getImage();
                images[2] = new ImageIcon("ThirdClockCycle.GIF").getImage();
                images[3] = new ImageIcon("FourthClockCycle.GIF").getImage();
 
                int width = 0;
                int height = 0;
 
                for (int i = 0; i < images.length; i++) {
                        width = Math.max(width, images[i].getWidth(this));
                        height = Math.max(height, images[i].getHeight(this));
                }
                setPreferredSize(new Dimension(width, height));
        }
 
        public static void main(String[] args) {
                SystolicAnimation app = new SystolicAnimation();
                app.start();
                JOptionPane.showMessageDialog(null, app);
        }
  private class AnimationShower implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			JOptionPane.showMessageDialog(SystolicSimulation.this);
		}
	}
}
 
        
 public class PEImage extends JPanel {
        JScrollPane scrollPane;
        ImageIcon icon;
 
        public PEImage(String iconPath) {
                icon = new ImageIcon(iconPath);
                setLayout(new BorderLayout());
 
                JPanel panel = new JPanel() {
                                public void paintComponent(Graphics g) {
                                        g.drawImage(icon.getImage(), 0, 0, null);
 
                                        setOpaque(false);
                                        super.paintComponent(g);
                                }
                        };
 
                scrollPane = new JScrollPane(panel);
                add(scrollPane);
                setPreferredSize(new Dimension(icon.getImage().getWidth(this),
                                icon.getImage().getHeight(this)));
        }
 
   
           
 }
 
        public static void main(String[] args) {
                try {
                        SwingUtilities.invokeAndWait(new Runnable() {
                                        public void run() {
                                                SystolicSimulation f = new SystolicSimulation();
                                                f.setGui();
                                                f.pack();
                                                f.setVisible(true);
                                        }
                                });
                } catch (Exception e) {
                        e.printStackTrace();
                }
        }
 
        private class Simulation extends AbstractAction {
                public Simulation(String name) {
                        super(name);
                }
 
                public void actionPerformed(ActionEvent e) {
                        int ix = ++imageIndex % images.length;
                        icon.setImage(images[ix]);
                        validate();
                        repaint();
                }
        }
}
 
 
 

Open in new window

Make that

javax.swing.Timer timer;
Sorry I just noticed now.I did a mistake on my question.The button that initializes the animation is not the "PE Content" but the one called "Numerical Version".

Ok I got rid the main method, I changed your improved code and I add the private class called AnimationShower.Compile with no error...great ,but what piece of code should I put to make the button "Numerical Version" to initialize the animation?
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.imageio.ImageIO;
import javax.swing.*;
 
public class SystolicSimulation extends JFrame {
        private Image[] images;
        private ImageIcon icon;
        private int imageIndex;
 
        private void loadImages() {
                try {
                        images = new Image[4];
                         images[0] = new ImageIcon("FirstClockCycleGeneral.GIF").getImage();
                         images[1] = new ImageIcon("SecondClockCycleGeneral.GIF").getImage();
                         images[2] = new ImageIcon("ThirdClockCycleGeneral.GIF").getImage();
                         images[3] = new ImageIcon("FourthClockCycleGeneral.GIF").getImage();
                         
                } catch (Exception e) {
                        e.printStackTrace();
                }
        }
 
      private void setGui() {
		try {
			loadImages();
			setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			icon = new ImageIcon(images[imageIndex]);
			getContentPane()
				.add(new JScrollPane(new JLabel(icon)), BorderLayout.CENTER);
 
			JPanel p = new JPanel();
			JButton b = new JButton(new Simulation("Next image"));
			JButton c = new JButton(new Simulation("Numeric Version"));
			JButton d = new JButton(new Simulation("PE Content"));
			d.addActionListener(new PEShower());
			p.add(b);
			p.add(c);
			p.add(d);
			getContentPane().add(p, BorderLayout.SOUTH);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
private class PEShower implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			JOptionPane.showMessageDialog(SystolicSimulation.this, new PEImage("PE.jpg"));
		}
	}
public class SystolicAnimation extends JPanel implements ActionListener {
        Image[] images = new Image[4];
        int frame = 0;
 
        // Display each image for 1 second
        int DELAY = 1000; // 1 second
        javax.swing.Timer timer;
 
 
        public SystolicAnimation() {
                loadImages();
        }
 
        public void start() {
                new javax.swing.Timer(DELAY, this).start();
        }
 
        public void actionPerformed(ActionEvent e) {
                // Move to the next image
                frame = (frame + 1) % images.length;
 
                // Causes the paint() method to be called
                repaint();
        }
 
        public void paintComponent(Graphics g) {
                super.paintComponent(g);
 
                Image image = images[frame];
 
                if (image != null) {
                        // Draw the current image
                        int x = 0;
                        int y = 0;
                        g.drawImage(image, x, y, this);
                }
        }
 
        private void loadImages() {
                // Load the array of images
                images[0] = new ImageIcon("FirstClockCycle.GIF").getImage();
                images[1] = new ImageIcon("SecondClockCycle.GIF").getImage();
                images[2] = new ImageIcon("ThirdClockCycle.GIF").getImage();
                images[3] = new ImageIcon("FourthClockCycle.GIF").getImage();
 
                int width = 0;
                int height = 0;
 
                for (int i = 0; i < images.length; i++) {
                        width = Math.max(width, images[i].getWidth(this));
                        height = Math.max(height, images[i].getHeight(this));
                }
                setPreferredSize(new Dimension(width, height));
        }
 
       
  private class AnimationShower implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			
		}
	}
}
 
        
 public class PEImage extends JPanel {
        JScrollPane scrollPane;
        ImageIcon icon;
 
        public PEImage(String iconPath) {
                icon = new ImageIcon(iconPath);
                setLayout(new BorderLayout());
 
                JPanel panel = new JPanel() {
                                public void paintComponent(Graphics g) {
                                        g.drawImage(icon.getImage(), 0, 0, null);
 
                                        setOpaque(false);
                                        super.paintComponent(g);
                                }
                        };
 
                scrollPane = new JScrollPane(panel);
                add(scrollPane);
                setPreferredSize(new Dimension(icon.getImage().getWidth(this),
                                icon.getImage().getHeight(this)));
        }
 
   
           
 }
 
        public static void main(String[] args) {
                try {
                        SwingUtilities.invokeAndWait(new Runnable() {
                                        public void run() {
                                                SystolicSimulation f = new SystolicSimulation();
                                                f.setGui();
                                                f.pack();
                                                f.setVisible(true);
                                        }
                                });
                } catch (Exception e) {
                        e.printStackTrace();
                }
        }
 
        private class Simulation extends AbstractAction {
                public Simulation(String name) {
                        super(name);
                }
 
                public void actionPerformed(ActionEvent e) {
                        int ix = ++imageIndex % images.length;
                        icon.setImage(images[ix]);
                        validate();
                        repaint();
                }
        }
}

Open in new window

>>The button that initializes the animation is not the "PE Content" but the one called "Numerical Version".

Yes, i was wondering about that ;-)

You need to approach it for button c exactly as button d is approached. Give your components more self-documenting names ;-)
>>>>Yes, i was wondering about that ;-)

Thanks....my fault!

>>>>>You need to approach it for button c exactly as button d is approached. Give your components more self-documenting names ;-)

Ok thanks i will give some better names.
Now about the classes I did it...I think...the code is on the previous post....but I still cant initialize the animation :(
Could you help me to figure out what's wrong with the code?
Thanks
>>.I think...the code is on the previous post.

No - it's on this one. I can see it. Think about it and look at the way the other code works
Ah ok.I didn't meant the previous post but the previous comment...sorry :$
So I did what you have asked..now I getting a error called "not an enclosing class:SistolicSimulation.SystolicAnimation
So I'm missing something here probably :( but where?


import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.imageio.ImageIO;
import javax.swing.*;
 
public class SystolicSimulation extends JFrame {
        private Image[] images;
        private ImageIcon icon;
        private int imageIndex;
 
        private void loadImages() {
                try {
                        images = new Image[4];
                         images[0] = new ImageIcon("FirstClockCycleGeneral.GIF").getImage();
                         images[1] = new ImageIcon("SecondClockCycleGeneral.GIF").getImage();
                         images[2] = new ImageIcon("ThirdClockCycleGeneral.GIF").getImage();
                         images[3] = new ImageIcon("FourthClockCycleGeneral.GIF").getImage();
                         
                } catch (Exception e) {
                        e.printStackTrace();
                }
        }
 
      private void setGui() {
		try {
			loadImages();
			setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			icon = new ImageIcon(images[imageIndex]);
			getContentPane()
				.add(new JScrollPane(new JLabel(icon)), BorderLayout.CENTER);
 
			JPanel p = new JPanel();
			JButton b = new JButton(new Simulation("Next image"));
			JButton c = new JButton(new Simulation("Numeric Version"));
			JButton d = new JButton(new Simulation("PE Content"));
			d.addActionListener(new PEShower());
			d.addActionListener(new AnimationShower());
			p.add(b);
			p.add(c);
			p.add(d);
			getContentPane().add(p, BorderLayout.SOUTH);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
private class PEShower implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			JOptionPane.showMessageDialog(SystolicSimulation.this, new PEImage("PE.jpg"));
		}
	}
	
 private class AnimationShower implements ActionListener {
		public void actionPerformed(ActionEvent e) {
		    JOptionPane.showMessageDialog(SystolicAnimation.this, new SystolicAnimation());
			
		}
	}
public class SystolicAnimation extends JPanel implements ActionListener {
        Image[] images = new Image[4];
        int frame = 0;
 
        // Display each image for 1 second
        int DELAY = 1000; // 1 second
        javax.swing.Timer timer;
 
 
        public SystolicAnimation() {
                loadImages();
        }
 
        public void start() {
                new javax.swing.Timer(DELAY, this).start();
        }
 
        public void actionPerformed(ActionEvent e) {
                // Move to the next image
                frame = (frame + 1) % images.length;
 
                // Causes the paint() method to be called
                repaint();
        }
 
        public void paintComponent(Graphics g) {
                super.paintComponent(g);
 
                Image image = images[frame];
 
                if (image != null) {
                        // Draw the current image
                        int x = 0;
                        int y = 0;
                        g.drawImage(image, x, y, this);
                }
        }
 
        private void loadImages() {
                // Load the array of images
                images[0] = new ImageIcon("FirstClockCycle.GIF").getImage();
                images[1] = new ImageIcon("SecondClockCycle.GIF").getImage();
                images[2] = new ImageIcon("ThirdClockCycle.GIF").getImage();
                images[3] = new ImageIcon("FourthClockCycle.GIF").getImage();
 
                int width = 0;
                int height = 0;
 
                for (int i = 0; i < images.length; i++) {
                        width = Math.max(width, images[i].getWidth(this));
                        height = Math.max(height, images[i].getHeight(this));
                }
                setPreferredSize(new Dimension(width, height));
        }
 
}
 
        
 public class PEImage extends JPanel {
        JScrollPane scrollPane;
        ImageIcon icon;
 
        public PEImage(String iconPath) {
                icon = new ImageIcon(iconPath);
                setLayout(new BorderLayout());
 
                JPanel panel = new JPanel() {
                                public void paintComponent(Graphics g) {
                                        g.drawImage(icon.getImage(), 0, 0, null);
 
                                        setOpaque(false);
                                        super.paintComponent(g);
                                }
                        };
 
                scrollPane = new JScrollPane(panel);
                add(scrollPane);
                setPreferredSize(new Dimension(icon.getImage().getWidth(this),
                                icon.getImage().getHeight(this)));
        }
 
   
           
 }
 
        public static void main(String[] args) {
                try {
                        SwingUtilities.invokeAndWait(new Runnable() {
                                        public void run() {
                                                SystolicSimulation f = new SystolicSimulation();
                                                f.setGui();
                                                f.pack();
                                                f.setVisible(true);
                                        }
                                });
                } catch (Exception e) {
                        e.printStackTrace();
                }
        }
 
        private class Simulation extends AbstractAction {
                public Simulation(String name) {
                        super(name);
                }
 
                public void actionPerformed(ActionEvent e) {
                        int ix = ++imageIndex % images.length;
                        icon.setImage(images[ix]);
                        validate();
                        repaint();
                }
        }
}

Open in new window

Well first of all you've add both listeners to the same button:

>>
            d.addActionListener(new PEShower());
                  d.addActionListener(new AnimationShower());
>>

Your animation shower class looks good.
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
>>>>>Well first of all you've add both listeners to the same button:

I think now is ok?

>>>>Your animation shower class looks good.

So do I need to point to the button?
The error called "not an enclosing class:SistolicSimulation.SystolicAnimation is still showing up.
No clue about it.
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.imageio.ImageIO;
import javax.swing.*;
 
public class SystolicSimulation extends JFrame {
        private Image[] images;
        private ImageIcon icon;
        private int imageIndex;
 
        private void loadImages() {
                try {
                        images = new Image[4];
                         images[0] = new ImageIcon("FirstClockCycleGeneral.GIF").getImage();
                         images[1] = new ImageIcon("SecondClockCycleGeneral.GIF").getImage();
                         images[2] = new ImageIcon("ThirdClockCycleGeneral.GIF").getImage();
                         images[3] = new ImageIcon("FourthClockCycleGeneral.GIF").getImage();
                         
                } catch (Exception e) {
                        e.printStackTrace();
                }
        }
 
      private void setGui() {
		try {
			loadImages();
			setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			icon = new ImageIcon(images[imageIndex]);
			getContentPane()
				.add(new JScrollPane(new JLabel(icon)), BorderLayout.CENTER);
 
			JPanel p = new JPanel();
			JButton b = new JButton(new Simulation("Next image"));
			JButton c = new JButton(new Simulation("Numeric Version"));
			JButton d = new JButton(new Simulation("PE Content"));
			d.addActionListener(new PEShower());
			d.addActionListener(new SystolicAnimation());
			p.add(b);
			p.add(c);
			p.add(d);
			getContentPane().add(p, BorderLayout.SOUTH);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
    private class PEShower implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			JOptionPane.showMessageDialog(SystolicSimulation.this, new PEImage("PE.jpg"));
 
		}
	}
	
	private class AnimationShower implements ActionListener {
		public void actionPerformed(ActionEvent e) {
		    JOptionPane.showMessageDialog(SystolicAnimation.this, new SystolicAnimation("Numeric Version"));
		   	
		}
	}
 
public class SystolicAnimation extends JPanel implements ActionListener {
        Image[] images = new Image[4];
        int frame = 0;
 
        // Display each image for 1 second
        int DELAY = 1000; // 1 second
        javax.swing.Timer timer;
 
 
        public SystolicAnimation() {
                loadImages();
        }
 
        public void start() {
                new javax.swing.Timer(DELAY, this).start();
        }
 
        public void actionPerformed(ActionEvent e) {
                // Move to the next image
                frame = (frame + 1) % images.length;
 
                // Causes the paint() method to be called
                repaint();
        }
 
        public void paintComponent(Graphics g) {
                super.paintComponent(g);
 
                Image image = images[frame];
 
                if (image != null) {
                        // Draw the current image
                        int x = 0;
                        int y = 0;
                        g.drawImage(image, x, y, this);
                }
        }
 
        private void loadImages() {
                // Load the array of images
                images[0] = new ImageIcon("FirstClockCycle.GIF").getImage();
                images[1] = new ImageIcon("SecondClockCycle.GIF").getImage();
                images[2] = new ImageIcon("ThirdClockCycle.GIF").getImage();
                images[3] = new ImageIcon("FourthClockCycle.GIF").getImage();
 
                int width = 0;
                int height = 0;
 
                for (int i = 0; i < images.length; i++) {
                        width = Math.max(width, images[i].getWidth(this));
                        height = Math.max(height, images[i].getHeight(this));
                }
                setPreferredSize(new Dimension(width, height));
        }
 
}
 
        
 public class PEImage extends JPanel {
        JScrollPane scrollPane;
        ImageIcon icon;
 
        public PEImage(String iconPath) {
                icon = new ImageIcon(iconPath);
                setLayout(new BorderLayout());
 
                JPanel panel = new JPanel() {
                                public void paintComponent(Graphics g) {
                                        g.drawImage(icon.getImage(), 0, 0, null);
 
                                        setOpaque(false);
                                        super.paintComponent(g);
                                }
                        };
 
                scrollPane = new JScrollPane(panel);
                add(scrollPane);
                setPreferredSize(new Dimension(icon.getImage().getWidth(this),
                                icon.getImage().getHeight(this)));
        }
 
   
           
 }
 
        public static void main(String[] args) {
                try {
                        SwingUtilities.invokeAndWait(new Runnable() {
                                        public void run() {
                                                SystolicSimulation f = new SystolicSimulation();
                                                f.setGui();
                                                f.pack();
                                                f.setVisible(true);
                                        }
                                });
                } catch (Exception e) {
                        e.printStackTrace();
                }
        }
 
        private class Simulation extends AbstractAction {
                public Simulation(String name) {
                        super(name);
                }
 
                public void actionPerformed(ActionEvent e) {
                        int ix = ++imageIndex % images.length;
                        icon.setImage(images[ix]);
                        validate();
                        repaint();
                }
        }
}

Open in new window

Ops I just saw now your second comment.I will try and I let u know after.Thanks
>>
            d.addActionListener(new PEShower());
                  d.addActionListener(new SystolicAnimation());
>>

You've still added to the same button
Thanks CEHJ
I had add both listeners to the same button.I got it rite now.Finally is working
Thank you very much.
Thank you very much for all the help.Bye
:-)