SlideShare a Scribd company logo
1
A Simple Applet
2
Applets and applications
 An applet is a Java program that runs on a web page
 Applets can be run within any modern browser
 To run modern Java applets, old browsers need an up-to-date
Java plugin
 appletviewer is a program that can run
 An application is a Java program that runs all by itself
3
Packages and classes
 Java supplies a huge library of pre-written “code,”
ready for you to use in your programs
 Code is organized into classes
 Classes are grouped into packages
 One way to use this code is to import it
 You can import a single class, or all the classes in a
package
4
The Applet class
 To create an applet, you must import the Applet class
 This class is in the java.applet package
 The Applet class contains code that works with a
browser to create a display window
 Capitalization matters!
 applet and Applet are different names
5
Importing the Applet class
 Here is the directive that you need:
import java.applet.Applet;
 import is a keyword
 java.applet is the name of the package
 A dot ( . ) separates the package from the class
 Applet is the name of the class
 There is a semicolon ( ; ) at the end
6
The java.awt package
 “awt” stands for “Abstract Window Toolkit”
 The java.awt package includes classes for:
 Drawing lines and shapes
 Drawing letters
 Setting colors
 Choosing fonts
 If it’s drawn on the screen, then java.awt is
probably involved!
7
Importing the java.awt package
 Since you may want to use many classes from the
java.awt package, simply import them all:
import java.awt.*;
 The asterisk, or star (*), means “all classes”
 The import directives can go in any order, but must be
the first lines in your program
8
C and C++ programmers only
 C and C++ have an #include directive that copies a
library function into your program
 This makes your program bigger
 Java’s import gives you access to the library
 It does not make your program bigger
 It’s OK to use lots of include directives!
9
The applet so far
import java.applet.Applet;
import java.awt.*;
10
Your applet class
public class Drawing extends Applet {
… }
 Drawing is the name of your class
 Class names should always be capitalized
 extends Applet says that our Drawing is a kind of
Applet, but with added capabilities
 Java’s Applet just makes an empty window
 We are going to draw in that window
 The only way to make an applet is to extend Applet
11
The applet so far
import java.applet.Applet;
import java.awt.*;
// CIT 591 example
public class Drawing extends Applet {
…we still need to put some code in here...
}
12
The paint method
 Our applet is going to have a method to paint some
colored rectangles on the screen
 This method must be named paint
 paint needs to be told where on the screen it can draw
 This will be the only parameter it needs
 paint doesn’t return any result
13
The paint method, part 2
 public void paint(Graphics g) { … }
 public says that anyone can use this method
 void says that it does not return a result
 A Graphics (short for “Graphics context”) is an
object that holds information about a painting
 It remembers what color you are using
 It remembers what font you are using
 You can “paint” on it (but it doesn’t remember what you
have painted)
14
The applet so far
import java.applet.Applet;
import java.awt.*;
// CIT 591 example
public class Drawing extends Applet {
public void paint(Graphics g) {
…we still need to put some code in here…
}
}
15
Colors
 The java.awt package defines a class named Color
 There are 13 predefined colors—here are their fully-
qualified names:
 For compatibility with older programs (before the naming
conventions were established), Java also allows color
names in lowercase: Color.black, Color.darkGray, etc.
Color.BLACK Color.PINK Color.GREEN
Color.DARK_GRAY Color.RED Color.CYAN
Color.GRAY Color.ORANGE Color.BLUE
Color.LIGHT_GRAY Color.YELLOW
Color.WHITE Color.MAGENTA
16
New colors
 Every color is a mix of red, green, and blue
 You can make your own colors:
new Color( red , green , blue )
 Amounts range from 0 to 255
 Black is (0, 0, 0), white is (255, 255, 255)
 We are mixing lights, not pigments
 Yellow is red + green, or (255, 255, 0)
17
Setting a color
 To use a color, we tell our Graphics g what color we
want:
g.setColor(Color.RED);
 g will remember this color and use it for everything
until we tell it some different color
18
The paint method so far
public void paint(Graphics g) {
g.setColor(Color.BLUE);
…draw a rectangle…
g.setColor(Color.RED);
…draw another rectangle…
}
}
19
Pixels
 A pixel is a picture (pix) element
 one pixel is one dot on your screen
 there are typically 72 to 90 pixels per inch
 java.awt measures everything in pixels
20
Java’s coordinate system
 Java uses an (x, y) coordinate system
 (0, 0) is the top left corner
 (50, 0) is 50 pixels to the right of (0, 0)
 (0, 20) is 20 pixels down from (0, 0)
 (w - 1, h - 1) is just inside the bottom right corner, where w
is the width of the window and h is its height
(0, 0)
(0, 20)
(50, 0)
(50, 20)
(w-1, h-1)
21
Drawing rectangles
 There are two ways to draw rectangles:
 g.drawRect( left , top , width , height );
 g.fillRect(left , top , width , height );
22
The complete applet
import java.applet.Applet;
import java.awt.*;
// CIT 591 example
public class Drawing extends Applet {
public void paint(Graphics g) {
g.setColor(Color.BLUE);
g.fillRect(20, 20, 50, 30);
g.setColor(Color.RED);
g.fillRect(50, 30, 50, 30);
}
}
23
Some more java.awt methods
 g.drawLine( x1 , y1 , x2 , y2 );
 g.drawOval( left , top , width , height );
 g.fillOval( left , top , width , height );
 g.drawRoundRect( left , top , width , height );
 g.fillRoundRect( left , top , width , height );
 g.drawArc( left , top , width , height ,
startAngle , arcAngle );
 g.drawString( string , x , y );
24
The HTML page
 You can only run an applet in an HTML page
 The HTML looks something like this:
 <html>
<body>
<h1>DrawingApplet Applet</h1>
<applet code="DrawingApplet.class"
width="250" height="200">
</applet>
</body>
</html>
 BlueJ will create this HTML for you
25
The End

More Related Content

What's hot

Applet in java
Applet in javaApplet in java
Applet in java
Jancypriya M
 
Graphics programming in Java
Graphics programming in JavaGraphics programming in Java
Graphics programming in Java
Tushar B Kute
 
Applet programming
Applet programming Applet programming
Applet programming
Devyani Vaidya
 
Java applets
Java appletsJava applets
Java applets
Pihu Goel
 
6.applet programming in java
6.applet programming in java6.applet programming in java
6.applet programming in javaDeepak Sharma
 
Applet life cycle
Applet life cycleApplet life cycle
Java applets
Java appletsJava applets
Java applets
M Vishnuvardhan Reddy
 
Java applets
Java appletsJava applets
Java appletslopjuan
 
Java Applet and Graphics
Java Applet and GraphicsJava Applet and Graphics
Java Applet and Graphics
Abdul Rahman Sherzad
 
ITFT- Applet in java
ITFT- Applet in javaITFT- Applet in java
ITFT- Applet in java
Atul Sehdev
 
Client Side Programming with Applet
Client Side Programming with AppletClient Side Programming with Applet
Client Side Programming with Appletbackdoor
 
Applets in Java
Applets in JavaApplets in Java
Applets in Java
RamaPrabha24
 
JAVA APPLETS
JAVA APPLETSJAVA APPLETS
JAVA APPLETS
Ramkrishna bhagat
 
Java applet basics
Java applet basicsJava applet basics
Java applet basicsSunil Pandey
 
java Applet Introduction
java Applet Introductionjava Applet Introduction
java Applet Introduction
yugandhar vadlamudi
 
JAVA APPLET BASICS
JAVA APPLET BASICSJAVA APPLET BASICS
JAVA APPLET BASICS
Shanid Malayil
 
Applet Architecture - Introducing Java Applets
Applet Architecture - Introducing Java AppletsApplet Architecture - Introducing Java Applets
Applet Architecture - Introducing Java Applets
amitksaha
 
Applets in java
Applets in javaApplets in java
Applets in java
Wani Zahoor
 
Applets
AppletsApplets
Applets
SanthiNivas
 

What's hot (20)

Applet in java
Applet in javaApplet in java
Applet in java
 
Graphics programming in Java
Graphics programming in JavaGraphics programming in Java
Graphics programming in Java
 
27 applet programming
27  applet programming27  applet programming
27 applet programming
 
Applet programming
Applet programming Applet programming
Applet programming
 
Java applets
Java appletsJava applets
Java applets
 
6.applet programming in java
6.applet programming in java6.applet programming in java
6.applet programming in java
 
Applet life cycle
Applet life cycleApplet life cycle
Applet life cycle
 
Java applets
Java appletsJava applets
Java applets
 
Java applets
Java appletsJava applets
Java applets
 
Java Applet and Graphics
Java Applet and GraphicsJava Applet and Graphics
Java Applet and Graphics
 
ITFT- Applet in java
ITFT- Applet in javaITFT- Applet in java
ITFT- Applet in java
 
Client Side Programming with Applet
Client Side Programming with AppletClient Side Programming with Applet
Client Side Programming with Applet
 
Applets in Java
Applets in JavaApplets in Java
Applets in Java
 
JAVA APPLETS
JAVA APPLETSJAVA APPLETS
JAVA APPLETS
 
Java applet basics
Java applet basicsJava applet basics
Java applet basics
 
java Applet Introduction
java Applet Introductionjava Applet Introduction
java Applet Introduction
 
JAVA APPLET BASICS
JAVA APPLET BASICSJAVA APPLET BASICS
JAVA APPLET BASICS
 
Applet Architecture - Introducing Java Applets
Applet Architecture - Introducing Java AppletsApplet Architecture - Introducing Java Applets
Applet Architecture - Introducing Java Applets
 
Applets in java
Applets in javaApplets in java
Applets in java
 
Applets
AppletsApplets
Applets
 

Similar to first-applet

Applets - lev' 2
Applets - lev' 2Applets - lev' 2
Applets - lev' 2
Rakesh T
 
Applet in java new
Applet in java newApplet in java new
Applet in java new
Kavitha713564
 
oops with java modules iii & iv.pptx
oops with java modules iii & iv.pptxoops with java modules iii & iv.pptx
oops with java modules iii & iv.pptx
rani marri
 
Basic of Applet
Basic of AppletBasic of Applet
Basic of Applet
suraj pandey
 
Smart material - Unit 3 (1).pdf
Smart material - Unit 3 (1).pdfSmart material - Unit 3 (1).pdf
Smart material - Unit 3 (1).pdf
GayathriRHICETCSESTA
 
Smart material - Unit 3 (2).pdf
Smart material - Unit 3 (2).pdfSmart material - Unit 3 (2).pdf
Smart material - Unit 3 (2).pdf
GayathriRHICETCSESTA
 
Slide8appletv2 091028110313-phpapp01
Slide8appletv2 091028110313-phpapp01Slide8appletv2 091028110313-phpapp01
Slide8appletv2 091028110313-phpapp01Abhishek Khune
 
GUI.pdf
GUI.pdfGUI.pdf
GUI.pdf
Poornima E.G.
 
Java applet
Java appletJava applet
Java applet
GaneshKumarKanthiah
 
AdvancedJava.pptx
AdvancedJava.pptxAdvancedJava.pptx
AdvancedJava.pptx
DrPrabakaranPerumal
 
Applet and graphics programming
Applet and graphics programmingApplet and graphics programming
Applet and graphics programming
srijavel
 
Java Applet
Java AppletJava Applet
Java Applet
jalinder123
 
Java Programming for Designers
Java Programming for DesignersJava Programming for Designers
Java Programming for Designers
R. Sosa
 
JAVA PPT -5 BY ADI.pdf
JAVA PPT -5 BY ADI.pdfJAVA PPT -5 BY ADI.pdf
JAVA PPT -5 BY ADI.pdf
Prof. Dr. K. Adisesha
 
Applets 101-fa06
Applets 101-fa06Applets 101-fa06
Applets 101-fa06nrayan
 
JavaAdvUnit-1.pptx
JavaAdvUnit-1.pptxJavaAdvUnit-1.pptx
JavaAdvUnit-1.pptx
DrPrabakaranPerumal
 
Core java over view basics introduction by quontra solutions
Core java over view basics introduction by quontra solutionsCore java over view basics introduction by quontra solutions
Core java over view basics introduction by quontra solutionsQUONTRASOLUTIONS
 
Applet
AppletApplet
Applet
pawan2579
 

Similar to first-applet (20)

Applets - lev' 2
Applets - lev' 2Applets - lev' 2
Applets - lev' 2
 
Applet in java new
Applet in java newApplet in java new
Applet in java new
 
oops with java modules iii & iv.pptx
oops with java modules iii & iv.pptxoops with java modules iii & iv.pptx
oops with java modules iii & iv.pptx
 
Basic of Applet
Basic of AppletBasic of Applet
Basic of Applet
 
Smart material - Unit 3 (1).pdf
Smart material - Unit 3 (1).pdfSmart material - Unit 3 (1).pdf
Smart material - Unit 3 (1).pdf
 
Smart material - Unit 3 (2).pdf
Smart material - Unit 3 (2).pdfSmart material - Unit 3 (2).pdf
Smart material - Unit 3 (2).pdf
 
Slide8appletv2 091028110313-phpapp01
Slide8appletv2 091028110313-phpapp01Slide8appletv2 091028110313-phpapp01
Slide8appletv2 091028110313-phpapp01
 
GUI.pdf
GUI.pdfGUI.pdf
GUI.pdf
 
Unit 7 Java
Unit 7 JavaUnit 7 Java
Unit 7 Java
 
Java applet
Java appletJava applet
Java applet
 
AdvancedJava.pptx
AdvancedJava.pptxAdvancedJava.pptx
AdvancedJava.pptx
 
Applet and graphics programming
Applet and graphics programmingApplet and graphics programming
Applet and graphics programming
 
Java Applet
Java AppletJava Applet
Java Applet
 
Java Programming for Designers
Java Programming for DesignersJava Programming for Designers
Java Programming for Designers
 
JAVA PPT -5 BY ADI.pdf
JAVA PPT -5 BY ADI.pdfJAVA PPT -5 BY ADI.pdf
JAVA PPT -5 BY ADI.pdf
 
Applets 101-fa06
Applets 101-fa06Applets 101-fa06
Applets 101-fa06
 
JavaAdvUnit-1.pptx
JavaAdvUnit-1.pptxJavaAdvUnit-1.pptx
JavaAdvUnit-1.pptx
 
Javalecture 1
Javalecture 1Javalecture 1
Javalecture 1
 
Core java over view basics introduction by quontra solutions
Core java over view basics introduction by quontra solutionsCore java over view basics introduction by quontra solutions
Core java over view basics introduction by quontra solutions
 
Applet
AppletApplet
Applet
 

More from Mohit Patodia

Services marketing mohit
Services marketing mohit Services marketing mohit
Services marketing mohit
Mohit Patodia
 
C intro
C introC intro
C intro
Mohit Patodia
 
Bluetooth
BluetoothBluetooth
Bluetooth
Mohit Patodia
 
Basic computer maintenance
Basic computer maintenanceBasic computer maintenance
Basic computer maintenance
Mohit Patodia
 
Android
AndroidAndroid
Android
Mohit Patodia
 
Internet
InternetInternet
Internet
Mohit Patodia
 
A quick introduction to c programming
A quick introduction to c programmingA quick introduction to c programming
A quick introduction to c programming
Mohit Patodia
 
cloud computing
cloud computingcloud computing
cloud computing
Mohit Patodia
 
the computer generations
the computer generationsthe computer generations
the computer generations
Mohit Patodia
 
intel ignite refresh_rmd
intel ignite refresh_rmdintel ignite refresh_rmd
intel ignite refresh_rmd
Mohit Patodia
 
inheritance
inheritanceinheritance
inheritance
Mohit Patodia
 
self motivate
self motivateself motivate
self motivate
Mohit Patodia
 
role of it in supply chain mangment system
role of it in supply chain mangment systemrole of it in supply chain mangment system
role of it in supply chain mangment system
Mohit Patodia
 

More from Mohit Patodia (13)

Services marketing mohit
Services marketing mohit Services marketing mohit
Services marketing mohit
 
C intro
C introC intro
C intro
 
Bluetooth
BluetoothBluetooth
Bluetooth
 
Basic computer maintenance
Basic computer maintenanceBasic computer maintenance
Basic computer maintenance
 
Android
AndroidAndroid
Android
 
Internet
InternetInternet
Internet
 
A quick introduction to c programming
A quick introduction to c programmingA quick introduction to c programming
A quick introduction to c programming
 
cloud computing
cloud computingcloud computing
cloud computing
 
the computer generations
the computer generationsthe computer generations
the computer generations
 
intel ignite refresh_rmd
intel ignite refresh_rmdintel ignite refresh_rmd
intel ignite refresh_rmd
 
inheritance
inheritanceinheritance
inheritance
 
self motivate
self motivateself motivate
self motivate
 
role of it in supply chain mangment system
role of it in supply chain mangment systemrole of it in supply chain mangment system
role of it in supply chain mangment system
 

Recently uploaded

S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
tarandeep35
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
thanhdowork
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Akanksha trivedi rama nursing college kanpur.
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
Levi Shapiro
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
JEE1_This_section_contains_FOUR_ questions
JEE1_This_section_contains_FOUR_ questionsJEE1_This_section_contains_FOUR_ questions
JEE1_This_section_contains_FOUR_ questions
ShivajiThube2
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
David Douglas School District
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBCSTRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
kimdan468
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
Jean Carlos Nunes Paixão
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
Balvir Singh
 
The Diamond Necklace by Guy De Maupassant.pptx
The Diamond Necklace by Guy De Maupassant.pptxThe Diamond Necklace by Guy De Maupassant.pptx
The Diamond Necklace by Guy De Maupassant.pptx
DhatriParmar
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
DhatriParmar
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
chanes7
 

Recently uploaded (20)

S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
JEE1_This_section_contains_FOUR_ questions
JEE1_This_section_contains_FOUR_ questionsJEE1_This_section_contains_FOUR_ questions
JEE1_This_section_contains_FOUR_ questions
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBCSTRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
 
The Diamond Necklace by Guy De Maupassant.pptx
The Diamond Necklace by Guy De Maupassant.pptxThe Diamond Necklace by Guy De Maupassant.pptx
The Diamond Necklace by Guy De Maupassant.pptx
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
 

first-applet

  • 2. 2 Applets and applications  An applet is a Java program that runs on a web page  Applets can be run within any modern browser  To run modern Java applets, old browsers need an up-to-date Java plugin  appletviewer is a program that can run  An application is a Java program that runs all by itself
  • 3. 3 Packages and classes  Java supplies a huge library of pre-written “code,” ready for you to use in your programs  Code is organized into classes  Classes are grouped into packages  One way to use this code is to import it  You can import a single class, or all the classes in a package
  • 4. 4 The Applet class  To create an applet, you must import the Applet class  This class is in the java.applet package  The Applet class contains code that works with a browser to create a display window  Capitalization matters!  applet and Applet are different names
  • 5. 5 Importing the Applet class  Here is the directive that you need: import java.applet.Applet;  import is a keyword  java.applet is the name of the package  A dot ( . ) separates the package from the class  Applet is the name of the class  There is a semicolon ( ; ) at the end
  • 6. 6 The java.awt package  “awt” stands for “Abstract Window Toolkit”  The java.awt package includes classes for:  Drawing lines and shapes  Drawing letters  Setting colors  Choosing fonts  If it’s drawn on the screen, then java.awt is probably involved!
  • 7. 7 Importing the java.awt package  Since you may want to use many classes from the java.awt package, simply import them all: import java.awt.*;  The asterisk, or star (*), means “all classes”  The import directives can go in any order, but must be the first lines in your program
  • 8. 8 C and C++ programmers only  C and C++ have an #include directive that copies a library function into your program  This makes your program bigger  Java’s import gives you access to the library  It does not make your program bigger  It’s OK to use lots of include directives!
  • 9. 9 The applet so far import java.applet.Applet; import java.awt.*;
  • 10. 10 Your applet class public class Drawing extends Applet { … }  Drawing is the name of your class  Class names should always be capitalized  extends Applet says that our Drawing is a kind of Applet, but with added capabilities  Java’s Applet just makes an empty window  We are going to draw in that window  The only way to make an applet is to extend Applet
  • 11. 11 The applet so far import java.applet.Applet; import java.awt.*; // CIT 591 example public class Drawing extends Applet { …we still need to put some code in here... }
  • 12. 12 The paint method  Our applet is going to have a method to paint some colored rectangles on the screen  This method must be named paint  paint needs to be told where on the screen it can draw  This will be the only parameter it needs  paint doesn’t return any result
  • 13. 13 The paint method, part 2  public void paint(Graphics g) { … }  public says that anyone can use this method  void says that it does not return a result  A Graphics (short for “Graphics context”) is an object that holds information about a painting  It remembers what color you are using  It remembers what font you are using  You can “paint” on it (but it doesn’t remember what you have painted)
  • 14. 14 The applet so far import java.applet.Applet; import java.awt.*; // CIT 591 example public class Drawing extends Applet { public void paint(Graphics g) { …we still need to put some code in here… } }
  • 15. 15 Colors  The java.awt package defines a class named Color  There are 13 predefined colors—here are their fully- qualified names:  For compatibility with older programs (before the naming conventions were established), Java also allows color names in lowercase: Color.black, Color.darkGray, etc. Color.BLACK Color.PINK Color.GREEN Color.DARK_GRAY Color.RED Color.CYAN Color.GRAY Color.ORANGE Color.BLUE Color.LIGHT_GRAY Color.YELLOW Color.WHITE Color.MAGENTA
  • 16. 16 New colors  Every color is a mix of red, green, and blue  You can make your own colors: new Color( red , green , blue )  Amounts range from 0 to 255  Black is (0, 0, 0), white is (255, 255, 255)  We are mixing lights, not pigments  Yellow is red + green, or (255, 255, 0)
  • 17. 17 Setting a color  To use a color, we tell our Graphics g what color we want: g.setColor(Color.RED);  g will remember this color and use it for everything until we tell it some different color
  • 18. 18 The paint method so far public void paint(Graphics g) { g.setColor(Color.BLUE); …draw a rectangle… g.setColor(Color.RED); …draw another rectangle… } }
  • 19. 19 Pixels  A pixel is a picture (pix) element  one pixel is one dot on your screen  there are typically 72 to 90 pixels per inch  java.awt measures everything in pixels
  • 20. 20 Java’s coordinate system  Java uses an (x, y) coordinate system  (0, 0) is the top left corner  (50, 0) is 50 pixels to the right of (0, 0)  (0, 20) is 20 pixels down from (0, 0)  (w - 1, h - 1) is just inside the bottom right corner, where w is the width of the window and h is its height (0, 0) (0, 20) (50, 0) (50, 20) (w-1, h-1)
  • 21. 21 Drawing rectangles  There are two ways to draw rectangles:  g.drawRect( left , top , width , height );  g.fillRect(left , top , width , height );
  • 22. 22 The complete applet import java.applet.Applet; import java.awt.*; // CIT 591 example public class Drawing extends Applet { public void paint(Graphics g) { g.setColor(Color.BLUE); g.fillRect(20, 20, 50, 30); g.setColor(Color.RED); g.fillRect(50, 30, 50, 30); } }
  • 23. 23 Some more java.awt methods  g.drawLine( x1 , y1 , x2 , y2 );  g.drawOval( left , top , width , height );  g.fillOval( left , top , width , height );  g.drawRoundRect( left , top , width , height );  g.fillRoundRect( left , top , width , height );  g.drawArc( left , top , width , height , startAngle , arcAngle );  g.drawString( string , x , y );
  • 24. 24 The HTML page  You can only run an applet in an HTML page  The HTML looks something like this:  <html> <body> <h1>DrawingApplet Applet</h1> <applet code="DrawingApplet.class" width="250" height="200"> </applet> </body> </html>  BlueJ will create this HTML for you