自己用Java写圣诞树 视觉效果很好

这篇具有很好参考价值的文章主要介绍了自己用Java写圣诞树 视觉效果很好。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

一:概述

此前看了许多用Java写圣诞树的代码和文章都达不到我想要的那种效果,干脆直接自己动手操作了

同时圣诞节也快到了,刚好做给媳妇儿一个小惊喜!

提前祝各位圣诞快乐!废话不多说,直接上效果图!!java圣诞树的代码编写,java

 视频效果如下:

Java 圣诞树

欧克!接下来是代码!各位有需要或者觉得还不错的希望能点个赞,如果觉得那里还可以改进的,评论留言,万谢!

二:一共有三个类:MyPanel ,MyFrame ,Main

1.这是 MyPanel 类 :

package Love.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 MyPanel extends JPanel implements ActionListener{
    int x, y ;
    JButton onOff ;
    Timer time ;
    boolean flag ;
    boolean color ;
    File file = new File("/星星.png") ;
    URL url = null;
    URI uri = null ;
    ImageIcon icon;
    AudioClip clip = null;
    MyPanel() {
        setLayout(null);
        onOff = new JButton() ;
        icon = new ImageIcon("/星星.png");
        icon.setImage(icon.getImage().getScaledInstance(50,50, 0)) ;
        onOff.addActionListener(this) ;
        onOff.setBorder(null) ;
        onOff.setContentAreaFilled(false) ;
        onOff.setBounds(0, 0, 50, 50) ;
        this.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 ;

        Color red = new Color(255, 0, 0) ;
        Color yellow = new Color(255, 241, 0) ;
        Color blue = new Color(0x2BB1EF) ;
        Color happy = new Color(0xAEF607);
        Color happies = new Color(0xF1C4F1) ;
        Color top1 = new Color(0xFDFA6F8B, true);
        Color top2 = new Color(0x9C44F5);

        if(color) {
            drawDecoration(x, y-25, 0, top1, g);
        }
        else {
            drawDecoration(x, y-25, 0, top2, g);
        }
        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, blue, g);
        }
        x = 380-2*22;
        drawTree(3, 6, g) ;
        if(color) {
            drawDecoration(x+22, y-44, 10, blue, 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+66, y-88, 10, yellow, g);
            drawDecoration(x+44, y-66, 12, red, g);
            drawDecoration(x+22, y-44, 14, blue, g);
            drawDecoration(x, y-22, 16, yellow, g);
        }
        else {
            drawDecoration(x+66, y-88, 10, blue, g);
            drawDecoration(x+44, y-66, 12, yellow, g);
            drawDecoration(x+22, y-44, 14, red, g);
            drawDecoration(x, y-22, 16, blue, g);
        }
        x = 380-1*22;
        drwaRoot(g) ;
        drwafloor(g) ;

        //love
        if(color){
            //L
            for(x=60,y=70;x>20&&y<150;x--,y+=2){
                drawDecoration(x, y, 0, happy, g);
            }
            for(x=20;x<80;x++){
                drawDecoration(x, 150, 0, happy, g);
            }

            //O
            for(x=170,y=70;x>130&&y<150;x--,y+=2){
                drawDecoration(x, y, 0, happy, g);
            }
            for(x=230,y=70;x>190&&y<150;x--,y+=2){
                drawDecoration(x, y, 0, happy, g);
            }
            for(x=230;x>170;x--){
                drawDecoration(x, 70, 0, happy, g);
            }
            for(x=190;x>130;x--){
                drawDecoration(x, 150, 0, happy, g);
            }

            //V
            for(x=20,y=200;x<60&&y<280;x++,y+=3){
                drawDecoration(x, y, 0, happy, g);
            }
            for(x=80,y=200;x>20&&y<280;x--,y+=3){
                drawDecoration(x, y, 0, happy, g);
            }

            //E
            for(x=230;x>170;x--){
                drawDecoration(x, 200, 0, happy, g);
            }
            for(x=210;x>150;x--){
                drawDecoration(x, 240, 0, happy, g);
            }
            for(x=190;x>130;x--){
                drawDecoration(x, 280, 0, happy, g);
            }
            for(x=170,y=200;x>130&&y<280;x--,y+=2){
                drawDecoration(x, y, 0, happy, g);
            }
        }else{
            //L
            for(x=70,y=70;x>30&&y<150;x--,y+=2){
                drawDecoration(x, y, 0, happies, g);
            }
            for(x=30;x<90;x++){
                drawDecoration(x, 150, 0, happies, g);
            }

            //O
            for(x=180,y=70;x>140&&y<150;x--,y+=2){
                drawDecoration(x, y, 0, happies, g);
            }
            for(x=240,y=70;x>200&&y<150;x--,y+=2){
                drawDecoration(x, y, 0, happies, g);
            }
            for(x=240;x>180;x--){
                drawDecoration(x, 70, 0, happies, g);
            }
            for(x=200;x>140;x--){
                drawDecoration(x, 150, 0, happies, g);
            }

            //V
            for(x=30,y=200;x<70&&y<280;x++,y+=3){
                drawDecoration(x, y, 0, happies, g);
            }
            for(x=90,y=200;x>30&&y<280;x--,y+=3){
                drawDecoration(x, y, 0, happies, g);
            }

            //E
            for(x=240;x>180;x--){
                drawDecoration(x, 200, 0, happies, g);
            }
            for(x=220;x>160;x--){
                drawDecoration(x, 240, 0, happies, g);
            }
            for(x=200;x>140;x--){
                drawDecoration(x, 280, 0, happies, g);
            }
            for(x=180,y=200;x>140&&y<280;x--,y+=2){
                drawDecoration(x, y, 0, happies, g);
            }
        }
    


        //祝福语
        if (color){
            g.setColor(Color.pink);
            g.setFont(new Font("方正舒体", Font.HANGING_BASELINE, 35));
            g.drawString("亲爱的小宝,圣诞将至,提前送你棵圣诞树!",100,535);
            g.drawString("相逢是首悠扬的歌,相识是杯醇香的酒,",130,580);
            g.drawString("相处是那南飞的雁,相知是跟古老的藤。",160,625);
            g.drawString("嬉闹是彼此最甜蜜的瞬间,愿幸福与平安永远伴随你!圣诞快乐!!",190,670);
            g.drawString("愿所有的期待和梦想都能如期而至,每一刻都是美好的时光!",220,715);
            g.drawString("爱你的大宝",1000,750);
        }


      //爱
        if (color) {
            g.setColor(Color.pink);
            g.setFont(new Font("方正舒体", Font.HANGING_BASELINE, 65));
            g.drawString("昕雅小宝贝",420,65);
            g.drawString("臭小宝",920,65);
            g.drawString("/(≧ x ≦)\\", 50, 452);
        }else {
            g.setColor(top1);
            g.setFont(new Font("方正舒体", Font.HANGING_BASELINE, 65));
            g.drawString("昕雅小宝贝",420,65);
            g.drawString("臭小宝",920,65);
            g.drawString("/(≧ x ≦)\\", 50, 452);
        }




        if (color){
            g.setColor(Color.pink);
            g.setFont(new Font("方正舒体", Font.HANGING_BASELINE, 40));
            g.drawString("笨蛋小宝",920,535);
            JLabel jLabel1 = new JLabel(new ImageIcon("D:\\Java\\Love\\src\\Love\\ChristmasTree\\爱心.png"));
            JLabel jLabel2 = new JLabel(new ImageIcon("D:\\Java\\Love\\src\\Love\\ChristmasTree\\爱心.png"));
            jLabel1.setBounds(895,555,40,40);
            jLabel2.setBounds(1140,555,40,40);
            this.add(jLabel1);
            this.add(jLabel2);
            g.drawString("I LOVE You",935,585);
            g.drawString(" Forever ! ",965,635);
        }else {
            g.setColor(top1);
            g.setFont(new Font("方正舒体", Font.HANGING_BASELINE, 40));
            g.drawString("笨蛋小宝",920,535);
            JLabel jLabel1 = new JLabel(new ImageIcon("D:\\Java\\Love\\src\\Love\\ChristmasTree\\爱心.png"));
            JLabel jLabel2 = new JLabel(new ImageIcon("D:\\Java\\Love\\src\\Love\\ChristmasTree\\爱心.png"));
            jLabel1.setBounds(895,555,40,40);
            jLabel2.setBounds(1140,555,40,40);
            this.add(jLabel1);
            this.add(jLabel2);
            g.drawString("I LOVE You",935,585);
            g.drawString(" Forever ! ",965,635);
        }

    

        //兔子
        if(color){
            g.setColor(red);
            g.setFont(new Font("方正舒体", Font.HANGING_BASELINE, 25));
            g.drawString("╭╮╭╮                 ╭╮╭╮", 725, 392);
            g.drawString("╭-┴┴★╮        ╭-┴┴★╮", 702, 412);
            g.drawString("│◎   ︵ │         │ ︵   ◎ │", 699, 432);
            g.drawString("╰○--○╯        ╰○--○╯", 696, 452);
        }else{
            g.setColor(top2);
            g.setFont(new Font("方正舒体", Font.HANGING_BASELINE, 25));
            g.drawString("╭╮╭╮                 ╭╮╭╮", 725, 392);
            g.drawString("╭-┴┴★╮        ╭-┴┴★╮", 702, 412);
            g.drawString("│◎   ︵ │         │ ︵   ◎ │", 699, 432);
            g.drawString("╰○--○╯        ╰○--○╯", 696, 452);
        }

        if(color){
            drawWrite(500,150,0, happy,g);
        }else {
            drawWrite(502,152,0, top1,g);
        }

        if(color){
            drawPhone(550,250,0, happy,g);
            drawPig(600,350,0, happy,g);
        }else {
            drawPhone(552,252,0, top2,g);
            drawPig(602,352,0, top2,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 drawWrite(int tx, int ty, int num, Color c, Graphics g) {
        g.setColor(c);
        g.setFont(new Font("方正舒体", Font.HANGING_BASELINE, 100));
        g.drawString("Merry Christmas", tx, ty);
    }

    void drawPhone(int tx, int ty, int num, Color c, Graphics g) {
        g.setColor(c);
        g.setFont(new Font("方正舒体", Font.HANGING_BASELINE, 50));
        g.drawString("(  ̄ ▽ ̄)o╭╯☆#╰ _─﹏─)╯ ", tx, ty);
    }

    void drawPig(int tx, int ty, int num, Color c, Graphics g) {
        g.setColor(c);
        g.setFont(new Font("方正舒体", Font.HANGING_BASELINE, 50));
        g.drawString("小宝,快看!!兔子!!", tx, ty);
    }

    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 ;
        }
    }
    void drwafloor(Graphics g) {
        Color c = new Color(0x5BAA0B) ;
        g.setColor(c);
        for(int i=0; i<2; i++) {
            x=0;
            for(int j=0; j<60; j++) {
                g.fillRect(x, y, 20, 20);
                x += 22 ;
            }
            y += 22 ;
        }
    }
    public void actionPerformed(ActionEvent e) {
        if(e.getSource() == onOff) {
            if(flag) {
                ImageIcon icon = new ImageIcon("/星星.png");
                icon.setImage(icon.getImage().getScaledInstance(50,50, 0)) ;
                flag = false ;
                clip.loop();
                time.restart() ;
            }
            else {
                ImageIcon icon = new ImageIcon("/星星.png");
                icon.setImage(icon.getImage().getScaledInstance(50,50, 0)) ;
                flag = true ;
                time.stop() ;
                clip.stop() ;
            }
        }
        else if(e.getSource() == time) {
            repaint() ;
            color = !color ;
        }
    }

}

注:里面的图片路径是我自己的,各位自己修改想要加载的图片路径即可

2. MyFrame 界面搭建 

package Love.ChristmasTree;

import javax.swing.*;

public class MyFrame extends JFrame {
    MyPanel p ;
    MyFrame() {
        p = new MyPanel() ;
        add(p) ;
        setTitle("给小宝的圣诞树");
        setBounds(400, 200, 1300, 800) ;
        setVisible(true);
        //this.setBackground(Color.BLACK);
        validate() ;
        setDefaultCloseOperation(MyFrame.EXIT_ON_CLOSE) ;

        //背景
       // this.getContentPane().removeAll();
        JLabel jLabel = new JLabel(new ImageIcon("D:\\Java\\Love\\src\\Love\\ChristmasTree\\24.jpeg"));//路径自己修改
        jLabel.setBounds(0,0,1300,800);
        this.getContentPane().add(jLabel);
        //this.getContentPane().repaint();
    }

}

3.Main 运行

package Love.ChristmasTree;

public class Main {
    public static void main(String[] args) {
        new MyFrame();
    }
}

以上即是所有代码,部分参数需各位自己修改成自己需要的效果文章来源地址https://www.toymoban.com/news/detail-762339.html

到了这里,关于自己用Java写圣诞树 视觉效果很好的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 【含源码】两种不同风格的圣诞树代码合集,其中还有可以改名字的圣诞树代码

    提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 文章目录 前言 一年一度的圣诞节马上就要到了,看到好多程序员小伙伴已经开始炫耀自己制作的圣诞树了。今天就跟大家分享2种不同风格的圣诞树,附上完整代码,拿来即用可以按照自己的喜好来去运

    2024年02月02日
    浏览(36)
  • 【AE圣诞树代码】

    首发网站 天风的人工智能小站 1、圣诞树代码第一种 2、圣诞树代码第二种 3、修改记事本后缀为html 4、点击文件查看效果

    2024年02月04日
    浏览(42)
  • 打印圣诞树

    今天遇到一个不算特别变态的题目,可是却往错误的方向思考了有一会儿。 这是这个题目的链接:[打印圣诞树](圣诞树_牛客题霸_牛客网 (nowcoder.com)) 题目大致意思是,给定一个高度,打印出圣诞树。 效果图类似以下这种: 首先我把每片叶子看作是 5X3的矩形 ,空白看作是

    2023年04月12日
    浏览(46)
  • Python 圣诞树代码

    1.本章将会讲解Python编程 实现圣诞树效果! 2.圣诞节介绍 基督教纪念耶稣诞生的重要节日。亦称耶稣圣诞节、主降生节,天主教亦称耶稣圣诞瞻礼。耶稣诞生的日期,《圣经》并无记载。公元336年罗马教会开始在12月25日过此节。12月25日原是罗马帝国规定的太阳神诞辰。有人

    2024年02月03日
    浏览(37)
  • Python圣诞树代码

    2024年02月04日
    浏览(34)
  • 圣诞树视频代码HTML

    如有侵权,与本人联系 通过更改文件名称使网页显示不同的视频 效果图片 文章最后记得点赞哦(●ˇ∀ˇ●)

    2024年02月04日
    浏览(38)
  • 圣诞树c语言代码

    圣诞树是一种视觉效果的程序设计模型,可以通过在控制台上使用字符和符号的不同组合来模拟出一棵带有装饰物的圣诞树的形象。以下是一个使用 C 语言实现圣诞树的简单示例代码: #include stdio.h int main(void) { printf(\\\" *n\\\"); printf(\\\" ***n\\\"); printf(\\\" *****n\\\"); printf(\\\" *******n\\\"); printf

    2024年02月04日
    浏览(58)
  • python圣诞树代码编程

    以下是一个简单的Python圣诞树代码: def draw_tree(height):     for i in range(height):         print(\\\' \\\' * (height - i - 1) + \\\'*\\\' * (2 * i + 1))     print(\\\' \\\' * (height - 1) + \\\'|\\\') draw_tree(10) 这个函数会绘制一个等腰三角形,其中每一行的星号数量从1开始,每次递增2,空格数量则相反,从等腰三

    2024年02月05日
    浏览(46)
  • 用Python画圣诞树

    我们的目标是将代码和素材全部隐藏起来,只需要双击一下鼠标就能自动带着BGM开始画圣诞树。 (这是电脑桌面上的一个文件,只需要双击一下,代码就会在后台自动运行了) (一秒后,就会看到弹出界面开始画圣诞树了,并且自动播放背景音乐哦) (再然后,就能看到画

    2023年04月18日
    浏览(31)
  • C语言—小小圣诞树

    这个代码会询问用户输入圣诞树的高度,然后根据输入的高度在控制台上显示相应高度的圣诞树。

    2024年02月04日
    浏览(33)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包