Resizing Applets from Java code - A second pass Ridimensionamento Applets da codice Java - Un secondo passaggio
Challenge: Resizing applet from within Java code. Sfida: Ridimensionare applet da codice Java. For example say the applet code calculates that the applet needs more space than it has been allocated, then how to go about it? Per esempio dire il codice applet calcola che l'applet richiede più spazio di quello che è stato assegnato, allora come procedere?
Last time when faced with this problem Ultima volta di fronte a questo problema I solved it with a nifty piece of Javascript method Ho risolto con un pezzo di nifty metodo Javascript . However I soon realized there were few issues with the approach. Tuttavia ho capito presto ci sono stati alcuni problemi con l'approccio.
First we made the applet dependent on external piece of Javascript method which creates undesirable dependency. In primo luogo abbiamo fatto l'applet dipende da esterno pezzo di Javascript metodo che crea dipendenza indesiderabili.
The main problem was when we wanted to embed multiple such applets in the same page. Il principale problema è stato quando abbiamo voluto incorporare più tali applet nella stessa pagina. Obviously now I cannot use the same resize method, which is hardcoded to call the applet by name. Ovviamente adesso non posso utilizzare lo stesso metodo di ridimensionamento, che è hardcoded di chiamare l'applet per nome. Also I found that document['appletname'] logic fails when the applet is within a table. Ho trovato anche questo documento [ 'appletname'] logica non quando l'applet è all'interno di una tabella. Then we have to refer to the applet using the hierarchy which we do not control. Quindi dobbiamo fare riferimento alla applet utilizzando la gerarchia che noi non controlliamo. We also cannot have two applets with the same name as then only the first one executes. Anche noi non può avere due applet con lo stesso nome come allora solo il primo esegue.
So it was obvious we needed to pass the name of the applet to this method. Così è stato evidente avevamo bisogno di passare il nome del applet a questo metodo. But how to get the name? Ma come ottenere il nome?
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. Ho intuito correttamente che, dal momento che applet.getParameter usato per ottenere i parametri dal di dentro l'applet tag, è possibile che siamo in grado di recuperare il nome del applet utilizzando lo stesso metodo. That turned out to be the case. Che si è rivelato essere il caso. 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. Così ora ho avuto questa versione, dove ho passato il nome di l'applet a ridimensionare il metodo e ha invitato l'applet con il suo nome (applet [ 'name']) e impostare la dimensione come trasmesso dal parametri. This worked fine with both the browsers ( Questo lavorato bene con entrambi i browser ( Internet Explorer Internet Explorer & Netscape ). However I was still not satisfied. Tuttavia ero ancora non soddisfatte. The external dependency was bugging me. La dipendenza esterna è stata bugging me. The next step was to try to execute javascript code within Java itself, using the same LiveConnect bridge. Il passo successivo è stato quello di tentare di eseguire codice JavaScript all'interno di Java stesso, utilizzando lo stesso LiveConnect ponte. My first attempt to set the size using Il mio primo tentativo di impostare la dimensione utilizzando JSObject.getMethod failed in IE. non in IE. Apparently Internet Explorer doesn’t support the method! Apparentemente di Internet Explorer non supporta il metodo! Then I simply evaluated the whole code using eval. Poi ho semplicemente valutato l'intero codice utilizzando eval. This worked well for both the browsers. Questo ha funzionato bene, sia per il browser. I could get rid of the pesky javascript method - resize. Ho potuto sbarazzarsi di pesky il metodo javascript - ridimensionare. Now finally I was happy. Ora finalmente ero felice. As I soon realized however there was still a little snag. Come ho realizzato presto tuttavia si è ancora un po 'intoppo. The fixes don’t work in Opera browser. Le correzioni non funzionano nel browser Opera. But I am not too worried considering the market share of this browser. Ma non sono troppo preoccupati considerando la quota di mercato di questo browser.
To summarize the key line of code: jso.eval(applet + “width = ” + width + “;”); Per riassumere la chiave riga di codice: jso.eval (applet + "width =" + larghezza + ";");
Filed under Elencato sotto Java Software Software Java , Web | |
| |
RSS 2.0 RSS 2,0 | |
Email this Article Invia questo articolo
You may also like to read Si può anche leggere come |





April 22nd, 2008 at 11:53 am 22 aprile 2008 11:53
import java.awt.*; importazione java.awt .*;
import java.awt.event.*; importazione java.awt.event .*;
import javax.swing.*; importazione javax.swing .*;
import javax.swing.JFileChooser; javax.swing.JFileChooser importazione;
import java.io.*; importazione java.io. *;
import javax.imageio.ImageIO; javax.imageio.ImageIO importazione;
class SceneViewer2 extends JFrame implements MouseListener, MouseMotionListener classe SceneViewer2 estende JFrame implementa MouseListener, MouseMotionListener
{ (
JFrame jf=new JFrame(”Your created Scene”); JFrame jf = new JFrame ( "Il tuo creato Scena");
JPanel jp=new JPanel(); JPanel jp = new JPanel ();
private int xAdjustment; INT xAdjustment privato;
private int yAdjustment; INT yAdjustment privato;
Component dragComponent; DragComponent componente;
public static void main(String args[]) public static void main (String args [])
{ (
new SceneViewer2(); nuovo SceneViewer2 ();
} )
SceneViewer2() SceneViewer2 ()
{ (
jf.setExtendedState (getExtendedState () | JFrame.MAXIMIZED_BOTH);//MAXIMIZES THE WINDOW ON EXECUTION jf.setExtendedState (getExtendedState () | JFrame.MAXIMIZED_BOTH); / / massimizza la finestra sul ESECUZIONE
jp = (JPanel) jf.getContentPane (); jp = (JPanel) jf.getContentPane ();
jp.setLayout (null); jp.setLayout (null);
jp.addMouseListener( this ); jp.addMouseListener (questo);
jp.addMouseMotionListener( this ); jp.addMouseMotionListener (questo);
JLabel[] lblPic=new JLabel[2]; JLabel [] lblPic = new JLabel [2];
//inserts 1st image / / inserti 1a immagine
lblPic[0]=new JLabel(); lblPic [0] = new JLabel ();
lblPic[0].setBounds(0,0,200,200); lblPic [0]. setBounds (0,0,200,200);
ImageIcon selected_image=new ImageIcon(”atif3.jpg”); ImageIcon selected_image = new ImageIcon ( "atif3.jpg");
Image sel=selected_image.getImage(); Immagine sel = selected_image.getImage ();
selected_image=new ImageIcon(sel.getScaledInstance(lblPic[0].getWidth(),lblPic[0].getHeight(),1)); selected_image = new ImageIcon (sel.getScaledInstance (lblPic [0]. getWidth (), lblPic [0]. getHeight (), 1));
lblPic[0].setIcon(selected_image); lblPic [0]. setIcon (selected_image);
jp.add(lblPic[0]); jp.add (lblPic [0]);
//inserts 2nd image / / inserti 2a immagine
lblPic[1]=new JLabel(); lblPic [1] = new JLabel ();
lblPic[1].setBounds(300,0,200,200); lblPic [1]. setBounds (300,0,200,200);
selected_image=new ImageIcon(”ash3.jpg”); selected_image = new ImageIcon ( "ash3.jpg");
sel=selected_image.getImage(); sel = selected_image.getImage ();
selected_image=new ImageIcon(sel.getScaledInstance(lblPic[1].getWidth(),lblPic[1].getHeight(),1)); selected_image = new ImageIcon (sel.getScaledInstance (lblPic [1]. getWidth (), lblPic [1]. getHeight (), 1));
lblPic[1].setIcon(selected_image); lblPic [1]. setIcon (selected_image);
jp.add(lblPic[1]); jp.add (lblPic [1]);
jp.repaint(); jp.repaint ();
jf.setVisible(true); jf.setVisible (vero);
} )
//CODE FOR MOVING THE IMAGES ON DRAG / / Codice per le immagini in movimento a trascinare
public void mousePressed(MouseEvent e) public void mousePressed (MouseEvent e)
{ (
Container container = (Container)e.getSource(); Contenitore contenitore = (container) e.getSource ();
Component component = container.findComponentAt(e.getX(), e.getY()); Componente componente = container.findComponentAt (e.getX (), e.getY ());
if (component instanceof JPanel) return; if (componente instanceof JPanel) return;
dragComponent = component; dragComponent = componente;
xAdjustment = dragComponent.getLocation().x - e.getX(); xAdjustment = dragComponent.getLocation (). x - e.getX ();
yAdjustment = dragComponent.getLocation().y - e.getY(); yAdjustment = dragComponent.getLocation (). y - e.getY ();
} )
public void mouseDragged(MouseEvent e) public void mouseDragged (MouseEvent e)
{ (
if (dragComponent == null) if (dragComponent == null)
return; ritorno;
dragComponent.setLocation(e.getX() + xAdjustment, e.getY() + yAdjustment); dragComponent.setLocation (e.getX () + xAdjustment, e.getY () + yAdjustment);
} )
public void mouseReleased(MouseEvent e) public void mouseReleased (MouseEvent e)
{ (
dragComponent = null; dragComponent = null;
} )
public void mouseClicked(MouseEvent e) {} public void mouseClicked (MouseEvent e) ()
public void mouseMoved(MouseEvent e) {} public void mouseMoved (MouseEvent e) ()
public void mouseEntered(MouseEvent e) {} public void mouseEntered (MouseEvent e) ()
public void mouseExited(MouseEvent e) {} public void mouseExited (MouseEvent e) ()
} )
this is my proggrame…. questo è il mio proggrame….
i want a resizing code so i can rezise the image also.. Voglio rivolgermi a un ridimensionamento del codice in modo che io possa rezise anche l'immagine ..