关闭 x
IT技术网
    技 采 号
    ITJS.cn - 技术改变世界
    • 实用工具
    • 菜鸟教程
    IT采购网 中国存储网 科技号 CIO智库

    IT技术网

    IT采购网
    • 首页
    • 行业资讯
    • 系统运维
      • 操作系统
        • Windows
        • Linux
        • Mac OS
      • 数据库
        • MySQL
        • Oracle
        • SQL Server
      • 网站建设
    • 人工智能
    • 半导体芯片
    • 笔记本电脑
    • 智能手机
    • 智能汽车
    • 编程语言
    IT技术网 - ITJS.CN
    首页 » JAVA »Java图形设计和多媒体基础

    Java图形设计和多媒体基础

    2014-12-12 00:00:00 出处:developerWorks 中国
    分享

    ITJS的这篇文章主要介绍了Java的图形设计以及多媒体处理,源码作者也做了详细的注释,对于初学者应该不难。详细请看下文。

    同心圆效果图:

    /**  
     *程序要求:新建一个600*600像素的应用程序窗口,并在窗口中绘制5个不同颜色的同心圆,  
     *所有圆心都是屏幕的中心点,相邻两个圆直接的半径相差50像素  
     *效果图如下图所示(颜色随机设置),源程序保存为Ex7_1.java。  
     *作者:wwj  
     *日期:2012/4/25  
     *功能:显示一个有5个不同颜色的同心圆  
     **/ 
    
     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("一个有5个不同颜色的同心圆");    //显示窗口名称  
             setSize(600,600);                      //设置窗口大小  
             setVisible(true);                      //设置为可见  
             setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置窗口关闭动作  
    
         }  
    
         public void paint(Graphics g)  
         {  
             //第一个圆  
            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);  
            //第二个圆  
            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);  
            //第三个圆  
            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);  
            //第四个圆  
            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);  
            //第五个圆  
            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();       
         }  
    
     }

    播放音乐和切换图片的小程序效果图:

    /**  
     *程序要求:编写一个Applet的小程序,准备5幅图片和三个音乐文件,绘制到Applet中,  
     *并增加几个按钮,控制图片的切换、放大、缩小和音乐文件的播放。  
     *作者:wwj  
     *日期:2012/4/29  
     *参考:neicole  
     *功能:能进行图片和歌曲的选择变换的applet小程序  
     **/ 
    
     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  
     {  
    
         //创建两个面板  
         JPanel p1=new JPanel();  
         JPanel p2=new JPanel();  
         JPanel p3=new JPanel();  
         //声音对象  
         AudioClip[] sound=new AudioClip[3];  
         int playingSong=0;  
         //切换图片的按钮  
         JButton lastPic=new JButton("上一张");  
         JButton setLarge=new JButton("放大");  
         JButton setLittle=new JButton("缩小");  
         JButton nextPic=new JButton("下一张");  
         //切换歌曲的按钮  
         JButton lastSound=new JButton("上一首");  
         JButton play=new JButton("播放");  
         JButton loop=new JButton("连续");  
         JButton stop=new JButton("停止");  
         JButton nextSound=new JButton("下一首");  
         //曲目下拉列表  
         JComboBox xx;  
         String names[]={ "曲目1.wav","曲目2.wav","曲目3.wav"};  
    
        //创建画布对象  
        MyCanvasl showPhotos;  
    
         public void init()  
         {  
             //窗口布局  
             this.setLayout(new BorderLayout());  
    
             //为图片控制按钮注册监听器  
             lastPic.addActionListener(this);  
             setLarge.addActionListener(this);  
             setLittle.addActionListener(this);  
             nextPic.addActionListener(this);  
    
             //向面板p1添加组件  
             p1.add(lastPic);  
             p1.add(setLarge);  
             p1.add(setLittle);  
             p1.add(nextPic);  
             p1.repaint();  
    
            //实例化下拉列表对象  
            xx = new JComboBox(names);  
            xx.addItemListener(this);  
    
            //为控制播放音乐按钮注册监听器  
            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/"+"曲目" 
                        +Integer.toString(i+1)+".wav");  
             }  
    
            //向面板p2添加组件  
             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();  
    
            //把面板p1和p2分别布置到窗口的北部和南部   
             add(p1,BorderLayout.NORTH);  
             add(p2,BorderLayout.SOUTH);  
             add(p3,BorderLayout.CENTER);  
    
             this.repaint();  
    
         }  
    
         //按钮的事件处理  
         public void actionPerformed(ActionEvent 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 if(e.getSource()==lastSound){  //上一首  
                sound[playingSong].stop();  
                playingSong=(playingSong-1+3)%3;  
                xx.setSelectedIndex(playingSong);  
                sound[playingSong].play();  
    
            }  
            else if(e.getSource()==play){       //按下播放按钮  
                sound[playingSong].play();  
            }  
            else if(e.getSource()==loop){       //按下循环按钮  
                sound[playingSong].loop();  
            }  
            else if(e.getSource()==stop){       //按下停止按钮  
                sound[playingSong].stop();  
            }  
            else{                               //下一首  
                sound[playingSong].stop();  
                playingSong=(playingSong+1)%3;  
                xx.setSelectedIndex(playingSong);  
                sound[playingSong].play();  
    
            }     
         }  
    
         //下拉列表的事件处理  
         public void itemStateChanged(ItemEvent e)  
         {  
    
             sound[playingSong].stop();  
             sound[playingSong]=getAudioClip(getCodeBase(),"music/"+xx.getSelectedItem());  
         }  
    
        class MyCanvasl extends Canvas  
        {  
    
            public Image[] img=new Image[5];  
    
            int MaxWidth = 600;  
            int MaxHeight = 500;  
            int nowImageIndex = 0;  
            int coordinateX = 0;  
            int coordinateY = 0;  
            int currentWidth = MaxWidth;  
            int currentHeight = MaxHeight;  
    
            MyCanvasl(){  
             setSize(MaxWidth,MaxHeight);  
             //获取当前目录下的图片  
             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 = MaxWidth;  
                    currentHeight = MaxHeight;  
                }  
                else if ('B' == command){  
                    if(MaxWidth >= (currentWidth + 100) && MaxHeight >= (currentHeight + 100)){  
                        currentWidth += 100;  
                        currentHeight += 100;  
                    }  
                }  
                else if('S' == command){  
                    if((0 < (currentWidth - 100)) && (0 < (currentHeight - 100))){  
                        currentWidth = currentWidth - 100;  
                        currentHeight = currentHeight - 100;  
                    }  
                }  
                coordinateX = (MaxWidth - currentWidth) / 2;  
                coordinateY = (MaxHeight - currentHeight) / 2;  
                repaint();  
            }  
                //paint方法用来在窗口显示图片  
         public void paint(Graphics g){  
               g.drawImage(img[nowImageIndex],coordinateX,coordinateY,currentWidth,currentHeight,this);  
    
         }  
        }  
     }
    上一篇返回首页 下一篇

    声明: 此文观点不代表本站立场;转载务必保留本文链接;版权疑问请联系我们。

    别人在看

    Edge浏览器百度被劫持/篡改怎么办,地址后边跟着尾巴#tn=68018901_7_oem_dg

    Google Chrome 在 iPhone 上新增了 Safari 数据导入选项

    Windows 11专业版 KMS工具激活产品密钥的方法

    DEDECMS安全策略官方出品

    Microsoft Text Input Application 可以关闭吗?

    新版本QQ如何关闭自带的浏览器?

    C++编程语言中continue的用法和功能,附举例示范代码

    c++ map 的数据结构、基本操作以及其在实际应用中的使用。

    C语言如何避免内存泄漏、缓冲区溢出、空指针解引用等常见的安全问题

    C语言中的break语句详解

    IT头条

    马斯克2026最新采访总结:2040年,全球机器人数量将突破100亿台

    23:52

    专家解读|规范人工智能前沿业态健康发展的新探索:解读《人工智能拟人化互动服务管理暂行办法》

    00:54

    用至强 6高存力搞定MoE卸载!

    17:53

    美国将允许英伟达向中国“经批准的客户”出售H200 GPU

    02:08

    苹果与微信就15%手续费达成一致?腾讯未置可否

    22:00

    技术热点

    PHP 和 Node.js 的10项对比挑战

    Javascript闭包深入解析及实现方法

    windows 7、windows 8.1手动增加右键菜单功能技巧

    MYSQL出错代码大汇总

    windows 7假死机怎么办 windows 7系统假死机的原因以及解决方法

    Ubuntu(Linux)下配置IP地址的方法

      友情链接:
    • IT采购网
    • 科技号
    • 中国存储网
    • 存储网
    • 半导体联盟
    • 医疗软件网
    • 软件中国
    • ITbrand
    • 采购中国
    • CIO智库
    • 考研题库
    • 法务网
    • AI工具网
    • 电子芯片网
    • 安全库
    • 隐私保护
    • 版权申明
    • 联系我们
    IT技术网 版权所有 © 2020-2025,京ICP备14047533号-20,Power by OK设计网

    在上方输入关键词后,回车键 开始搜索。Esc键 取消该搜索窗口。