954,554 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Need to convert application (swing) to applet

I'm having problems figuring where I need to go next in this project to create an applet from this application... any suggestions where I should start (in layman's terms)?

import javax.swing.*;

public class storageSP {

	public static void main(String[] args) {
		//declare variables
		float res;
		String kfilm;
		String sfps;
		float fps;
		String sminutes;
		String sseconds;
		float minutes;
		float seconds;
		float minsec;
		float Bit;
		float Byte;
		float kbyte;
		float mbyte;
		float gbyte;
		float tbyte;
		//int pbyte; if needed in the future
		Object size;
		float storage;
		double perc50;
		int ultStorage;

		
		//#get k of film
		kfilm = JOptionPane.showInputDialog("Please enter film type 1k,2k, 4k, or 8k? ");
		
	    if (kfilm.equals("1k")){
	    	res = 1024*778;//796672
	    }
	    else if (kfilm.equals("2k")){
	    	res = 2048*1556;//3186688
	    }
	    else if (kfilm.equals("4k")){
	    	res = 4096*3112;//12746752
	    }
	    else if (kfilm.equals("8k")){
	    	res = 8192*6224;//50987008
	    }
	    else {
	    	return;
	    }
	    
		//get fps
		sfps = JOptionPane.showInputDialog("How many frames/second? 30 (COMMON - mono), 60 (stereo), or 24 (film)  ");
		fps = Integer.parseInt(sfps.trim());
		
		//How long?
		sminutes = JOptionPane.showInputDialog("How many minutes? ");
		sseconds = JOptionPane.showInputDialog("How many seconds? ");
		minutes = Integer.parseInt(sminutes.trim());
		seconds = Integer.parseInt(sseconds.trim());
		
		if (minutes >0){
			minsec = minutes*60;
			minsec = minsec+seconds;
		}
		else {
		    minsec = seconds;
		}	
		
		//# Measurement in Bits, Bytes...etc
		Bit=res*4*fps*minsec;//resolution*4channels*fps*minutes and seconds
		Byte = Bit/8;
		kbyte = Byte/1024;
		mbyte = kbyte/1024;
		gbyte = mbyte/1024;
		tbyte = gbyte/1024;
		
		//# figure the bits, bytes, kbytes, mbytes, gbytes, tbytes, or pbytes needed
		if (Bit < 1024){
			size = "Bit";
			storage = Bit;
		}
		else if (Byte < 1024){
			size = "Byte";
			storage = Byte;
		}
		else if (kbyte < 1024){
			size = "kB";
			storage = kbyte;
		}
		else if (mbyte < 1024){
			size = "MB";
			storage = mbyte;
		}
		else if (gbyte < 1024){
			size = "GB";
			storage = gbyte;
		}
		else if (tbyte < 1024){
			size = "TB";
		    storage = tbyte;
		}
		else{
			return;
		}
			
		perc50 = storage * .50;//50 percent of storage
		ultStorage = (int) (storage+perc50);
		//#need to ensure proper storage space
		JOptionPane.showMessageDialog(null,"Size of project is: "+storage+" "+size+
				"\nTherefore, you'll need "+ultStorage+" "+size+" of space.");
	}
}
abbyo
Newbie Poster
18 posts since May 2010
Reputation Points: 10
Solved Threads: 1
 

First read the API doc on applets and the Tutorial about Applets
Start with a class that extends JApplet.
Move the code in main() to an init() method.

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

Okay, I've tried to research on how to convert apps to applets and it's still not making sense. Here is what I did, but this still won't run in the applet.

import java.applet.Applet;
import java.util.*;
import java.awt.*;
import javax.swing.*;


public class storageApplet extends JApplet {
	public void init(){//declare variables
		float res;
		String kfilm;
		String sfps;
		float fps;
		String sminutes;
		String sseconds;
		float minutes;
		float seconds;
		float minsec;
		float Bit;
		float Byte;
		float kbyte;
		float mbyte;
		float gbyte;
		float tbyte;
		//int pbyte; if needed in the future
		Object size = null;
		float storage;
		double perc50;
		int ultStorage;	
	
		//#get k of film
		kfilm = JOptionPane.showInputDialog("Please enter film type 1k,2k, 4k, or 8k? ");
		
	    if (kfilm.equals("1k")){
	    	res = 1024*778;//796672
	    }
	    else if (kfilm.equals("2k")){
	    	res = 2048*1556;//3186688
	    }
	    else if (kfilm.equals("4k")){
	    	res = 4096*3112;//12746752
	    }
	    else if (kfilm.equals("8k")){
	    	res = 8192*6224;//50987008
	    }
	    else {
	    	return;
		}
	  //get fps
		sfps = JOptionPane.showInputDialog("How many frames/second? 30 (COMMON - mono), 60 (stereo), or 24 (film)  ");
		fps = Integer.parseInt(sfps.trim());
		
		//How long?
		sminutes = JOptionPane.showInputDialog("How many minutes? ");
		sseconds = JOptionPane.showInputDialog("How many seconds? ");
		minutes = Integer.parseInt(sminutes.trim());
		seconds = Integer.parseInt(sseconds.trim());
		
		if (minutes >0){
			minsec = minutes*60;
			minsec = minsec+seconds;
		}
		else {
		    minsec = seconds;
		}	
		
		//# Measurement in Bits, Bytes...etc
		Bit=res*4*fps*minsec;//resolution*4channels*fps*minutes and seconds
		Byte = Bit/8;
		kbyte = Byte/1024;
		mbyte = kbyte/1024;
		gbyte = mbyte/1024;
		tbyte = gbyte/1024;
		
		//# figure the bits, bytes, kbytes, mbytes, gbytes, tbytes, or pbytes needed
		if (Bit < 1024){
			storage = Bit;
			size = "Bits";
		}
		else if (Byte < 1024){
			storage = Byte;
			size = "Bytes";
		}
		else if (kbyte < 1024){
			storage = kbyte;
			size = "kB";
		}
		else if (mbyte < 1024){
			storage = mbyte;
			size = "MB";
		}
		else if (gbyte < 1024){
			storage = gbyte;
			size = "GB";
		}
		else if (tbyte < 1024){
		    storage = tbyte;
		    size = "TB";
		}
		else{
			return;
		}
		
		perc50 = storage * .50;//50 percent of storage
		ultStorage = (int) (storage+perc50);
		
		JOptionPane.showMessageDialog(null, "The project takes up:  " + storage + size + "\nTherefore, you'll need:  " + ultStorage + size + " of space.");
		//#need to ensure proper storage space
	}
}
abbyo
Newbie Poster
18 posts since May 2010
Reputation Points: 10
Solved Threads: 1
 

do this

public void init()
{
write all here
}

Thats all you nothing else

Please do not forget to extend JApplet for th class

freesoft_2000
Practically a Master Poster
623 posts since Jun 2004
Reputation Points: 25
Solved Threads: 10
 
but this still won't run in the applet.


Can you explain please?

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

This won't run in the applet, even thought the applet is open. The code still runs in the JOption Pane... not the applet. Please test this out if needed. If I'm needing another plugin for Eclipse, please let me know so this runs properly.

Thanks!

abbyo
Newbie Poster
18 posts since May 2010
Reputation Points: 10
Solved Threads: 1
 
This won't run in the applet, even thought the applet is open. The code still runs in the JOption Pane


What is the code supposed to do?
What does "run" mean? Does the code execute? Does it do what you'vecoded it to do?
It looks like it uses some JOptionPanes to ask a question and display a result.

so this runs properly


What does "properly" mean?

I think you need to read up on GUI programming if you want to change the code.

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

I'm not sure the correct java terminology (I'm assuming that's what you need from me). But, I need the output (questions for user and response from user) to show up INSIDE the applet. I've tried researching under API's, GUI sites, along with other sites, but I'm still not finding the answer.

If you want [NORMR1], you can help me correct my java terminology so I can form better questions that can be understood by other programmers. That way, the thread communication between us and others will be improved. Otherwise, I have no idea what or how I can request assistance on this project.
Thank you!

abbyo
Newbie Poster
18 posts since May 2010
Reputation Points: 10
Solved Threads: 1
 

The basic things to consider when moving an application to an applet is to have all the components that are added to the JFrame's contentPane be added to the JApplet which extends Pane or to a JPanel which is added to the applet.
The other things to consider is where to put initialization code. Applets have several methods that are called by the browser. It does NOT call a main() method.
You can do some things in the constructor, but most should be done in the init() method.

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: