• 0

java applet loop indefinately


Question

Hi, my java applet loops indefinately!

Is this normal? (I'm about 3 weeks into java here)

Also, can I get it to stop?

The thing is, I use the javax.swing.JOptionPane package to ask the user for a question, but it keeps on asking the same question!!!!

Yes, very annoying!!

Help plz :)

thx

Link to comment
Share on other sites

3 answers to this question

Recommended Posts

  • 0

Ya sure

Please help!

//Tech151.htm file

<HTML>

<HEAD>

</HEAD>

<BODY BGCOLOR="000000">

<CENTER>

<APPLET

code = "GraphApplet.class"

width = "820"

height = "500"

>

</APPLET>

</CENTER>

</BODY>

</HTML>

// dispGraph class

import java.awt.Graphics2D;

import java.awt.geom.Rectangle2D;

public class dispGraph

{

private double pos_x;

private double pos_y;

public dispGraph(double x, double y)

{

pos_x = x; // x and y positions

pos_y = y;

}

public void draw(Graphics2D g2, int height)

{

Rectangle2D.Double body = new Rectangle2D.Double(pos_x, pos_y, 40, height);

g2.draw(body);

}

}

Link to comment
Share on other sites

  • 0

// main class (how do you use the 'pre' tag with this board?)

import java.applet.Applet;

import java.awt.Graphics;

import java.awt.Graphics2D;

import javax.swing.JOptionPane;

public class GraphApplet extends Applet // Applet! :)

{

int testifdone;

public static int findLoopTimes(int testifdone)

{

if (testifdone != 5)

{

String inputNum = JOptionPane.showInputDialog("Input the number of times 2 dice should be rolled.\nNote: A higher number will result in much more valid results");

int num = Integer.parseInt(inputNum);

testifdone = 5;

return num;

}

int nothing = -1;

return nothing;

}

public void paint(Graphics g)

{

int die1, die2, addtoo, largest, loopfor;

int rollResults[] = new int[100000];

int resultsPerc[] = new int[100000];

loopfor = findLoopTimes(testifdone);

for (int j = 1; j < 13; j++) // initializes array to 0

{

rollResults[j] = 0;

}

for (int i = 1; i <= loopfor; i++) // randomly generate dice rolles x number of times

{

die1 = (int) (Math.random()*6 + 1); // random dice roll #1 returns as int

die2 = (int) (Math.random()*6 + 1); // random dice roll #2 returns as int

addtoo = die1 + die2;

rollResults[addtoo] = rollResults[addtoo] + 1; // adds one to the array that counts how many times the sum of the dice equals that number

}

for (int k = 2; k < 13; k++) // dice roll count

System.out.println("Hit a " + k + ": " + rollResults[k]);

largest = rollResults[2];

for (int l = 3; l < 13; l++) // finds largest number

{

if (rollResults[l] > largest)

largest = rollResults[l];

}

System.out.println("largest: "+largest); // prints larggest number

for (int m = 2; m < 13; m++) //puts numbers in a new array (resultsPerc) that organizes them as a percentage of the largest number (for graphing later)

{

String str = Integer.toString(rollResults[m]);

double d = Double.valueOf(str).doubleValue(); // converts to double so we can calculate them

resultsPerc[m] = (int) ((d/largest)*100); // sets percentages as whole numbers (ints)

System.out.println(resultsPerc[m]);

}

Graphics2D g2 = (Graphics2D)g;

for (int n = 2; n < 13; n++) // this prints the bar graph, uses dispGraph class

{

dispGraph data = new dispGraph(n * 55, 100+(100-resultsPerc[n])*3); // gotta raise the height so it looks like the graphs come UP from the bottom

data.draw(g2, resultsPerc[n]*3); // prints graph

String hi = Integer.toString(n);

g.drawString(hi, n*55+20, 70); // number 1, 2, 3.. etc.

String str = Integer.toString(rollResults[n]); // convert number to string

g.drawString(str, n*55+5, 420); // prints string which is the number of times a specific number has been randomly reached

}

g.drawString("Number of times", 5, 420);

g.drawString("sum of that number", 5, 432);

g.drawString("has been reached", 5, 444);

}

}

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.