In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "how to achieve image and sound effects in Java". In daily operation, I believe that many people have doubts about how to achieve image and sound effects in Java. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts of "how to achieve image and sound effects in Java". Next, please follow the editor to study!
If Java's support for graphics and text media does not have a significant advantage over other languages, then
Java's support for image and sound media is really superior. Displaying images and playing sounds is like displaying a line of text.
It's just as convenient as Ben. At the same time, it is the flexible use of image and sound media in Java animation that makes Web pages more
Charming.
4.2.1 display of image files
As described in the previous section, there are a number of ways to draw graphics in the Graphics class, but if you use them
Drawing a more complex graph (such as a lively and lovely puppy) in real time during the operation of applet is like
Is using axes and wood blocks to build space shuttles. Therefore, for complex graphics, most of them use special drawing software in advance.
Draw a good piece, or use other image capture tools (such as scanners, visual effects cards, etc.) to obtain image data information
Then save them into the image file in a certain format. When applet is running, as long as you find the location where the image file is stored, set the
Just load it into memory and display it on the screen at the right time.
1. Loading of image files
At present, there are only two image file formats supported by Java, which are GIF and JPEG formats (with .GIF,
Files with .JPG, .JPEG suffixes). So if you have other image files, you have to convert them to these two first.
A variety of formats. There are many software that can convert image format, such as PhotoStyler and so on.
The getImage () method is provided in the Applet class to load the prepared image file into applet, but I
We must first indicate where the image file is stored. Because the Java language is oriented to network applications, the storage of files
The storage location is not limited to the local machine's disk directory, but in most cases it is direct access to the Web server on the network.
Image files, therefore, Java uses URL (Universal Resource Location, uniform Resource Locator) to determine
The network location of the bit image file. Therefore, Java specifically provides URL classes to manage URL information
Representing an URL information can be divided into two forms:
One form is called absolute URL, which indicates the full path name of the network resource. Such as:
Absolute URL: "http://www.xyz.com/java/imgsample/images/m1.gif"
The other is called the relative URL form, which consists of the benchmark URL (that is, base URL) plus the relative to the benchmark URL
The relative URL consists of two parts. For example, the above example can be expressed as follows:
Benchmark URL: "https://cache.yisu.com/upload/information/20200703/145/53370"
Now let's take a look at the calling format of the getImage () method:
Image getImage (URL url)
Image getImage (URL url, String name)
We can find that the return values of both invocation formats are Image objects. Indeed, Java specially provides
Java.awt.Image class to manage information related to image files, so do not perform operations related to image files
Forget the class import. The first call format of the getImage () method requires only a URL object as a parameter, which
It's absolute URL. The latter format takes two parameters, the URL object given by the first parameter is the benchmark URL, and the second
Parameters are string types that describe the path and file name information relative to the benchmark URL, so these two parameters
The contents of the URL are combined to form an absolute Web. For example, the following two ways of writing return the same result:
Image img=getImage (new URL ("https://cache.yisu.com/upload/information/20200703/145/53369.gif");
Image img=getImage (new URL ("https://cache.yisu.com/upload/information/20200703/145/53371.gif");
On the surface, it seems that the first invocation format is more convenient, but in fact the second invocation format is more common.
Because this format is more flexible. It turns out that two methods are provided in the Applet class to help us easily get the benchmark
URL objects, which are called in the following format:
URL getDocumentBase ()
URL getCodeBase ()
Where the benchmark URL object returned by the getDocumentBase () method represents the HTML file containing the applet
The directory at, for example, the file is stored in http://www.xyz.com/java/imgsample/m1.html", the
Method returns "https://cache.yisu.com/upload/information/20200703/145/53372");"
Then even if the entire imgsample directory is moved anywhere else, the image file can be loaded correctly, using the
For the absolute URL form, you need to modify the applet code and recompile it.
two。 Display of image files
The getImage () method simply loads the image file from the network and leaves it to the Image object to manage. Then we...
How to display the image in the resulting Image object on the screen? This brings us back to our old friend Graphics class.
Because the Graphics class provides a drawImage () method, which completes the image in the Image object
Display at a specific location on the screen is as convenient as displaying text. The call format of the drawImage () method is as follows:
Boolean drawImage (Image img, int x, int y, ImageObserver observer)
Where the img parameter is the Image object to be displayed. The x and y parameters are the coordinate values of the upper-left corner of the image. Observer
Parameter is an ImageObserver interface (interface), which is used to track whether the loading of image files has been completed.
We usually set this parameter to this, that is, passing a reference to this object to implement this interface.
In addition to outputting the image file as is, another call format of the drawImage () method can also specify the graph
Like the area size shown:
Boolean drawImage (Image img, int x, int y, int width, int height, ImageObserver observer)
This format has two more parameters width and height than the first format, which represents the width and height of the image display.
. If the width and height of the actual image are different from these two parameter values, the Java system will automatically scale it to
It is suitable for the rectangular area we have determined.
Sometimes, in order not to deform and distort the image due to scaling, we can make the width and height of the original image in the same proportion.
OK, zoom out or zoom in. So how do you know the size of the original picture? You only need to call two methods in the Image class to separate
Get the width and height of the original drawing. Their invocation format is as follows:
Int getWidth (ImageObserver observer)
Int getHeight (ImageObserver observer)
Like the drawImage () method, we usually use this as the parameter value of observer.
The following program segment gives an example of displaying an image file, the result of which is shown in figure 4 Mel 14.
Import java.awt.Graphics
Import java.awt.Image
Public class Images extends java.applet.Applet {
Image img
Public void init () {
Img=getImage (getCodeBase (), "man.gif")
}
Public void paint (Graphics g) {
Int w=img.getWidth (this)
Int h=img.getHeight (this)
G.drawImage (img,20,10,this); / / original image
G.drawImage (img,200,10,w/2,h/2,this); / / halved
G.drawImage (img,20,200,w*2,h/3,this); / / wide flat chart
G.drawImage (img,350,10,w/2,h*2,this); / / thin height map
}
}
Fig. 4 MUE 14 shows the image file
4.2.2 playback of sound files
Direct support for sound media can be said to be a major feature of Java, especially in animation with sound effects.
Can make people in the visual and auditory beauty to enjoy, that is called pleasure. Playing sound files and display pictures in Java
As convenient as a file, you just need to load the sound file first and then play it.
Java currently supports only one format of sound files, and that is SUN's AU format (.AU file), also known as
Is in u-law format. Because the sound in AU format only has the sampling frequency of 8KHz and does not support stereo effects, so the sound quality is not
That's too good. The only advantage is that the size of AU sound files is smaller than other formats, which is convenient for online transmission. In general, we
Most of the familiar sound files are in WAV format, so you must first convert them to AU format (you can choose Goldwave
Software to carry out this format conversion).
Once the sound file is ready, you can consider loading it and playing it. Play () provided in the Applet class
Method can complete the loading and playback of the sound file, and the call format is as follows:
Void play (URL url)
Void play (URL url, String name)
It can be seen that the calling format of the play () method is exactly the same as that of the getImage () method, and it also uses URL to locate
Sound files. For example, if a sound file audio.au is stored in the same directory as the applet file, you can write:
Play (getCodeBase (), "audio.au")
As soon as the play () method loads the sound file, it plays it immediately. If the sound file under the specified URL cannot be found
The play () method does not return an error message, just can't hear the sound you want to hear.
Because the play () method can only play a sound once, if you want to loop a sound as background music, you need to
Using the more powerful AudioClip class, it can manage the playback operation of sound more effectively. Because it is defined in the
In the java.applet package, so if you use this class, don't forget to add:
Import java.applet.AudioClip
To get the AudioClip object, we can call the getAudioClip () method in the Applet class. It can hold.
Loads the sound file of the specified URL and returns an AudioClip object in the following call format:
AudioClip getAudioClip (URL url)
AudioClip getAudioClip (URL url, String name)
Once you have the AudioClip object, you can call various methods provided in the AudioClip class to manipulate the
Sound data, these methods are shown in Table 4.
If the getAudioClip () method does not find the specified sound file, it returns a null value. So, in
You should check that the resulting AudioClip object is not null before calling the methods listed in table 4, because in null
Calling the above method on the object will cause an error.
If necessary, we can also load several sound files in applet at the same time to play together.
These sounds will be mixed together, just like a duet. Another point to note is that if we use the
When playing background music repeatedly with the loop () method of the AudioClip object, don't forget to call the
The stop () method of the AudioClip object to end playback, otherwise, even if the user leaves the Web page, the sound
The tone will not stop, which will undoubtedly annoy users. Therefore, we usually add to the stop () method of applet
The code that stops playback.
For example, the following program will play two sounds, one is continuous background music, and the other is a speech recording.
Sound.
Import java.applet.AudioClip
Public class Audios extends java.applet.Applet {
AudioClip bgmusic,speak
Public void init () {
Bgmusic=getAudioClip (getDocumentBase (), "space.au")
Speak=getAudioClip (getDocumentBase (), "intro.au")
}
Public void start () {
If (bgmusicskills null)
Bgmusic.loop ()
If (null in recent years)
Speak.play ()
}
Public void stop () {
If (bgmusicskills null)
Bgmusic.stop (); / / turn off the background music, remember.
}
}
At this point, the study of "how to achieve image and sound effects in Java" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.