File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Applets and the fly likes JavaScript to Applet Communication Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Applets
Bookmark "JavaScript to Applet Communication" Watch "JavaScript to Applet Communication" New topic
Author

JavaScript to Applet Communication

vineet kumar chaudhry
Ranch Hand

Joined: Apr 02, 2011
Posts: 45
Hi JAVA Lovers

I wanna render the graph on the applet as the radio button changes i.e. it should show the graph for selected radio button

[code]
Applet Code

import java.applet.Applet;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.Shape;
import java.awt.Stroke;

import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JPanel;

import org.jCharts.*;
import org.jCharts.axisChart.AxisChart;
import org.jCharts.axisChart.customRenderers.axisValue.renderers.ValueLabelPosition;
import org.jCharts.axisChart.customRenderers.axisValue.renderers.ValueLabelRenderer;
import org.jCharts.chartData.AxisChartDataSet;
import org.jCharts.chartData.DataSeries;
import org.jCharts.chartData.processors.AxisChartDataProcessor;
import org.jCharts.properties.AxisProperties;
import org.jCharts.properties.ChartProperties;
import org.jCharts.properties.LegendProperties;
import org.jCharts.properties.LineChartProperties;
import org.jCharts.properties.PointChartProperties;
import org.jCharts.test.TestDataGenerator;
import org.jCharts.types.ChartType;
import LineGraph.*;
public class LineGraph extends JApplet {

AxisChart axisChart;
Container container;
private JPanel panel;
String date[]={"11","12","13"};
double[] passed={12, 22, 32};
double[] failed={42, 12, 6};
double[] skipped={10, 8, 2};

public void init(){

try{

String[] xAxisLabels = date;
String xAxisTitle= "Date";
String yAxisTitle= "Count";
String title= "History";

this.container = getContentPane();
this.container.setLayout(null);
this.container.setSize(500, 700);
this.panel = new JPanel(true);
this.panel.setSize(500, 500);
this.container.add(this.panel);
this.container.setVisible(true);

double[]s1 = passed;
double[]s2 = failed;
double[]s3 = skipped;
double[][] data= new double[][]{s1,s2,s3};

String[] legendLabels= { "Passed","Failed","Skipped" };
Paint[] paints= new Paint[]{ Color.green,Color.red,Color.yellow };

Stroke[] strokes= new Stroke[ 3 ];
strokes[ 0 ]= new BasicStroke( 5.0f);
strokes[ 1 ]= new BasicStroke( 5.0f );
strokes[ 2 ]= new BasicStroke( 5.0f );
//Stroke[] strokes= { LineChartProperties.DEFAULT_LINE_STROKE,LineChartProperties.DEFAULT_LINE_STROKE};
Shape[] shapes= { PointChartProperties.SHAPE_CIRCLE ,PointChartProperties.SHAPE_CIRCLE,PointChartProperties.SHAPE_CIRCLE};
LineChartProperties lineChartProperties= new LineChartProperties( strokes, shapes );

AxisChartDataSet axisChartDataSet= new AxisChartDataSet( data, legendLabels, paints, ChartType.LINE, lineChartProperties );

DataSeries dataSeries = new DataSeries( xAxisLabels, xAxisTitle, yAxisTitle, title );

dataSeries.addIAxisPlotDataSet(axisChartDataSet);



ChartProperties chartProperties= new ChartProperties();
AxisProperties axisProperties= new AxisProperties();
LegendProperties legendProperties= new LegendProperties();

legendProperties.setFontPaint(Color.green.darker());
legendProperties.setPlacement( LegendProperties.LEFT );
legendProperties.setNumColumns( 1 );

/*ValueLabelRenderer valueLabelRenderer = new ValueLabelRenderer(true, true, true, 0);
valueLabelRenderer.setValueLabelPosition(ValueLabelPosition.AXIS_BOTTOM);
valueLabelRenderer.useVerticalLabels(false);

lineChartProperties.addPostRenderEventListener(valueLabelRenderer);*/

axisChart= new AxisChart( dataSeries, chartProperties, axisProperties, legendProperties, 400, 400 );



}catch(Exception e){}

}

public void paint(Graphics g) {
try {
System.out.println("Inside paint");
this.axisChart.setGraphics2D((Graphics2D)this.container.getGraphics());
this.axisChart.render();
} catch (Exception e) {
System.out.println(e.getMessage()); }
}

public void makeGraph(String str){

String[] date={"111","121","131"};
double[] passed={123, 223, 323};
double[] failed={422, 122, 62};
double[] skipped={120, 82, 21};
getContentPane().removeAll();



try{

String[] xAxisLabels = date;
String xAxisTitle= "Date";
String yAxisTitle= "Count";
String title= "History";

this.container = getContentPane();
this.container.setLayout(null);
this.container.setSize(500, 700);
this.panel = new JPanel(true);
this.panel.setSize(500, 500);
this.container.add(this.panel);
this.container.setVisible(true);

double[]s1 = passed;
double[]s2 = failed;
double[]s3 = skipped;
double[][] data= new double[][]{s1,s2,s3};

String[] legendLabels= { "Passed","Failed","Skipped" };
Paint[] paints= new Paint[]{ Color.green,Color.red,Color.yellow };

Stroke[] strokes= new Stroke[ 3 ];
strokes[ 0 ]= new BasicStroke( 5.0f);
strokes[ 1 ]= new BasicStroke( 5.0f );
strokes[ 2 ]= new BasicStroke( 5.0f );
//Stroke[] strokes= { LineChartProperties.DEFAULT_LINE_STROKE,LineChartProperties.DEFAULT_LINE_STROKE};
Shape[] shapes= { PointChartProperties.SHAPE_CIRCLE ,PointChartProperties.SHAPE_CIRCLE,PointChartProperties.SHAPE_CIRCLE};
LineChartProperties lineChartProperties= new LineChartProperties( strokes, shapes );

AxisChartDataSet axisChartDataSet= new AxisChartDataSet( data, legendLabels, paints, ChartType.LINE, lineChartProperties );

DataSeries dataSeries = new DataSeries( xAxisLabels, xAxisTitle, yAxisTitle, title );

dataSeries.addIAxisPlotDataSet(axisChartDataSet);



ChartProperties chartProperties= new ChartProperties();
AxisProperties axisProperties= new AxisProperties();
LegendProperties legendProperties= new LegendProperties();

legendProperties.setFontPaint(Color.green.darker());
legendProperties.setPlacement( LegendProperties.LEFT );
legendProperties.setNumColumns( 1 );

/*ValueLabelRenderer valueLabelRenderer = new ValueLabelRenderer(true, true, true, 0);
valueLabelRenderer.setValueLabelPosition(ValueLabelPosition.AXIS_BOTTOM);
valueLabelRenderer.useVerticalLabels(false);

lineChartProperties.addPostRenderEventListener(valueLabelRenderer);*/

axisChart= new AxisChart( dataSeries, chartProperties, axisProperties, legendProperties, 400, 400 );

this.axisChart.setGraphics2D((Graphics2D)this.container.getGraphics());
this.axisChart.render();

}catch(Exception e){}



}
}

Here I have taken the hard code values in data[],passed[,failed,skipped[]

I have also tried with only provding the value for data[],passed[,failed,skipped[]
in makeGraph(String){}

Please also let me know what is the flow in these programs,As far as I know init() is called whenever the applet first loads.Thus in this case our public f() {makeGraph(String sr){}} is called first or the init() is called first,

HTML Code

<html>
<HEAD>
<TITLE>
JavaScript to Applet Communication
</TITLE >
<SCRIPT LANGUAGE="JavaScript">
function selectedCity()
{
if(document.CityChoice.City[0].checked == true)
{
document.applets['LineGraph'].makeGraph(document.CityChoice.City[0].value);
}
else if(document.CityChoice.City[1].checked == true)
{
document.applets['LineGraph'].makeGraph(document.CityChoice.City[1].value);
}
else if(document.CityChoice.City[2].checked == true)
{
document.applets['LineGraph'].makeGraph(document.CityChoice.City[2].value);
}
}
</SCRIPT>
</HEAD>
<BODY >
<b>This is the Applet</b>
<br><br>
<APPLET ARCHIVE ="LineGraph3.jar" CODE="LineGraph.class" NAME="LineGraph" WIDTH=600 HEIGHT=400 >
</APPLET >

<br><br><hr><br><br>
<b>This the HTML with Java Script</b>
<br>
(Select the radio button to send message to the above Applet)
<br>
<FORM NAME="CityChoice">
<input type="radio" name="City" value="user" onClick="selectedCity()"> User<br>
<input type="radio" name="City" value="admin" onClick="selectedCity()"> Admin<br>
<input type="radio" name="City" value="contact" onClick="selectedCity()"> Contact
</form>

<br><br><hr><br><br>

</BODY >
</html>


[code]

The initial graphs renders on the page but it doesnt changes with change in radio button
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JavaScript to Applet Communication