- /*
- Using Applet dimension to print center aligned text Example
- This Java example shows how to print text in center of an applet window using
- Dimension class.
- */
- import java.applet.Applet;
- import java.awt.Dimension;
- import java.awt.Font;
- import java.awt.FontMetrics;
- import java.awt.Graphics;
- /*
- <applet code = "AppletDimensionExample" width = 500 height = 300>
- </applet>
- */
- public class AppletDimensionExample extends Applet{
- public void paint(Graphics g){
- int x,y;
- String s = "Hello World";
- //get applet size using getSize method
- Dimension d = getSize();
- Font f = new Font("Arial",Font.BOLD,24);
- g.setFont(f);
- //determine x and y coordinates
- FontMetrics fm = g.getFontMetrics();
- x = d.width/2 - fm.stringWidth(s)/2;
- y = d.height/2 - fm.getHeight();
- //print string at specified location using drawString method
- g.drawString(s,x,y);
- }
- }
Welcome to the world of Java examples, organized by categories and Java packages. Java examples (Java sample source code) help to understand functionality of various Java classes and methods as well as various programming techniques in a simple way, which is otherwise very hard to learn by reading tutorials or Java API. So start exploring...
Sunday, November 27, 2011
Using Applet dimension to print center aligned text Example
Subscribe to:
Post Comments (Atom)
How to find the most appropriate Keywords?
🔍 Step 1: Understand Your Business and Audience Define your products, services, or content . Identify your target audien...
-
/* Pause Thread Using Sleep Method Example This Java example shows how to pause currently running thread using sleep method of Jav...
-
/* Java String split example. This Java String split example describes how Java String is split into multiple Java String objects. */ ...
-
// File Name GreetingClient.java import java.net.*; import java.io.*; public class GreetingClient { public static void main(String [] ...
No comments:
Post a Comment