This is a Java program to display text in the frame by using drawString and inheriting JPanel class to override the paintComponent method.
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.
For drawing a text, we can have the following set of input and output.
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.
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.
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.
/* Java Program to draw a string using methods of Graphics class
and inheriting JPanel class to override the paintComponent */
import javax.swing.*;
import java.awt.*;
class Draw_Text extends JPanel
{
//Driver function
public static void main(String args[])
{
//Create a Frame
JFrame frame=new JFrame("Draw a Text");
frame.setSize(500,500);
frame.getContentPane().setBackground(Color.white);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create an object of the class
Draw_Text obj=new Draw_Text();
//Add object of class to frame
frame.add(obj);
//Display the frame
frame.setVisible(true);
}
//Function to draw a text using drawString method
@Override
public void paintComponent(Graphics g)
{
g.setColor(Color.black);
Font myFont=new Font("TimesRoman",Font.BOLD,30);
g.setFont(myFont);
g.drawString("Hello World",100,225);
}
}
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.
Here’s the run time test cases for drawing text using method of Graphics class and inheriting paintComponent of JPanel class.
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.
Sanfoundry Global Education & Learning Series – Java Programs.
- Practice BCA MCQs
- Check Programming Books
- Apply for Computer Science Internship
- Apply for Java Internship
- Practice Information Technology MCQs