Home > Archive > Java Help > October 2004 > Help: Java Applet: How to get ActionEvent inside paint()
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Help: Java Applet: How to get ActionEvent inside paint()
|
|
| da Vinci 2004-10-13, 3:57 am |
| Hello,
I am a beginning Java student with background in C++. I have an
assignment to do, and no I am not asking someone to do it for me. I am
simply looking for a hint on what I can do here. Any help is greatly
appreciated.
Assignment is to make a "slide show" applet. Basically, take X amount
of .gif files in a directory and display them as the user clicks the
next button. This should all be done by loading the images into an
array as it is in the Array chapter of the class. A variation is to
make them display randomly. I will get to the random part later but
want to conquer the button click assignment first.
My problem is that as I incriment the imageN variable in the
ActionEvent method, it will not change images. The images are
controled by the paint method.
I simply do not know how to make them play nice together and my book
doesnt help in that department one bit.
I have tried to call the paint() method again inside of actionevent
and, as I bet you are thinking right now, NO WAY! Didn't work. :)
Thanks in advance. Code follows:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class PicViewerApplet extends Applet implements ActionListener
{
private int imageN = 0;
private static int NIMAGES = 2;
private Button next = new Button("Next");
private Image image[] = new Image[NIMAGES];
public void init()
{
add(next);
for (int n=0; n < 2; n++)
image[n] = getImage(getCodeBase(), "images" + n +
".gif");
setSize(800,600);
}
public void paint (Graphics g)
{
g.drawImage(image[imageN],1,1, this);
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == next)
{
imageN = (imageN+1) % NIMAGES;
}
}
private void delay(int n)
{
try
{
Thread.sleep(n);
}
catch (InterruptedException e)
{
}
}
}
| |
| Mark Murphy 2004-10-13, 3:57 am |
| da Vinci wrote:
> Hello,
>
> I am a beginning Java stu dentwithbackgroundinC++.Ihavean
> assignment to do, and no I am not asking someone to do it for me. I am
> simply looking for a hint on what I can do here. Any help is greatly
> appreciated.
>
> Assignment is to make a "slide show" applet. Basically, take X amount
> of .gif files in a directory and display them as the user clicks the
> next button. This should all be done by loading the images into an
> array as it is in the Array chapter of the class. A variation is to
> make them display randomly. I will get to the random part later but
> want to conquer the button click assignment first.
>
> My problem is that as I incriment the imageN variable in the
> ActionEvent method, it will not change images. The images are
> controled by the paint method.
>
> I simply do not know how to make them play nice together and my book
> doesnt help in that department one bit.
>
> I have tried to call the paint() method again inside of actionevent
> and, as I bet you are thinking right now, NO WAY! Didn't work. :)
>
> Thanks in advance. Code follows:
>
> import java.applet.*;
> import java.awt.*;
> import java.awt.event.*;
>
> public class PicViewerApplet extends Applet implements ActionListener
> {
>
> private int imageN = 0;
> private static int NIMAGES = 2;
>
> private Button next = new Button("Next");
> private Image image[] = new Image[NIMAGES];
>
> public void init()
> {
>
> add(next);
>
> for (int n=0; n < 2; n++)
> image[n] = getImage(getCodeBase(), "images" + n +
> ".gif");
>
> setSize(800,600);
>
> }
>
> public void paint (Graphics g)
> {
> g.drawImage(image[imageN],1,1, this);
> }
>
>
> public void actionPerformed(ActionEvent e)
> {
> if (e.getSource() == next)
> {
> imageN = (imageN+1) % NIMAGES;
> }
> }
>
>
> private void delay(int n)
> {
> try
> {
> Thread.sleep(n);
> }
>
> catch (InterruptedException e)
> {
> }
> }
>
> }
>
Hint, hint ... http://www.mindprod.com/jgloss/repaint.html
Glad you plan on learning Java over coping it!
Mark
| |
| Paul Lutus 2004-10-13, 3:57 am |
| da Vinci wrote:
> Hello,
>
> I am a beginning Java student with background in C++. I have an
> assignment to do, and no I am not asking someone to do it for me. I am
> simply looking for a hint on what I can do here. Any help is greatly
> appreciated.
>
> Assignment is to make a "slide show" applet. Basically, take X amount
> of .gif files in a directory and display them as the user clicks the
> next button. This should all be done by loading the images into an
> array as it is in the Array chapter of the class.
For GIF files of a certain size, this requirement may be fatal to the
project (too much storage is required). But, since your example only uses
two images, this should not be a problem for the moment.
> A variation is to
> make them display randomly. I will get to the random part later but
> want to conquer the button click assignment first.
>
> My problem is that as I incriment the imageN variable in the
> ActionEvent method, it will not change images. The images are
> controled by the paint method.
You have not assigned an ActionListener to your button.
> I simply do not know how to make them play nice together and my book
> doesnt help in that department one bit.
Of course it does. Look at any example that uses a button. See how there is
an ActionListener added to the button?
BTW thanks for posting your code. It is always easier to offer help when
there is a nice, short code listing to refer to.
--
Paul Lutus
http://www.arachnoid.com
|
|
|
|
|