• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

converting into applet

 
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I need to convert the below coding into applet and want to add scrollbar to it..can anyone help me...

code:





import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Ellipse2D;
import java.text.NumberFormat;
import javax.swing.*;
public class GraphicOption extends JPanel
{
double[] data = { 20.2, 12.7, 19.0, 35.75, 16.2, 48.9, 31.0, 10.2 };
double[] data1 = { 20.2, 12.7, 19.0, 35.75, 16.2, 48.9, 31.0, 10.2 };
final int PAD = 20;
final int PROX_DIST = 10;
Point[] dataLocs;
NumberFormat nf;
GraphicOption()
{
nf = NumberFormat.getInstance();
nf.setMinimumFractionDigits(1);
nf.setMaximumFractionDigits(2);
dataLocs = new Point[data.length];
for(int j = 0; j < dataLocs.length; j++)
dataLocs[j] = new Point();
}
/** * The JComponent method summary section for this method* explains how to do this. See api for the details. */
public String getToolTipText(MouseEvent e)
{
Point p = e.getPoint();
for(int j = 0; j < dataLocs.length; j++)
{
if(p.distance(dataLocs[j]) < PROX_DIST)
{
String n1=" "+data[j]+data1[j];
return n1;
}
}
return null;
}
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
int w = getWidth();
int h = getHeight();
// Draw axes.
g2.drawLine(PAD, PAD, PAD, h-PAD);
g2.drawLine(PAD, h-PAD, w-PAD, h-PAD);

double scale = getScale(h);
// Origin location is (PAD, h-PAD).
// So y == zero at:
double y0 = h-PAD;
g2.setPaint(Color.red);
// Plot data.
for(int j = 0; j < data.length; j++)
{
double x = PAD + j*data1[j];
double y = y0 - scale*data[j];
// Set location for each point.
dataLocs[j].setLocation(x, y);
g2.fill(new Ellipse2D.Double(x-1.5, y-1.5, 4, 4));
}
}
private double getScale(int h)
{
double max = -Double.MAX_VALUE;
for(int j = 0; j < data.length; j++)
{
if(data[j] > max)
max = data[j];
}
return (h - 2*PAD)/max;
}
public static void main(String[] args)
{
JFrame f = new JFrame();
GraphicOption graphicOption = new GraphicOption();
DataSurveyor surveyor = new DataSurveyor(graphicOption, f);
graphicOption.addMouseMotionListener(surveyor);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(graphicOption);
f.setSize(400,400);
f.setLocation(200,200);
f.setVisible(true);
}
}
class DataSurveyor extends MouseMotionAdapter
{
GraphicOption component;
JWindow toolTip; JLabel label;
Point loc;
NumberFormat nf;
DataSurveyor(GraphicOption go, JFrame f)
{
component = go;
label = new JLabel();
label.setBorder(UIManager.getBorder("ToolTip.border"));
toolTip = new JWindow(f);
toolTip.getContentPane().setBackground(UIManager.getColor("ToolTip.background")); toolTip.getContentPane().add(label);
}
public void mouseMoved(MouseEvent e)
{
String text = component.getToolTipText(e);
boolean hover = false;
if(text != null)
{
if(loc == null)
{
Point p = e.getPoint();
SwingUtilities.convertPointToScreen(p, component);
label.setText(text);
int height = label.getPreferredSize().height;
loc = new Point(p.x, p.y-height);
toolTip.setLocation(loc);
toolTip.pack();
toolTip.setVisible(true);
}
hover = true;
}
if(!hover && toolTip.isVisible())
{
toolTip.dispose();
loc = null;}}}
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would start by creating a class that extends JApplet, and add the JPanel to its content pane. Once you got that, put the JScrollPane in between the JApplet and the JPanel.

As an aside, please UseCodeTags when posting code of an length.
 
shalini gnana
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No,i'm not getting your concept,since i'm new to java..
so,can you please modify the above coding and show me?
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The way we like to help people here at JavaRanch is to get them to arrive at a solution themselves, instead of showing them the solution. You'll learn much more that way.

Are you familiar with applets in general? If so, using a JApplet instead of an Applet should be no big change. If not, you might want to try and create a simple JApplet first, before doing anything complicated.

As to the content pane, in Swing components are added to the content pane of a container (and not to add the container itself, as they are in AWT). You can get the content pane from the JApplet by using the getContentPane method.

Does this make things clearer? If not, let us know in detail what you have so far, and what you're struggling with.
 
shalini gnana
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried to change but i my applet is not getting initialized



import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Ellipse2D;
import java.text.NumberFormat;
import javax.swing.*;
public class test extends JApplet
{
double[] data = { 20.2, 12.7, 19.0, 35.75, 16.2, 48.9, 31.0, 10.2 };
final int PAD = 20;
final int PROX_DIST = 10;
Point[] dataLocs;
NumberFormat nf;
test()
{
nf = NumberFormat.getInstance();
nf.setMinimumFractionDigits(1);
nf.setMaximumFractionDigits(2);
dataLocs = new Point[data.length];
for(int j = 0; j < dataLocs.length; j++)
dataLocs[j] = new Point();
}
/** * The JComponent method summary section for this method* explains how to do this. See api for the details. */
public String getToolTipText(MouseEvent e)
{
Point p = e.getPoint();
for(int j = 0; j < dataLocs.length; j++)
{
if(p.distance(dataLocs[j]) < PROX_DIST)
{
return nf.format(data[j]);
}
}
return null;
}
protected void paintComponent(Graphics g)
{
super.paintComponents(g) ;
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
int w = getWidth();
int h = getHeight();
// Draw axes.
g2.drawLine(PAD, PAD, PAD, h-PAD);
g2.drawLine(PAD, h-PAD, w-PAD, h-PAD);
double xInc = (w - 2*PAD)/(data.length-1);
double scale = getScale(h);
// Origin location is (PAD, h-PAD).
// So y == zero at:
double y0 = h-PAD;
g2.setPaint(Color.red);
// Plot data.
for(int j = 0; j < data.length; j++)
{
double x = PAD + j*xInc;
double y = y0 - scale*data[j];
// Set location for each point.
dataLocs[j].setLocation(x, y);
g2.fill(new Ellipse2D.Double(x-1.5, y-1.5, 4, 4));
}
}
private double getScale(int h)
{
double max = -Double.MAX_VALUE;
for(int j = 0; j < data.length; j++)
{
if(data[j] > max)
max = data[j];
}
return (h - 2*PAD)/max;
}
public void init()
{
Container cont = getContentPane();
JPanel panel = new JPanel();
JFrame f = new JFrame();
cont.add(panel);
test graphicOption = new test();
DataSurveyor surveyor = new DataSurveyor(graphicOption, f);
graphicOption.addMouseMotionListener(surveyor);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(graphicOption);
f.setSize(400,400);
f.setLocation(200,200);
f.setVisible(true);

}
class DataSurveyor implements MouseMotionListener
{
test component;
JWindow toolTip; JLabel label;
Point loc;
NumberFormat nf;
DataSurveyor(test go, JFrame f)
{
component = go;
label = new JLabel();
label.setBorder(UIManager.getBorder("ToolTip.border"));
toolTip = new JWindow(f);
toolTip.getContentPane().setBackground(UIManager.getColor("ToolTip.background"));
toolTip.getContentPane().add(label);
}
public void mouseMoved(MouseEvent e)
{
String text = component.getToolTipText(e);
boolean hover = false;
if(text != null)
{
if(loc == null)
{
Point p = e.getPoint();
SwingUtilities.convertPointToScreen(p, component);
label.setText(text);
int height = label.getPreferredSize().height;
loc = new Point(p.x, p.y-height);
toolTip.setLocation(loc);
toolTip.pack();
toolTip.setVisible(true);
}
hover = true;
}
if(!hover && toolTip.isVisible())
{
toolTip.dispose();
loc = null;
}
}

public void mouseDragged(MouseEvent e) {
}

}
}
 
shalini gnana
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I changed my constructor into Public and then i tried but still nothing was displayed in the Appletviewer...Can you able to give me solution?
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

my applet is not getting initialized


What does that mean? Are there any error messages in the Java Console? Which methods are or are not being called?



Your code should not call the constructor. The browser JVM does this for you. Try replacing the above lines by "DataSurveyor surveyor = new DataSurveyor(this, f);"

As an aside, the code is very hard to read the way you have posted it. As I asked you before, please UseCodeTags, and make sure that the indentation stays intact.
 
shalini gnana
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I changed my constructor into Public and then i tried but still nothing was displayed in the Appletviewer...Can you able to give me solution?
 
shalini gnana
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for replying(very patiently)....
Previously it thrown an error as,there is no public constructor or class..so,i changed my constuctor as public..
later,I made changes that you asked to..
But still only my appletviewer is showing,my graph was not showing...
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What does your init method look like now?
 
shalini gnana
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public init()
{

JPanel panel = new JPanel();
JFrame f = new JFrame();
cont.add(panel);
DataSurveyor surveyor = new DataSurveyor(this, f);
graphicOption.addMouseMotionListener(surveyor);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(graphicOption);
f.setSize(400,400);
f.setLocation(200,200);
f.setVisible(true);
}

And also i changed my paintComponent into paint()
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

f.getContentPane().add(graphicOption);


That looks wrong. Earlier, graphicOption was a newly created instance on the applet. When I pointed out that you should not create instances of Applet, you said you removed it. So what kind of object is it referencing now? Also, you can't add the applet to it like the above statement does 9assuming that's what graphicOption is).

The JPanel seems to serve no purpose. Try removing the lines


And also i changed my paintComponent into paint()


Swing does not use paint, but paintComponent, so you should undo that change.
 
shalini gnana
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for replying constantly...
If not a costructor then what..
I worked well with swing why not with applet..
Swing will not use pain(),
but JApplet will..I'm right?
I to convert the coding into JApplet.
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

If not a costructor then what..
It worked well with swing why not with applet..


Applications work differently than applets. Generally, an applet should not have a constructor, and any initialization should be done in the init method.

Swing will not use paint(), but JApplet will..I'm right?


No. JApplet is a Swing class (like everything else in the javax.swing package).
 
shalini gnana
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes...Now i'm getting the concept that i should not use constructor in Applet..
But,I have no idea how to proceed...
i.e.,what to do to the constructor and the refer to the "DataSurveyor class"

Thank you
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Whatever is in the constructor should go into the init method.

You can pass "this" to any class that needs a reference to the applet.
 
shalini gnana
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks alot...Now the problem is my Scrollbar is getting not getting displayed once the screen is launched...and along with the applet viewer one more window is getting displated...can use JFrame in JApplet because it is also a high level container....

Now my init() is:

JFrame f = new JFrame();
JScrollBar hbar = new JScrollBar(JScrollBar.HORIZONTAL,30,40,0,300);
JScrollBar vbar = new JScrollBar(JScrollBar.VERTICAL,30,40,0,300);
hbar.setUnitIncrement(2);
vbar.setUnitIncrement(2);
add(hbar,BorderLayout.SOUTH);
add(vbar,BorderLayout.EAST);
f.add(new JScrollBar());
f.setVisible(true);
f.setSize(300,300);
test graphicOption = new test();
DataSurveyor surveyor = new DataSurveyor(this, f);
this.addMouseMotionListener(surveyor);

}
 
shalini gnana
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moreover,while executing it in the IE,along with the tooltip,one more message is getting displayed ,the message is 'java window applet' Why so? what might be the problem?
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A JFrame is a window by itself, so as long as you have one, there will be two windows.

test graphicOption = new test();


You really need to remove this, like I said before.
 
shalini gnana
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I removed that that quote but still i'm getting the same error as a mention in the previous...another Java window applet screen is also getting displayed
So,i should not use JFrame..I did the entire thing in JFrame..so, instead of that how can i change?

Thank you
[ October 10, 2007: Message edited by: shalini gnana ]
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can absolutely use JFrame. I mentioned it because you said you didn't want two windows. If now you're saying that it's fine to have two windows (the appletviewer window and the JFrame), then there's no problem.

I'm wondering, though, what good the JFrame does, and why you couldn't display whatever you're displaying in it, display in the applet itself instead. Is the JFrame necessary?
 
shalini gnana
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because i want to add scrollbar/scrollPane

while executing i want only the appletviewer to be displayed..But along with that i'm getting one more window,'java applet Window'...In IE along with the tooltip message,'java applet window ' is getting displayed please give me a solution..

thank you..
<code>

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Ellipse2D;
import java.text.NumberFormat;
import javax.swing.*;
public class test extends JApplet
{
double[] data = {100.0,99,45,66,32,90,20.2, 12.7, 19.0, 35.75, 16.2, 48.9, 31.0, 10.2 ,78.9,89,50};
double[] data1 = {45 ,43,56,89,12,67,90,100,200,250,20.2, 12.7, 19.0, 45.00, 16.2, 48.9, 31.0};
final int PAD = 20;
final int PROX_DIST = 10;
Point[] dataLocs;
NumberFormat nf;


public test()
{
nf = NumberFormat.getInstance();
nf.setMinimumFractionDigits(1);
nf.setMaximumFractionDigits(2);
dataLocs = new Point[data.length];
for(int j = 0; j < dataLocs.length; j++)
dataLocs[j] = new Point();
}
/** * The JComponent method summary section for this method* explains how to do this. See api for the details. */
public String getToolTipText(MouseEvent e)
{
Point p = e.getPoint();
for(int j = 0; j < dataLocs.length; j++)
{
if(p.distance(dataLocs[j]) < PROX_DIST)
{
String n1= "BH"+" " +data[j]+", "+data1[j];
return n1;
}
}
return null;
}
public void paint(Graphics g)
{
//super.paintComponent(g) ;
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
int w = getWidth();
int h = getHeight();
// Draw axes.
g2.drawLine(PAD, PAD, PAD, h-PAD);
g2.drawLine(PAD, h-PAD, w-PAD, h-PAD);
double xInc = (w - 2*PAD)/(data.length-1);
double scale = getScale(h);
// Origin location is (PAD, h-PAD).
// So y == zero at:
double y0 = h-PAD;
g2.setPaint(Color.red);
// Plot data.
for(int j = 0; j < data.length; j++)
{
double x = PAD + j*xInc;
double y = y0 - scale*data[j];
// Set location for each point.
dataLocs[j].setLocation(x, y);
g2.fill(new Ellipse2D.Double(x-1.5, y-1.5, 4, 4));
}
}
private double getScale(int h)
{
double max = -Double.MAX_VALUE;
for(int j = 0; j < data.length; j++)
{
if(data[j] > max)
max = data[j];
}
return (h - 2*PAD)/max;
}
public void init()
{

JFrame f = new JFrame();
JScrollBar hbar = new JScrollBar(JScrollBar.HORIZONTAL,30,40,0,300);
JScrollBar vbar = new JScrollBar(JScrollBar.VERTICAL,30,40,0,300);
hbar.setUnitIncrement(2);
vbar.setUnitIncrement(2);
add(hbar,BorderLayout.SOUTH);
add(vbar,BorderLayout.EAST);
f.add(new JScrollBar());
f.setVisible(true);
f.setSize(300,300);
test graphicOption = new test();
DataSurveyor surveyor = new DataSurveyor(this, f);
this.addMouseMotionListener(surveyor);

}
class DataSurveyor extends JApplet implements MouseMotionListener
{
test component;
JWindow toolTip; JLabel label;
Point loc;
NumberFormat nf;
DataSurveyor(test go, JFrame f)
{
component = go;
label = new JLabel();
label.setBorder(UIManager.getBorder("ToolTip.border"));
toolTip = new JWindow(f);
toolTip.getContentPane().setBackground(UIManager.getColor("ToolTip.background"));
toolTip.getContentPane().add(label);
}
public void mouseMoved(MouseEvent e)
{
String text = component.getToolTipText(e);
boolean hover = false;
if(text != null)
{
if(loc == null)
{
Point p = e.getPoint();
SwingUtilities.convertPointToScreen(p, component);
label.setText(text);
int height = label.getPreferredSize().height;
loc = new Point(p.x, p.y-height);
toolTip.setLocation(loc);
toolTip.pack();
toolTip.setVisible(true);
}
hover = true;
}
if(!hover && toolTip.isVisible())
{
toolTip.dispose();
loc = null;}}

public void mouseDragged(MouseEvent e) {
}
}
}

</code>
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pleas edit your last post (by clicking on its icon) so that it properly uses the code tags. As I said twice before, the code is unreadable like this.

I'm willing to help you, but you need to put a little effort into it.
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please re-read my last post. I didn't say "RE-POST the code and put QUOTE tags around it", I said "EDIT your last post to fix the CODE tags".
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Something seems to have changed, but not the code formatting, which is as illegible as before. Please take a moment to verify what you post after posting it.

If you are unsure what code tags are, or how they look like and work, please read this.
 
shalini gnana
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have edited..

Because i want to add scrollbar/scrollPane

while executing i want only the appletviewer to be displayed..But along with that i'm getting one more window,'java applet Window'...In IE along with the tooltip message,'java applet window ' is getting displayed please give me a solution..

