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:
Till now I have succeeded in partitioning the screen and creating separate panels for the each part and adding the corresponding components in them.
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?
/*<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);
}
}}
Start with the container used in ImageViewer. It centers the image inside a scroll-pane whose scroll-bars appear reliably.
setSize(..)
in an applet. The size is in the HTML.