Source Code : Draw Text Demo

Java Is Open Source Programming Language You Can Download From Java and Java Libraries From http://www.oracle.com. Click Here to download
We provide this code related to title for you to solve your developing problem easily. Libraries which is import in this program you can download from http://www.oracle.com. Click Here or search from google with Libraries Name you get jar file related it

Draw Text Demo

Draw Text Demo
 
/******************************************************************************
 * All Right Reserved. 
 * Copyright (c) 1998, 2004 Jackwind Li Guojie
 * 
 * Created on 2004-4-19 13:48:04 by JACK
 * $Id$
 * 
 *****************************************************************************/

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class DrawTextDemo {
  Display display = new Display();
  Shell shell = new Shell(display);

  public DrawTextDemo() {
    shell.setLayout(new FillLayout());
    
    final Canvas canvas = new Canvas(shell, SWT.NO_BACKGROUND);
    
    final Image image = new Image(display, "java2s.gif");
    canvas.addPaintListener(new PaintListener() {
      public void paintControl(PaintEvent e) {
        Rectangle size = image.getBounds();
        // Draws the background image.
        e.gc.drawImage(image, 0, 0, size.width, size.height, 0, 0, canvas.getSize().x, canvas.getSize().y);
        
        Font font = new Font(display, "Tahoma", 18, SWT.BOLD);
        e.gc.setFont(font);
        e.gc.setForeground(display.getSystemColor(SWT.COLOR_WHITE));
        e.gc.setBackground(display.getSystemColor(SWT.COLOR_BLUE));

        String english = "SWT rocks!";
        String chinese = "u4e2du6587u6c49u5b57u6d4bu8bd5";
        
        e.gc.drawString(english, 10, 10);
        e.gc.drawString(chinese, 10, 80, true);
        
        String text = "Text to be drawn in the center";
        Point textSize = e.gc.textExtent(text);
        e.gc.drawText(text, (canvas.getSize().x - textSize.x)/2, (canvas.getSize().y - textSize.y)/2);
        
        font.dispose();
      }
    });
    

    shell.setSize(300, 150);
    shell.open();
    //textUser.forceFocus();

    // Set up the event loop.
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        // If no more entries in event queue
        display.sleep();
      }
    }

    display.dispose();
  }

  private void init() {

  }

  public static void main(String[] args) {
    new DrawTextDemo();
  }
}
           
         
  

Thank with us