【Python | Java】代码绘制圣诞树

简介: 【Python | Java】代码绘制圣诞树

Python

效果

源码

'''
Author: coder-jason
Date: 2021-12-14 15:49:17
LastEditTime: 2021-12-14 16:21:45
'''
from turtle import *
import random 
import time
n = 94.0 # main line height
speed("normal") # setting speeds: fast slow fastest slowest
setup(700,650)  # setting window size
screensize(bg='seashell')
left(90)
forward(3 * n)
color("orange", "yellow")
begin_fill()
left(126)
for i in range(5):
    forward(n / 5)
    right(144)
    forward(n / 5)
    left(72)
end_fill()
right(126)
color("dark green")
backward(n * 4.8)
def tree(d, s):
    if d <= 0:
        return
    forward(s)
    tree(d - 1, s * .8)
    right(120)
    tree(d - 3, s * .5)
    right(120)
    tree(d - 3, s * .5)
    right(120)
    backward(s)
tree(15, n)
backward(n / 2)
for i in range(200):
    a = 200 - 400 * random.random()
    b = 10 - 20 * random.random()
    up()
    forward(b)
    left(90)
    forward(a)
    down()
    if random.randint(0, 1) == 0:
        color('tomato')
    else:
        color('wheat')
    circle(2)
    up()
    backward(a)
    right(90)
    backward(b)
time.sleep(60)

Java

效果

源码

Start 类

/*
 * @Author: coder-jason
 * @Date: 2021-12-14 15:26:49
 * @LastEditTime: 2021-12-14 16:43:55
 */
package com.christmasTree;
public class Start {
    public static void main(String[] args) {
        new Frame();
    }
}

Frame 类

/*
 * @Author: coder-jason
 * @Date: 2021-12-14 15:26:49
 * @LastEditTime: 2021-12-14 16:43:55
 */
package com.christmasTree;
import javax.swing.*;
public class Frame extends JFrame {
    Panel p;
    Frame() {
        p = new Panel();
        add(p);
        setBounds(200, 200, 650, 500); //设置窗口尺寸和位置 x,y坐标 width,height窗口宽高
        setVisible(true);
        validate();
        setDefaultCloseOperation(Frame.EXIT_ON_CLOSE);
    }
}

Panel 类

package com.christmasTree;
import javax.swing.*;
import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
public class Panel extends JPanel implements ActionListener {
    int x, y;
    JButton onOff;
    Timer time;
    boolean flag;
    boolean color;
    File file = new File("your music location");
    URL url = null;
    URI uri = null;
    AudioClip clip = null;
    Panel() {
        setLayout(null);
        ImageIcon icon = new ImageIcon("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) {
        }
        clip = Applet.newAudioClip(url);
    }
    public void paintComponent(Graphics g) {
        x = 380;
        y = 100;
        if (color) {
            ImageIcon image1 = new ImageIcon("star1.png");
            g.drawImage(image1.getImage(), x - 3, y - 25, 28, 26, null);
        } else {
            ImageIcon image1 = new ImageIcon("star2.png");
            g.drawImage(image1.getImage(), x - 3, y - 25, 25, 25, 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 - 1 * 22;
        drwaRoot(g);
    }
    void drawTree(int from, int to, Graphics g) {
        Color c = new Color(9, 124, 37);
        g.setColor(c);
        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 drwaRoot(Graphics g) {
        Color c = new Color(131, 78, 0);
        g.setColor(c);
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 3; j++) {
                g.fillRect(x, y, 20, 20);
                x += 22;
            }
            x = 380 - 1 * 22;
            y += 22;
        }
    }
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == onOff) {
            if (flag) {
                ImageIcon icon = new ImageIcon("on.png");
                icon.setImage(icon.getImage().getScaledInstance(50, 50, 0));
                onOff.setIcon(icon);
                flag = false;
                clip.loop();
                time.restart();
            } else {
                ImageIcon icon = new ImageIcon("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;
        }
    }
}


相关文章
|
8天前
|
JavaScript NoSQL Java
接替此文【下篇-服务端+后台管理】优雅草蜻蜓z系统JAVA版暗影版为例-【蜻蜓z系列通用】-2025年全新项目整合搭建方式-这是独立吃透代码以后首次改变-独立PC版本vue版搭建教程-优雅草卓伊凡
接替此文【下篇-服务端+后台管理】优雅草蜻蜓z系统JAVA版暗影版为例-【蜻蜓z系列通用】-2025年全新项目整合搭建方式-这是独立吃透代码以后首次改变-独立PC版本vue版搭建教程-优雅草卓伊凡
152 96
接替此文【下篇-服务端+后台管理】优雅草蜻蜓z系统JAVA版暗影版为例-【蜻蜓z系列通用】-2025年全新项目整合搭建方式-这是独立吃透代码以后首次改变-独立PC版本vue版搭建教程-优雅草卓伊凡
|
23天前
|
存储 缓存 Java
Python高性能编程:五种核心优化技术的原理与Python代码
Python在高性能应用场景中常因执行速度不及C、C++等编译型语言而受质疑,但通过合理利用标准库的优化特性,如`__slots__`机制、列表推导式、`@lru_cache`装饰器和生成器等,可以显著提升代码效率。本文详细介绍了这些实用的性能优化技术,帮助开发者在不牺牲代码质量的前提下提高程序性能。实验数据表明,这些优化方法能在内存使用和计算效率方面带来显著改进,适用于大规模数据处理、递归计算等场景。
58 5
Python高性能编程:五种核心优化技术的原理与Python代码
|
1月前
|
SQL Java 数据库连接
如何在 Java 代码中使用 JSqlParser 解析复杂的 SQL 语句?
大家好,我是 V 哥。JSqlParser 是一个用于解析 SQL 语句的 Java 库,可将 SQL 解析为 Java 对象树,支持多种 SQL 类型(如 `SELECT`、`INSERT` 等)。它适用于 SQL 分析、修改、生成和验证等场景。通过 Maven 或 Gradle 安装后,可以方便地在 Java 代码中使用。
272 11
|
1月前
|
JSON Java 数据挖掘
利用 Java 代码获取淘宝关键字 API 接口
在数字化商业时代,精准把握市场动态与消费者需求是企业成功的关键。淘宝作为中国最大的电商平台之一,其海量数据中蕴含丰富的商业洞察。本文介绍如何通过Java代码高效、合规地获取淘宝关键字API接口数据,帮助商家优化产品布局、制定营销策略。主要内容包括: 1. **淘宝关键字API的价值**:洞察用户需求、优化产品标题与详情、制定营销策略。 2. **获取API接口的步骤**:注册账号、申请权限、搭建Java开发环境、编写调用代码、解析响应数据。 3. **注意事项**:遵守法律法规与平台规则,处理API调用限制。 通过这些步骤,商家可以在激烈的市场竞争中脱颖而出。
|
2月前
|
Python
课程设计项目之基于Python实现围棋游戏代码
游戏进去默认为九路玩法,当然也可以选择十三路或是十九路玩法 使用pycharam打开项目,pip安装模块并引用,然后运行即可, 代码每行都有详细的注释,可以做课程设计或者毕业设计项目参考
78 33
|
2月前
|
JavaScript API C#
【Azure Developer】Python代码调用Graph API将外部用户添加到组,结果无效,也无错误信息
根据Graph API文档,在单个请求中将多个成员添加到组时,Python代码示例中的`members@odata.bind`被错误写为`members@odata_bind`,导致用户未成功添加。
52 10
|
2月前
|
安全 Java 编译器
深入理解Java中synchronized三种使用方式:助您写出线程安全的代码
`synchronized` 是 Java 中的关键字,用于实现线程同步,确保多个线程互斥访问共享资源。它通过内置的监视器锁机制,防止多个线程同时执行被 `synchronized` 修饰的方法或代码块。`synchronized` 可以修饰非静态方法、静态方法和代码块,分别锁定实例对象、类对象或指定的对象。其底层原理基于 JVM 的指令和对象的监视器,JDK 1.6 后引入了偏向锁、轻量级锁等优化措施,提高了性能。
77 3
|
2月前
|
前端开发 Java 测试技术
java日常开发中如何写出优雅的好维护的代码
代码可读性太差,实际是给团队后续开发中埋坑,优化在平时,没有那个团队会说我专门给你一个月来优化之前的代码,所以在日常开发中就要多注意可读性问题,不要写出几天之后自己都看不懂的代码。
78 2
|
2月前
|
数据可视化 Python
以下是一些常用的图表类型及其Python代码示例,使用Matplotlib和Seaborn库。
通过这些思维导图和分析说明表,您可以更直观地理解和选择适合的数据可视化图表类型,帮助更有效地展示和分析数据。
105 8
|
2月前
|
Python
探索Python中的装饰器:简化代码,增强功能
在Python的世界里,装饰器就像是给函数穿上了一件神奇的外套,让它们拥有了超能力。本文将通过浅显易懂的语言和生动的比喻,带你了解装饰器的基本概念、使用方法以及它们如何让你的代码变得更加简洁高效。让我们一起揭开装饰器的神秘面纱,看看它是如何在不改变函数核心逻辑的情况下,为函数增添新功能的吧!

热门文章

最新文章

推荐镜像

更多