タイトル【今から始めるCocoaプログラミング】抽選ソフトが簡単に作成できた(3)カテゴリーCocoa, 今から始めるCocoaプログラミング
作成日2001/3/8 16:39:3作成者新居雅行
/* MyDrawer.javaの内容は以下の通り */


import com.apple.cocoa.foundation.*;
import com.apple.cocoa.application.*;
import java.util.*;

public class MyDrawer extends NSObject {

Object drawingButton;
Object maxNumber;
Object pastNumbers;
Object resultNumber;

int[] nums;
int seq;
int maxOfSeq;

public void goDrawing(Object sender) {
NSTextField resultNumberText = (NSTextField)resultNumber;
NSTextField pastNumList = (NSTextField)pastNumbers;
NSButton drawingBtn = (NSButton)drawingButton;

resultNumberText.setStringValue(String.valueOf(nums[seq]));
pastNumList.setStringValue(pastNumList.stringValue() + "," + String.valueOf(nums[seq]));

seq++;
if(seq>=maxOfSeq) {
pastNumList.setStringValue(pastNumList.stringValue() + " - Finish");
drawingBtn.setEnabled(false);
}
}

public void insertNextLine(Object sender) {
NSTextField pastNumList = (NSTextField)pastNumbers;
pastNumList.setStringValue(pastNumList.stringValue() + " - ");
}

public void initNumberSequence(Object sender) {
NSTextField maxNumberText = (NSTextField)maxNumber;
NSTextField pastNumList = (NSTextField)pastNumbers;
NSButton drawingBtn = (NSButton)drawingButton;

maxOfSeq = maxNumberText.intValue();
nums = new int[maxOfSeq];
for(int i=1 ; i<=maxOfSeq ; i++)
nums[i-1] = i;
seq = 0;

Random myRnum = new Random();
int repeating = maxOfSeq * 10;
for( int i=0 ; i<repeating ; i++) {
int index1 = (int)(myRnum.nextFloat()*maxOfSeq);
int index2 = (int)(myRnum.nextFloat()*maxOfSeq);
int x = nums[index1];
nums[index1] = nums[index2];
nums[index2] = x;
}
pastNumList.setStringValue("Initialized by " + String.valueOf(maxOfSeq) + " - ");
drawingBtn.setEnabled(true);
}
}
関連リンク