I'm new to java and having trouble.
I need to take a class I wrote and write a test applet for it to see if it works.
The applet is supposed to have a length label, an input for it, an width label, an input for it, a button to "calculate" and an output field below.

All the class does is find the perimeter and area of a rectangle
I wrote the class / methods but I have absolutely no idea how to turn it into an applet... I've been trying to read tutorials and asking for help but I just can't grasp it so any help or hints would be great.

import java.util.Scanner;

public class rectangle
{
	public static void main( String[] args ) 
	{
		rectangle rec = new rectangle(); 
		

	}

		double length = 1.0 
		double width = 1.0 
		double perimeter 
		double area 
		
	public class calcPerimeter()
	{
		perimeter = 2*length + 2*width; 
	}
	
	
	public class calcArea()
	{
		area = length*width; 
	}
	
	
	private double getLength()
	{
		return length;
	}
	
	private double getWidth()
	{
		return width; 
	}
	
	private double setLength( length )
	{
		double recLength = length; 
		if (recLength > 0.0 && recLength < 20.0) {
			length = recLength; 
		}	
	}
	
	private double setWidth( width )
	{
		double recWidth = width; 
		if (recWidth > 0.0 && recWidth < 20.0) {
			width = recWidth; 
		}	
	}
	
	private void toString()
	{
		System.out.printf("%s%.2f" + "  " "%s%.2f" + "  " + "%s%.2f" + "  " + "%s%.2f" + "  " +,
			length, width, perimeter, area); 
	}
	
} // end main

Recommended Answers

All 2 Replies

Oracle provides you with a basic yet solid step-by-step tutorial on how to write an applet, right here.

It seems like you did this thinking that you could use the command prompt, however applets a web based programs, so there is no command prompt there. use JTextField(s) to get input and JLabel(s) to output things. use the basic layout for applets:

import javax.swing,*;
import java.awt.event.*;
import java.awt.*;

public class Program extends JApplet implements Actionlistener { // JAppet tells program that it is an applet and ActionListener you need may need if you decide to put in a calculate button.


public void init (){ 
// put anything that you need to appear as soon as the applet loads. Text areas, size of the Applet, rectangle, button, labels, etc
}

public void paintComponent (Graphics g){ // the area where you print your rectangle or anything else involving java.awt.Graphics.
super.paintComponent (g); // tells java to repaint everything in the paintComponent method.
}


}

to call the paintComponent method call repaint(); this will call the paintComponent method. Good Luck

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.