Solved

loading a url image to a canvas on a panel?

Posted on 1998-10-31
9
349 Views
Last Modified: 2012-05-05
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
Comment
Question by:Uncle_Jed
  • 4
  • 3
  • 2
9 Comments
 
LVL 1

Expert Comment

by:borup
ID: 1226761
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
 
LVL 1

Expert Comment

by:borup
ID: 1226762
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
 

Author Comment

by:Uncle_Jed
ID: 1226763
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
 

Author Comment

by:Uncle_Jed
ID: 1226764
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 16

Expert Comment

by:heyhey_
ID: 1226765
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
 
LVL 16

Expert Comment

by:heyhey_
ID: 1226766
again?
why do subclass Canvas ?
Panel should be OK too :)
0
 

Author Comment

by:Uncle_Jed
ID: 1226767
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
 
LVL 16

Expert Comment

by:heyhey_
ID: 1226768
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
 
LVL 16

Accepted Solution

by:
heyhey_ earned 200 total points
ID: 1226769
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

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now