今天是Java體驗課,需要的軟體就是Java的編輯器-Eclipse
在這之前,完全沒有接觸過Java,對於Java的程式語言完全不是很了解,但之前稍微學過一些很基本的python,所以在聽的過程中可以了解老師的邏輯。雖說如此,礙於不懂Java的編輯,在編輯上還是有許多困難,有點像“知道怎麼做,可是又做不出來”。
上圖是一個Eclipse操作界面。綠色框框是整個java的架構(包含了package、class等),看起來有階層關係;紅色框框是編輯模式、除錯模式及其執行按鈕;紫色框框是主要編輯區,上方有類似瀏覽器的分頁,其功能就是打開不同的class像分頁一樣瀏覽。紫色框框的下方有個叫console的分頁,那是結果輸出的區域。
在看這些程式碼的時候,讓我覺得困惑的是package & class之間所代表的立場是什麼?為什麼要這樣分?不同package之間的class是不能共用的嗎?
在創一個新的java時,需要先新增一個class。
當然一開始需要讓程式與使用者互動,最簡單的方式就是讓程式說點什麼!
開始 Java 的 Hello World 吧~
輸出數字或文字的程式碼即是
【System.out.println(沒有“”代表程式內某變數的輸出,“引號內以純文字輸出”);】
---------------------------------------------------------------------------------------------------------
作業!
4. 寫一個 Java 程式進行 X-Y 平面的粒子運動並視覺化,這一題只要可以視覺化就好,沒有要求要做什麼樣的運動模型 (請將程式碼貼在自己的網頁上,完成這一題的同學90分)。
以下並不是利用Java語言編寫成的程式,而是利用 Python來編寫。
Python安裝檔 & Pygame模組:按此下載
作業檔案:按此下載
---------------------------------------------------------------------------------------------------------
import pygame
import random
import math
# Define some colors
BLACK = ( 0, 0, 0)
WHITE = ( 255, 255, 255)
GREEN = ( 0, 255, 0)
RED = ( 255, 0, 0)
pygame.init()
# Set the width and height of the screen [width, height]
width = 700
high = 500
size = (width, high)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Atom Movement")
# Loop until the user clicks the close button.
done = False
# Used to manage how fast the screen updates
clock = pygame.time.Clock()
# Parameters
atom_list = []
change_list = []
atom_size = 10
atom_num = 100
frame_speed = 20
# Generate atoms with random coordinate in the window
for i in range (atom_num):
atom_x = random.randint(2 * atom_size, width - 2 * atom_size )
atom_y = random.randint(2 * atom_size, high - 2 * atom_size)
atom_list.append([atom_x, atom_y])
for m in range(i):
i_x = atom_list[i][0]
i_y = atom_list[i][1]
m_x = atom_list[m][0]
m_y = atom_list[m][1]
if math.sqrt((i_x-m_x)*(i_x-m_x)+(i_y-m_y)*(i_y-m_y)) <= 20 and math.sqrt((i_x-m_x)*(i_x-m_x)+(i_y-m_y)*(i_y-m_y)) != 0:
atom_list.pop()
atom_x = random.randint(2 * atom_size, width - 2 * atom_size)
atom_y = random.randint(2 * atom_size, high - 2 * atom_size)
atom_list.append([atom_x, atom_y])
# Generate different movement with different speed in XY direction
for l in range (atom_num):
x_change = random.randint(-5,5)
y_change = random.randint(-5,5)
change_list.append([x_change, y_change])
# -------- Main Program Loop -----------
while not done:
# --- Main event loop
for event in pygame.event.get(): # User did something
if event.type == pygame.QUIT: # If user clicked close
done = True # Flag that we are done so we exit this loop
screen.fill(BLACK)
# Draw atoms
for atom in atom_list:
pygame.draw.circle(screen, GREEN, atom, atom_size)
# Change atom movement with adding different speed by another list
for j in range(len(atom_list)):
atom_list[j][0] += change_list[j][0]
atom_list[j][1] += change_list[j][1]
# Create a condition for simple bounce on wall
if atom_list[j][0] > (width - atom_size) or atom_list[j][0] < atom_size:
change_list[j][0] *= -1
if atom_list[j][1] > (high - atom_size) or atom_list[j][1] < atom_size:
change_list[j][1] *= -1
# Create a simple bounce on atom
for k in range(len(atom_list)):
j_x = atom_list[j][0]
j_y = atom_list[j][1]
k_x = atom_list[k][0]
k_y = atom_list[k][1]
if math.sqrt((j_x-k_x)*(j_x-k_x)+(j_y-k_y)*(j_y-k_y)) < (2 * atom_size) and math.sqrt((j_x-k_x)*(j_x-k_x)+(j_y-k_y)*(j_y-k_y)) != 0:
change_list[j][0] *= -1
change_list[j][1] *= -1
# --- Go ahead and update the screen with what we've drawn.
pygame.display.flip()
# --- Limit to 60 frames per second
clock.tick(frame_speed)
pygame.quit()
---------------------------------------------------------------------------------------------------------
import pygame
import random
import math
# Define some colors
BLACK = ( 0, 0, 0)
WHITE = ( 255, 255, 255)
GREEN = ( 0, 255, 0)
RED = ( 255, 0, 0)
pygame.init()
# Set the width and height of the screen [width, height]
width = 700
high = 500
size = (width, high)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Atom Movement")
# Loop until the user clicks the close button.
done = False
# Used to manage how fast the screen updates
clock = pygame.time.Clock()
# Parameters
atom_list = []
change_list = []
atom_size = 10
atom_num = 100
frame_speed = 20
# Generate atoms with random coordinate in the window
for i in range (atom_num):
atom_x = random.randint(2 * atom_size, width - 2 * atom_size )
atom_y = random.randint(2 * atom_size, high - 2 * atom_size)
atom_list.append([atom_x, atom_y])
for m in range(i):
i_x = atom_list[i][0]
i_y = atom_list[i][1]
m_x = atom_list[m][0]
m_y = atom_list[m][1]
if math.sqrt((i_x-m_x)*(i_x-m_x)+(i_y-m_y)*(i_y-m_y)) <= 20 and math.sqrt((i_x-m_x)*(i_x-m_x)+(i_y-m_y)*(i_y-m_y)) != 0:
atom_list.pop()
atom_x = random.randint(2 * atom_size, width - 2 * atom_size)
atom_y = random.randint(2 * atom_size, high - 2 * atom_size)
atom_list.append([atom_x, atom_y])
# Generate different movement with different speed in XY direction
for l in range (atom_num):
x_change = random.randint(-5,5)
y_change = random.randint(-5,5)
change_list.append([x_change, y_change])
# -------- Main Program Loop -----------
while not done:
# --- Main event loop
for event in pygame.event.get(): # User did something
if event.type == pygame.QUIT: # If user clicked close
done = True # Flag that we are done so we exit this loop
screen.fill(BLACK)
# Draw atoms
for atom in atom_list:
pygame.draw.circle(screen, GREEN, atom, atom_size)
# Change atom movement with adding different speed by another list
for j in range(len(atom_list)):
atom_list[j][0] += change_list[j][0]
atom_list[j][1] += change_list[j][1]
# Create a condition for simple bounce on wall
if atom_list[j][0] > (width - atom_size) or atom_list[j][0] < atom_size:
change_list[j][0] *= -1
if atom_list[j][1] > (high - atom_size) or atom_list[j][1] < atom_size:
change_list[j][1] *= -1
# Create a simple bounce on atom
for k in range(len(atom_list)):
j_x = atom_list[j][0]
j_y = atom_list[j][1]
k_x = atom_list[k][0]
k_y = atom_list[k][1]
if math.sqrt((j_x-k_x)*(j_x-k_x)+(j_y-k_y)*(j_y-k_y)) < (2 * atom_size) and math.sqrt((j_x-k_x)*(j_x-k_x)+(j_y-k_y)*(j_y-k_y)) != 0:
change_list[j][0] *= -1
change_list[j][1] *= -1
# --- Go ahead and update the screen with what we've drawn.
pygame.display.flip()
# --- Limit to 60 frames per second
clock.tick(frame_speed)
pygame.quit()
---------------------------------------------------------------------------------------------------------
影片為程式的輸出結果
程式中有幾點Bug:
1. 各個原子的運動為隨機,原子間的碰撞以半徑小於 2r 為基準,當判斷為碰撞時,只以反方向反彈,並不會有加速的現象。所以當兩原子出現的位置重疊,則會無限反彈。
2. 兩個原子同時撞擊另一原子時,也會出現第1點的狀況,也會出現跑到視窗外的現象,而且一樣以無限反彈的形式移動。脫離視窗後,便不會再返回視窗。
沒有留言:
張貼留言