栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

圣诞节用java画一棵圣诞树给你的女友

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

圣诞节用java画一棵圣诞树给你的女友

一、背景

本文主要内容包括如何使用 Java-Jframe可视化开发,实现一棵动态的圣诞树。

好文章 记得 收藏+点赞+关注 !!!

二、效果预览

通过左上角的按钮打开动态效果!

话不多说,直接上代码!

三、完整代码

public class MyPanel extends JPanel implements ActionListener {

    
    public int x, y;

    
    public JButton onOff;

    
    public Timer time;

    
    public boolean flag;

    
    public boolean color;
    //获取本地音乐
    File file = new File("E:\christmasTree\music.wav");
    URL url = null;
    URI uri = null;
    AudioClip clip;

    MyPanel() {
        setLayout(null);
        //获取本地关闭按钮
        ImageIcon icon = new ImageIcon("E:\christmasTree\OFF.png");
        icon.setImage(icon.getImage().getScaledInstance(50, 50, 0));
        onOff = new JButton();
        onOff.addActionListener(this);
        //添加按钮图片
        onOff.setIcon(icon);
        //设置 取消边框
        onOff.setBorder(null);
        //设置 取消默认背景颜色
        onOff.setContentAreaFilled(false);
        onOff.setBounds(0, 0, 50, 50);
        add(onOff);
        flag = true;
        color = true;
        time = new Timer(300, this);
        time.stop();
        try {
            uri = file.toURI();
            url = uri.toURL();
        } catch (MalformedURLException e1) {
            e1.printStackTrace();
        }
        clip = Applet.newAudioClip(url);
    }

    public void paintComponent(Graphics g) {
        x = 380;
        y = 100;
        if (color) {
            ImageIcon image1 = new ImageIcon("E:\christmasTree\2.png");
            g.drawImage(image1.getImage(), x - 3, y - 25, 25, 25, null);
        } else {
            ImageIcon image1 = new ImageIcon("E:\christmasTree\1.png");
            g.drawImage(image1.getImage(), x - 2, y - 24, 26, 26, null);
        }
        Color red = new Color(255, 0, 0);
        Color yellow = new Color(255, 241, 0);
        //画第一个三角形(第一层圣诞树)
        drawTree(1, 4, g);
        if (color) {
            //画第一个三角形的黄色装饰
            drawDecoration(x + 22, y - 44, 6, yellow, g);
            //画第一个三角形的红色装饰
            drawDecoration(x, y - 22, 8, red, g);
        } else {
            //画第一个三角形的红色装饰
            drawDecoration(x + 22, y - 44, 6, red, g);
            //画第一个三角形的黄色装饰
            drawDecoration(x, y - 22, 8, yellow, g);
        }
        x = 380 - 2 * 22;
        //画第二个三角形(中间层)
        drawTree(3, 6, g);
        if (color) {
            //画第二个三角形的黄色装饰
            drawDecoration(x + 22, y - 44, 10, yellow, g);
            //画第二个三角形的红色装饰
            drawDecoration(x, y - 22, 12, red, g);
        } else {
            //画第二个三角形的红色装饰
            drawDecoration(x + 22, y - 44, 10, red, g);
            //画第二个三角形的黄色装饰
            drawDecoration(x, y - 22, 12, yellow, g);
        }
        x = 380 - 4 * 22;
        //画第三个三角形(最底层)
        drawTree(5, 8, g);
        if (color) {
            //画第三个三角形的黄色装饰
            drawDecoration(x + 22, y - 44, 14, yellow, g);
            //画第三个三角形的红色装饰
            drawDecoration(x, y - 22, 16, red, g);
        } else {
            //画第三个三角形的红色装饰
            drawDecoration(x + 22, y - 44, 14, red, g);
            //画第三个三角形的黄色装饰
            drawDecoration(x, y - 22, 16, yellow, g);
        }
        x = 380 - 22;
        //画出树根
        drawRoot(g);
    }

    //画树
    void drawTree(int from, int to, Graphics g) {
       //设置树的颜色
        g.setColor(new Color(9, 124, 37));
        for (int i = from; i <= to; i++) {
            for (int j = 0; j < (i * 2 - 1); j++) {
                g.fillRect(x, y, 20, 20);
                x += 22;
            }
            x = 380 - i * 22;
            y += 22;
        }
    }

    //画装饰
    void drawDecoration(int tx, int ty, int num, Color c, Graphics g) {
        g.setColor(c);
        g.fillRoundRect(tx, ty, 18, 18, 18, 18);
        g.fillRoundRect(tx + num * 22, ty, 18, 18, 18, 18);
    }

    //画树根
    void drawRoot(Graphics g) {
        //设置树根颜色
        g.setColor(new Color(131, 78, 0));
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 3; j++) {
                g.fillRect(x, y, 20, 20);
                x += 22;
            }
            x = 380 - 22;
            y += 22;
        }
    }

    public void actionPerformed(ActionEvent e) {
        //按钮事件
        if (e.getSource() == onOff) {
            //开关打开
            if (flag) {
                ImageIcon icon = new ImageIcon("E:\christmasTree\ON.png");
                icon.setImage(icon.getImage().getScaledInstance(50, 50, 0));
                onOff.setIcon(icon);
                flag = false;
                clip.loop();
                time.restart();
            } else {
                //开关关闭
                ImageIcon icon = new ImageIcon("E:\christmasTree\OFF.png");
                icon.setImage(icon.getImage().getScaledInstance(50, 50, 0));
                onOff.setIcon(icon);
                flag = true;
                time.stop();
                clip.stop();
            }
        } else if (e.getSource() == time) {
            repaint();
            color = !color;
        }
    }

}

public class Myframe extends Jframe {

    MyPanel p;
    Myframe() {
        p = new MyPanel();
        add(p);
        //设置布局
        setBounds(400, 200, 800, 800);
        setVisible(true);
        validate();

        setDefaultCloseOperation(Myframe.EXIT_ON_CLOSE);
        //添加文字标签 可以添加你想发给的人的名字哦
        JLabel label1 = new JLabel("merry christmas!");
        //设置字体,格式(斜体,粗细),尺寸
        label1.setFont(new Font("宋体", Font.BOLD, 30));
        //设置标签颜色
        label1.setForeground(Color.red);
        label1.setBounds(250, 300, 500, 400);
        p.add(label1);
        p.setVisible(true);
    }

}

接下来是主函数,用这个来运行!

public class Main {

    public static void main(String[] args) {
        new Myframe();
    }
}

推荐阅读:

  • 由银行填表时返回上一步引发的对回溯算法的思考(java实现回溯算法)
  • 上班摸鱼时引发的对多线程的思考

参考博客:https://blog.csdn.net/weixin_44689154/article/details/103690144


写在最后
我的女朋友说这个圣诞树极其丑陋,建议发给女朋友之前三思!

以下是她的评价:


ok,我话说完…

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/671185.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号