Java Applet Getting Started 21

zhaozj2021-02-11  187

Getting started with Java Applet

YY435

Pacific Network Academy

Sixth day

Reading of a graphic file

In Java, you can load and output graphic file format, with two kinds, which are GIF and JPEG files (don't doubt, really do not support BMP file format). As long as you can give the location and file name of the graphic file, you can load the desired graphic by getImage.

Here, the location refers to which directory is not on the disk, but the URL location in the graphic file. In other words, you can not only use the graphic files stored on your hard drive in the Java program, but also through the URL specified by the URL, the graphics files on the network resource are directly used, and the way the use is very simple, and there is no generally Writing the difficulties and burdens of the network program.

Let's take a look at GetImage's way of use, the method of getImage mainly appears in two classes, the first is in java.applet.applet, and the other is java.awt.tookit. When writing a Java application, you can only use the method in java.awt.tookit, and the good species can be used when writing a Java applet. The format of the GetImage method is: 1) Image GetImage (url url, string name) in java.applet.applet, image getImage (String filename) iMage GetImage (String FileName) in Java.applet.applet (URL URL)

For example, its calling method is:

URL URL1, URL2; URL1 = New URL ("File: / D: /Image/Pic1.gif"); URL2 = New URL ("http://abc.cde.edu.cn/applet/pic2.jpg") Image image1 = getImage (URL1): Image Image2 = GetImage (URL2): Image Image3 = GetImage (getcodebase (), "p1.jpg");

The getCodeBase () method is to return a relative path, that is, where your web file is now, then its return value is this location of your web file.

After reading the graphics file into the memory, the next step is to display it, how to display it, use the g.drawimage (image1, x, y, this) method, x, y is the coordinate displayed point. If you want to change the size, you can use the following display mode.

G.drawImage (image1, x, y, width, height, this); g.drawimage (image1, x, y, width, height, color.blue, this)

One of the following methods is to set the background color of the picture into a Blue color, change Width and Height to change the display size and proportion of the image, can form an enlargement and reduction, or pull long pull flat.

Let us look at a specific example!

This is a picture display twice, please check its source code [View Source Code]

Reading in two sound files

The sound file is the same, but now Java seems to support the sound file format of the AU format, this format is not much, so you may need to convert other file formats into this. Format. Its transfer method and playback method are as follows:

Import java.applet.audioclip; audioclip bgsound = getaudioclip (URL URL): BgSound.Play (); // Play a bgsound.loop (); // loop play BGSound.stop (); // Stop playback // You also You can read and play Play directly on time (getCodeBase (), "Audio / Welcome.au");

......

The method of using its URL is a principle when reading an image in front, this is not much told here!

Font attribute settings in three javapple

[

转载请注明原文地址:https://www.9cbs.com/read-4716.html

New Post(0)