Unit-15 Java Applet Basics

PASSING PARAMETERS TO APPLET…

Example : BarChart extends Applet

import java.awt.*;
import java.applet.*;
/*<applet code="BarChart.java" width=300 Height=250>
<param name="columns"value="4">
<param name="c1"value="110">
<param name="c2"value="150">
<param name="c3"value="100">
<param name="c4"value="170">
<param name="label1"value="91">
<param name="label2"value="92">
<param name="label3"value="93">
<param name="label4"value="94">
</applet>
*/
public class BarChart extends Applet
{
int n=0;
String label[];
int value[];
public void init()
{
try
{
n=Integer.parseInt(getParameter("columns"));
label=new String[n];
value=new int[n];
label[0]=getParameter("label1");
label[1]=getParameter("label2");
label[2]=getParameter("label3");
label[3]=getParameter("label4");
value[0]=Integer.parseInt(getParameter("c1"));
value[1]=Integer.parseInt(getParameter("c2"));
value[2]=Integer.parseInt(getParameter("c3"));
value[3]=Integer.parseInt(getParameter("c4"));
}
catch(NumberFormatException e)
{}
}
public void paint(Graphics g)
{
for(int i=0;i<n;i++)
{
g.setColor(Color.RED);
g.drawString(label[i],20,i*50+30);
g.fillRect(50,i*50+10,value[i],40);
}
}
}
Output : BarChart extends Applet

Drawing and Filling in Java…

Example : Lines extends Applet

 import java.awt.*;
import java.applet.*;
/*
<applet code="Lines.java" width=300 height=200>
</applet>
*/
public class Lines extends Applet
{
public void paint(Graphics g)
{
g.drawLine(0,0,100,100);
g.drawLine(0,100,100,0);
g.drawLine(40,25,250,180);
g.drawLine(75,90,400,400);
g.drawLine(20,150,400,40);
g.drawLine(5,290,80,19);
}
}
Output : Lines extends Applet

Rectangles in Java…

Example :  Rectangles extends Applet

import java.awt.*;
import java.applet.*;
/*
<applet code="Rectangles.java" width=300 height=200>
</applet>
*/ 
public class Rectangles extends Applet 
{
public void paint(Graphics g)
{
g.drawRect(10,10,60,50);
g.fillRect(100,10,60,50);
g.drawRoundRect(190,10,60,50,15,15);
g.fillRoundRect(70,90,140,100,30,40);
}
}     
Output :  Rectangles extends Applet

Polygons in Java…

Example : HourGlass extends Applet

import java.awt.*;
import java.applet.*;
/*
<applet code="HourGlass"width=230 height=210>
</applet>
*/
public class HourGlass extends Applet
{
public void paint(Graphics g)
{
int xpoints[]={30,200,30,200,30};
int ypoints[]={30,30,200,200,30};
g.drawPolygon(xpoints,ypoints,5);
}
}
Output : HourGlass extends Applet

Ovals in Java…

Example : Ellipses5 extends Applet

import java.awt.*;
import java.applet.*;
/*
<applet code="Ellipses5"width=300 height=200>
</applet>
*/
public class Ellipses5 extends Applet
{
public void paint(Graphics g)
{
g.drawOval(10,10,50,50);
g.fillOval(100,10,75,50);
g.drawOval(190,10,90,30);
g.fillOval(70,90,140,100);
}
}
Output : Ellipses5 extends Applet

Arc in Java…

Example : Arcs extends Applet

import java.awt.*;
import java.applet.*;
/*
<applet code="Arcs" width=300 height=200>
</applet>
*/
public class Arcs extends Applet
{
public void paint(Graphics g)
{
g.drawArc(10,40,70,70,0,75);
g.drawArc(100,40,70,70,0,75);
g.drawArc(10,100,70,80,0,175);
g.fillArc(100,100,70,90,0,270);
g.drawArc(200,80,80,80,0,180);
}
}
Output : Arcs extends Applet

A Simple Graphics Example in Java…

Example : Lamp

import java.awt.*;
/*
<applet code="Lamp.java"width=300 height=200>
</applet>
*/

public class Lamp extends java.applet.Applet{
            public void paint(Graphics g){
            g.fillRect(0,250,290,290);
            g.drawLine(125,250,125,160);
            g.drawLine(175,250,175,160);
            g.drawArc(85,157,130,50,-65,312);
            g.drawArc(85,87,130,50,62,58);
            g.drawLine(85,117,119,89);
            g.drawLine(215,177,181,89);
            g.fillArc(78,120,40,40,63,-174);
            g.fillOval(120,96,40,40);
            g.fillArc(173,100,40,40,110,180);
            }

}
Output : Lamp

