Applet – Resize to Maximum Size

package AppletExamples;

import java.applet.*;
import java.awt.*;
//import java.awt.Dimension;

/**
*
* @author Kodingkeys
*/
public class Dimension extends Applet {
public void paint(Graphics g) {
this.resize(this.getMaximumSize());

}
}

Advertisements

Applet- Counter

package AppletExamples;

import java.applet.Applet;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
/**
*
* @author kodingkeys
*/
public class Prog2 extends Applet implements Runnable {

int counter;
Thread t;
public void init(){
counter = 0;
t = new Thread(this);
t.start();
}
public void run(){
try{

while(true){
repaint();
Thread.sleep(1000);
++counter;
}
}
catch(Exception e){
}
}

public void paint(Graphics g){

g.setFont(new Font(“Arial”,Font.BOLD,24));
FontMetrics fm = g.getFontMetrics();
Dimension d = getSize();
int x = d.width/2 – fm.stringWidth(“”+counter)/2;
int y = d.height/2;
g.drawString(“”+counter,x,y);
}
}

Applet – Hello World

prog1

package AppletExamples;

import java.applet.Applet;
import java.awt.Graphics;

/**
*
* @author kodingkeys
*/
public class Prog1 extends Applet {

public void init() {
}
public void paint(Graphics g){
//write text using drawString method of Graphics class
g.drawString(“This is my First Applet”,20,100);
}
}