The Artima Developer Community
Sponsored Link

Java Answers Forum
How to get this work?

2 replies on 1 page. Most recent reply: Jun 5, 2002 9:44 AM by Thomas Dautzenberg

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Thomas Dautzenberg

Posts: 7
Nickname: dautz99
Registered: Jun, 2002

How to get this work? Posted: Jun 4, 2002 12:29 PM
Reply to this message Reply
Advertisement
I want to use InputStream to read a file. but I don't want to use the filename in the html code of the applet (like param name=inputfile value="data1.txt">
I used this code but it doesn't work (actually nothing is shown when i open the html file). I had it working before but somehow now it doesn't.
This is my code:

/********************************************/
/* Kruskal.java */
/* Copyright (C) 1997, 1998, 2000 K. Ikeda */
/********************************************/

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
import java.net.URL;

class Node {
int x;
int y;
int set;
int first;
int next;
int w;
int h;
String name;
}

class Edge {
int rndd_plus; /* initial vertex of this edge */
int rndd_minus; /* terminal vertex of this edge */
int len; /* length */
int select;
String name;
}

public class Kruskal extends Applet implements MouseListener {
String inputfile = "data1.txt";
int n,m;
int num,den;
int u, usel, step;
Node v[] = new Node[100];
Edge e[] = new Edge[200];
int idx[] = new int[200];

int findNode(String name) {
for (int i=0; i<n; i++)
if (v.name.equals(name))
return i;
return -1;
}

void input_graph(InputStream is) throws IOException {

int x,y,l;
String s;

Reader r = new BufferedReader(new InputStreamReader(is));
StreamTokenizer st = new StreamTokenizer(r);
st.commentChar('#');
st.nextToken(); n = (int)st.nval;
st.nextToken(); m = (int)st.nval;
st.nextToken(); s = st.sval;
for (int i = 0; i<n; i++) {
Node node = new Node();
st.nextToken(); node.name = st.sval;
st.nextToken(); node.x = (int)st.nval;
st.nextToken(); node.y = (int)st.nval;
v = node;
}
for (int i = 0; i<m; i++) {
Edge edge = new Edge();
st.nextToken(); edge.name = st.sval;
switch (st.nextToken()) {
case StreamTokenizer.TT_NUMBER:
edge.rndd_plus = (int)st.nval;
break;
case StreamTokenizer.TT_WORD:
edge.rndd_plus = findNode(st.sval);
break;
default:
break;
}
switch (st.nextToken()) {
case StreamTokenizer.TT_NUMBER:
edge.rndd_minus = (int)st.nval;
break;
case StreamTokenizer.TT_WORD:
edge.rndd_minus = findNode(st.sval);
break;
default:
break;
}
st.nextToken(); edge.len = (int)st.nval;
e = edge;
}
for (int i=0; i<m; i++)
e.select = -1;
den = 0;
num = 128;
for (int i=0; i<m; i++)
if (e.len>den)
den = e.len;
step1();

step = 2;
}

void swap(int i, int j) {
int k = idx;

idx = idx[j];
idx[j] = k;
}

int partition(int left, int right) {
int pivot = e[idx[(int)((left+right)/2)]].len;

while (left<=right) {
while (e[idx[left]].len < pivot)
left++;
while (e[idx[right]].len > pivot)
right--;
if (left <= right)
swap(left++,right--);
}
return left;
}

void qsort(int left, int right) {
int i;

if (left >= right)
return;
i = partition(left,right);
qsort(left,i-1);
qsort(i,right);
}

void step1() { /* initialize */
for (int i=0; i<m; i++)
idx = i;
for (int i=0; i<m; i++)
e.select = -1;
qsort(0,m-1);
for (int i=0; i<m; i++)
e.select = -1;
for (int i=0; i<n; i++) {
v.set = i;
v.first = i;
v.next = -1;
}
usel = u = 0;
}

void step2() { /* select the shortest edge */
e[idx].select = 1; /* pick up the edge */
}

void step3() { /* check the loop */
int vl = e[idx].rndd_plus;
int vr = e[idx].rndd_minus;
int i,j,k;

if (v[vl].set == v[vr].set) {
e[idx[u++]].select = -2; /* de-select the edge */
return;
}
usel ++;
e[idx[u++]].select = 2; /* select the edge */
for (i = vl; v.next>=0; i = v.next)
;
v.next = v[vr].first;
j = v[vl].first;
k = v[vl].set;
for (i = v[vr].first; i>=0; i = v.next) {
v.first = j;
v.set = k;
}
}

void step4() {
for (; u<m; u++)
e[idx].select = -2;
}

public void init() {
String mdname = "inputfile";
if ( mdname != null){
inputfile = new String(mdname);
}
try {
InputStream is;
is = new URL(inputfile).openStream();
input_graph(is);
try {
if (is != null)
is.close();
} catch(Exception e) {
}
} catch (FileNotFoundException e) {
System.err.println("File not found.");
} catch (IOException e) {
System.err.println("Cannot access file.");
}

setBackground(Color.white);
addMouseListener(this);

}


public void paintNode(Graphics g, Node n, FontMetrics fm) {
String s;
int x = n.x;
int y = n.y;
int w = fm.stringWidth(n.name) + 10;
int h = fm.getHeight() + 4;
n.w = w;
n.h = h;

g.setColor(Color.black);
g.drawRect(x-w/2,y-h/2,w,h);

g.setColor(getBackground());
g.fillRect(x-w/2+1,y-h/2+1,w-1,h-1);

g.setColor(Color.black);
g.drawString(n.name,x-(w-10)/2,(y-(h-4)/2)+fm.getAscent());
}

int [] xy(int a, int b, int w, int h) {
int x[] = new int[2];

if (Math.abs(w*b)>=Math.abs(h*a)) {
x[0] = ((b>=0)?1:-1)*a*h/b/2;
x[1] = ((b>=0)?1:-1)*h/2;
} else {
x[0] = ((a>=0)?1:-1)*w/2;
x[1] = ((a>=0)?1:-1)*b*w/a/2;
}
return x;
}

void drawEdge(Graphics g,int x1,int y1,int x2,int y2) {
g.drawLine(x1,y1,x2,y2);
}

public void paintEdge(Graphics g, Edge e, FontMetrics fm) {
Node v1 = v[e.rndd_plus];
Node v2 = v[e.rndd_minus];

int a = v1.x-v2.x;
int b = v1.y-v2.y;

int x1[] = xy(-a,-b,v1.w,v1.h);
int x2[] = xy(a,b,v2.w,v2.h);

if (e.select == -1 )
g.setColor(Color.black);
else if (e.select == -2)
g.setColor(Color.lightGray);
else if (e.select == 1)
g.setColor(Color.blue);
else
g.setColor(Color.red);
drawEdge(g,v1.x+x1[0],v1.y+x1[1],v2.x+x2[0],v2.y+x2[1]);

int w = fm.stringWidth("" + e.len);
int h = fm.getHeight();

g.setColor(getBackground());
g.fillRect((v1.x+v2.x-w)/2,(v1.y+v2.y-h)/2,w,h);

if (e.select == -1 )
g.setColor(Color.black);
else if (e.select == -2)
g.setColor(Color.lightGray);
else if (e.select == 1)
g.setColor(Color.blue);
else
g.setColor(Color.red);
g.drawString("" + e.len,(v1.x+v2.x-w)/2,(v1.y+v2.y-h)/2+fm.getAscent());
}

public void paint(Graphics g) {
FontMetrics fm = g.getFontMetrics();
for (int i=0; i<n; i++)
paintNode(g,v,fm);
for (int i=0; i<m; i++)
paintEdge(g,e,fm);

}

public void update(Graphics g) {
paint(g);
}

public void mousePressed(MouseEvent e) {
if (step == 1) {
step1();
step = 2;
} else if (usel >= n-1) {
step4();
step = 1;
} else {
if (step == 3) {
step3();
step = 2;
} else {
step2();
step = 3;
}
}
repaint();
}
public void mouseClicked(MouseEvent event) {}
public void mouseReleased(MouseEvent event) {}
public void mouseEntered(MouseEvent event) {}
public void mouseExited(MouseEvent event) {}
}


