• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 388
  • Last Modified:

loading a url image to a canvas on a panel?

I'm new a java and am trying to do a slideshow where i can view 20 predetermind slides names pic01.jpg to pic20.jpg from a url passed to the class file by the calling html file. Can anyone look over the code i have so far and tell me if im going in the right direction? I cant seem to display the first slide in the canvas i added to my panel. my buttons show up but not the image. How do i pass the url to the slideshow.class file and display the first pic01.jpg file. Also how do I do an array of pictures i can scroll through and how do i update the slide canvas?

Many questions, I Know, I'll offer 200 points up to anyone that can help me. See attached code below..


// not sure if im importing all i need or too much
import java.awt.*;
import java.awt.image.*;
import java.awt.Graphics;
import java.net.*;
import java.awt.event.*;
import java.applet.*;
import corejava.*;

public class SlideShow extends Applet implements ActionListener
{   public void init()
    {      // use default panel layout));
      // add five buttons
      Panel p = new Panel();
      Button firstButton = new Button("<<");
      p.add(firstButton);
      firstButton.addActionListener(this);

      Button backButton = new Button("<");
      p.add(backButton);
      backButton.addActionListener(this);

      Button nextButton = new Button(">");
      p.add(nextButton);
      nextButton.addActionListener(this);

      Button lastButton = new Button(">>");
      p.add(lastButton);
      lastButton.addActionListener(this);

      Button closeButton = new Button("Close");
      p.add(closeButton);
      closeButton.addActionListener(this);

      // add canvas for displaying slide
      add(p,"South");
      slide = new SlideCanvas();
      add(slide,"North");
    }
    public void actionPerformed(ActionEvent evt)
    {      String arg = evt.getActionCommand();
      if (arg.equals("<<")) slide.first();
      else if (arg.equals("<")) slide.back();
      else if (arg.equals(">")) slide.next();
      else if (arg.equals(">>")) slide.last();
      else if (arg.equals("Close")) System.exit(0);
    }  
    private SlideCanvas slide;
}

class SlideCanvas extends Canvas
{   public void paint(Graphics g)       
    {      // show first slide in canvas

      String s = "pic01.jpg";
        Image image = Toolkit.getDefaultToolkit().getImage(s);      
        g.drawImage(image,0,0,this);
    }

    public void first()
    {      // go to first slide      
      repaint();
    }

    public void back()
    {   // back one slide      
      repaint();
    }

    public void next()
    {      // forward one slide
      repaint();
    }

    public void last()
    {      // go to last slide
      repaint();
    }
    private int counter = 0;
}
0
Uncle_Jed
Asked:
Uncle_Jed
  • 4
  • 3
  • 2
1 Solution
 
borupCommented:
It looks like you are going in the right direction.

You can pass the pictures to the applet using the param tag.

<APPLET CODE="your_applet.class">
  <PARAM NAME="pic1" VALUE="pic01.jpg">
  <PARAM NAME="pic2" VALUE="pic02.jpg">
  .
  .
  .
  <PARAM NAME="pic20" VALUE="pic20.jpg">
</APPLET>

And now to the code:

First create an array to hold the images:

Image[] ImageList = new Image[21]; //One more than 20 while I'm not using index 0.

Then fill the array:

for (int pic=1; pic<20; pic++) {
  if (getParameter("pic" + pic) != null) {
    ImageList[Pic] = getImage(getCodeBase(), getParameter("pic" + pic));
  }
}

Now you have an array of images you can show with:

g.drawImage(ImageList[counter],0,0,this);

All you need now is to assign the right value to counter when clicking at the buttons.

regards,
botup
0
 
borupCommented:
ImageList[Pic] = getImage(getCodeBase(), getParameter("pic" + pic));

has to be changed to

ImageList[pic] = getImage(getCodeBase(), getParameter("pic" + pic));

I had misspelled pic (Uppercase p). Not good ;-)
0
 
Uncle_JedAuthor Commented:
Thanks for the quick reply :-)

Ok I added the array into my init() secton before the buttons get added. It seems to compile ok when I did that. I'm still not clear on how to insert the

g.drawImage(ImageList[counter],0,0,this);       

I fixed up the class declaration and tried to add the drawimage line in 5 diferent spots ( all commented out now, see below). without the commenting out the 5 lines i recieve the following compiler errors.

Undefinde variable:ImageList
Undefined variable or class name g
Undefined variable or class name g
Undefined variable or class name g
Undefined variable or class name g

Here's my updated class section..

class SlideCanvas extends Canvas
{   public void paint(Graphics g)       
    {      // show first slide in canvas
//      g.drawImage(ImageList[counter],0,0,this);       
    }

    public void first()
    {      // go to first slide
      counter = 1;
//      g.drawImage(ImageList[counter],0,0,this);      
    }

    public void back()
    {   // back one slide      
      counter--;
      if (counter < 1)
          {
          counter =1;
          }
//      g.drawImage(ImageList[counter],0,0,this);      
    }

    public void next()
    {      // forward one slide
      counter++;
      if (counter > 20)
          {
           counter =20;
          }
//      g.drawImage(ImageList[counter],0,0,this);      
    }

    public void last()
    {      // go to last slide
      counter = 20;
//      g.drawImage(ImageList[counter],0,0,this);      
    }
    private int counter = 0;
}
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
Uncle_JedAuthor Commented:
Sorry borup, I'll have to reject your answer as it did not address my question asked in the title and would mislead those looking for answers in the future. I have waited 5 days for you to respond to my reply and no luck so I will reopen the question to others. Can you or anyone else help further?
0
 
heyhey_Commented:
the only GOOD place to use Graphics.drawImage is
in the update or paint methods

I can't test your code, but there may be some problems with

1. layout problems - are you sure that you canvas isn't size 0 x 0 . try to paint the background to see the canvas1.

2. Image URL - are you sure you have LOADED the images. check the URL. applet can load images only from its origin server

if you see the canvas (painted in red :) and you have the Images URL right you can (cosmetic :) wait for Image to load using MediaTracket.
(sorry I don't have any docs round here. it shouldn't be that hard)

so that's for tha moment
hope this helps
  heyhey





0
 
heyhey_Commented:
again?
why do subclass Canvas ?
Panel should be OK too :)
0
 
Uncle_JedAuthor Commented:
I appreciate your responce heyhey, but it still doesnt help much. Allthough you have given me a few questions to think about, You have given me no code examples I can try to see if it fixes things up. For 200 points I would think someone could look at my code and show me a re-edited version of it that would work. I realize not everyone can import the corejava class, but they could show me how to do the code without it.
0
 
heyhey_Commented:
if you just need annimation code snippet just check the jdk demo subdirectory
jdk1.1.7/demo/Animator
there is full working animator demo with source...

if you need me to check your code (for 200 points :) then post a full version that I can check and compile ...

Note: messages that you receive indicate syntax error - you can't use objects that you haven't defined
Undefinde variable:ImageList
Undefined variable or class name g
Undefined variable or class name g
Undefined variable or class name g
Undefined variable or class name g

0
 
heyhey_Commented:
but if you want to give me the points :) - here is the answer (not comment)

I'd like to help you solving any other problems ...

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 4
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now