Skip to main content

Drawing text in java


              Drawing text swing awt,in java

 

Drawing is done with the drawString() method. We specify the string we want to draw and the position of the text on the window area.
 

import java.awt.Font;
package PRACTICEPROGRAM;

import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

class DrawPanel extends JPanel {

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        Graphics2D g2d = (Graphics2D) g;

        RenderingHints rh = new RenderingHints(
                RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);

        rh.put(RenderingHints.KEY_RENDERING,
                RenderingHints.VALUE_RENDER_QUALITY);

        g2d.setRenderingHints(rh);

        Font font = new Font("URW Chancery L", Font.BOLD, 21);
        g2d.setFont(font);

        g2d.drawString("Not marble, nor the gilded monuments", 20, 30);
        g2d.drawString("Of princes, shall outlive this powerful rhyme;", 20, 60);
        g2d.drawString("But you shall shine more bright in these contents",
                20, 90);
        g2d.drawString("Than unswept stone, besmear'd with sluttish time.",
                20, 120);
        g2d.drawString("When wasteful war shall statues overturn,", 20, 150);
        g2d.drawString("And broils root out the work of masonry,", 20, 180);
        g2d.drawString("Nor Mars his sword, nor war's quick "
                + "fire shall burn", 20, 210);
        g2d.drawString("The living record of your memory.", 20, 240);
        g2d.drawString("'Gainst death, and all oblivious enmity", 20, 270);
        g2d.drawString("Shall you pace forth; your praise shall still "
                + "find room", 20, 300);
        g2d.drawString("Even in the eyes of all posterity", 20, 330);
        g2d.drawString("That wear this world out to the ending doom.", 20, 360);
        g2d.drawString("So, till the judgment that yourself arise,", 20, 390);
        g2d.drawString("You live in this, and dwell in lovers' eyes.", 20, 420);
    }
}

public class TextExample extends JFrame {

    public TextExample() {
        initUI();
    }

    public final void initUI() {

        DrawPanel dpnl = new DrawPanel();
        add(dpnl);

        setSize(500, 470);
        setTitle("Sonnet55");
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {

        SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                TextExample ex = new TextExample();
                ex.setVisible(true);
            }
        });
    }
}


In our example, we draw a sonnet on the panel component.
RenderingHints rh = new RenderingHints(
    RenderingHints.KEY_ANTIALIASING,
    RenderingHints.VALUE_ANTIALIAS_ON);

rh.put(RenderingHints.KEY_RENDERING,
    RenderingHints.VALUE_RENDER_QUALITY);

g2d.setRenderingHints(rh);


This code is to make our text look better. We apply a technique called antialiasing.
Font font = new Font("URW Chancery L", Font.BOLD, 21);
g2d.setFont(font);


We choose a nice font for our text.
g2d.drawString("Not marble, nor the gilded monuments", 20, 30);
This is the code, that draws the text.
 

Join us on Facebook: Click Here 

Comments

Popular posts from this blog

Menu bar example with applet viewer in java

      Menu bar +applet viewer example-3 import java.applet.*; import java.awt.*;  /*   <applet code="MenuDemo" height=200 width=200>   </applet> */ public class MenuDemo extends Applet {             public void init( )             {                         Menus m = new Menus("Menu Bar Demo");                                    m.resize(200,200);       m.show();             } } class Menus extends Frame {             Menus(String s)             {                         super(s);                         Me...

Java question

  Java ques interview Ques:-List any five features of Java?  Ans :-Some features include Object Oriented, Platform Independent, Robust, Interpreted, Multi-threaded Ques . write full form of jdbc & odbc ?  Ans :-  JDBC -java database connectivity.             ODBC - open database connectivity Ques:- what are types of packages ? Ans: 1. Pre-defined packages 2. User defined packages Ques :-Define class?  Ans:- A class is a blue print from which individual objects are created. A class can contain fields and methods to describe the behavior of an object. Ques:- Denfie string bulider? Ans :- Use StringBuilder whenever possible because it is faster than StringBuffer. But, if thread safety is necessary then use StringBuffer objects. Ques :- What is Singleton class?  Ans:Singleton class control object creation, limiting the number to one but allowing the flexibility to create m...

MVC Example 1

                        Swing MVC Example in java Java Swing first programs In this chapter, we will program our first programs in Swing toolkit. The examples are going  to be very simple. We will cover some basic functionality. Our first example In our first example, we will show a basic window. import javax.swing.JFrame; public class Simple extends JFrame { public Simple() { setSize(300, 200); setTitle("Simple"); setDefaultCloseOperation(EXIT_ON_CLOSE); } public static void main(String[] args) { Simple simple = new Simple(); simple.setVisible(true); } } While this code is very small, the application window can do quite a lot. It can be resized, maximized, minimized. All the complexity that comes with it has been hidden from the application programmer. import javax.swing.JFrame; Here we import the JFrame widget. It is a toplevel container, which is used for plac...