本文主要是介绍没事瞎玩一下,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
大晚上睡前玩一下,写个小程序。。。哈哈,代码如下:
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;import javax.swing.JFrame;
import javax.swing.JPanel;/*** 画出一颗蒲公英。。。。* @author LONG**/
public class TheTree extends JFrame {private static final long serialVersionUID = 1L;private Dimension di = null;private Graphics2D gr = null;/*** 程序主入口* @param args*/public static void main(String[] args){TheTree tt = new TheTree();tt.showFrame();}public void showFrame(){this.setTitle("The Tree");Toolkit tl = Toolkit.getDefaultToolkit();di = tl.getScreenSize();this.setSize(di.width,di.height);this.setDefaultCloseOperation(3);JPanel jp = new JPanel();jp.setPreferredSize(new Dimension(di.width,di.height));this.add(jp);this.setResizable(false);jp.setBackground(Color.BLACK);this.setVisible(true);gr = (Graphics2D) jp.getGraphics();jp.addMouseListener(new MouseAdapter(){public void mouseReleased(MouseEvent e){draw(e);}}); }public void draw(MouseEvent e){int x = e.getX(); //自己选择蒲公英生长的位置int y = e.getY();BasicStroke bs = new BasicStroke(4);gr.setStroke(bs);gr.setColor(Color.GREEN);gr.drawLine(x, y, x, y - 400); //用来先画出蒲公英的主干doSomething(x,y - 400,150);}/***那我们来画蒲公英吧* * @param x1 起始点x坐标* @param y1 起始点y坐标* @param h 长度h*/public void doSomething(int x1, int y1, int h){//使循环将所有的蒲公英分支画出来for(int i = 0; i < 90; i++){double angle = i * 2 * Math.PI/90; //得到等分的九十个角int x2 = x1 + (int)(Math.sin(angle) * h);int y2 = y1 - (int)(Math.cos(angle) * h);BasicStroke bs_next = new BasicStroke();gr.setStroke(bs_next);gr.setColor(Color.gray);gr.drawLine(x1, y1, x2, y2);gr.setColor(Color.WHITE);gr.fillOval(x2 - 1, y2 - 1, 5, 5);}}
}
这篇关于没事瞎玩一下的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!