Galahady Gao

Posts: 4
Nickname: galahady
Registered: Jun, 2002

Re: How to get this work? Posted: Jun 4, 2002 1:44 PM
Reply to this message Reply
Maybe the Security Manager restrict your applet
to get the file.

Where is your "Data1.txt" located? Is it on the
same computer where the applet runs? or is it
on the server? If Data1.txt is on the server,
than you will be able to read this file fairly
easy; however, you must supply the accurate URL
to the file. You didn't post the HTML, and the
URL in your code is not complete, so I figure
that you didn't supply the CODEBASE parameter.

But if the data1.txt is on the machine where
the applet runs, and this machine is not the
server where the applet classes resides in,
then you should to know something about
Security Manager and the browser specific
security capability.

Anyway, hope this helps you a little.

Thomas Dautzenberg

Posts: 7
Nickname: dautz99
Registered: Jun, 2002

Re: How to get this work? Posted: Jun 5, 2002 9:44 AM
Reply to this message Reply
This is my html code:
<title>Kruskal Shortest Path Algorithm</title>
<applet code="Kruskal.class" width=565 height=400>
</applet>
and data1.txt is in same directory as Kruskal.class/java, not on a server but on my pc
and changed the code into (but still doesn't work):

String mdname = "inputfile";
if ( mdname != null){
inputfile = new String(mdname);

try {
InputStream is;
is = new URL(getCodeBase),mdname).openStream();
input_graph(is);

Flat View: This topic has 2 replies on 1 page
Topic: help with blackjack game GUI Previous Topic   Next Topic Topic: problem with classes

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use