Applets and GUI Event Handling in JAVA

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 31

Applets and GUI Event

Handling in JAVA

Applets and applications


An applet is a Java program that runs on a web
page
Applets can be run within any modern browser
You can run applet on appletviewer or in an HTML page.

import java.applet.Applet;
import java.awt.Graphics;
public class hello extends Applet {
public void paint(Graphics g) {
g.drawString("Hello world!", 50, 25);
}
}
2

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 its drawn on the screen, then java.awt is


probably involved!

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

The applet so far


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

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
Javas 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

10

The applet so far


import java.applet.Applet;
import java.awt.*;
example
public class Drawing extends Applet {
we still need to put some code in here...
}

11

import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
public class hello extends Applet {
String msg;
public void init(){
setBackground(Color.cyan);
setForeground(Color.red);
msg = "Inside init()";
}
public void start()
{setForeground(Color.red);
msg+=" NOW in start";
}
public void paint(Graphics g) {setForeground(Color.red);
msg+=" NOW in paint";
g.drawString(msg, 50, 25);
}
}
12

13

Applet Life Cycle.

14

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 doesnt return any result


15

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

16

It remembers what color you are using


It remembers what font you are using
You can paint on it (but it doesnt
remember what you have painted)

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
}
}
17

Colors
The java.awt package defines a class named Color
There are 13 predefined colorshere are their
fully-qualified names:
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

For compatibility with older programs (before


the naming conventions were established),
Java also allows color names in lowercase:
Color.black, Color.darkGray, etc.
18

New colors
Every color is a mix of red, green,
and blue
You can make your own colors:
Color n= 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,

19

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

20

The paint method so far


public void paint(Graphics g) {
g.setColor(Color.BLUE);
draw a rectangle
g.setColor(Color.RED);
draw another rectangle
}
}

21

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

22

Javas coordinate system


(0, 0)

(50, 0)

(0, 20)

(50, 20)

(w-1, h-1)

23

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

Drawing rectangles
There are two ways to draw rectangles:
g.drawRect( left , top , width , height );

g.fillRect(left , top , width , height );

24

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);
}
}

25

Chess board
import java.applet.Applet;
import java.awt.*;
// CIT 591 example
public class Drawing extends Applet {
public void paint(Graphics g) {
int row, col;
int x,y;
for(row=0;row<8;row++)
{ for(col=0;col<8;col++)
{ x=col*20; y=row*20;
If( (row%2)==(col%2))
g.setColor(Color.RED);
else
g.setColor(Color.Black);
g.fillRect(x,y,20,20);
}}}}

26

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 );

27

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>
30

Home assignment submitted by


05/04/16
Software Engineering portion(UML
diagrams including class diagram,
use case diagram and activity
diagram)
Software development life cycle
marks [10]

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy