Resizing Applets from Java code - A second pass

By Angsuman Chakraborty, Gaea News Network
Thursday, February 12, 2004

Challenge: Resizing applet from within Java code. For example say the applet code calculates that the applet needs more space than it has been allocated, then how to go about it?

Last time when faced with this problem I solved it with a nifty piece of Javascript method. However I soon realized there were few issues with the approach.

First we made the applet dependent on external piece of Javascript method which creates undesirable dependency.

The main problem was when we wanted to embed multiple such applets in the same page. Obviously now I cannot use the same resize method, which is hardcoded to call the applet by name. Also I found that document['appletname'] logic fails when the applet is within a table. Then we have to refer to the applet using the hierarchy which we do not control. We also cannot have two applets with the same name as then only the first one executes.
So it was obvious we needed to pass the name of the applet to this method. But how to get the name?
I intuited correctly that since applet.getParameter used to get parameters from within the applet tag, it is possible that we can fetch the name of the applet using the same method. That turned out to be the case. So now I had this version where I passed the name of the applet to the resize method and it called the applet by its name ( applets['name'] ) and set the size as passed on by the parameters. This worked fine with both the browsers (Internet Explorer & Netscape). However I was still not satisfied. The external dependency was bugging me. The next step was to try to execute javascript code within Java itself, using the same LiveConnect bridge. My first attempt to set the size using JSObject.getMethod failed in IE. Apparently Internet Explorer doesn’t support the method! Then I simply evaluated the whole code using eval. This worked well for both the browsers. I could get rid of the pesky javascript method - resize. Now finally I was happy. As I soon realized however there was still a little snag. The fixes don’t work in Opera browser. But I am not too worried considering the market share of this browser.

To summarize the key line of code: jso.eval(applet + “width = ” + width + “;”);

Filed under: Java Software, Web
Discussion

Jimmy
April 22, 2008: 11:53 am

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JFileChooser;
import java.io.*;
import javax.imageio.ImageIO;

class SceneViewer2 extends JFrame implements MouseListener, MouseMotionListener
{
JFrame jf=new JFrame(”Your created Scene”);
JPanel jp=new JPanel();
private int xAdjustment;
private int yAdjustment;
Component dragComponent;

public static void main(String args[])
{
new SceneViewer2();
}

SceneViewer2()
{
jf.setExtendedState (getExtendedState () | JFrame.MAXIMIZED_BOTH);//MAXIMIZES THE WINDOW ON EXECUTION
jp = (JPanel) jf.getContentPane ();
jp.setLayout (null);
jp.addMouseListener( this );
jp.addMouseMotionListener( this );

JLabel[] lblPic=new JLabel[2];

//inserts 1st image
lblPic[0]=new JLabel();
lblPic[0].setBounds(0,0,200,200);
ImageIcon selected_image=new ImageIcon(”atif3.jpg”);
Image sel=selected_image.getImage();
selected_image=new ImageIcon(sel.getScaledInstance(lblPic[0].getWidth(),lblPic[0].getHeight(),1));

lblPic[0].setIcon(selected_image);

jp.add(lblPic[0]);

//inserts 2nd image
lblPic[1]=new JLabel();
lblPic[1].setBounds(300,0,200,200);
selected_image=new ImageIcon(”ash3.jpg”);
sel=selected_image.getImage();
selected_image=new ImageIcon(sel.getScaledInstance(lblPic[1].getWidth(),lblPic[1].getHeight(),1));

lblPic[1].setIcon(selected_image);

jp.add(lblPic[1]);

jp.repaint();
jf.setVisible(true);
}

//CODE FOR MOVING THE IMAGES ON DRAG
public void mousePressed(MouseEvent e)
{
Container container = (Container)e.getSource();
Component component = container.findComponentAt(e.getX(), e.getY());

if (component instanceof JPanel) return;

dragComponent = component;
xAdjustment = dragComponent.getLocation().x - e.getX();
yAdjustment = dragComponent.getLocation().y - e.getY();

}

public void mouseDragged(MouseEvent e)
{
if (dragComponent == null)
return;

dragComponent.setLocation(e.getX() + xAdjustment, e.getY() + yAdjustment);
}

public void mouseReleased(MouseEvent e)
{
dragComponent = null;
}

public void mouseClicked(MouseEvent e) {}
public void mouseMoved(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
}

this is my proggrame….

i want a resizing code so i can rezise the image also..

YOUR VIEW POINT
NAME : (REQUIRED)
MAIL : (REQUIRED)
will not be displayed
WEBSITE : (OPTIONAL)
YOUR
COMMENT :