Home Java javaTutorial Share a small Java application: applet

Share a small Java application: applet

May 18, 2017 am 10:15 AM

Java Applets are small applications written in Java language. They can be directly embedded into web pages and can produce special effects.

After the applet is compiled, it will Generate .class files and embed the .class files in html web pages. As long as the user connects to a web page, the applet will be downloaded to the user's computer along with the web page and run

The inheritance relationship of the applet class is as follows:

java.lang.Object

## java.awt.Component

java.awt.Container

java.awt.Panel

## java.applet.Applet

The life cycle of applet

is as follows

##The four main methods of applet
public void init(): Called by the browser or appletviewer to tell the current applet that it has been loaded into the system. This method is always called before the start() method is called for the first time.

public void start(): Called by the browser or appletviewer to tell the current applet that it should start executing. This method is called after the init() method, and this method is called every time the web page accesses the applet

public void stop(): Called by the browser or appletviewer to tell the current applet that it should stop execution. This method is called when the Web page containing the current applet is replaced by another Web page. This method is also called before calling the destroy() method. Method

public void destroy(): Called by the browser or appletviewer to tell the current applet that it has been asked to return and that it should clear any resources assigned to it

Please see the following code

import java.awt.*;
import java.util.*;
public class cam1 extends java.applet.Applet
{
     String s;
     int inits=0,starts=0,stops=0;
     public void init()
     {
    	 inits++;
    	 showStatus("now init");
    	 System.out.println("now init");
    	 pause();
    	 showStatus("leave init");
    	 System.out.println("leave init");
    	 pause();
     }
     public void start()
     {
    	 starts++;
    	 showStatus("now start");
    	 System.out.println("now start");
    	 pause();
    	 showStatus("leave start");
    	 System.out.println("leave start");
    	 pause();
     }
     public void stop()
     {
    	 stops++;
    	 showStatus("now stop");
    	 System.out.println("now stop");
    	 pause();
    	 showStatus("leave stop");
    	 System.out.println("leave stop");
    	 pause();
     }
     public void paint(Graphics g)
     {
    	 s="inits: "+inits+"starts: "+starts+"stops: "+stops;
    	 g.drawString(s, 10, 10);
    	 System.out.println("now paint: "+s);
    	 pause();
     }
     public void pause()
     {
    	 Date d=new Date();
    	 long t=d.getTime();
    	 while(t+1000>d.getTime())
    	 {
    		 d=new Date();
    	 }
     }
}
Copy after login

The applet viewer is as follows


##eclipse The Console in
is as follows

now init

leave init

now start

leave start

now paint: inits: 1starts: 1stops: 0

now paint: inits: 1starts: 1stops: 0 //Zoom (zoom in)

now paint: inits: 1starts: 1stops: 0 //Zoom (zoom out)

now stop //Restart

leave stop

now init

##leave init

now start

leave start

now paint: inits: 2starts: 2stops: 1

now stop

leave stop

Embed it in html

<HTML>
<HEAD>
<TITLE>WELCOME </TITLE>
</HEAD>
<BODY> test
<APPLET code="cam1.class" WIDTH=750 HEIGHT=325>
</APPLET>
</BODY>
</HTML>
Copy after login
I am using Google Chrome and the web page opens as follows

## View the output of #System.out.println in the java console


Before setting the java console to display in the java control panel


When the window is resized, moved, or its contents change, paint
Function

Redraw the applet window

The console displays as follows

Java 插件10.13.2.20
使用 JRE 版本 1.7.0_13-b20 Java HotSpot(TM) Client VM
用户主目录 = C:\Users\Administrator
----------------------------------------------------
c:   清除控制台窗口
f:   终结在结束队列上的对象
g:   垃圾收集
h:   显示此帮助消息
l:   转储类加载器列表
m:   打印内存使用情况
o:   触发日志记录
q:   隐藏控制台
r:   重新加载策略配置
s:   转储系统和部署属性
t:   转储线程列表
v:   转储线程堆栈
x:   清除类加载器高速缓存
0-5: 设置跟踪级别为<n>
----------------------------------------------------
now init
leave init
now start
now paint: inits: 1starts: 1stops: 0
leave start
now paint: inits: 1starts: 1stops: 0   //缩放
now paint: inits: 1starts: 1stops: 0
now paint: inits: 1starts: 1stops: 0
now paint: inits: 1starts: 1stops: 0
now stop  //刷新网页
leave stop
Exception in thread "thread applet-cam1.class-1" java.lang.NullPointerException
	at java.awt.EventQueue.isDispatchThread(Unknown Source)
	at com.sun.deploy.uitoolkit.impl.awt.AWTAppletAdapter.runOnEDT(Unknown Source)
	at com.sun.deploy.uitoolkit.impl.awt.AWTAppletAdapter.doClearAppletArea(Unknown Source)
	at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
	at java.lang.Thread.run(Unknown Source)
now init
leave init
now start
now paint: inits: 1starts: 1stops: 0
leave start
Copy after login
[Related recommendations]

1.

Special Recommendation

:

"php Programmer Toolbox" V0.1 version download

2. Java Free Video Tutorial

3. Take you to get to know the Java Applet program

4. Teach you how to configure the Applet environment

5. Detailed explanation of the differences between Application and Applet

The above is the detailed content of Share a small Java application: applet. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Java Spring Interview Questions Java Spring Interview Questions Aug 30, 2024 pm 04:29 PM

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Break or return from Java 8 stream forEach? Break or return from Java 8 stream forEach? Feb 07, 2025 pm 12:09 PM

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

TimeStamp to Date in Java TimeStamp to Date in Java Aug 30, 2024 pm 04:28 PM

Guide to TimeStamp to Date in Java. Here we also discuss the introduction and how to convert timestamp to date in java along with examples.

Java Program to Find the Volume of Capsule Java Program to Find the Volume of Capsule Feb 07, 2025 am 11:37 AM

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

Create the Future: Java Programming for Absolute Beginners Create the Future: Java Programming for Absolute Beginners Oct 13, 2024 pm 01:32 PM

Java is a popular programming language that can be learned by both beginners and experienced developers. This tutorial starts with basic concepts and progresses through advanced topics. After installing the Java Development Kit, you can practice programming by creating a simple "Hello, World!" program. After you understand the code, use the command prompt to compile and run the program, and "Hello, World!" will be output on the console. Learning Java starts your programming journey, and as your mastery deepens, you can create more complex applications.

How to Run Your First Spring Boot Application in Spring Tool Suite? How to Run Your First Spring Boot Application in Spring Tool Suite? Feb 07, 2025 pm 12:11 PM

Spring Boot simplifies the creation of robust, scalable, and production-ready Java applications, revolutionizing Java development. Its "convention over configuration" approach, inherent to the Spring ecosystem, minimizes manual setup, allo

See all articles