This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Five Lines of Code and have Christian Clausen on-line!
See this thread for details.
Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

HYPERLINKING FROM APPLETS

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello everyone..
Is there a method to link to an URL when we click on applets?
i.e I wrote an applet drawn with two rectangles.My idea is to link to an URL when I click on one of the rectangles.
Can I do this??If so,how can I do this?
please help me.
thanks
chaitu
 
author and iconoclast
Posts: 24203
43
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use showDocument(URL) method in AppletContext -- i.e.,

If you use this form of the method, the new page will replace your applet; if you want the applet to stay, there's an overloaded version that takes a second argument, a String "target" that names an HTML frame in which to display the other document.
 
Chaitanya Reddy
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Mr.Ernest Friedman-Hill ,it worked.
My applets are to be loaded online.But there is a refresh problem with my applets.If I maximize the webpage having my applets,the content of my applet is getting changed.
What should I do to avoid such problems??
Please reply me
thanks
chaitu
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24203
43
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Resizing the window may cause the stop() method and then the start() method of your applet to be called. You need to make sure you implement the applet lifecycle methods correctly, or resizing and browsing can mess your applet up. For example, you should set up your GUI in the init() method, not the start() method. Also, anything that you do in start(), you should undo in stop().
 
Chaitanya Reddy
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could u please describe more in detail about how to go about it.

Thanks
chaitu
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24203
43
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about instead you post your applet code and I'll critique it?
P.S. Use tags, please!
[ August 13, 2003: Message edited by: Ernest Friedman-Hill ]
 
Chaitanya Reddy
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
This is the code for my applet.Please tell me the changes for efficiency.
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.net.*;
//<applet code="Pro" width=900 height=200 ></applet>
public class Pro extends Applet implements ActionListener,MouseMotionListener
{
Button jb1,jb2;
int rect1s=-1,rect1e=-1,rect2s=-1,rect2e=-1,ovals=-1,ovale=-1;
String upstream=new String("");
String downstream=new String("");
int mouseX=0,mouseY=0,x,m;
TextField tf=new TextField(20);
public void init()
{
setBackground(Color.white);
addMouseMotionListener(this);
}
public void start()
{
System.out.println("Hey it started");
}
public void paint(Graphics g)
{
rect1s=900;
rect1e=950;
rect2s=2500;
rect2e=3000;
ovals=1500;
ovale=1800;
upstream="29389";
downstream="45466";
jb1=new Button(upstream);
jb2=new Button(downstream);
x=250+(int)(500*(ovals-rect1e)/(rect2s-rect1e));
m=(int)(500*(ovale-ovals)/(rect2s-rect1e));
g.setColor(Color.magenta);
jb1.setBounds(10,100,100,30);
jb1.setBackground(Color.orange);
jb1.setForeground(Color.black);
jb1.addActionListener(this);
add(jb1);
jb2.setBounds(700,100,100,30);
jb2.setBackground(Color.orange);
jb2.setForeground(Color.black);
jb2.addActionListener(this);
add(jb2);
g.setColor(Color.pink);
g.drawLine(100,125,700,125);
g.setColor(Color.green);
g.fillOval(x-40,100,m,50);
tf.setBounds(20,20,100,20);
add(tf);
g.setColor(Color.black);
g.drawString("UPSTREAM ",30,170);
g.drawString(""+(int)ovals,x-40,170);
g.drawString(""+(int)ovale,x+m-40,170);
g.drawString("DOWNSTREAM",710,170);
g.drawString(String.valueOf(ovals-rect1e)+"bp",(200+x)/2,150);
g.drawString(String.valueOf(rect2s-ovale)+"bp",x+m+50,150);
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==jb1)
{
try
{

AppletContext ac=getAppletContext();
ac.showDocument(new URL("http://www.ncbi.nlm.nih.gov:80/entrez/query.fcgi?cmd=Retrieve&db=protein&list_uids="+upstream));
}catch(Exception xe){}
}
if(e.getSource()==jb2)
{
try
{
AppletContext ac=getAppletContext();
ac.showDocument(new URL("http://www.ncbi.nlm.nih.gov:80/entrez/query.fcgi?cmd=Retrieve&db=protein&list_uids="+downstream));
}catch(Exception xe){}
}
}
public void mouseMoved(MouseEvent me)
{
mouseX=me.getX();
mouseY=me.getY();
if((mouseX>=(x-40))&&(mouseX<=500))
tf.setText("yes u got it");
else
tf.setText("");
}
public void mouseDragged(MouseEvent me){}
}
If u don't understand the code,please let me know.

thanks
chaitu
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24203
43
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aaaaaaaak!
You're setting up your GUI in the paint() method! That's crazy!
paint() is called every time the window is partially obscured, resized, or even moved, so obviously, your GUI is going to grow many buttons very quickly!
Move everything from paint() that doesn't directly use the Graphics object "g" into the init() method -- i.e., creating the buttons, calling add() on them, etc. The "drawing" code does indeed belong in the paint() method, so that you can leave where it is.
 
Chaitanya Reddy
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks..
It worked..

regards
chaitu
 
I like you because you always keep good, crunchy cereal in your pantry. This tiny ad agrees:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic