hi guys i am doing an applet that reads information from a website, stores it and uses it to graph the data and displays it in a very friendly way. it works perfectly fine in the viewer when i run it in eclipse but as soon as i try to use it in a html the webpage turns gray and doesnt display anything. Here is the code:
the main class
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class MainGUI extends java.applet.Applet implements Runnable {
int xaxis[]=new int[1000];
int yaxisp[];
double xdata[];
double ydata[];
int Adx=-800;
String dates;
//Double Buffering
private Image dbImage;
private Graphics dbg;
//Clear Screen
boolean clear;
Thread th;
public void init(){
setSize(1000,600);
//System.out.println(htmldata[1]);
for(int k=0;k<=32;k++){
xaxis[k]=k*100;
}
}
public void start(){
if(th==null)
{
th=new Thread(this);
th.start();
}
}
public void stop(){
if(th!=null)
{
th.stop();
th=null;
}
}
public String now(String dateFormat) {
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
return sdf.format(cal.getTime());
}
public void run(){
while(true){
repaint();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
}
}
}
public void clear(Graphics g)
{
// if(!clear)return; // optional
g.setColor(getBackground());
g.fillRect(0,0,(int)this.getWidth(),(int)this.getHeight());
clear=false;
}
public void update(Graphics g){
if(dbImage==null){
dbImage=createImage(this.getSize().width,this.getSize().height);
dbg=dbImage.getGraphics();
}
dbg.setColor(getBackground());
dbg.fillRect(0, 0, this.getSize().width, this.getSize().height);
dbg.setColor(getForeground());
paint(dbg);
g.drawImage(dbImage, 0, 0, this);
}
public void paint(Graphics g){
Graph spr=new Graph();
spr.paintComponent(g, xaxis);
g.setColor(Color.BLACK);
g.drawString("Real Time Energy Price For "+ now("MMMMM dd, yyyy hh:mm:ss"), 290, 13);
}
}
the code that is called in the paint method which is the graph
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;
public class Graph extends MainGUI{
private int x=120;
private int y=500;
private double data[]=new double[5000];
private int xtime;
private int price=0;
boolean flagresize=false;
private int multiresize=4;
private int priceresize=10;
private double maxvalue=0;
String htmldata[]=new String[1000];
public int RoundOff (double maxvalue2)
{
return (((int)Math.round(maxvalue2 / 10.0)) * 10)+10;
}
public void paintComponent(Graphics g,int xaxis[]) {
webdata w=new webdata();
htmldata=w.TheArray();
// htmldata[0]="22.55";
// htmldata[1]= "50.10";
for(int k=0;k<=94;k++){
try{
data[k]=Double.parseDouble(htmldata[k]);
}catch (java.lang.NullPointerException exception)
{
data[k]=0;
}
if(data[k]<0){
try{
data[k]=data[k-1];
}catch (java.lang.NullPointerException exception)
{
data[k]=0;
}
}
try{
if(maxvalue>=data[k]){
}else{
maxvalue=data[k];
}
}catch (java.lang.ArrayIndexOutOfBoundsException exception)
{}
if(data[k]>=150){
flagresize=true;
priceresize=28;
multiresize=1;
}
//System.out.println(data[k]);
}
maxvalue=RoundOff(maxvalue);
Graphics2D g2 = (Graphics2D) g;
Font font1 = new Font("Courier New",Font.BOLD+Font.PLAIN,14);
g2.setFont(font1);
xtime=(xaxis[0]/4)+128;
g2.drawLine((xaxis[0]/4)+120, 500,( xaxis[29]/4)+114, 500);
//g.drawLine(50, 500, 50, 50);
for(int k=0;k<=24;k++){
if(k==0){
g2.setColor(Color.BLACK);
}else{
g2.setColor(Color.GRAY);
}
g2.drawLine(x, 500, x, 20);
g2.setColor(Color.BLACK);
g2.drawString(Integer.toString(k), x-5, 520);
x=x+32;
}
g2.drawString("Time", 465, 550);
g2.drawString("Price(Dlls)", 4, 230);
g2.drawString("Per MWHr", 10, 250);
x=120;
for(int k=0;k<=20;k++){
if(k==0){
g2.setColor(Color.BLACK);
}else{
g2.setColor(Color.GRAY);
}
g2.drawLine(x, y, (xaxis[29]/4)+163, y);
g2.setColor(Color.BLACK);
g2.drawString(Integer.toString(price),x-24,y);
price+=maxvalue/10;
y-=30;
}
//ReadData r=new ReadData();
//data=r.LoadLevels();
for(int k=0;k<=96;k++){
g2.setColor(Color.RED);
//g.drawLine(xtime, 500-(int)data[k+1]*3,xtime+8, 500-(int)data[k+1]*3);
g2.setStroke(new BasicStroke(3));
try{
if(data[k+1]==0){
}else if(data[k]==0){
}else{
g2.draw(new Line2D.Float(xtime, 500-((int)data[k]*30/((int)maxvalue/10)),xtime+8, 500-(int)data[k+1]*30/((int)maxvalue/10)));
}
}catch (java.lang.ArrayIndexOutOfBoundsException exception)
{}
xtime+=8;
}
g2.clearRect(890, 10, 50, 500);
}
}
then the part that is in font red is called. this is the one that reads the webpage and manipulates the information to meet my standards. this is where i believe is the problem after doing several tests but i do not know what is the problem inside this part.like i said before it works perfectly fine in the viewer but not in web browser.
import java.io.*;
import java.net.URL;
public class webdata {
String htm[]=new String[5000];
int x=0;
int space=18;
String h[]=new String[5000];
public String[] TheArray(){
String u="http://www.ercot.com/content/cdr/html/real_time_spp";
URL url;
InputStream is;
InputStreamReader isr;
BufferedReader in;
try
{
url=new URL(u);
is=url.openStream();
isr=new InputStreamReader(is);
in=new BufferedReader(isr);
String line;
for(int k=0;k<=38;k++){
line = in.readLine();
}
while ((line = in.readLine()) != null) //file reading
{
String[] values = line.split("<><>><<>");
for (String str : values)
{
htm[x]=str;
try{
htm[x]=htm[x].substring(31,36);
}catch (java.lang.StringIndexOutOfBoundsException exception)
{
//Solve the problem
}
x++;
}
}
in.close();
}catch( IOException ioException ) {}
h[0]=htm[4];
for(int k=0;k<=94;k++){
try{
h[k+1]=htm[space+4];
}catch (java.lang.StringIndexOutOfBoundsException exception)
{
h[k+1]="0";
}
space=space+18;
}
return h;
}
}