Java Programming Applets and Drawing 1

Java Programming Applets and Drawing 1 What is an Applet? pp „ „ „ „ „ An applet is a special kind of Java program that a browser enabled with Ja...
Author: Frederick Ryan
2 downloads 0 Views 269KB Size
Java Programming Applets and Drawing

1

What is an Applet? pp „

„ „ „ „

An applet is a special kind of Java program that a browser enabled with Java technology can download from the internet and run. An applet is typically embedded inside a web-page and runs in the context of the browser browser. The best support isn't a browser, but the standalone program appletviewer In general you want to write applets that can be run with any browser A applet An l t is i a Panel P l that th t allows ll iinteraction t ti with ith a JJava program.

2

Applets ¾ All applet classes must be declared as public ¾ Applets do not begin execution at main( ) method method, in fact most applets don’t have a main( ) method ¾ Applet pp is compiled p in the same way y as a normal application ¾ To execute an applet in a web browser, you need to write a HTML file that contains the appropriate applet tag −

3

Html File H d Java Example 4

HelloWorld Applet import javax.swing.JApplet; javax swing JApplet; import java.awt.Graphics; public class HelloWorld extends JApplet{ public void paint(Graphics g) { gg.drawString("Hello g( world!",, 5,, 15); ); } }

5

Life Cycle of an Applet „

Init(): This method is intended for whatever initialization is needed for your applet. Invoked when the applet is first loaded and again if the applet is reloaded

„

Start(): This method is automatically called after init method It is also called whenever user returns to the method. page containing the applet after visiting other pages.

„

Stop(): This method is automatically called whenever the user moves away from the page containing applets. You can use this method to stop an animation.

„

Destroy(): This method is only called when the browser shuts down normally. Thus, the applet can be initialized once and only once once, started and stopped one or more times in its life, and destroyed once and only once. 6

Browser Calling Applet Methods Browser creates the applet Loaded

Browser invokes init() Created

Browser invokes start() Initialized

Browser invokes start() Started

Started Browser invokes stop()

JVM loads the applet class

Browser invokes destroyed() Destroyed

7

Sequence of execution public void init( ) public void start( ) public void paint(Graphics g ) public bli void id stop( t () public void destroy( )

Called first Called second and whenever an applet is restarted Called when the window must be restored Called when the applet is stopped Called when the applet is destroyed and is the last method executed 8

public void paint(Graphics g) „ „ „ „ „

Almost always needed Any painting you want to do should be done here, or in a method you call from here Painting that you do in other methods may or may not happen Don’t call this method. It’s called automatically.Call repaint( ) instead. Applets inherit the paint method from the Abstract Window Toolkit (AWT) Container class class.

Sample Graphics methods

„

A Graphics is something you can paint on. g.drawString(“Hello, World”, 20, 20); g.drawRect(x, g ( yy, width, height); g ) g.fillRect(x, y, width, height); g.drawOval(x, g ( , y, width,, height); g ); g.fillOval(x, y, width, height);

„

drawArc(int x, int y, int width, int height, int startAngle, int endAngle)

„

g.setColor(Color.red);

„ „ „ „ „

repaint( ) „

Call repaint( ) when you have changed something and want your changes to show up on the screen

„

When you call repaint( ), Java schedules a call to update(Graphics update(G ap cs g) g).

Applets „ „

JApplet is like a JFrame Already has a panel „

JApplet

Access panel with JApplet.getContentPane( )

contentPane import javax.swing.*; class l h hello ll extends t d JApplet JA l t { JButton public void init(){ JButton b = new JButton(“press me”); getContentPane().add(b); } }

Example-2 import java.awt.*; import java.applet.*; public class DrawExample extends Applet { // The colors you will use Color redColor; Color weirdColor; Color bgColor; public void init() () { redColor = Color.red; weirdColor = new Color(60,60,122); bgColor = Color Color.blue; bl e setBackground(bgColor); } public bli void id stop() t () { }

13

Example 2 Example-2

public void paint(Graphics g) { g.drawString("Shapes g g( p and Colors",80,20); , , ); g.setColor(redColor); g.drawRect(100,100,100,100); g g.fillRect(110,110,80,80); ( , , , ); // change colors again g g.setColor(weirdColor); ( );

// a circle (int x, int y, int width, int height,int startAngle, int arcAngle); g.fillArc(120,120,60,60,0,360); g.setColor(Color.yellow); g.drawLine(140,140,160,160); // Draw a line (int x1, int y1, int x2, int y2) g.setColor(Color.black); } }

14

Output

15

Example-2 p import java.awt.*; import java.applet.*; public class myd extends Applet { private Image p g img; g; public void init() { img = getImage(getCodeBase(),“img.gif"); } public void paint(Graphics g) { g drawImage(img 0 0 this); g.drawImage(img,0,0,this); g.drawString("hiiiiiiiiii",600,600); } }

16

17

Application and Applets „ „

An application is a standalone program consisting of at least one class with a main method method. Applets differ significantly from applications. First, applets do not have a main method that is automatically called ll d to t begin b i the th program. … Instead, several methods are called at different points in the execution of an applet. …

„ „

The difference Th diff between b t Java J applets l t and d applications li ti lies in how they are run. Applications pp are usually y run by y loading g the application's pp main class file with a Java interpreter, such as the java tool in the JDK(TM) 6.

18

Application + Applet

import javax.swing.*;

Command line

Browser

JFrame

JApplet pp

class helloApp { public static void main(String[] args){ // create Frame and put my mainPanel in it JFrame f = new JFrame(“title”); mainPanel p = new mainPanel(); f.setContentPane(p); f.show(); } } class helloApplet extends JApplet { public void init(){ // put my mainPanel in the Applet mainPanel p = new mainPanel(); getContentPane().add(p); } } // my main GUI is in here: class mainPanel extends JPanel { mainPanel(){ (){ setLayout(new FlowLayout()); JButton b = new JButton(“press me”); add(b); } }

or

contentPane

JPanel JButton

Example Applet in an Application public class DisplayImageWithURL extends JApplet { public DisplayImageWithURL() { java.net.URL url = this.getClass().getResource("us.gif"); add(new JLabel(new ImageIcon(url))); } /** Main method */ public static void main(String[] args) { // Create a frame JFrame frame = new JFrame("DisplayImageWithURL"); // Create an instance of the applet DisplayImageWithURL applet = new DisplayImageWithURL(); applet.init(); // Add the applet instance to the frame frame.getContentPane().add(applet, java.awt.BorderLayout.CENTER); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Display the frame ( 150); ) frame.setSize(200, frame.setVisible(true); } }

20