New Posts  All Forums:Forum Nav:

project help

post #1 of 7
Thread Starter 
i am writing a simple java program that does various things because we were just supposed to take some of the stuff we have learned in my class so far and write a program. the problem is that i can't seem to figure out how to make it so that when they input the number 3 it runs a java applet... i have it so when they input 1 it generates a random number, and when they input 2 it calculates pi, but i think to have it run an applet i have to keep the class files seperate and somehow make one class start the applet. how do you do that?

here's the main program so far.
Code:
package EthansWork;
import javax.swing.JOptionPane;


public class messin {


/**
 * 
 */
private static final long serialVersionUID = 1L;

public static void main(String[] args) {
String numStr = null;
int num = 0;
int again = 0;
do
{

System.out.println("Please tell me what you want to do.");
System.out.println("To generate a random number type 1");
System.out.println("To Find out what PI equals, type 2");
System.out.println("To see 10 colors and their color codes, type 3");




numStr = JOptionPane.showInputDialog("enter an integer, 1, 2, or 3 according to the instructions.");


if ( numStr != null)
{
try{
num = Integer.parseInt(numStr);
} catch (java.lang.NumberFormatException e)
{
System.out.println ("That was not a Integer!");
JOptionPane.showMessageDialog(null, "That was not a Integer!");

}

System.out.println("Your choice was: " + numStr);
System.out.println();


if (num == 1)
{
int Random =  (int) (Math.random() * 1000);

System.out.println("Thinking.");
try {
Thread.sleep (2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Thinking..");
try {
Thread.sleep (2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Thinking...");
try {
Thread.sleep (2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Your random number is: " + Random);
JOptionPane.showMessageDialog(null, "Your random number is: " + Random);
again = JOptionPane.showConfirmDialog (null, "Do Another?");

}
else
if (num == 2)
{

double PI = (Math.PI);

System.out.println("Thinking.");
try {
Thread.sleep (2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Thinking..");
try {
Thread.sleep (2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Thinking...");
try {
Thread.sleep (2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

System.out.println("PI Equals: " + PI);
System.out.println();
JOptionPane.showMessageDialog(null, "PI Equals: " + PI);
again = JOptionPane.showConfirmDialog (null, "Do Another?");
}
else 
if (num == 3){

System.out.println("Loading.");
try {
Thread.sleep (2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Loading..");
try {
Thread.sleep (2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Loading...");
try {
Thread.sleep (2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Here is where i want it to run the applet

}
else
System.out.println ("you didn't choose 1, 2 or 3!!!");


}
else 
{
System.exit(0);
}
}

while (again == JOptionPane.YES_OPTION);

}


}
and here is the applet i want it to run
Code:
package EthansWork;
import java.applet.Applet;
import java.awt.*;
import java.util.Random;

public class Colors extends Applet
{
/**
 * 
 */
private static final long serialVersionUID = 1L;
int i = 0;

public void paint (Graphics page)
{
while (true){
if (i <= 10){

i++;
// Declare size constraints
final int PAGE_WIDTH = 600;
final int PAGE_HEIGHT = 400;
Random generator = new Random ();
int colorCode = generator.nextInt();

// Declare variables
int x, y; // x and y coordinates of upper left corner of each shape
int width, height; // width and height of each shape

// Start color code reader.
Color myColor = new Color (colorCode);
int redCode = myColor.getRed();
int greenCode = myColor.getGreen();
int blueCode = myColor.getBlue();

// Set background color and paint the screen with a white rectangle
setBackground (Color.white);
page.setColor(Color.white);
page.fillRect (0, 0, PAGE_WIDTH, PAGE_HEIGHT);

// set color for the rectangle
page.setColor (myColor);

// Assign the corner width and height then draw
x = 10;
y = 10;
width = 200;
height = 150;


page.fillRect (x, y, width, height);
page.drawString("Red: " + redCode, x, y);
page.drawString("Green: " + greenCode, x+55, y);
page.drawString("Blue: " + blueCode, x+120, y);
try {
Thread.sleep (990);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else
System.exit(0);
}
}
}
i have tried alot of things but i just can't figure it out. any help?
post #2 of 7
You have to make a Colors object and have it perform run() IIRC
It goes to eleven
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6300 DS3 EVGA 8600GTS 2GB XMS2 DDR2-800 
Hard DriveOSMonitorKeyboard
1.294 TB Arch Linux/XP Samsung 226bw Eclipse II 
PowerCaseMouse
Corsair 520HX Lian-Li v1000B Plus G7 
  hide details  
Reply
It goes to eleven
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6300 DS3 EVGA 8600GTS 2GB XMS2 DDR2-800 
Hard DriveOSMonitorKeyboard
1.294 TB Arch Linux/XP Samsung 226bw Eclipse II 
PowerCaseMouse
Corsair 520HX Lian-Li v1000B Plus G7 
  hide details  
Reply
post #3 of 7
Thread Starter 
Thanks, i found a site that talks about this but it says that the applet has to have a
public void run()
{
}
method. i have no idea what i'm supposed to put in that method if i want to run the applet.
post #4 of 7
I'm a little fuzzy on applets but I think I can help. You should make a thread in the initialization method and start it in another method - I don't remember what these methods are called anymore. In the run method (which actually belongs to the thread) you have all the code that you want executed every time the applet calls that method inside of a while(true) loop - so as long as the thread is alive it's running. At the end you have <your thread>.sleep(<time>); where time is the milliseconds between calls of run(). That explanation isn't very clear so here's an example. Lets say you want an applet that just counts and outputs the number.

Code:
public class Counter extends Applet

Thread t;
int num;

//constructor here

public void init()
{
     t = new Thread(this);//creates a thread that is contained in this class
//That means run() will be in this class
     num=0;
}

public void start()
{
  t.start();
}

public void stop()
{
     t.stop();
}

public void run()
{
  while(true)
  {
      paint();//method not included - just draws num to the screen
      num++;

      t.sleep(20);
   }
}
}
That should run if you add the paint method that applets use. If you have questions about the code just post
It goes to eleven
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6300 DS3 EVGA 8600GTS 2GB XMS2 DDR2-800 
Hard DriveOSMonitorKeyboard
1.294 TB Arch Linux/XP Samsung 226bw Eclipse II 
PowerCaseMouse
Corsair 520HX Lian-Li v1000B Plus G7 
  hide details  
Reply
It goes to eleven
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6300 DS3 EVGA 8600GTS 2GB XMS2 DDR2-800 
Hard DriveOSMonitorKeyboard
1.294 TB Arch Linux/XP Samsung 226bw Eclipse II 
PowerCaseMouse
Corsair 520HX Lian-Li v1000B Plus G7 
  hide details  
Reply
post #5 of 7
Thread Starter 
when i put type
Code:
run()
{
paint()
}
into the applet
and then
Code:
Colors color = new Colors();
color.run();
into the application, it wants an argument to be passed... but the only argument it will let me pass is paint(null), which causes errors.
post #6 of 7
I'm not really sure what the exact methods are so the problem is probably that the names are wrong. Just look up an applet tutorial for the correct titles
It goes to eleven
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6300 DS3 EVGA 8600GTS 2GB XMS2 DDR2-800 
Hard DriveOSMonitorKeyboard
1.294 TB Arch Linux/XP Samsung 226bw Eclipse II 
PowerCaseMouse
Corsair 520HX Lian-Li v1000B Plus G7 
  hide details  
Reply
It goes to eleven
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6300 DS3 EVGA 8600GTS 2GB XMS2 DDR2-800 
Hard DriveOSMonitorKeyboard
1.294 TB Arch Linux/XP Samsung 226bw Eclipse II 
PowerCaseMouse
Corsair 520HX Lian-Li v1000B Plus G7 
  hide details  
Reply
post #7 of 7
Thread Starter 
well i figured it out, here is how i did it.
Code:
package EthansWork;
import java.io.IOException;
import javax.swing.JOptionPane;


public class messin {


/**
 * 
 */
private static final long serialVersionUID = 1L;

public static void main(String[] args) throws IOException {
String numStr = null;
int num = 0;
int again = 0;
do
{

System.out.println("Please tell me what you want to do.");
System.out.println("To generate a random number type 1");
System.out.println("To Find out what PI equals, type 2");
System.out.println("To see 10 colors and their color codes, type 3");
System.out.println("To roll some dice, type 4");

numStr = JOptionPane.showInputDialog("enter an integer, 1, 2, 3, or 4 according to the instructions.");


if ( numStr != null)
{
try{
num = Integer.parseInt(numStr);
} catch (java.lang.NumberFormatException e)
{
System.out.println ("That was not a Integer!");
JOptionPane.showMessageDialog(null, "That was not a Integer!");

}

System.out.println("Your choice was: " + numStr);
System.out.println();


if (num == 1)
{
int Random =  (int) (Math.random() * 1000);

System.out.println("Thinking.");
try {
Thread.sleep (2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Thinking..");
try {
Thread.sleep (2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Thinking...");
try {
Thread.sleep (2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Your random number is: " + Random);
JOptionPane.showMessageDialog(null, "Your random number is: " + Random);
again = JOptionPane.showConfirmDialog (null, "Do Another?");

}
else
if (num == 2)
{

double PI = (Math.PI);

System.out.println("Calculateing.");
try {
Thread.sleep (2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Calculateing..");
try {
Thread.sleep (2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Calculateing...");
try {
Thread.sleep (2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

System.out.println("PI Equals: " + PI);
System.out.println();
JOptionPane.showMessageDialog(null, "PI Equals: " + PI);
again = JOptionPane.showConfirmDialog (null, "Do Another?");
}
else 
if (num == 3){

System.out.println("Loading.");
try {
Thread.sleep (2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Loading..");
try {
Thread.sleep (2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Loading...");
try {
Thread.sleep (2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

Runtime.getRuntime().exec("c:\\\\Program Files\\\\Mozilla Firefox\\\\firefox.exe \\"d:\\\\Documents and Settings\\\\Ethan\\\\workspace\\\\messin\\\\EthansWork.Colors.html\\"");
again = JOptionPane.showConfirmDialog (null, "Do Another?");
}
else
if(num == 4)
{
System.out.println("Loading.");
try {
Thread.sleep (2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Loading..");
try {
Thread.sleep (2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Loading...");
try {
Thread.sleep (2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

Runtime.getRuntime().exec("c:\\\\Program Files\\\\Mozilla Firefox\\\\firefox.exe \\"d:\\\\Documents and Settings\\\\Ethan\\\\workspace\\\\messin\\\\EthansWork.RollingDice.html\\"");
again = JOptionPane.showConfirmDialog (null, "Do Another?");
}
else
System.out.println ("you didn't choose 1, 2 or 3!!!");


}
else 
{
System.exit(0);
}
}

while (again == JOptionPane.YES_OPTION);

}


}
It opens the applet in firefox, and as long as the computer has firefox on it, it works.
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Application Programming