Graphics: drawString Method in Java

Today in Java, i will teach you how to create java program that will have the drawString method in creating graphics in applet. The drawString() method, takes as parameters an instance of the String class containing the text to be drawn, and two integer values specifying the coordinates where the text is placed (x and y coordinates in the screen). So, now let's start this tutorial! 1. Open JCreator or NetBeans and make a java program with a file name of drawString.java. In the classname, extend a java.applet.Applet to have an applet library. 2. Import java.awt.* package because we will have the Graphics and Font class library.
  1. import java.awt.*;
3. Create a paint() function to paint and draw the graphics in the applet. Then have this code inside the paint() function.
  1.  
  2. public void paint(Graphics g) {
  3. // initialize a name variable to be displayed in the drawString method
  4. String name = new String("Lyndon R. Bermoy");
  5.  
  6. // set the font style to Times New Roman with a plain text and a font size of 30
  7. g.setFont(new Font("TimesRoman", Font.PLAIN, 30));
  8.  
  9. //font color is set to Red
  10. g.setColor(Color.red);
  11.  
  12. //and then draw the string with the specific coodrinates
  13. g.drawString(name, 50, 100);
  14. }
The drawString() method calls your variable name and it has the x and y coordinates. drawString(String text, int x, int y) Note: You have to build your program to have the .class file extension for viewing of the applet. 4. Now, create a new file. Click File Type, choose Other Folder and click HTML Applet. html Applet After creating it, it will have the given code. Then in the Applet Code, put and encode your file name of the created java file, the drawString.class. Follow this code below:
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  2.  
  3. <html>
  4. <head>
  5. </head>
  6. <body bgcolor="000000">
  7. <center>
  8. <applet
  9. code = "drawString.class"
  10. width = "500"
  11. height = "300"
  12. >
  13. </applet>
  14. </center>
  15. </body>
  16. </html>
Press F5 to run the program. Output: output Full source code of the drawString.java:
  1. import java.awt.*;
  2.  
  3. public class drawString extends java.applet.Applet {
  4.  
  5.  
  6.  
  7.  
  8. public void paint(Graphics g) {
  9. // initialize a name variable to be displayed in the drawString method
  10. String name = new String("Lyndon R. Bermoy");
  11.  
  12. // set the font style to Times New Roman with a plain text and a font size of 30
  13. g.setFont(new Font("TimesRoman", Font.PLAIN, 30));
  14.  
  15. //font color is set to Red
  16. g.setColor(Color.red);
  17.  
  18. //and then draw the string with the specific coodrinates
  19. g.drawString(name, 50, 100);
  20. }
  21. }
Best Regards, Engr. Lyndon Bermoy IT Instructor/System Developer/Android Developer/Freelance Programmer If you have some queries, feel free to contact the number or e-mail below. Mobile: 09488225971 Landline: 826-9296 E-mail:[email protected] Add and Follow me on Facebook: https://www.facebook.com/donzzsky Visit and like my page on Facebook at: https://www.facebook.com/BermzISware

Add new comment