search
HomeJavajavaTutorialShare a small Java application: applet

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

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

[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
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
What aspects of Java development are platform-dependent?What aspects of Java development are platform-dependent?Apr 26, 2025 am 12:19 AM

Javadevelopmentisnotentirelyplatform-independentduetoseveralfactors.1)JVMvariationsaffectperformanceandbehavioracrossdifferentOS.2)NativelibrariesviaJNIintroduceplatform-specificissues.3)Filepathsandsystempropertiesdifferbetweenplatforms.4)GUIapplica

Are there performance differences when running Java code on different platforms? Why?Are there performance differences when running Java code on different platforms? Why?Apr 26, 2025 am 12:15 AM

Java code will have performance differences when running on different platforms. 1) The implementation and optimization strategies of JVM are different, such as OracleJDK and OpenJDK. 2) The characteristics of the operating system, such as memory management and thread scheduling, will also affect performance. 3) Performance can be improved by selecting the appropriate JVM, adjusting JVM parameters and code optimization.

What are some limitations of Java's platform independence?What are some limitations of Java's platform independence?Apr 26, 2025 am 12:10 AM

Java'splatformindependencehaslimitationsincludingperformanceoverhead,versioncompatibilityissues,challengeswithnativelibraryintegration,platform-specificfeatures,andJVMinstallation/maintenance.Thesefactorscomplicatethe"writeonce,runanywhere"

Explain the difference between platform independence and cross-platform development.Explain the difference between platform independence and cross-platform development.Apr 26, 2025 am 12:08 AM

Platformindependenceallowsprogramstorunonanyplatformwithoutmodification,whilecross-platformdevelopmentrequiressomeplatform-specificadjustments.Platformindependence,exemplifiedbyJava,enablesuniversalexecutionbutmaycompromiseperformance.Cross-platformd

How does Just-In-Time (JIT) compilation affect Java's performance and platform independence?How does Just-In-Time (JIT) compilation affect Java's performance and platform independence?Apr 26, 2025 am 12:02 AM

JITcompilationinJavaenhancesperformancewhilemaintainingplatformindependence.1)Itdynamicallytranslatesbytecodeintonativemachinecodeatruntime,optimizingfrequentlyusedcode.2)TheJVMremainsplatform-independent,allowingthesameJavaapplicationtorunondifferen

Why is Java a popular choice for developing cross-platform desktop applications?Why is Java a popular choice for developing cross-platform desktop applications?Apr 25, 2025 am 12:23 AM

Javaispopularforcross-platformdesktopapplicationsduetoits"WriteOnce,RunAnywhere"philosophy.1)ItusesbytecodethatrunsonanyJVM-equippedplatform.2)LibrarieslikeSwingandJavaFXhelpcreatenative-lookingUIs.3)Itsextensivestandardlibrarysupportscompr

Discuss situations where writing platform-specific code in Java might be necessary.Discuss situations where writing platform-specific code in Java might be necessary.Apr 25, 2025 am 12:22 AM

Reasons for writing platform-specific code in Java include access to specific operating system features, interacting with specific hardware, and optimizing performance. 1) Use JNA or JNI to access the Windows registry; 2) Interact with Linux-specific hardware drivers through JNI; 3) Use Metal to optimize gaming performance on macOS through JNI. Nevertheless, writing platform-specific code can affect the portability of the code, increase complexity, and potentially pose performance overhead and security risks.

What are the future trends in Java development that relate to platform independence?What are the future trends in Java development that relate to platform independence?Apr 25, 2025 am 12:12 AM

Java will further enhance platform independence through cloud-native applications, multi-platform deployment and cross-language interoperability. 1) Cloud native applications will use GraalVM and Quarkus to increase startup speed. 2) Java will be extended to embedded devices, mobile devices and quantum computers. 3) Through GraalVM, Java will seamlessly integrate with languages ​​such as Python and JavaScript to enhance cross-language interoperability.

See all articles

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

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor