JAVA圣诞树

这是一个使用JAVA编写的圣诞树动画程序,包括主类、窗口类和面板类。程序通过绘制多个三角形形成一棵圣诞树,并用不同颜色的圆点作为装饰,同时包含了音乐播放功能。用户可以通过按钮控制动画的开启和关闭,以及音乐的播放和停止。
摘要由CSDN通过智能技术生成

免费分享JAVA圣诞树代码

主类.java

package christmasTree;

public class 主类 {

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

}

MyFrame.java

package christmasTree;

import javax.swing.JFrame;

public class MyFrame extends JFrame{
    /**
     *
     */
    private static final long serialVersionUID = 1L;
    MyPanel p ;
    MyFrame() {
        p = new MyPanel() ;
        add(p) ;
        setBounds(400, 200, 800, 800) ;
        setVisible(true) ;
        validate() ;
        setDefaultCloseOperation(MyFrame.EXIT_ON_CLOSE) ;
    }
}

MyPanel.java

package christmasTree;

import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.*;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;

import javax.swing.*;

public class MyPanel extends JPanel implements ActionListener{
    int x, y ;        //坐标
    JButton onOff ;        //开关按钮
    Timer time ;        //触发时间事件
    boolean flag ;        //判断是开还是关
    boolean color ;        //控制动画
    File file = new File("music.wav") ;
    URL url = null;
    URI uri = null ;
    AudioClip clip = null;
    MyPanel() {
        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("2.png") ;
            g.drawImage(image1.getImage(), x-3, y-25, 28, 26, null) ;
        }
        else {
            ImageIcon image1 = new ImageIcon("1.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 ;
        }
    }
}

效果图:

 

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

信仰12800

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值