Wednesday, November 5, 2014

Java applet window pro



Set Background Color of an Applet Window

import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
public class SetBackgroundColorExample extends Applet
{
public void paint(Graphics g)
{
setBackground(Color.red);
  }
}

Set Foreground Color of an Applet Window

import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
public class SetForegroundColorExample extends Applet
{
public void paint(Graphics g)
{
setForeground(Color.red);
g.drawString("Foreground color set to red", 50, 50);
   }
}

Set Status Message in Applet Window

import java.applet.Applet;
import java.awt.Graphics;
public class SetStatusMessageExample extends Applet
{
public void paint(Graphics g)
{
g.drawString("Show Status Example", 50, 50);
showStatus("This is a status message of an applet window");
   }
}

Using Applet dimension to print center aligned text 

import java.applet.Applet;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
public class AppletDimensionExample extends Applet
{
public void paint(Graphics g)
{
int x,y;
String s = "Hello World";
Dimension d = getSize();
Font f = new Font("Arial",Font.Bold,24);
g.setFont(f);
FontMetrics fm = g.getFontMetrics();
x = d.width/2 - fm.stringWidth(s)/2;
y = d.height/2 - fm.getHeight();
g.drawString(s,x,y);
  }
}


No comments:

Post a Comment