ペイントコンポーネント内。パラメータとして g を取り、g はグラフィックスまたは graphics2d にすることができます。クラスは jpanel を拡張します。それから:
super.paintComponent(g);
this.setBackground( Color.BLACK );
g がグラフィックスの場合は機能しますが、graphics2d の場合は機能しません。両方でコンパイルされますが、graphics2d は背景色を変更しません。どうして?
JPanel
(これは のサブクラスですJComponent
) にはメソッドしかありませんpaintComponent(Graphics)
。シグネチャを持つメソッドはありませんpaintComponent(Graphics2D)
。
メソッドのオーバーライドはpaintComponent(Graphics)
、次の方法で実行できます。
public void paintComponent(Graphics g)
{
// Do things.
}
ただし、次のようなシグネチャを使用してメソッドを定義することpaintComponent(Graphics2D)
は合法ですが、で定義されているメソッドをオーバーライドしていないため、呼び出さJComponent
れることはありません。
public void paintComponent(Graphics2D g)
{
// Do things.
// However, this method will never be called, as it is not overriding any
// method of JComponent, but is a method of the class this is defined in.
}
クラス( のスーパークラス)のJava API 仕様にJComponent
JPanel
は、クラスの一部であるすべてのメソッドを一覧表示するメソッドの概要があります。
Swing でのペイントに関する詳細情報。