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;}}}