Search code examples
javaimageswingappletjapplet

Display multiple images in a single applet


I have to build a small Java applet for a project. I have previously never used applets. Hence I don't know much about the various inbuilt functions available. The layout of this applet is as below:

  1. The screen is divided into 3 parts
  2. Top most part will have a set of buttons and text boxes
  3. Middle part and bottom part will be displaying different images
  4. These Images can vary in size when the program is run each time (Hence it requires scroll bars in case the image goes out of the screen)

Till now I have succeeded in partitioning the screen and creating separate panels for the each part and adding the corresponding components in them.

Problem:

The bottom image is not completely visible. And also scroll bars are not appearing for each Image when it does not fit into the panel.

I tried using setSize(), setMinimumSize() methods but it does not produce any changes in the output. Can you please help me with the above problem?

This is what I have done till now:

/*<applet code=DOSlayout.java width=400 height=400>
</applet>*/ 

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class DOSlayout extends JApplet implements ActionListener {

    Button ViewButton;
    Panel1 Top;
    Panel2 LeftSide;
    Panel3  RightSide;
    Label l1,l2,l3;
    
    Image img;
    
    public void init() {

        setSize(400,400);
        setLayout(new BorderLayout());
        
        Top = new Panel1();
        LeftSide = new Panel2();
        RightSide = new Panel3();
        
        Top.setSize(getSize().width, getSize().height/3);
        LeftSide.setSize(getSize().width,getSize().height/3); 
        RightSide.setSize(getSize().width,getSize().height/3);

//RightSide.setMinimumSize (new Dimension(400, 10000));
        
        add(Top, BorderLayout.NORTH);
        add(LeftSide, BorderLayout.CENTER);
        add(RightSide, BorderLayout.SOUTH);
        
        ViewButton = new Button("View");

        
        l1 = new Label("North");
        l2 = new Label("East");
        l3 = new Label("West");
        
                    
        Top.add(ViewButton);
        
        
        Top.add(l1);        
        //LeftSide.add(l2);
        //RightSide.add(l3);
        
        ViewButton.addActionListener(this);     
    }
    
    
    @Override
    public void actionPerformed(ActionEvent arg0) {
        // TODO Auto-generated method stub
        
    }

class Panel1 extends JPanel {

Panel1() {
super();
}

public void paint(Graphics g) {
}

}

class Panel2 extends JPanel {

Panel2() {
super();
}

   private Image img;

   public void init()
   {
      img = null;
   }
   public void loadImage()
   {
      try
      { 
         img = getImage(getCodeBase(), "input1.png");
      }
      catch(Exception e) { }
   }
   public void paint(Graphics g)
   {
      if (img == null)
         loadImage();
      g.drawImage(img, 0, 0, this);
      //g.drawImage(img,0,0,(int)getBounds().getWidth(), (int)getBounds().getHeight(),this);
   }

}

class Panel3 extends JPanel {

Panel3() {
super();
}

   private Image img;

   public void init()
   {
      img = null;
//setSize(400,400);
   }
   public void loadImage()
   {
      try
      { 
         img = getImage(getCodeBase(), "input2.png");
      }
      catch(Exception e) { }
   }
   public void paint(Graphics g)
   {
      if (img == null)
         loadImage();
      g.drawImage(img, 0, 0, this);
//g.drawImage(img,0,0,(int)getBounds().getWidth(), (int)getBounds().getHeight(),this);
   }
}}

Solution

  • Start with the container used in ImageViewer. It centers the image inside a scroll-pane whose scroll-bars appear reliably.

    Further tips

    1. "I have previously never used applets." That is ominous, given applets are tougher to develop & deploy than frames. I suggest develop this first in a JFrame (then launch the frame from a link using Java Web Start). Only after seeing the frame launched using JWS should you consider using an applet. By that stage, any 'conversion' needed will be much simpler.
    2. Don't ever call setSize(..) in an applet. The size is in the HTML.
    3. Don't mix Swing and AWT components unless necessary, use all Swing.
    4. It is typically a bad idea to set the sizes (preferred, max or min) of any components.
    5. Please learn common Java naming conventions (specifically the case used for the names) for class, method & attribute names & use it consistently.