アプリ版:「スタンプのみでお礼する」機能のリリースについて

Java初心者なのですが、ランダムユニークナンバーをarrayに代入し、長方形の高さが全て違う様に表記したいのですが、全く表記出来ません。
おそらく、arrayに代入する時点までは大丈夫だとは思うのですが、どのようにしたら長方形に高さを代入してappletで表記出来るのでしょうか?




import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.util.Random;
import java.io.*;

public class GUI extends Applet
{

public void update(Graphics g)
{
paint(g);
}


Button button = new Button("Sort Me");
Label text = new Label("Pink: Selection Sort" + "\n" + "Green: Inseration Sort");

int[] store = new int[20];


public static int[] findValue(int [] store){
int rand;
for (int i = 0; i < store.length; i ++){
do{
rand = (int)(Math.random()*21)+10;
}
while(doesExists(rand, store, i));
store[i] = rand;
}
return store;
}

private static boolean doesExists(int rand, int[] arr, int i){
if(i != 0){
for(int j =0; j < i; j++){
if(rand == arr[j]){
return true;
}
}
}
return false;
}

int Counter = 0;
int xScale = 0;
public void displayRectangles(Graphics g)
{
if(Counter < 20)
{
xScale += 15;
int x = 80 + xScale;
int H = store[Counter];

g.setColor(Color.pink);
g.fillRect(x, (140 - H), 10, H);

g.setColor(Color.green);
g.fillRect(x, 140, 10, H);
Counter++;
}
}

public void init()
{
setSize(500, 350);
setBackground(Color.WHITE);
add(button);
add(text);
button.addActionListener(new buttonHandler());
}

int c = 0;
public void paint(Graphics g)
{

c++;
displayRectangles(g);

if(c < 20)
{
repaint();
}
}
int count = 0;
class buttonHandler implements ActionListener{
public void actionPerformed(ActionEvent e){
count ++;
button.setLabel("pass " + count);
if(e.getActionCommand()=="pass")
repaint();

}
}
}

最終的にはボタンをクリックするたびに、長方形の長さが右側になるにつれて大きくなってソートされます。
http://hills.ccsf.cc.ca.us/~cconner/Java/Sorts/S …

どうしたら良いのかどうしても分かりません。
よろしくお願い致します。

A 回答 (1件)

まだ、作成中だと思いますので、ヒントだけ。


この状態で、グラフを表示したいということですよね?
(それを前提に進めます。)

storeは、配列の初期化されますが、値が入っていません。

public void init()で、初期値を設定する必要があると思います。
findValueメソッドを利用して値をいれてみたら、表示されました。

試したこと
storeに値を入れるためinitメソッドに追加してみました。

public void init()
{
setSize(500, 350);
setBackground(Color.WHITE);
add(button);
add(text);
button.addActionListener(new buttonHandler());
store = findValue(store);
}
    • good
    • 0
この回答へのお礼

本当に本当にありがとうございます。
これからbuble sortにselection sortを考えてみます。
まだまだ分からない事だらけなので、もしお時間がったらまた教えて下さい。
この先、ボタンを押すごとに各値が右にソートされて行く状態にしたいと思っています。
来週の火曜日が提出期限なので、また質問コーナーに分からない事があったら上げてみます。

本当に、本当に大感謝です。
ありがとうございました。

お礼日時:2012/04/28 17:13

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!