71 Program to display Parameters of HTML
import java.awt.*;
import java.applet.*;
public class ParamDemo extends Applet {String fontName;
int fontSize;
float leading;
boolean active;
public void start() {
String param;
fontName=getParameter("fontName");
if(fontName==null)
fontName="Not Found";
param=getParameter("fontSize");
try {
if(param != null)
fontSize=Integer.parseInt(param);
else
fontSize=0;
}
catch(NumberFormatException e) {
fontSize=-1;
}
param = getParameter("leading");
try {
if(param != null)
leading = Float.valueOf(param).floatValue();
else
leading=0;
}
catch(NumberFormatException e) {
leading=-1;
}
param=getParameter("accountEnabled");
if(param != null)
active = Boolean.valueOf(param).booleanValue();
}
public void paint(Graphics g) {
g.drawString("Font name: "+fontName,0,10);
g.drawString("Font size: "+fontSize,0,26);
g.drawString("Leading: "+leading,0,42);
g.drawString("Account Active: "+active,0,58);
}
}
72 Program to Demonstrate all shapes in Graphics class
import java.awt.*;
import java.applet.*;
public class FigPaint extends Applet{
public void paint(Graphics g)
{
g.drawLine(0,0,100,100);
g.drawRect(10,10,60,50);
g.fillRect(100,10,60,50);
g.drawOval(10,10,50,50);
g.fillOval(100,10,75,50);
g.drawArc(10,40,70,70,0,75);
g.fillArc(100,40,70,70,0,75);
}
}
73 Program to Show a Hut,Mountains & Face
import java.awt.*;
import java.applet.*;
public class house extends Applet{
public void paint(Graphics g)
{
setBackground(Color.black);
setForeground(Color.white);
g.drawLine(330,220,400,120);
g.drawLine(400,120,470,220);
g.drawLine(349,200,349,300);
g.drawLine(450,200,450,300);
g.drawLine(349,300,450,300);
g.drawRect(385,260,20,40);
g.drawRect(420,220,15,15);
g.fillOval(80,50,40,40);
setBackground(Color.black);
g.drawLine(20,380,60,130);
g.drawLine(60,130,100,360);
g.drawLine(100,360,140,140);
g.drawLine(140,140,180,340);
g.drawLine(180,340,220,150);
g.drawLine(220,150,260,320);
g.drawLine(260,320,315,165);
g.drawLine(315,165,335,210);
g.drawOval(330,350,130,40);
g.drawOval(550,60,60,60);
g.drawOval(565,80,10,10);
g.drawOval(585,80,10,10);
g.drawArc(566,81,30,30,0,-180);
g.drawLine(580,120,580,300);
g.drawLine(520,190,580,160);
g.drawLine(580,160,640,190);
g.drawLine(530,340,580,300);
g.drawLine(580,300,630,340);
}
}
74 Program to Show Status of Applet Window
import java.awt.*;
import java.applet.*;
public class StatusWindow extends Applet {public void init() {
setBackground(Color.cyan);
}
public void paint(Graphics g)
{
g.drawString("This is in the applet window.",10,20);
showStatus("This is shown in the status window.");
}
}
75 Program to Show Position of the Mouse in Applet Window
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class AppWindow extends Frame {
String keymsg = "";
String mousemsg = "";
int mouseX=30, mouseY=30;
public AppWindow()
{
addKeyListener(new MyKeyAdapter(this));
addMouseListener(new MyMouseAdapter(this));
addWindowListener(new MyWindowAdapter());
}
public void paint(Graphics g)
{
g.drawString(keymsg,10,40);
g.drawString(mousemsg,mouseX,mouseY);
}
public static void main(String args[])
{
AppWindow appwin=new AppWindow();
appwin.setSize(new Dimension(400,200));
appwin.setTitle("an AWT-Based Application");
appwin.setVisible(true);
}
}
class MyKeyAdapter extends KeyAdapter{
AppWindow appWindow;
public MyKeyAdapter(AppWindow appWindow)
{
this.appWindow=appWindow;
}
public void keyTyped(KeyEvent ke) {appWindow.keymsg +=ke.getKeyChar();
appWindow.repaint();
};}
class MyMouseAdapter extends MouseAdapter {AppWindow appWindow;
public MyMouseAdapter(AppWindow appWindow) {
this.appWindow=appWindow;
}
public void mousePressed(MouseEvent me) {
appWindow.mouseX=me.getX();
appWindow.mouseY=me.getY();
appWindow.mousemsg="Mouse Down at "+appWindow.mouseX +", "+appWindow.mouseY;
appWindow.repaint();
}}
class MyWindowAdapter extends WindowAdapter {
public void windowClosing(WindowEvent we) {
System.exit(0);
}}
76 Program to Demonstrate Colors
import java.awt.*;
import java.applet.*;
public class Colordemo extends Applet{
public void paint(Graphics g){
Color c1 = new Color(255,100,100);
Color c2 = new Color(10,255,100);
Color c3 = new Color(100,200,255);
g.setColor(c1);
g.drawLine(110,50,100,100);
g.drawLine(130,100,100,40);
g.setColor(c2);
g.fillRect(60,120,50,50);
g.setColor(c3);
g.fillOval(20,30,70,70);
g.setColor(Color.black);
g.fillRect(120,50,180,90);
}}
77 Program to Pass Parameters perform Mathematical Operations
import java.awt.*;
import java.applet.*;
public class HelloJavaParam1 extends Applet {
int a,b;
public void start() {
a=Integer.parseInt(getParameter("num1")); //recieving parameter value
b=Integer.parseInt(getParameter("num2")); //recieving parameter value
}
public void paint(Graphics g) {
g.drawString(" a = "+a +" b = "+b,20,50);
g.drawString("Mathematical Operation",20,70);
g.drawString("Sum = "+(a+b),20,100);
g.drawString("Difference = "+(a-b),20,120);
g.drawString("Product = "+(a*b),20,140);
g.drawString("Mod = "+(a%b),20,160);
}
}
78 Program To Get Document Code Bases
import java.applet.*;
import java.net.*;
public class Bases extends Applet{
// Display code and document bases.
public void paint(Graphics g) {
String msg;
URL url = getCodeBase(); // get code base
msg = "Code base: " + url.toString();
g.drawString(msg, 10, 20);
url = getDocumentBase(); // get document base
msg = "Document base: " + url.toString();
g.drawString(msg, 10, 40);
}}
79 Program to Show an Image
import java.awt.*;
import java.applet.*;
public class DrawImageApp extends Applet {
Image i;
public void start()
{
i = getImage(getDocumentBase(),"sunflower.jpg");
}
public void paint(Graphics g)
{
g.drawImage(i,50,50,this);
}
}
80 Program to Show an Image
import java.awt.*;
import java.applet.*;
public class SimpleBanner extends Applet implements Runnable
{
String msg = " A Simple Moving Banner.";
Thread t = null;
int state;
boolean stopFlag;
// Set colors and initialize thread.
public void init() {
setBackground(Color.cyan);
setForeground(Color.red);
}
// Start thread
public void start() {
t = new Thread(this);
stopFlag = false;
t.start();
}
// Entry point for the thread that runs the banner.
public void run() {
char ch;
// Display banner
for( ; ; ) {
try {
repaint();
Thread.sleep(250);
ch = msg.charAt(0);
msg = msg.substring(1, msg.length());
msg += ch;
if(stopFlag)
break;
} catch(InterruptedException e) {}
}
}
// Pause the banner.
public void stop() {
stopFlag = true;
t = null;
}
// Display the banner.
public void paint(Graphics g) {
g.drawString(msg, 50, 30);
}
}
0 comments:
Post a Comment