Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Java Programming - Applets and Graphics in Java, Slides of Java Programming

This document from docsity.com covers the basics of applets and graphics in java, including creating applets in jbuilder7, the applet life cycle, and exception handling. It includes code examples for creating a simple applet that displays 'hello world' and changes the font color.

Typology: Slides

2011/2012

Uploaded on 07/03/2012

aapti
aapti 🇮🇳

4.6

(28)

82 documents

1 / 38

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Java-Programming
Day-03
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26

Partial preview of the text

Download Java Programming - Applets and Graphics in Java and more Slides Java Programming in PDF only on Docsity!

Java-Programming

Day-

Outlines

  • Applets

 Creation

 Applets and Html

 Graphics in Applets

 Displaying Pictures

 Applet life Cycle

 Creating Applets in JBuilder

Writing Your First Applet

  • Required Packages/Classes

 java.applet.*

 java.applet.Applet

 java.awt.*

 javax.swings.*

  • Few steps to write an applet

 Import the required package into your program

import java.applet.*;

import java.awt.*;

 Create a class of your choice

public class HelloApplet{ }

 Extend the class from java.applet.Applet class

public class helloApplet extends Applet{ }

Helloapplet..cont..

  • Add the following method

 public void paint(Graphics g) { }

public class HelloApplet extends Applet{

public void paint(Graphics g){ } }

  • Add g.drawString(“Hello World”,25,25)

public class HelloApplet extends Applet{

public void paint(Graphics g){ g.drawString(“Hello World”,25,25); } }

  • Save the program to a file HelloWorld.java
  • Compile it

Complete HelloWorld Applet

  • HelloWorld.java

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

public class HelloWorld extends Applet{ public void paing(Graphics g){ g.drawString(“Hello World”,25,25); } }

  • HelloWorld.html

<applet codebase=“.” code=“HelloWorld.class” height= width=100 >

HelloWorld Eplained

  • Few questions about HelloWorld program

 Why I need a html file

 Applet are to be executed in a web browser

 Why extending from Applet class

 Each web browser require specific method to be in an applet  There are numerious (one book say about 200 ) methods required by the browser  java.applet.Applet have the core functionality required by the browser

 Why using public void paint(Graphics g) method

 Inherited from the parent  Empty implementation  Child creates its own version according to requirements

 What is the parameter (Graphic g)

 A java class for rendering graphics on the screen  Browser provides the implementation according to the local system

Graphics Class

  • Contains extensive set for free hand drawing

 Drawing Lines, Circles, rectangles etc

 Drawing polygons, 3D rectangles, spheres etc

 Color filling in drawings

  • Contains method for drawing images and playing audioclips
  • Contains method for displaying different fonts with different colors
  • Few of such methods are

 drawLine();

 drawRect();

 setFont();

 drawImage();

 drawOval();

Changing font color

public void paint(Graphics g){ g.setColor(Color.green); g.drawString("The changed world",25,25); }

  • setColor(color)

 Require a color class object

 Used to change the current drawing color

  • The awt package contain extensive set of classes for color and fonts

 Color class

 Can be uses to define colors  Color c=new Color(12,34,20);  Contains predefined colors  Color.red  Color.green

Loading images to you applet

public void paint(Graphics g){ Image img=getImage(getCodeBase(),"test.gif"); g.drawImage(img,40,40,50,50,this); }

 Image Class

 An abstract class in awt package  Provide image manipulation method such as controlling height width etc..

 GetImage

 A method provided by Applet parent class  Use to load images from local system and servers as well

 getCodeBase()

 Returns a url pointing to the location where the class file is saved

 Can also use getDocumentBase()

 Returns a url pointing to the location where the html file of the applet is saved

Applet Life Cycle

  • Browser calls specific methods from your applets at specific events
  • The life cycle of an applet

 init()  The first method called by the browser  Can be used to do some initialization work  start()  Called by the browser when init() method has finished execution  Called to restart the applet when it is stopped by the user or browser  paint()  The paint method is called after start to draw graphics on the screen  re-called each time when there is something to draw on the screen or there is some changing in the drawing  If the applet has GUI components it follow a different path  Stop()  Called by the browser when the applet is not being displayed  This method stops the execution of the applet thread  destroy()  Called by the browser just before destroying the applet  This method can be used to do some wrap up work

Creating Applets in JBuilder

  • Step-
  • Open file menu

 select new  Select applet  Click OK

Step-

  • Create package

 Write your package name  Write class name for the applet  Select one of the two base file  Choose Applet  If checked unchecked generate standard methods  Used to create all standard method e.g. init ,start stop…  Click next

Step-

  • The HTML file

 Choose a file name for your html file  Choose a title  Choose height and width parameter values for your applet  You can ignore hspace and vspace  Click next

Step-

  • Applet Configuration

 Ignore it and click finish