In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-13 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how Java makes the program more moving". Friends who are interested might as well take a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "how Java makes the program more moving"!
11.1 Image
Impart new knowledge
Unlike other programming languages, the Java language does not provide image files with controls like any image frame (in Java terms, classes), but images are everywhere. We can display images on tags, in buttons, on many ordinary GUI widgets.
In general, in the Java language, using an image takes three steps:
1. Load Ima
Java is developed with Internet and is considered to be the best Internet programming language, so we often see a lot of details reflect this style or spirit.
For example, the Java language makes file references through URL (uniform Resource location, Uniform Resource Location, the most common URL is used to point to WEB pages). Such a mechanism makes it no different for the Java language to reference a local file than to reference a file on Internet.
We load the image through the getImage () method, which has the following syntax format:
Public Image getImage (String url)
Public Image getImage (String url,String name)
The first form: give the URL pointing to the file directly
The second form: a new url is formed by merging the specified URL with the subsequent name.
Some tips:
Considering that readers may not know how to use URL to represent local files, an example is given here for everyone to understand:
File:/c:/javastudy/test.jpg
The format is: file:/ drive letter: / path / file name. It is important to note that we use "/" to separate parts, rather than using the "" character under Windows.
We often need to call the files in the current directory, even so, we also need to write out the URL of this directory, otherwise we will not be able to find the program.
This is too painful! However, Java also provides a very considerate way to easily get the URL of the current directory:
GetCodeBase ()
In this way, if we want to call the 1.jpg file in the current directory, we can use the statement:
GetImage (getCodeBase (), "1.jpg")
The getImage method returns an Image object. Therefore, we usually define an Image object first to store the loaded images, such as:
Image image1=getImage (getCodeBase (), "1.jpg")
In this way, 1.jpg is loaded into image1.
two。 Generate ImageIcon
After loading the image, it cannot be used by the program directly, so we need to call the ImageIcon method to generate an ImageIcon:
ImageIcon (Image)
This method will return an ImageIcon object, and once you have the ImageIcon object, you can use it. Here is an example where we convert an image1 object containing an image of 1.jpg into an ImageIcon object icon1:
ImageIcon icon1=new ImageIcon (image1)
3. Display image
Now, we can display the image, we can take the ImageIcon object as a parameter, create labels, buttons, and other parts, we can display the image. Let's introduce them separately:
1) label:
Public JLabel (ImageIcon icon)
Public JLabel (String text,ImageIcon icon)
The first constructor allows only the image to be displayed in the tag, while the second constructor can display both the text label and the image. It can be said that the image is luxuriant.
2) Button:
Public JButton (ImageIcon icon)
Public JButton (String text,ImageIcon icon)
Obviously, it is very similar to the tag constructor.
Note:
Everything is evolving, and the parts provided in the java.AWT package do not contain images well, while those provided in more advanced java.Swing packages do.
Example illustration
Next, let's build a program that displays images. In order to facilitate the completion of this example, we have selected an image file that we all have:
C:windowssystemoobeimagescnncterr.jpg file.
In order to facilitate operation, we first copy it to the C:javastudy directory.
Source program: useImage.java
Import java.applet.*
Import java.awt.*
Import javax.swing.*
Public class useImage extends JApplet
{
Image image1
Public void init ()
{
JPanel panel1= (JPanel) getContentPane ()
Panel1.setLayout (new BorderLayout ())
Image1=getImage (getCodeBase (), "cnncterr.jpg")
ImageIcon icon1=new ImageIcon (image1)
JLabel imagelabel=new JLabel (icon1)
Panel1.add (imagelabel)
}
}
After compiling this program, the result of running is shown in the following figure:
Figure 11-1 output of program useImage.java
Next, let's take a look at an interesting program:
Source program: useImage2.java
Import java.applet.*
Import java.awt.*
Import javax.swing.*
Public class useImage2 extends JApplet
{
Image image1,image2
Public void init ()
{
JPanel panel1= (JPanel) getContentPane ()
Panel1.setLayout (new BorderLayout ())
Image1=getImage (getCodeBase (), "ontbvsa.gif")
ImageIcon icon1=new ImageIcon (image1)
Image2=getImage (getCodeBase (), "offtbvsa.gif")
ImageIcon icon2=new ImageIcon (image2)
JButton imagebutton=new JButton (icon2)
Imagebutton.setPressedIcon (icon1)
Panel1.add (imagebutton,BorderLayout.CENTER)
}
}
The output of this program is:
Figure 11-2 initial display of the program
Figure 11-3 when this button is pressed
Here, a new method is used to achieve a special effect!
Imagebutton.setPressedIcon (icon1)
This is the image that icon1 points to when you press this button.
Self-test exercise
1) the parts defined in the package can use the image directly.
A.java.awt.b.java.swing.* c.javax.swing.*
2) A button _ supports both a text label and an image.
a. Can't b. Can
3) the _ object can be used directly in the button.
A.ImageIcon b.Image c.Icon
4) use the _ method to get the URL of the current directory.
A.getURLbase b.getURL c.getCodeBase
5) A tag _ supports both a text label and an image.
a. Can b. Can't.
6) write a program that displays only two buttons by default: MasterCard and VisaCard
Figure 11-4 exercise questions (1)
When the MasterCard button is pressed, an image of MasterCard is displayed:
Figure 11-5 exercise questions (2)
When the VisaCard button is pressed, the image of M VisaCard is displayed. As shown in the following figure:
Figure 11-6 exercise questions (3)
Note: these two pictures are c:windowssystemoobdgmc.jpg and bgvisa.jpg respectively.
Practice the answer
1) C the widget defined in the java.awt package does not support images well; the java.swing package does not exist and should be javax.swing.
2) b of course.
3) An Image object should be converted to an ImageIcon object first.
4) c there is nothing to explain, the method name provided is getCodeBase ().
5) an of course.
6) the following is an implementation example:
Source program: lianxi11_1.java
Import java.applet.*
Import java.awt.*
Import javax.swing.*
Import java.awt.event.*
Public class lianxi11_1 extends JApplet
{
Image image1,image2
ImageIcon icon1,icon2
JButton mc,visa
JLabel imagelabel
Public void init ()
{
Frame frame1=new Frame ("lianxi11_1")
Frame1.setLayout (new BorderLayout ())
Image1=getImage (getCodeBase (), "bgmc.jpg")
Icon1=new ImageIcon (image1)
Image2=getImage (getCodeBase (), "bgvisa.jpg")
Icon2=new ImageIcon (image2)
Imagelabel=new JLabel ()
Frame1.add (imagelabel,BorderLayout.CENTER)
JPanel panel1= (JPanel) getContentPane ()
Panel1.setLayout (new GridLayout (1pm 2))
Mc=new JButton ("MasterCard")
Visa=new JButton ("VisaCard")
Panel1.add (mc)
Panel1.add (visa)
Mc.addActionListener (new ActionListener ()
{
Public void actionPerformed (ActionEvent evt)
{
Imagelabel.setIcon (icon1)
}
});
Visa.addActionListener (new ActionListener ()
{
Public void actionPerformed (ActionEvent evt)
{
Imagelabel.setIcon (icon2)
}
});
Frame1.add (panel1,BorderLayout.SOUTH)
Frame1.pack ()
Frame1.show ()
}
}
11.2 Sound
Impart new knowledge
Sometimes we need to release sound in the program, and I can enable the Java program to play sound files in WAV, AU, and SND formats with the support of the import java.applet.* package.
1. Load sound files
Like referencing image files, we have to load the sound files in before they can be used by Java programs. Again, we use URL to locate a sound file.
To load a sound file in the Java language, you use the getAudioClip method, whose syntax format is:
Public AudioClip getAudioClip (String url)
Public AudioClip getAudioClip (String url,String name)
Similarly, we can call the getCodeBase () method to get the URL of the current directory, so that we can easily call the sound files in the current directory. For example:
AudioClip clip1=getAudioClip (getCodeBase (), "carbrake.wav)
Note: carbreak.wav is included in office97, which you can find in the c:windowsmediaoffice97 directory, which is assumed to have been copied to the c:javastudy directory.
In this way, we get an AudioClip (sound clip) clip1, in which the sound file carbreak.wav is stored.
two。 Manipulate sound clips
Using sound is much easier than using images, and we can now operate directly on an AudioClip (sound clip) without the need for conversion (images have to be converted from Image to ImageIcon).
1) play the sound clip:
Sound clip name .play ()
In a word, it can be played out by directly calling the play () method of the sound clip object.
2) stop playback:
Sound clip name .stop ()
It is very simple, play is to play, stop is to stop, as if using a player.
3) play it in a loop:
Sound clip name .loop ()
Sometimes, we may need to play a piece of sound all the time, which provides a very simple way to do it in the Java language, which is the loop method of the sound clip.
Example illustration
First copy the c:windowsmediaoffice97carbrake.wav to the c:javastudy directory, and then enter the following source program.
Source program: useAudio.java
Import javax.swing.*
Import java.applet.*
Import java.awt.*
Import java.awt.event.*
Public class useAudio extends JApplet
{
AudioClip carbrake
Public void init ()
{
JPanel panel1= (JPanel) getContentPane ()
Panel1.setLayout (new GridLayout (3jue 1))
Carbrake=getAudioClip (getCodeBase (), "carbrake.wav")
JButton buttonPlay=new JButton ("Play")
JButton buttonStop=new JButton ("Stop")
JButton buttonLoop=new JButton ("Loop")
Panel1.add (buttonPlay)
Panel1.add (buttonStop)
Panel1.add (buttonLoop)
ButtonPlay.addActionListener (new ActionListener ()
{
Public void actionPerformed (ActionEvent evt)
{
Carbrake.stop ()
Carbrake.play ()
}
});
ButtonStop.addActionListener (new ActionListener ()
{
Public void actionPerformed (ActionEvent evt)
{
Carbrake.stop ()
}
});
ButtonLoop.addActionListener (new ActionListener ()
{
Public void actionPerformed (ActionEvent evt)
{
Carbrake.stop ()
Carbrake.loop ()
}
});
}
}
After compiling this program, you can see the following interface with appletviewer:
Figure 11-7 Program interface of useAudio.java
When you press the play button, you will hear a car brake. And when you press the Loop button, you will hear the continuous sound of car brakes. When you get bored, press the stop button and the sound disappears.
Self-test exercise
1) in the Java language, use the _ class to load sound files.
A.Sound b.SoundClip c.AudioClip d.Clip
2) _ package provides support for sound.
A.java.awt.b.javax.swing.c.java.swing.* d.javax.awt.*
3) there is a The Microsoft Sound.wav in the c:windowsmedia directory, which is the welcome music when Windows starts, and there is a chimes.wav in the c:windowsmediaoffice97 directory, which is a very harmonious piece of music. Next, please write a program to build two buttons, the first is Microsoft Sound, the first music is played when pressed, and the second button is chimes, which plays the second music when pressed. The program interface is shown in the following figure:
Practice the answer
1) c in Java, use AudioClip to load sound files.
2) a
3) here is an example of a program:
Source program: lianxi11_2.java
Import javax.swing.*
Import java.applet.*
Import java.awt.*
Import java.awt.event.*
Public class lianxi11_2 extends JApplet
{
AudioClip clip1,clip2
Public void init ()
{
JPanel panel1= (JPanel) getContentPane ()
Panel1.setLayout (new GridLayout (2pm 1))
Clip1=getAudioClip (getCodeBase (), "The Microsoft Sound.wav")
Clip2=getAudioClip (getCodeBase (), "chimes.wav")
JButton button1=new JButton ("Microsoft Sound")
JButton button2=new JButton ("Play chime")
Panel1.add (button1)
Panel1.add (button2)
Button1.addActionListener (new ActionListener ()
{
Public void actionPerformed (ActionEvent evt)
{
Clip2.stop ()
Clip1.play ()
}
});
Button2.addActionListener (new ActionListener ()
{
Public void actionPerformed (ActionEvent evt)
{
Clip1.stop ()
Clip2.play ()
}
});
}
}
11.3 change the font
Impart new knowledge
Unwittingly, we have learned a lot about Java, from character interface to graphical interface, from dull text to colorful images and sounds.
However, I do not know if you have noticed, we have not changed the font of the text displayed in the program! Now, let's make up some knowledge in this area.
Font is a very magical thing, for example, we use boldface to reflect each chapter, using italics to reflect some comments, tips, instructions, and so on. It makes our text more vivid and intuitive.
Fonts are determined by three properties:
1) Font name (family name)
Font names can be divided into two categories: one is Chinese font, such as Arial, italic, boldface, etc., and the other is English font, such as Arial, Times New Roman, etc.
A little knowledge:
There are two terms in English fonts: "equal width font" and "variable width font". Equal width font means that the width of each English letter is the same, while the width of wide font is not the same as that of m and l.
2) size (size)
Like font names, there are two ways to measure size: five and six, the smaller the number, the bigger the word; the other is the pound (a common unit in the printing world), such as 11 and 12, the bigger the word. However, only the latter, that is, point, is supported in Java.
3) style
Such as italics, bold, underlining and so on. There are four font styles in Java, defined by the values of the Font class.
Font.PLAIN (normal)
Font.BOLD (bold)
Font.ITALIC (italic)
Font.BOLD+ Font.ITALIC (bold italics)
That is, the style of underlining is not supported in the Java language.
Now that we know the details of the font, let's take a look at how to set and change the font in the program. Quite simply, let's first construct a font object using the following constructor:
Public Font (String familyName,int style,int size)
Then, call the object's setFont method to set the font:
Public void setFont (Font font)
As mentioned above, we can easily set the style and size, but which font names can we choose? I can't answer this question, no one can answer it! Because the answer to this question is in your system! Which fonts you can use depends entirely on what fonts are installed on your system.
Example illustration
Let's also do an experiment to modify the font:
Source program: lianxi11_2.java
Import javax.swing.*
Import java.applet.*
Import java.awt.*
Public class useFont extends JApplet
{
Public void init ()
JPanel panel1= (JPanel) getContentPane ()
Panel1.setLayout (new GridLayout (2pm 1))
JLabel fonttest=new JLabel ("This is a Font test")
Font font=new Font ("Arial", Font.BOLD,30)
Fonttest.setFont (font)
Panel1.add (fonttest)
JButton button1=new JButton ("Test")
Font font1=new Font ("Courier New", Font.BOLD,20)
Button1.setFont (font1)
Panel1.add (button1)
}
}
After we compile, use appletviewer to observe the output of the program, you will find that the font has changed!
Figure 11-9 output of useFont.java
Self-test exercise
1) "bold" is the _ of the font.
a. Style b. Font set c. Size
2) in the Java language, font size is in _ units.
A.dot b.pixels c.point
3) the width of the characters occupied by the letters m and l in equal width fonts.
a. Equal to b. Not equal
4) as long as the operating system installed on both machines is the same, the font set that can be used is the same. ______
a. To b. Wrong
5) in Java, _ is not supported.
a. Bold b. Underscore c. Italic d. Rough italic
6) write a program with the following interface:
Figure 11-10 exercise problem map
In this program, we print out 8 strings with growing fonts. Its font set is Arial and its style is ordinary.
Practice the answer
1) A bold, italic and bold italics are all font styles.
2) C uses the printing industry term "pound", that is, point.
3) the so-called equal width of a means that all letters occupy the same width.
4) b No, what font is installed is very personalized, in addition to the operating system, there are applications, manual installation makes the font different.
5) b does not support underlining in java.
3) here is an example of a program:
Source program: lianxi11_3.java
Import javax.swing.*
Import java.applet.*
Import java.awt.*
Public class lianxi11_3 extends JApplet
{
Public void init ()
{
JPanel panel1= (JPanel) getContentPane ()
Panel1.setLayout (new GridLayout (8. 1))
For (int size=10;size
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.