?
Solved

java applet source code

Posted on 2000-01-21
5
Medium Priority
?
242 Views
Last Modified: 2013-11-23
I am writing java applet which i will upload it to a webpage on net. i face with a problem .i don't know what is the source code for it.
I will add a button,and when i press it, it display another frame or link to another java applet. Inside the frame, there will have a button that can be pressed until 16 times,and when it is pressed the label on the button will change,and every time a image file( a.gif) will display beside the button, meaning that when the button is pressed, label on button will change and display a image file.
Example, i want the label on the button to display the number 0 to 16,and for every number on the button, there will also have 17 different image files will display with the response of the button.
Every time , the next image file display (at the same position for all the image file display), the previous one will change into the next image file.
At first, the label of the button is 0 and there also a image file display there.
can i do that?...pls tell me the java applet source code to write like that.
Thanks!
0
Comment
Question by:jeannieyeap
  • 2
  • 2
5 Comments
 

Expert Comment

by:vichu
ID: 2377328
Can you increase the points.I can then
start to code and send it to you.
0
 

Author Comment

by:jeannieyeap
ID: 2378722
Dear vichu,
i hope u really can help me, i am willing to increase the point, but i don't sure that whether ur answer is suitable to my question, i hope u really understand what i mean.
Thanks!
0
 
LVL 9

Accepted Solution

by:
Ovi earned 800 total points
ID: 2381029
REQUEST POINTS :

Hi,

I have what you need. Pllease try to compile the next source code and run in browser :


// Author Ovi
import java.applet.Applet;
import java.awt.*;
import java.net.URL;
import java.net.MalformedURLException;
import java.awt.event.*;

public class ImageViewer extends Applet {
      String imgs[] = {"1.gif", "2.gif", "3.gif", "4.gif"};
      Image images[] = new Image[4];
      Button b = null;
      
      public void init() {
          for(int i=0; i<imgs.length; i++) {
              try {
                  images[i] = getImage(new URL(getCodeBase().toString()+"/Images/"+imgs[i]));
              }catch(MalformedURLException e) {
                  System.out.println(" Bad URL for index : "+i+".");
              }
          }
            b = new Button("Press me");
            b.addActionListener(new ActionListener() {
                  public void actionPerformed(ActionEvent e) {
                        ViewerFrame f = new ViewerFrame("Image viewer", images);
                        f.setLocation(100, 100);
                        f.setSize(250, 400);
                        f.show();
                  }
                  });
            add(b);            
      }
      
      public class ViewerFrame extends Frame {
          public Image images[] = null;
          public Button b1;
          public Button b2;
          public int curentIndex = 0;
          private Image curentImage = null;
          
          public ViewerFrame(String title, Image imgStream[]) {
              super(title);
              setBackground(Color.lightGray);
              addWindowListener(new WindowAdapter() {
                  public void windowClosing(WindowEvent e) {
                    Window w = e.getWindow();
                    w.setVisible(false);
                    w.dispose();
                }
                  });
              images = imgStream;
              curentImage = images[0];
              createButtons();
              setLayout(null);
              add(b1);
              add(b2);
          }
          
          private void createButtons() {
              b1 = new Button("Back");
              b1.move(60, 30);
              b1.setSize(50, 20);
              if(curentIndex == 0)
                  b1.setEnabled(false);
              b1.addActionListener(new ActionListener() {
                  public void actionPerformed(ActionEvent e) {
                      curentIndex--;
                      if(curentIndex == 0)
                          b1.setEnabled(false);
                      b2.setLabel("" + curentIndex);
                      curentImage = images[curentIndex];
                      repaint();
                  }
                  });
                  
              b2 = new Button("" + curentIndex);
              b2.move(130, 30);
              b2.setSize(60, 20);
              b2.addActionListener(new ActionListener() {
                  public void actionPerformed(ActionEvent e) {
                      if(curentIndex < images.length -1)
                          curentIndex++;
                      if(curentIndex != 0)
                          b1.setEnabled(true);
                      b2.setLabel("" + curentIndex);
                      curentImage = images[curentIndex];
                      repaint();
                  }
                  });
          }
          
          public void update(Graphics g) {
              paint(g);
          }
          
          public void paint(Graphics g) {
              if(curentImage != null)
                  g.drawImage(curentImage, 35, 60, 180, 180, this);
          }
      }
}

////////////////////////////////////////////////////////////////////////
HTML file

<HTML>
<HEAD>
<TITLE>Ovi's Image viewer</TITLE>
</HEAD>
<BODY>
<APPLET CODE=ImageViewer.class WIDTH=300 HEIGHT=300></APPLET>
</BODY>
</HTML>

///////////////////////////////////////////////////////////////////////
This is more that you want in your question. To be more clear, you have in your oppened frame from applet TWO BUTTONS one for back return to previous image, and one labeled with curent image number wich you can use to go to next pic. The labels are updated automatically depending on your action.

This version is one demonstrative for only 4 pictures named "1.gif", "2.gif", "3.gif", "4.gif". For adding more images please modify this lines in the source code :

String imgs[] = {"1.gif", "2.gif", "3.gif", "4.gif"};//add more names
Image images[] = new Image[4];//change 4 to your number

In this version is required that you have the images stored in your codebase+Images directory.
If you want to change the URL from where you load the images, please modif. next line :

images[i] = getImage(new URL(getCodeBase().toString()+"/Images/"+imgs[i]));

with
images[i] = getImage(new URL("Your URL"+imgs[i]));

More : You can send parameters from html Page with the number of images, and names of images.

More, next episode......
0
 

Author Comment

by:jeannieyeap
ID: 2455441
Thanks Ovi!!!
0
 
LVL 9

Expert Comment

by:Ovi
ID: 2455482
Me too...:)
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month5 days, 5 hours left to enroll

601 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