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
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 :

⚡ Claim Your Free Java Certification - June 2026
>>> 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.

👉 For weekly programming practice and certification updates, join Sanfoundry’s official WhatsApp & Telegram channels
advertisement
Manish Bhojasia – Founder & CTO at Sanfoundry

I’m Manish, Founder & CTO at Sanfoundry, with 25+ years of experience across Linux systems, SAN technologies, advanced C programming, and building large-scale, performance-driven learning and certification platforms focused on clear skill validation.

LinkedIn  ·  YouTube MasterClass  ·  Telegram Classes  ·  Career Guidance & Conversations