2016-09-05 7 views
-2

Javaでアニメーションゲームを作成します。しかしJPanelでイメージを持っているときよりもゆっくり動いています。多くの画像をアニメーション化すると、Javaがゆっくり実行されます

Multi_PaintはJFrameのがのActionListener {

JPanel pn1 = new JPanel(); 
JPanel pn2 = new JPanel(); 

static int x=100,y=100; 
Timer timer; 

Multi_Paint(){ 

    setLayout(new BorderLayout()); 

    pn1.setBackground(Color.GREEN); 
    pn2.setBackground(Color.red); 
    pn2.setPreferredSize(new Dimension(300, 300)); 

    add(pn1,BorderLayout.CENTER); 
    add(pn2,BorderLayout.WEST); 

    setSize(1000, 1000); 
    setVisible(true); 
    pn1.add(new DrawPanel()); 
    pn2.add(new DrawPanel());  

    timer = new Timer(1, this); 
    timer.start(); 

} 

public void actionPerformed(ActionEvent e) {   
    moveBall(); 
    repaint(); 
} 

void moveBall(){ 
    x=x+10; 
    y=y+10; 
} 

public static void main(String[] args) { 
    new Multi_Paint(); 
} 

}

クラスDrawPanelはJPanelの{

DrawPanel(){ 
    setBorder(BorderFactory.createLineBorder(Color.black)); 
} 

public Dimension getPreferredSize() { 
    return new Dimension(500,500); 
} 

public void paintComponent(Graphics g) { 
    super.paintComponent(g);  

    int x= Multi_Paint.x; 
    int y= Multi_Paint.y; 

    //If we decline this "try" Java will run faster. 
    try {    
     BufferedImage img = ImageIO.read(new File("D:\\pict1.jpg")); 
     double scale = 0.5 ; 
     double w = scale * img.getWidth(this); 
     double h = scale * img.getHeight(this); 

     g.drawImage(img, x, y, (int) w, (int) h, this); 


    } catch (IOException e) {   
     e.printStackTrace(); 
    } 

    g.fillOval(x, y, 30, 30); 
} 

}

+3

なぜあなたは 'paintComponent'内の画像ファイル**毎回**を読んでする必要がありますか? – QBrute

+0

JPanelにイメージを追加すると思うのですか? –

答えて

0

それが今であるように、ImageIO.readが内部作成を拡張実装延びパブリッククラスImageInputStream新しいBufferedImageインスタンスにデータを書き込み、高価なIO操作であるのすべての単一フレームの1つのフレームを閉じます。それがゆっくりと走っている理由です。

paintComponentメソッドでロジックを使用しないでください。そうしないと、処理が遅くなります。コンストラクタで画像ファイルを一度に読み込み、ペイント方法でのみアクセスする必要があります。画像ファイルはプログラムの過程で変化しないので、これで十分です。このような

何か作業をする必要があります:

class DrawPanel extends JPanel { 
    private final BufferedImage img; 
    private int w; 
    private int h; 

    DrawPanel() { 
     setBorder(BorderFactory.createLineBorder(Color.black)); 
     this.img = createImage("D:\\pict1.jpg"); 
    } 

    private BufferedImage createImage(String path) { 
     BufferedImage img = null; 
     try { 
      img = ImageIO.read(new File(path)); 
      double scale = 0.5; 
      this.w = (int) (scale * img.getWidth(this)); 
      this.h = (int) (scale * img.getHeight(this)); 
     } catch (IOException e) { 
      System.err.println("Could not read file with path " + path); 
      e.printStackTrace(); 
     } 
     return img; 
    } 

    public Dimension getPreferredSize() { 
     return new Dimension(500,500); 
    } 

    public void paintComponent(Graphics g) { 
     super.paintComponent(g);  

     int x= Multi_Paint.x; 
     int y= Multi_Paint.y; 

     // img could be null 
     if(this.img != null) { 
      g.drawImage(img, x, y, w, h, this); 
     } 

     g.fillOval(x, y, 30, 30); 
    } 
} 
+0

ありがとうございました。わかった。 –

関連する問題