前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【Python | Java】代码绘制圣诞树

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

作者头像
攻城狮杰森
发布2022-06-03 12:32:38
1.9K0
发布2022-06-03 12:32:38
举报
文章被收录于专栏:技术集锦技术集锦

马上圣诞节了,祝大家圣诞快乐,快来看看程序员的圣诞节都做了什么

Python

效果

请添加图片描述
请添加图片描述

源码

代码语言:javascript
复制
'''
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 类

代码语言:javascript
复制
/*
 * @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 类

代码语言:javascript
复制
/*
 * @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 类

代码语言:javascript
复制
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;
        }
    }
}
在这里插入图片描述
在这里插入图片描述
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-04-06,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Python
    • 效果
      • 源码
      • Java
        • 效果
          • 源码
            • Start 类
            • Frame 类
            • Panel 类
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档