Results 1 to 9 of 9
- 03-26-2008, 01:36 AM #1
Member
- Join Date
- Mar 2008
- Posts
- 14
- Rep Power
- 0
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) { } }
- 03-26-2008, 02:42 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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 } }
- 03-26-2008, 02:50 AM #3
Member
- Join Date
- Mar 2008
- Posts
- 14
- Rep Power
- 0
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) } }
- 03-26-2008, 02:58 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Did you try it? If so what happened?
- 03-26-2008, 03:01 AM #5
Member
- Join Date
- Mar 2008
- Posts
- 14
- Rep Power
- 0
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, 03:07 AM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
You have to change the following code
asJava Code:if("firstButton".equals(event.getActionCommand())){
And also how can you call the lineJava Code:if("firstButton".equals(e.getActionCommand())){
with out referring the graphic class. At the same time you have miss the semicolon at the end of that line.Java Code:g.drawImage(imageArr[0], 0, 0, this)
Last edited by Eranga; 03-26-2008 at 03:47 AM.
- 03-26-2008, 03:11 AM #7
Member
- Join Date
- Mar 2008
- Posts
- 14
- Rep Power
- 0
Ok, I changed it as you stated, here is what I have then:
But now there are four errors:Java Code:public void actionPerformed(ActionEvent e) { if("firstButton".equals(e.getActionCommand())) { public void paint (Graphics g) { g.drawImage(imageArr[0], 0, 0, this); } } }
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 03:47 AM.
- 03-26-2008, 03:49 AM #8
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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.
- 03-26-2008, 04:02 AM #9
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Is that you have seen buttons on the applet before adding the actionePerformed() in your code?
Similar Threads
-
Issue with Buttons and ActionPerformed
By Deathmonger in forum Advanced JavaReplies: 1Last Post: 04-17-2008, 08:47 AM -
avoiding if statements
By valoyivd in forum New To JavaReplies: 1Last Post: 04-02-2008, 09:08 AM -
actionPerformed problem
By tomitzel in forum New To JavaReplies: 1Last Post: 01-08-2008, 06:10 PM -
Problems with jButton ActionPerformed
By susan in forum AWT / SwingReplies: 3Last Post: 08-07-2007, 04:19 AM -
Help with if else statements
By zoe in forum New To JavaReplies: 1Last Post: 07-24-2007, 07:56 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks