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

您所在的位置:网站首页 树的java实现 自己用Java写圣诞树 视觉效果很好

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

2024-01-21 10:07| 来源: 网络整理| 查看: 265

一:概述

此前看了许多用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&&y130;x--){ drawDecoration(x, 150, 0, happy, g); } //V for(x=20,y=200;x150;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&&y30&&y140;x--){ drawDecoration(x, 150, 0, happies, g); } //V for(x=30,y=200;x160;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


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3