thank you..
<code>
[Package]
</import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Ellipse2D;
import java.text.NumberFormat;
import javax.swing.*; />
[/Package]
[code]
public class test extends JApplet
{
double[] data ={100.0,99,45,66,32,90,20.2, 12.7, 19.0, 35.75,16.2, 48.9,31.0, 10.2 ,78.9,89,50};
double[] data1 ={45 ,43,56,89,12,67,90,100,200,250,20.2, 12.7,19.0, 45.00, 16.2, 48.9, 31.0};
final int PAD = 20;
final int PROX_DIST = 10;
Point[] dataLocs;
NumberFormat nf;
[constructor of class test]
public test()
{
nf = NumberFormat.getInstance();
nf.setMinimumFractionDigits(1);
nf.setMaximumFractionDigits(2);
dataLocs = new Point[data.length];
for(int j = 0; j < dataLocs.length; j++)
dataLocs[j] = new Point();
}
[The JComponent method summary section for this method* explains how to do this. See api for the details. */]
[</returning the co-ordinates value>]
public String getToolTipText(MouseEvent e)
{
Point p = e.getPoint();
for(int j = 0; j < dataLocs.length; j++)
{
if(p.distance(dataLocs[j]) < PROX_DIST)
{
String n1= "BH"+" " +data[j]+", "+data1[j];
return n1;
}
}
return null;
}
[</returning the co-ordinates value>]
[</Plotting the points> ]
public void paint(Graphics g)
{
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint (RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
int w = getWidth();
int h = getHeight();
[ Draw axes. ]
g2.drawLine(PAD, PAD, PAD, h-PAD);
g2.drawLine(PAD, h-PAD, w-PAD, h-PAD);
double xInc = (w - 2*PAD)/(data.length-1);
double scale = getScale(h);
[ Origin location is (PAD, h-PAD). ]
[ So y == zero at: ]
double y0 = h-PAD;
g2.setPaint(Color.red);
[ Plot data. ]
for(int j = 0; j < data.length; j++)
{
double x = PAD + j*xInc;
double y = y0 - scale*data[j];
[ Set location for each point. ]
dataLocs[j].setLocation(x, y);
g2.fill(new Ellipse2D.Double(x-1.5, y-1.5, 4, 4));
}
[/draw axis]
}

private double getScale(int h)
{
double max = -Double.MAX_VALUE;
for(int j = 0; j < data.length; j++)
{
if(data[j] > max)
max = data[j];
}
return (h - 2*PAD)/max;
}
[Plotting the points>]
[init method>]
public void init()
{
JFrame f = new JFrame();
JScrollBar hbar = new JScrollBar(JScrollBar.HORIZONTAL,30,40,0,300);
JScrollBar vbar = new JScrollBar(JScrollBar.VERTICAL,30,40,0,300);
hbar.setUnitIncrement(2);
vbar.setUnitIncrement(2);
add(hbar,BorderLayout.SOUTH);
add(vbar,BorderLayout.EAST);
f.add(new JScrollBar());
f.setVisible(true);
f.setSize(300,300);
test graphicOption = new test();
DataSurveyor surveyor = new DataSurveyor(this, f);
this.addMouseMotionListener(surveyor);
}
[init method>]
[getting the tooltip value]
class DataSurveyor extends JApplet implements MouseMotionListener
{
test component;
JWindow toolTip; JLabel label;
Point loc;
NumberFormat nf;
DataSurveyor(test go, JFrame f)
{
component = go;
label = new JLabel();
label.setBorder(UIManager.getBorder("ToolTip.border"));
toolTip = new JWindow(f);
toolTip.getContentPane().setBackground(UIManager.getColor ("ToolTip.background"));
toolTip.getContentPane().add(label);
}
public void mouseMoved(MouseEvent e)
{
String text = component.getToolTipText(e);
boolean hover = false;
if(text != null)
{
if(loc == null)
{
Point p = e.getPoint();
SwingUtilities.convertPointToScreen(p, component);
label.setText(text);
int height = label.getPreferredSize().height;
loc = new Point(p.x, p.y-height);
toolTip.setLocation(loc);
toolTip.pack();
toolTip.setVisible(true);
}
hover = true;
}
if(!hover && toolTip.isVisible())
{
toolTip.dispose();
loc = null;}}
[code]
public void mouseDragged(MouseEvent e) {}
}
}

I tried my best!!!

this what you want me to do?
Kindly help me...
 
shalini gnana
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have edited..

Because i want to add scrollbar/scrollPane

while executing i want only the appletviewer to be displayed..But along with that i'm getting one more window,'java applet Window'...In IE along with the tooltip message,'java applet window ' is getting displayed please give me a solution..

thank you..
<code>
[Package]
</import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Ellipse2D;
import java.text.NumberFormat;
import javax.swing.*; />
[/Package]
[code]
public class test extends JApplet
{
double[] data ={100.0,99,45,66,32,90,20.2, 12.7, 19.0, 35.75,16.2, 48.9,31.0, 10.2 ,78.9,89,50};
double[] data1 ={45 ,43,56,89,12,67,90,100,200,250,20.2, 12.7,19.0, 45.00, 16.2, 48.9, 31.0};
final int PAD = 20;
final int PROX_DIST = 10;
Point[] dataLocs;
NumberFormat nf;
[constructor of class test]
public test()
{
nf = NumberFormat.getInstance();
nf.setMinimumFractionDigits(1);
nf.setMaximumFractionDigits(2);
dataLocs = new Point[data.length];
for(int j = 0; j < dataLocs.length; j++)
dataLocs[j] = new Point();
}
[The JComponent method summary section for this method* explains how to do this. See api for the details. */]
[</returning the co-ordinates value>]
public String getToolTipText(MouseEvent e)
{
Point p = e.getPoint();
for(int j = 0; j < dataLocs.length; j++)
{
if(p.distance(dataLocs[j]) < PROX_DIST)
{
String n1= "BH"+" " +data[j]+", "+data1[j];
return n1;
}
}
return null;
}
[</returning the co-ordinates value>]
[</Plotting the points> ]
public void paint(Graphics g)
{
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint (RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
int w = getWidth();
int h = getHeight();
[ Draw axes. ]
g2.drawLine(PAD, PAD, PAD, h-PAD);
g2.drawLine(PAD, h-PAD, w-PAD, h-PAD);
double xInc = (w - 2*PAD)/(data.length-1);
double scale = getScale(h);
[ Origin location is (PAD, h-PAD). ]
[ So y == zero at: ]
double y0 = h-PAD;
g2.setPaint(Color.red);
[ Plot data. ]
for(int j = 0; j < data.length; j++)
{
double x = PAD + j*xInc;
double y = y0 - scale*data[j];
[ Set location for each point. ]
dataLocs[j].setLocation(x, y);
g2.fill(new Ellipse2D.Double(x-1.5, y-1.5, 4, 4));
}
[/draw axis]
}

private double getScale(int h)
{
double max = -Double.MAX_VALUE;
for(int j = 0; j < data.length; j++)
{
if(data[j] > max)
max = data[j];
}
return (h - 2*PAD)/max;
}
[Plotting the points>]
[init method>]
public void init()
{
JFrame f = new JFrame();
JScrollBar hbar = new JScrollBar(JScrollBar.HORIZONTAL,30,40,0,300);
JScrollBar vbar = new JScrollBar(JScrollBar.VERTICAL,30,40,0,300);
hbar.setUnitIncrement(2);
vbar.setUnitIncrement(2);
add(hbar,BorderLayout.SOUTH);
add(vbar,BorderLayout.EAST);
f.add(new JScrollBar());
f.setVisible(true);
f.setSize(300,300);
test graphicOption = new test();
DataSurveyor surveyor = new DataSurveyor(this, f);
this.addMouseMotionListener(surveyor);
}
[init method>]
[getting the tooltip value]
class DataSurveyor extends JApplet implements MouseMotionListener
{
test component;
JWindow toolTip; JLabel label;
Point loc;
NumberFormat nf;
DataSurveyor(test go, JFrame f)
{
component = go;
label = new JLabel();
label.setBorder(UIManager.getBorder("ToolTip.border"));
toolTip = new JWindow(f);
toolTip.getContentPane().setBackground(UIManager.getColor ("ToolTip.background"));
toolTip.getContentPane().add(label);
}
public void mouseMoved(MouseEvent e)
{
String text = component.getToolTipText(e);
boolean hover = false;
if(text != null)
{
if(loc == null)
{
Point p = e.getPoint();
SwingUtilities.convertPointToScreen(p, component);
label.setText(text);
int height = label.getPreferredSize().height;
loc = new Point(p.x, p.y-height);
toolTip.setLocation(loc);
toolTip.pack();
toolTip.setVisible(true);
}
hover = true;
}
if(!hover && toolTip.isVisible())
{
toolTip.dispose();
loc = null;}}
[code]
public void mouseDragged(MouseEvent e) {}
}
}

I tried my best!!!

this what you want me to do?
Kindly help me...
 
shalini gnana
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Applet Window is also getting displayed while i execute the program along with the applet viewer...What can i do to avoid this...
Or else is there any other simple way to add tooltip?

Kindly reply me..Its quite urgent...

Thank you..
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic