Results 1 to 9 of 9
  1. #1
    wco5002 is offline Member
    Join Date
    Mar 2008
    Posts
    14
    Rep Power
    0

    Default Help with actionPerformed Statements

    Hi, I'm creating a small photo album that will read images in from an array. The applet will also have a few buttons (first, previous, next, last), which will allow the user to switch to the next photo. However, I'm a little unsure on how to write the actionPerformed statements to switch the photos, hopefully you guys can give me a few pointers. Here is the code thus far:

    Java Code:
    import java.applet.*;
    import java.awt.*;
    import java.io.*;
    import javax.swing.*;
    import java.awt.event.*;
    
    
    public class Virtual_Photo_Album extends Applet implements ActionListener
    {
        private Image imageArr[] = new Image [5];
        private AudioClip myAudioClip;
        private Button firstButton, previousButton, nextButton, lastButton;
        
        public void init() 
        {
           myAudioClip = getAudioClip(getCodeBase(), "../../audio.au");  // Obtians sound clip.
           myAudioClip.loop();   // Loops the audio clip.
           
               
           imageArr[0] = getImage(getCodeBase(),"../../images/image1.jpg");
           imageArr[1] = getImage(getCodeBase(),"../../images/image2.jpg");
           imageArr[2] = getImage(getCodeBase(),"../../images/image3.jpg");
           imageArr[3] = getImage(getCodeBase(),"../../images/image4.jpg");
           imageArr[4] = getImage(getCodeBase(),"../../images/image5.jpg");
        
            
           firstButton = new Button("First");     // Buttons
           firstButton.addActionListener(this);   // Assigns the ActionListeners
           previousButton = new Button("Previous");
           previousButton.addActionListener(this);
           nextButton = new Button("Next");
           nextButton.addActionListener(this);
           lastButton = new Button("Last");
           lastButton.addActionListener(this);
           
           add(firstButton);
           add(previousButton);
           add(nextButton);
           add(lastButton);
        }
        
    
            public void paint (Graphics g) 
            {
                g.drawImage(imageArr[0], 0, 0, this);
                g.drawImage(imageArr[1], 0, 0, this);
                g.drawImage(imageArr[2], 0, 0, this);
                g.drawImage(imageArr[3], 0, 0, this);
                g.drawImage(imageArr[4], 0, 0, this);
            }
        
        public void actionPerformed(ActionEvent e)
        {
            
        }
    }

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    You have to get the correct action command depend on the buttons action listener. Something like this.
    Java Code:
        public void actionPerformed(ActionEvent e)
        {
                if("First".equals(event.getActionCommand())){
                // Do the processing here
            }    
        }

  3. #3
    wco5002 is offline Member
    Join Date
    Mar 2008
    Posts
    14
    Rep Power
    0

    Default

    How can I incorporate the repaint() method into this?

    Would I have to write something like this:

    Java Code:
         public void actionPerformed(ActionEvent e)
         {
              if("firstButton".equals(event.getActionCommand())){
                   g.drawImage(imageArr[0], 0, 0, this)
              }
         }

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  5. #5
    wco5002 is offline Member
    Join Date
    Mar 2008
    Posts
    14
    Rep Power
    0

    Default

    I receive two errors:

    C:\Users\William\Virtual_Photo_Album\src\Virtual_P hoto_Album.java:69: cannot find symbol
    symbol : variable event
    location: class Virtual_Photo_Album
    if("firstButton".equals(event.getActionCommand())) {
    C:\Users\William\Virtual_Photo_Album\src\Virtual_P hoto_Album.java:70: cannot find symbol
    symbol : variable g
    location: class Virtual_Photo_Album
    g.drawImage(imageArr[0], 0, 0, this);

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    You have to change the following code

    Java Code:
    if("firstButton".equals(event.getActionCommand())){
    as

    Java Code:
    if("firstButton".equals(e.getActionCommand())){
    And also how can you call the line

    Java Code:
    g.drawImage(imageArr[0], 0, 0, this)
    with out referring the graphic class. At the same time you have miss the semicolon at the end of that line.
    Last edited by Eranga; 03-26-2008 at 04:47 AM.

  7. #7
    wco5002 is offline Member
    Join Date
    Mar 2008
    Posts
    14
    Rep Power
    0

    Default

    Ok, I changed it as you stated, here is what I have then:

    Java Code:
        public void actionPerformed(ActionEvent e)
        {
              if("firstButton".equals(e.getActionCommand()))
              {
                  public void paint (Graphics g)
                  {
                      g.drawImage(imageArr[0], 0, 0, this);
                  }
              }
         }
    But now there are four errors:

    C:\Users\William\Virtual_Photo_Album\src\Virtual_P hoto_Album.java:69: illegal start of expression
    public void paint (Graphics g)

    C:\Users\William\Virtual_Photo_Album\src\Virtual_P hoto_Album.java:69: illegal start of expression
    public void paint (Graphics g)

    C:\Users\William\Virtual_Photo_Album\src\Virtual_P hoto_Album.java:69: ';' expected
    public void paint (Graphics g)

    C:\Users\William\Virtual_Photo_Album\src\Virtual_P hoto_Album.java:69: ';' expected
    public void paint (Graphics g)
    Last edited by Eranga; 03-26-2008 at 04:47 AM.

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Yes this is wrong, you can't build a method inside the other. You can call any method within it with correct arguments actually. Just look at last two errors. It outline that after it if condition-loop it expecting statements, not a method.

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. Issue with Buttons and ActionPerformed
    By Deathmonger in forum Advanced Java
    Replies: 1
    Last Post: 04-17-2008, 09:47 AM
  2. avoiding if statements
    By valoyivd in forum New To Java
    Replies: 1
    Last Post: 04-02-2008, 10:08 AM
  3. actionPerformed problem
    By tomitzel in forum New To Java
    Replies: 1
    Last Post: 01-08-2008, 07:10 PM
  4. Problems with jButton ActionPerformed
    By susan in forum AWT / Swing
    Replies: 3
    Last Post: 08-07-2007, 05:19 AM
  5. Help with if else statements
    By zoe in forum New To Java
    Replies: 1
    Last Post: 07-24-2007, 08:56 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •