|
|
Welcome to the Java Forums.
You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:
- have access to post topics
- communicate privately with other members (PM)
- not see advertisements between posts
- have the possibility to earn one of our surprises if you are an active member
- access many other special features that will be introduced later.
Registration is fast, simple and absolutely free so please, join our community today!
If you have any problems with the registration process or your account login, please contact us.
|
|

03-26-2008, 03:36 AM
|
|
Member
|
|
Join Date: Mar 2008
Posts: 14
|
|
|
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:
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)
{
}
}
|
|

03-26-2008, 04:42 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,402
|
|
You have to get the correct action command depend on the buttons action listener. Something like this.
public void actionPerformed(ActionEvent e)
{
if("First".equals(event.getActionCommand())){
// Do the processing here
}
}
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

03-26-2008, 04:50 AM
|
|
Member
|
|
Join Date: Mar 2008
Posts: 14
|
|
How can I incorporate the repaint() method into this?
Would I have to write something like this:
public void actionPerformed(ActionEvent e)
{
if("firstButton".equals(event.getActionCommand())){
g.drawImage(imageArr[0], 0, 0, this)
}
}
|
|

03-26-2008, 04:58 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,402
|
|
|
Did you try it? If so what happened?
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

03-26-2008, 05:01 AM
|
|
Member
|
|
Join Date: Mar 2008
Posts: 14
|
|
|
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);
|
|

03-26-2008, 05:07 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,402
|
|
You have to change the following code
if("firstButton".equals(event.getActionCommand())){
as
if("firstButton".equals(e.getActionCommand())){
And also how can you call the line
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.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Last edited by Eranga : 03-26-2008 at 05:47 AM.
|
|

03-26-2008, 05:11 AM
|
|
Member
|
|
Join Date: Mar 2008
Posts: 14
|
|
Ok, I changed it as you stated, here is what I have then:
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 05:47 AM.
|
|

03-26-2008, 05:49 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,402
|
|
|
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.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

03-26-2008, 06:02 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,402
|
|
|
Is that you have seen buttons on the applet before adding the actionePerformed() in your code?
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|