Drawing Characters and Strings in Java…

Example : ManyFonts

import java.awt.*;

/*
<applet code=ManyFonts width=300 height=300>
</applet>
*/
public class ManyFonts extends java.applet.Applet
{
public void paint(Graphics g)
{
Font f=new Font("TimesRoman",Font.PLAIN,18);
Font fb=new Font("TimesRoman",Font.BOLD,18);
Font fi=new Font("TimesRoman",Font.ITALIC,18);
Font fbi=new Font("TimesRoman",Font.BOLD+Font.ITALIC,18);
g.setFont(f);
g.drawString("This is a plain font", 10,25);
g.setFont(fb);
g.drawString("This is a bold font",10,50);
g.setFont(fi);
g.drawString("This is a italic font",10,75);
g.setFont(fbi);
g.drawString("This is a bold italic font",10,100);
}
}
Output : ManyFonts

Finding Out Information about a Font in Java…

Example : FontMetricsDemo

import java.awt.*;
import java.applet.*;
/*<applet code="FontMetricsDemo.java" width=300 height=300>
</applet>*/
public class FontMetricsDemo extends Applet
{
Font f1,f2;
int ascent,descent,height,leading;
String one,two,three,four;
public void init()
{
f1=new Font("Helvetica",Font.BOLD,14);
f2=new Font("TimesRoman",Font.BOLD+Font.ITALIC,12);
}
public void paint(Graphics g)
{
Font f=new Font("TimesRoman", Font.BOLD,36);
FontMetrics fmetrics=getFontMetrics(f);
g.setFont(f);
g.setFont(f1);
ascent=g.getFontMetrics().getAscent();
descent=g.getFontMetrics().getDescent();
height=g.getFontMetrics().getHeight();
leading=g.getFontMetrics().getLeading();
one="Ascent of font f1 is: "+ascent;
two="Descent of font f1 is: "+descent;
three="Height of font f1 is: "+height;
four="Leading of font f1 is: "+leading;
g.drawString(one,20,20);
g.drawString(two,20,50);
g.drawString(three,20,80);
g.drawString(four,20,110);
}
}
Output : FontMetricsDemo

Font metrics methods in Java…

Example : Centered

import java.awt.Font;
import java.applet.*;
import java.awt.Graphics;
import java.awt.FontMetrics;
/*<applet code="Centered" width=400 height=400>
</applet>*/
public class Centered extends java.applet.Applet
{
public void paint(Graphics g)
{
Font f=new Font("TimesRoman",Font.PLAIN,36);
FontMetrics fm=getFontMetrics (f);
g.setFont(f);
String s ="This is how the world ends.";
int xstart=(size().width-fm.stringWidth(s))/2;
int ystart=(size().height+fm.getHeight())/2;
g.drawString(s,xstart,ystart);
}
}
Output : Centered

Testing and Setting the Current colors in Java…

Example : ColorAppletDemo

import java.awt.*;
import java.applet.*;
/*
<applet code="ColorAppletDemo"width=300 height=300>
</applet> 
*/
public class ColorAppletDemo extends java.applet.Applet
{
public void paint(Graphics g)
{
Font f=new Font("TimesRoman",Font.BOLD,20);
Font f1=new Font("Courier",Font.ITALIC,20);
Font f2=new Font("Helevitica",Font.PLAIN,20);
g.setColor(Color.red);
g.setFont(f);
g.drawString("Be Happy.Be hopeful",30,30);
g.setColor(Color.blue);
g.setFont(f1);
g.drawString("Be happy,Be hopeful",30,70);
g.setColor(Color.pink);
g.setFont(f2);
g.drawString("Be happy,Be hopeful",30,110);
}
}
Output :  ColorAppletDemo

Getting Images in Java…

Example : LadyBug

import java.awt.Graphics;
import java.awt.Image;
/*<applet code="LadyBug"width=300 height=300>
</applet>
*/
public class LadyBug extends java.applet.Applet
{
Image bugimg;
public void init()
{
bugimg=getImage(getCodeBase(),"Lighthouse.jpg");
}
public void paint(Graphics g)
{
g.drawImage(bugimg,10,10,this);
}
} 
Output : LadyBug