First applet The related applet |
|
/* 1. Introduction The main class to import is AWT (Abstract Windowing Toolkit)which is a set of calasses allowing graphical interfaces. The class Graphics is a class of AWT. The supper class is Applet, java.applet is a package of the class Applet. We imort then: import java.awt.*; import java.applet.*; Graphics class Methods applied on an object: if g is an object of the class Graphics, we have: g.drawString(a string, x value, y value);//Dispaly a string in a certain position g.drawLine(xA,yA,xB,yB);//Line between the point A(xA,yA) and the point B(xB,yB). g.drawRect(xA,yA,xD,yD)//Rectancle (A,B,C,D), with the first and the last points. g.drawArc(xA,yA,withRectange. lengthRectangle, 0Angle, Angle);//draw an arc inside a rectangle g.drawOval(xA,yA,xB,yB); Two principal predefined methods: void int() and void paint (Graphics g). 1. Initialization: ------------------ We initialize fonts, dimentions pictures ,... Here we initialize fonts public void init(){ The constructor Font of the class Font has three parameters: font-family, font-style, and font-size, we write: setFont(new Font("font-familty", Font.STYLE , size)); We can have a combinaition of styles as: Font.ITALIC + Font.BOLD We use geleraly Font.ITALIC,Font.BOLD, and Font.PLAIN } 2. Drawing ----------- public void paint(Graphics g){ String Regards = "Regards";// seven characters Position of each character of the string int [][] Position {{xR,yR}, {xe,ye} , {xg,yg} , {xa,ya} , {xr,yr}, {xd,yd}, {xs,ys}}; Let's set color: g.setColor(Color.green); //Using drawString method, we can write: for (int i = 0; i < Regards.length(); i++){ g.drawString(Regards.substring(i,i+1), Position[i][x], Position[i][y]); Whatever x and y. Example: x = 0 and y =1. } 2. Applet program example */ import java.awt.*; import java.applet.*; public class Regards extends Applet { static void Display(Graphics g, String text, int x, int y) { g.setFont(new Font("Bookman old style", Font.ITALIC+Font.BOLD,17)); g.setColor( Color.blue); g.drawString(text,x,y); } public void paint (Graphics g){ String text1 ="The file that contains the class Regards is named: Regards.java."; String text2 ="Compiled with : javac Regards.java, gives rise to : Regards.class."; String text3 ="It is not an application that runs with: java Regards, command line." ; String text4 ="But the browser shows Regards.class , that is the applet. " ; Display(g, text1,100,100); Display(g, text2,100,150); Display(g, text3,100,200); Display(g, text4,100,250); } } |