Java Program to Display String in a Rectangle

«
»

This is a Java Program to Display String in a Rectangle

Problem Description

We have to write a program in Java such that it creates a rectangle and displays a string inside it.

Expected Input and Output

For drawing string in a rectangle, we can have the following set of input and output.

advertisement

To View the String in a Rectangle :

On the execution of the program,
it is expected that a string is displayed inside a rectangle.
Given string is "String inside Rectangle".
Rectangle shape color should be "Red" and string color "Green".
Problem Solution

1. Draw a rectangle with required dimensions.
2. Create a string and draw it with proper co-ordinates.

Program/Source Code

Here is source code of the Java Program to display string in a rectangle. The program is successfully compiled and tested using javac compiler on Fedora 30. The program output is also shown below.

advertisement
  1. /* Java Program to Display String in a Rectangle */
  2. import java.applet.*;
  3. import java.awt.*;
  4. public class String_Rectangle extends Applet
  5. {
  6.     //Initialize the applet
  7.     public void init()
  8.     {
  9. 	setBackground(Color.white);
  10.     }
  11.     //Function to draw rectangle and string
  12.     public void paint(Graphics g)
  13.     {
  14. 	g.setColor(Color.red);
  15.  
  16. 	int wid = 300;
  17. 	int len = 150;
  18. 	//Draw a rectangle
  19. 	g.drawRect(100,175,wid,len);
  20.  
  21. 	//Create a font and draw string
  22. 	Font myFont = new Font("TimesRoman",Font.BOLD,15);
  23. 	g.setFont(myFont);
  24. 	g.setColor(Color.green);
  25. 	String s = "String inside Rectangle";
  26. 	g.drawString(s,150,250);
  27.     }
  28. }
  29. /*
  30. <applet code = String_Rectangle.class width = 500 height = 500>
  31. </applet>
  32. */

To compile and execute the program use the following commands :

>>> javac String_Rectangle.java
>>> appletviewer String_Rectangle.java
Program Explanation

1. Use drawRect(int x,int y,int width,int length) method of Graphics class to draw a rectangle with dimensions width * length starting from (x,y) co-ordinate.
2. To draw a string use method drawString.

advertisement
Runtime Test Cases

Here’s the run time test cases to draw a string in rectangle for different input cases.

Test case 1 – To View a String in Rectangle.
java-program-string-in-rectangle

Sanfoundry Global Education & Learning Series – Java Programs.

advertisement
advertisement
advertisement
advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn