Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

loading a url image to a canvas on a panel?

Posted on 1998-10-31
9
362 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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
 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SHA2 certs for IIS AND Java? 2 113
iterator/ListIterator approach 17 39
Android development question 2 54
ejb stateless example 2 23
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

856 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