Showing posts with label Applet. Show all posts
Showing posts with label Applet. Show all posts

Saturday, 17 October 2015

Moving Text Animation In Applet

By using applet you can also create a simple animation program like as we have created in this program.In this we have shown yoy how text can be animated using applet.

HTML CODING:

<html>
<body>
<applet code="myapp" width="300" height="300">
</applet>
</body>
</html>

JAVA CODING:

import java.awt.*;
import java.applet.*;

public class myapp extends Applet implements Runnable {
String msg="PRO JAVA TRICKS. ";
char ch;
boolean  stopFlag= true;
Thread t= null;

public void start()
{
t = new Thread(this);
stopFlag=false;
t.start();
}

public void run(){
for(;;){
try{
repaint();
Thread.sleep(250);
ch = msg.charAt(0);
msg = msg.substring(1,msg.length());
msg = msg + ch;
if(stopFlag)
break;
}catch(InterruptedException e) {}
}
}

public void stop(){
stopFlag=true;
t = null;
}


public void paint(Graphics g)
{
Font f= new Font("forte",Font.ITALIC,36);
g.setFont(f);
g.drawString(msg,220,350);
Color c2 = new Color(52,137,137);
setBackground(c2);
}

}

OUTPUT:



Thursday, 15 October 2015

Tutorial On GetDocument Base And GetCodeBase Using Applet

Often we need to create applets that will need to explicitly load media and text.java that allow the applet to load data from directory holding html file that sart the applet and directory from which applet file was loaded.these directories are returned as URL objects .

JAVA CODING:

import java.applet.*;
import java.awt.*;
import java.net.*;
public class bases extends Applet
{

public void paint(Graphics g)
{
String msg;
URL url = getCodeBase();
msg = "Code base:"+url.toString();
g.drawString(msg,10,40);
}

}

HTML CODING:

<html>  
<body>  
<applet code="bases" width="300" height="300">  
</applet>  
</body>  
</html>



Saturday, 28 March 2015

How To Draw Arc In Applet

We can also draw arc in  Applet class it can be draw with drawArc() & fillArc() shown below:
void  drawArc(int top,int left,int width,int height,int startAngle,int sweepAngle)

void fillArc(int top,int left,int width,int start Angle,int sweepAngle)
The arc is drawn counterclockwise if sweep angle is positive if it is negativearc will drawn clockwise.

HTML CODING:

<html>  
<body>  
<applet code="arc" width="300" height="300">  
</applet>  
</body>  
</html>

JAVA CODING:

import java.applet.*;  
import java.awt.*;  
public class arc extends Applet
{  
  
public void paint(Graphics g)
{  
g.drawArc(10,40,70,70,0,75);
g.fillArc(100,40,70,70,0,75);
g.fillArc(100,100,70,90,0,270);  
g.drawArc(200,80,80,80,0,180);
}  
  
}  


OUTPUT:



Thursday, 29 January 2015

How to change Font of text in applet

For changing font of text in apple applet  you must follow just three steps
To select a new font,you must construct a font object that describes that font.
Font(String fontname,int fontStyle,int pointSize)
There are three font style Font.PLAIN,Font.BOND and Font.ITALIC
Once we we have created we must select it using setFont(),its constructor is shown below
void setFont(Font fontObj)

HTML CODING : 

<html>
<body>
<applet code="fontapp" width="300" height="300">
</applet>
</body>
</html>

CODING :

import java.applet.*;
import java.awt.*;
public class fontapp extends Applet{

public void paint(Graphics g){

Color c2 = new Color(0,139,69);
g.drawString("Pro Java Tricks(without font style)",200,130);
Font f= new Font("forte",Font.ITALIC,36);
g.setFont(f);
g.setColor(c2);
g.drawString("Pro Java Tricks",200,200);
}

}

OUTPUT:


Monday, 26 January 2015

How To Draw A Rectangles In Applet


The drawRect() and fillRect() methods display an outlined and filled rectangle respectively.They shown below.
void drawRect(int top,int left,int width,int height)
void fillRect(int top,int left,int width,int height)
The upper-left corner of the rectangle is at top left.The dimensions of the rectangle are specified by width and height.

