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.

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.

advertisement
advertisement
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.

  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.

Runtime Test Cases

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

advertisement

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

Sanfoundry Global Education & Learning Series – Java Programs.

advertisement
If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.