Java Program to Display Text in the Frame by using drawString and Inheriting JPanel Class

«
»

This is a Java program to display text in the frame by using drawString and inheriting JPanel class to override the paintComponent method.

Problem Description

We have to write a program in Java such that it creates a frame and draws a text using the drawString method of Graphics class by overriding the paintComponent method of the JPanel class.

Expected Input and Output

For drawing a text, we can have the following set of input and output.

advertisement

To test drawString: We expect a string “Hello World” on the frame. The string should have the TimesRoman font, with Bold letters and legible font size.

Problem Solution

1. Create a frame with white background.
2. Create an object of the class and add it to the frame. To add the object to the frame, inheriting the JPanel class in needed.
3. Display the frame.
4. Override the paintComponent method of the JPanel class using @Override keyword.
5. Set the color of the graphics object to black using setColor(Color.color name) method.
6. Create a new font as required by creating an object of class Font.
7. Change the font of the graphics object with the new font created.
8. Use method drawString to draw the string.

Program/Source Code

Here is source code of the Java Program to draw sting using methods of Graphics class and inheriting JPanel class. The program is successfully compiled and tested using javac compiler on Fedora 30.

advertisement
  1. /* Java Program to draw a string using methods of Graphics class 
  2. and inheriting JPanel class to override the paintComponent */
  3. import javax.swing.*;
  4. import java.awt.*;
  5. class Draw_Text extends JPanel
  6. {
  7. 	//Driver function
  8. 	public static void main(String args[])
  9. 	{
  10. 		//Create a Frame
  11. 		JFrame frame=new JFrame("Draw a Text");
  12. 		frame.setSize(500,500);
  13. 		frame.getContentPane().setBackground(Color.white);	
  14. 		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  15. 		//Create an object of the class
  16. 		Draw_Text obj=new Draw_Text();
  17. 		//Add object of class to frame
  18. 		frame.add(obj);
  19. 		//Display the frame
  20. 		frame.setVisible(true);
  21. 	}
  22. 	//Function to draw a text using drawString method
  23. 	@Override
  24. 	public void paintComponent(Graphics g)
  25. 	{
  26. 		g.setColor(Color.black);
  27. 		Font myFont=new Font("TimesRoman",Font.BOLD,30);
  28. 		g.setFont(myFont);
  29. 		g.drawString("Hello World",100,225);
  30. 	}
  31. }
Program Explanation

1. Create a class Draw_Text which inherits the JPanel class.
2. Create a frame with background color as white by using setBackground(Color.white);. frame.setSize(500,500); is used to set the width and height of the frame.
3. Create an object of the class Draw_Text obj=new Draw_Text(); and add it to the frame by using frame.add(obj);.
4. frame.setVisible(true); is used to display the frame.
5. Override the paintComponent of the JPanel class using the @Override keyword.
6. Create a font myFont as an object of the class Font in the following way:
Font myFont = new Font(“Font Name”,Font.type,Font Size);
type means the kind of font required like BOLD, ITALIC, etc.
7. myFont values used in the program are:
myFont=new Font(“TimesRoman”,Font.BOLD,30);
“TimesRoman” is the Font Name, “Font.BOLD” is the Font.type and “30” is the Font Size of the string.
8. Set the created font with the graphics object using setFont method.
9. Use drawString(str,len,wid) method to draw the string “str” with the maximum length “len” and width “wid”. The values of drawString method is drawString(“Hello World”,100,225);. Here “Hello World” is the string, “100” is the maximum length of the string and “225” is the maximum width of the string.

Runtime Test Cases

Here’s the run time test cases for drawing text using method of Graphics class and inheriting paintComponent of JPanel class.

advertisement

Test case 1 – Here’s the runtime output of the drawString method. The program displays the string “Hello World” on the frame with TimesRoman font, Bold letters and legible font size.
java-program-display-text-frame-using-drawString

Sanfoundry Global Education & Learning Series – Java Programs.

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