1. 程式人生 > >java swing實現動態心形圖案的程式碼下載

java swing實現動態心形圖案的程式碼下載

               
package com.zuidaima.swing;import java.awt.Color;import java.awt.Graphics;import java.awt.Image;import java.awt.Toolkit;import javax.swing.JFrame;@SuppressWarnings("serial")class Cardioid extends JFrame // 定義視窗大小 private static final int WIDTH = 480private static final int HEIGHT = 600// 獲取螢幕大小 private
static int WINDOW_WIDTH = Toolkit.getDefaultToolkit()   .getScreenSize().width; private static int WINDOW_HEIGHT = Toolkit.getDefaultToolkit()   .getScreenSize().height; // 建構函式 public Cardioid() {  // 設定視窗標題  super("?形線");  // 設定背景色  this.setBackground(Color.BLACK);  // 設定視窗位置  this.setLocation((WINDOW_WIDTH - WIDTH) / 2
,    (WINDOW_HEIGHT - HEIGHT) / 2);  // 設定視窗大小  this.setSize(WIDTH, HEIGHT);  // 設定窗口布局  this.setLayout(getLayout());  // 設定視窗可見  this.setVisible(true);  // 設定視窗預設關閉方式  this.setDefaultCloseOperation(DISPOSE_ON_CLOSE); } public void paint(Graphics g) {  double x, y, r; // 橫縱座標及半徑  Image image = this.createImage(WIDTH, HEIGHT);  Graphics pic = image.getGraphics();  // 繪製圖形
  for (int i = 0; i < 100; i++) {   for (int j = 0; j < 100; j++) {    r = Math.PI / 45 + Math.PI / 45 * i      * (1 - Math.sin(Math.PI / 45 * j)) * 18;    x = r * Math.cos(Math.PI / 45 * j) * Math.sin(Math.PI / 45 * i)      + WIDTH / 2;    y = -r * Math.sin(Math.PI / 45 * j) + HEIGHT / 2;    pic.setColor(Color.MAGENTA);    pic.fillOval((int) x, (int) y, 2, 2);   }   // 生成圖片   g.drawImage(image, 0, 0, this);  } } public static void main(String[] args) {  new Cardioid(); }}