ROUND RECTANGLES:

To draw a rounded rectangle,use drawRondRect() or fillRoundRect(),both shown below
void drawRoundRect(int top,int left,int width,int height,int xDiam,int yDiam)
void fillRoundRect(int top,int left,int width,int height,int xDiam,int yDiam)

A round rectangle has rounded corners.The upper left-corner is at top-left.The dimension is specified as same as rectangle additionally it also has arc along x and y axis specified by xDiam,yDiam.

HTML CODING:

<html>
<body>
<applet code="recapp" width="300" height="300">
</applet>
</body>
</html>

JAVA CODING:

import java.applet.*;
import java.awt.*;
public class recapp 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);
}

}



Sunday, 25 January 2015

How to use colour in applet

Java supports color in a portable,device independent fashion.The AWT color system allows to specify any color we want.It then find best match og color.
COLOR defines several  constant such as Color.red as we use in this tutorial and we  can also create
color by color constructors.The most common constructor are:


  1. Color(int red,int green,int blue) in this tutorial we use this constructor.
  2. Color(int rgbValue).
  3. Color(float red,float green,float blue).

HTML CODE OF APPLET:


<html>
<body>
<applet code="colorapp" width="300" height="300">
</applet>
</body>
</html>

JAVA CODING:

import java.applet.*;
import java.awt.*;
public class colorapp extends Applet{

public void paint(Graphics g){
Color c2 = new Color(100,255,100);
Color c3 = new Color(100,100,255);
g.setColor(c2);
g.fillRoundRect(70,90,140,100,30,40);
g.setColor(c3);
g.fillOval(70,90,140,100);
g.setColor(Color.red);
g.drawString("Pro java tricks",90,130);
setBackground(Color.red);
}

}

OUTPUT:







Thursday, 22 January 2015

How to draw a lines in applet.


The awt supports a rich of graphics methods.All graphics are drawn relative to a window all output to a window takes place through a graphic context is encapsulated by the GRAPHICS class it is obtained by two ways


  • It is passed to an applet when one of its various methods,such as paint(). 
  • It's returned by the getGraphics() method of component.

DRAWLINE METHOD IN JAVA:

Lines are drawn in java by drawLine method,shown below

void drawLine(int startX,int startY,int endX,int endY)
drawLine() display a line in the current drawing color that begins at starX,startsY and endsX,endsY.

HTML CODE:

<html>
<body>
<applet code="lineapp" width="300" height="300">
</applet>
</body>
</html>

JAVA CODING:

import java.applet.*;
import java.awt.*;
public class lineapp extends Applet
{

public void paint(Graphics g){
g.drawLine(10,10,300,10);
g.drawLine(300,10,300,300);
g.drawLine(300,300,10,300);
g.drawLine(10,300,10,10);
}

}

OUTPUT:




Wednesday, 21 January 2015

How to run simple applet using command prompt

Applets are small application that are accessed on an internet server and run as a part of web document .The width and height statements specify the dimension of the display 
void drawString(String message,intx,inty) here message is the string output in java the upper right corner is location 0,0 .Pro Java Tricks is displayed at the position 200,200

KEY POINTS ON APPLET:

  • Applet do not need main class.
  • Applet must be read under appletviewer OR a Java-compatible browser.
  • Applet use interface provided by awt.

STEPS FOR CREATING AND RUNNING APPLET:

Following are the three simple steps to  create and run applet in command prompt

STEP #1:

paste this code in notepad file and save file name as firstapp.html choose the file type as all type and sve in it in the bin folder
<html>
<body>
<applet code="firstapp" width="300" height="300">
</applet>
</body>
</html>

STEP# 2:

paste the below code in notepad file and save file name as firstapp.java choose the file type as all type

import java.applet.*;
import java.awt.*;
public class firstapp extends Applet
{

public void paint(Graphics g){
g.drawString("Pro Java Tricks",200,200);
}

}

NOTE:

That class name and name in the html code should be same.

STEP #3:

compile the code by following command 

name of drive:\jdk\bin>javac firstapp.java
name of drive:\jdk\bin>appletviewer firstapp.html


OUTPUT: