ジェイミー:
私は「ボール」オブジェクトを作成し、JFrameのウィンドウを作成し、JFrameのウィンドウに、このボールオブジェクトを追加しています。問題は正確には何ですか?
public class Main {
public static void main(String[] args){
JFrameWindow j= new JFrameWindow(300,500);
Ball b = new Ball(150,200,10,20);
j.add(b);
}
}
import javax.swing.*;
import java.awt.*;
public class JFrameWindow extends JFrame {
int width;
int height;
public JFrameWindow(int width,int height){
this.width=width;
//blah
import javax.swing.*;
import java.awt.*;
public class Ball extends JPanel{
int sX,sY;
Color color;
int speed;
int height;
int width;
public Ball(int sX,int sY,int height,int width){
this.sX=sX;
this.sY=sY;
this.color=color;
this.speed=speed;
this.height=height;
this.width=width;
}
public void paintComponent(Graphics g){
super.paint(g);
Graphics2D g2d=(Graphics2D)g;
//g2d.setColor(color.RED);
g2d.fillOval(sX,sY,width,height);
}
}
基本的に、私はこのプログラムを実行すると、「にsuper.paint(g)は」オーバー呼び出されると何度も繰り返し、これはなぜ私は知りません。私は、なぜ問題が発生していますので、タイマーか何かに移動するようにボールを設定していませんか?
ウナギのホバークラフトのフル:
Abhinav状態として、問題はそれがあるsuper.paint(g);
で再起動、その後のJPanelの呼び出し、塗装チェーンpaintComponent(g)
方式呼び出しsuper.paint(g)
のJPanelの呼び出し、paintComponent(g)
その呼び出し方法を...と上と上
解決策は単純である - 、正しいスーパーメソッドあなたが呼んでいることを塗る方法と一致するものを呼び出します。
@Override
protected void paintComponent(Graphics g) { // protected, not public
// super.paint(g); // ******** REMOVE *********
super.paintComponent(g); // ******** ADD *********
Graphics2D g2d = (Graphics2D) g;
g2d.fillOval(sX, sY, width, height);
}