This article achieves two effects:
The first type, concentric circle renderings:
/** *Program requirements: Create a new application window of 600*600 pixels, and draw 5 concentric circles of different colors in the window. *All circles are the center points of the screen, and the direct radius of the adjacent two circles is different The 50 pixels* rendering is shown in the figure below (color randomly set), and the source program is saved as Ex7_1.java. *Author: wwj *Date: 2012/4/25 *Function: Display a concentric circle with 5 different colors**/ import javax.swing.*; import java.awt.*; import java.awt.Color; public class Ex7_1 extends JFrame { int red,green,blue; Color color; public Ex7_1() { super("A concentric circle with 5 different colors"); //Show window name setSize(600,600); //Set window size setVisible(true); //Set to visible setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Set window close action} public void paint(Graphics g) { //The first circle red=(int) (Math.random()* 255); green=(int)(Math.random()*255); blue=(int)(Math.random()*255); color=new Color(red,green,blue); g.setColor(color ); g.fillOval(175,175,250,250); //The second circle red=(int)(Math.random()*255); green=(int)(Math.random()*255); blue=(int) (Math.random()*255); color=new Color(red,green,blue); g.setColor(color); g.fillOval(200,200,200,200); //The third circle red=(int)(Math. random()*255); green=(int)(Math.random()*255); blue=(int)(Math.random()*255); color=new Color(red,green,blue); g .setColor(color); g.fillOval(225,225,150,150); //The fourth circle red=(int)(Math.random()*255); green=(int)(Math.random()*255); blue =(int)(Math.random()*255); color=new Color(red,green,blue); g.setColor(color); g.fillOval(250,250,100,100); //The fifth circle red=(int )(Math.random()*255); green=(int)(Math.random()*255); blue=(int)(Math.random()*255); color=new Color(red,green, blue); g.setColor(color); g.fillOval(275,275,50,50); } public static void main(String[] args) { Ex7_1 e = new Ex7_1(); } }
The second type is the renderings of mini-programs that play music and switch pictures:
/** *Program requirements: Write an Applet applet, prepare 5 pictures and three music files, draw them into the Applet, * and add several buttons to control the switching, zoom in, zoom out and playback of music files. *Author: wwj *Date: 2012/4/29 *Reference: neicole *Function: applet applet that can transform pictures and songs**/ import javax.swing.*; import java.awt.*; import java .awt.event.*; import java.applet.Applet; import java.applet.AudioClip; public class Ex7_2 extends Applet implements ActionListener,ItemListener { //Create Build two panels JPanel p1=new JPanel(); JPanel p2=new JPanel (); JPanel p3=new JPanel(); //Sound object AudioClip[] sound=new AudioClip[3]; int playingSong=0; //Switch the button to the picture JButton lastPic=new JButton("Previous picture"); JButton setLarge=new JButton("Zoom in"); JButton setLittle=new JButton("Zoom out"); JButton nextPic=new JButton("Next"); //Turn the button for the song JButton lastSound =new JButton("Previous First "); JButton play=new JButton("play"); JButton loop=new JButton("continuous"); JButton stop=new JButton("stop"); JButton nextSound=new JButton("next song "); //Track drop-down list JComboBox xx; String names[]={ "Track 1.wav", "Track 2.wav", "Track 3.wav"}; //Create canvas object MyCanvasl showPhotos; public void init() { //Window layout this.setLayout(new BorderLayout()); //Register listener for picture control button lastPic.addActionListener(this); setLarge.addActionListener(this); setLittle.addA ctionListener(this); nextPic.addActionListener(this) ; //Add component p1.add(lastPic); p1.add(setLarge); p1.add(setLittle); p1.add(nextPic); p1.repaint(); //Instantiate the drop-down list object xx = new JComboBox(names); xx.addItemListener(this); //Register the listener for controlling the playback music button lastSound.addActionListener(this); play.addActionListener(this); loop. addActionListener(this); stop.addActionListener(this ); nextSound.addActionListener(this); for(int i=0;i<3;i++) { sound[i]=getAudioClip(getCodeBase(),"music/"+"Type" +Integer.toStrin g(i+1 )+".wav"); } //Add component p2.add(xx); p2.add(lastSound); p2.add(play); p2.add(loop); p2.add(stop) ; p2.add(nextSound); p2.repaint(); showPhotos = new MyCanvasl(); p3.add(showPhotos); p3.repaint(); //Arrange panels p1 and p2 to the north and south of the window respectively add (p1,BorderLayout.NORTH); add(p2,BorderLayout.SOUTH); add(p3,BorderLayout.CENTER); this.repaint(); } // Event handling of the button public void actionPerformed(Act ionEvent e) { if(e .getSource() == lastPic){ showPhotos.changePhotoShow('P'); } else if(e.getSource() == nextPic){ showPhotos.changePhotoShow('N' ); } else if(e.getSource() == setLarge){ showPhotos.changePhotoSize('B'); } else if(e.getSource() == setLittle){ showPhotos.changePhotoSize('S'); } else i f(e.getSource()==lastSound) { //Previous song sound[playingSong].stop(); playingSong=(playingSong-1+3)%3; xx.setSelectedIndex(playingSong); sound[playingSong].play(); } else i f(e.getSource ()==play){ //Press the play button sound[playingSong].play(); } else if(e.getSource()==loop){ //Press the loop button sound[playingSong].loop() ; } else if(e.getSource()==stop){ //Press the stop button sound[playingSong].stop(); } else{ //Next sound[playingSong].stop(); playingSong=( playingSong+1)%3; xx.setSelectedIndex(playingSong); sound[playingSong].play(); } } // Event handling in drop-down list public void itemStateChanged(ItemEvent e) { sound[playingSong].stop(); sound [playingSong]=getAudioClip(getCodeBase(),"music/"+xx.getSelectedItem()); } class MyCanvasl extends Canvas { public Image[] img=new Im age[5]; int MaxWidth = 600; int MaxHeight = 500; int nowImageIndex = 0; int coordinateX = 0; int coordinateY = 0; int currentWidth = MaxWidth; int currentHeight = MaxHeight; MyCanvasl(){ setSiz e(MaxWidth,MaxHeight); //Get the picture in the current directory for(int i=0 ;i<5;i++){ img[i]=getImage(getCodeBase(),"image/"+Integer.toString(i+1)+".jpg"); } } private void changePhotoIndex(int index) { nowImageIndex = index; changePhotoSize('M'); } public void changePhotoShow(char command){ if('P' == command){ changePhotoIndex((nowImageIndex + 5 - 1 ) % 5); } else if('N' = = command){ changePhotoIndex((nowImageIndex + 1) % 5); } } public void changePhotoSize(char command){ if ('M' == command){ currentWidth = MaxWi dth; currentHeight = MaxHeight; } else if ('B' == command){ if(MaxWidth >= (currentWidth + 100) && MaxHeight >= (currentHeight + 100)){ currentWidth += 100; currentHeight += 100; } } els e if('S' == command){ if ((0 < (currentWidth - 100)) && (0 < (currentHeight - 100))){ currentWidth = currentWidth - 100; currentHeight = currentHeight - 100; } } coordinateX = (M axWidth - currentWidth) / 2; coordinateY = (MaxHeight - currentHeight) / 2; repaint(); } //paint method is used to display pictures in the window public void paint(Graphics g){ g.drawImage(img[nowImageIndex],coordinateX,coordinateY,curre ntWidth,currentHeight,this); } } }
The above is all about Java's graphic design and multimedia processing. I hope it will be helpful to everyone's learning.