0

How do i get an java applet to search an image from the specified path. I want to make a simple imageviewer applet which has 2 buttons NEXT,PREV . By clicking Next it should show next image and by clicking prev it should show the previous image. i have written the following code .Please help me in writing the code for the updatenext() and updateprev() function.

 public class pic extends Applet implements ActionListener 
 {
   Button prev,next;
   private static final String PREV="Previous";
   private static final String NEXT="Next";
   Image img;

   public void init()
   {
     img=getImage(getCodeBase(),"image1.jpg");
     prev = new Button();
     prev.setLabel(PREV);
     prev.setActionCommand(PREV);
     prev.addActionListener(this);
     add(prev);
     next = new Button();
     next.setLabel(NEXT);
     next.setActionCommand(NEXT);
     next.addActionListener(this);
     add(next);
   }

   public void paint(Graphics g)
   {
     g.drawImage(img,0,0,600,700,this);
   }

   public void actionPerformed(ActionEvent e)
   {
     if(e.getActionCommand().equals(NEXT))
     updatenext();

     else if(e.getActionCommand().equals(PREV))
     updateprev();
   }

   void updatenext()
   {
    //updateImagehere
   }

   void updateprev()
   {
    //updateimage here
    repaint();
   }
}
ASD
  • 107
  • 2
  • 11
  • how do i update the img variable? – ASD Nov 05 '14 at 12:41
  • go to simple tutorials that is available on the google – Kishan Bheemajiyani Nov 05 '14 at 12:47
  • 1) Why code an applet? If it is due to the teacher specifying it, please refer them to [Why CS teachers should **stop** teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) Why use AWT? See [this answer](http://stackoverflow.com/questions/6255106/java-gui-listeners-without-awt/6255978#6255978) for many good reasons to abandon AWT using components in favor of Swing. – Andrew Thompson Nov 06 '14 at 22:41

2 Answers2

0

A very simple example is this.

import java.applet.*; 
import java.awt.*; 

public class AppletImage extends Applet{
   Image img;
   MediaTracker tr;
   public void paint(Graphics g) {
      tr = new MediaTracker(this);
      img = getImage(getCodeBase(), "demoimg.gif");
      tr.addImage(img,0);
      g.drawImage(img, 0, 0, this);
   }
 public static void main(String args[]){
    AppletImage appletImage = new AppletImage();
    appletImage.setVisible(true);

}

}

try this and ya u can take image according to your self.

Kishan Bheemajiyani
  • 3,429
  • 5
  • 34
  • 68
  • How do i get the applet to search for the next image in the directory? – ASD Nov 05 '14 at 12:48
  • just put control of label and add click event and according to that change the imaage of the panel. have u used Swing? – Kishan Bheemajiyani Nov 05 '14 at 12:49
  • No i havent been Experienced with Swing .I still have a doubt though how would i access the name of the images in my prg.? for eg. my folder contains bike.jpg ,car,jpg etc how will i be able to first display bike.jpg and on clicking next it should display car.jpg. – ASD Nov 05 '14 at 13:00
  • @ASD add them into the arraylist and one by one u can display them. just first learn how to do event programming in applet. – Kishan Bheemajiyani Nov 05 '14 at 13:02
-1

If you want to get a list of the files in the location of your applet you can create a File object of the current directory with:

File currentDir = new File(getCodeBase().getPath());

Then to iterate over the files call:

for (File file : currentDir.listFiles()) {
    // do something with the file
}

You will need to filter out directories and non image files, I would suggest you use a FileNameFilter as an argument when calling listFiles().

EDIT:

Here's an example of the solution you could use with this method:

public class pic extends Applet implements ActionListener
{
Button prev, next;
private static final String PREV = "Previous";
private static final String NEXT = "Next";
Image img;
private File[] imageFiles;
private int currentIndex = 0;

public void init()
{
    File baseDir = new File(getCodeBase().getPath());
    imageFiles = baseDir.listFiles(new FilenameFilter() {
        @Override
        public boolean accept(File dir, String name) {
            String image = ".*\\.(png|jpg|jpeg|gif)";
            Pattern pattern = Pattern.compile(image);
            Matcher matcher = pattern.matcher(name.toLowerCase());
            return matcher.matches();
        }
    });
    setImage();
    prev = new Button();
    prev.setLabel(PREV);
    prev.setActionCommand(PREV);
    prev.addActionListener(this);
    add(prev);

    next = new Button();
    next.setLabel(NEXT);
    next.setActionCommand(NEXT);
    next.addActionListener(this);
    add(next);
}

public void paint(Graphics g)
{
    g.drawImage(img, 0, 0, 600, 700, this);
}

public void actionPerformed(ActionEvent e)
{
    if (e.getActionCommand().equals(NEXT))
        updatenext();
    else if (e.getActionCommand().equals(PREV))
        updateprev();
}

private void setImage() {
    img = getImage(getCodeBase(), imageFiles[currentIndex].getName());
}

void updatenext()
{
    // needs circular increment
    if (currentIndex == imageFiles.length - 1) {
        currentIndex = 0;
    } else {
        currentIndex++;
    }
    setImage();
    repaint();
}

void updateprev()
{
    // needs circular decrement
    if (currentIndex == 0) {
        currentIndex = imageFiles.length-1;
    } else {
        currentIndex--;
    }
    setImage();
    repaint();
}

}

fr1eza
  • 125
  • 6
  • Why would the end user want to use an applet on the net to display images off their own hard disks? You do realize that any file operations performed by an applet relate to files on the user's computer right? We can not obtain a 'file listing' from the server that way. – Andrew Thompson Nov 06 '14 at 22:45
  • The question doesn't mention that. – fr1eza Nov 07 '14 at 13:10