≒ 初swing

昨日からswing。以下のコードの場合、JPanelを`普通に別クラスとして書くか、インナークラスで書くかで変数のスコープが変わるのは理解してるんですけど、

なぜ、showStatus()がインナーだと活きて、分けるとJApplet側に書いても、もちろんJPanel側に書いても動作しないのかがわかりません。

 

import java.awt.*;

import javax.swing.*;

public class Ch13Rewrite4 extends JApplet {
public void init() {
getContentPane().add(new MyPane());
}

class MyPane extends JPanel {
public void paintComponent(Graphics g) {
Dimension d = getSize();
int xc = d.width / 2;
int yc = d.height / 2;
int radius = (int) (((d.width < d.height) ? d.width : d.height) * 0.35);
int rr = (int) (Math.random() * 255.9);
int gg = (int) (Math.random() * 255.9);
int bb = (int) (Math.random() * 255.9);
Color c = new Color(rr, gg, bb);
g.setColor(c);

g.fillOval(xc – radius, yc – radius, radius * 2, radius * 2);

g.setColor(Color.white);
FontMetrics fm=g.getFontMetrics();
String s=rr+” : “+gg+” : “+bb;
g.drawString(s, xc-(fm.stringWidth(s)/2), yc);
showStatus(d.width + ” : ” + d.height + ” : ” + radius);

g.drawLine(0, yc, d.width, yc);
g.drawLine(xc, 0, xc, d.height);
}
}
}

とりあえず備忘。追求はおいおい。