2013-10-10 41 views
0

你好)有一個問題,即是否有可能加快的代碼,因爲這其中是下面的:加速輸出圖像

import java.awt.Canvas; 
import java.awt.Color; 
import java.awt.EventQueue; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.image.BufferedImage; 
import javax.swing.JFrame; 

public class Game extends Canvas { 

    private static final long serialVersionUID = 1L; 

    private static final int WIDTH = 400; 
    private static final int HEIGHT = 400; 

    @Override 
    public void paint(Graphics g) { 

     super.paint(g); 

     int w = 20; 
     int h = 20; 
     int type = BufferedImage.TYPE_INT_ARGB; 

     BufferedImage image = new BufferedImage(w, h, type); 
     Graphics2D g2d = image.createGraphics(); 
     g2d.setColor(Color.GREEN); 
     g2d.fillRect(0, 0, w, h); 
     g2d.dispose(); 
     int MapWidth = image.getWidth(this); 
     int MapHeight = image.getHeight(this); 

     for (int s = MapWidth - MapWidth; s < MapWidth * 10; s++) { 
      for (int i = MapHeight - MapHeight; i < MapHeight * 10; i++) { 
       g.drawImage(image, s, i, (int) w, (int) h, this); 
      } 
     } 

    } 

    public static void main(String[] args) { 

     EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       JFrame frame = new JFrame(); 

       frame.setSize(WIDTH, HEIGHT); 
       frame.add(new Game()); 

       frame.setVisible(true); 
      } 
     }); 
    } 
} 

他畫的圖片了這麼久。它如何加快速度?請幫助)

和一個程序不能關閉,直到繪製的圖像。 有必要加快繪圖

+0

你有沒有意識到:「MapWidth - MapWidth」將始終爲零:)。 – libik

回答

1

它是緩慢的,因爲在你的

for (int s = 0; s < MapWidth * 10; s++) { 
     for (int i = 0; i < MapHeight * 10; i++) { 
      g.drawImage(image, s, i, (int) w, (int) h, this); 
     } 
    } 

爲400 * 400 MapWidth,MapHeight要繪製4000 * 4000對象,所以你繪製了16 000 000個對象,它應該很慢。

如果這種替換它,這將是快速的地獄:):

for (int s = 0; s < MapWidth * 10; s += w) { 
     for (int i = 0; i < MapHeight * 10; i += h) { 
      g.drawImage(image, s, i, (int) w, (int) h, this); 
     } 
    } 
0

只是嘗試下面的代碼

import java.awt.Canvas; 
import java.awt.Color; 
import java.awt.EventQueue; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.image.BufferedImage; 
import javax.swing.JFrame; 

public class Game extends Canvas { 

    private static final long serialVersionUID = 1L; 

    private static final int WIDTH = 400; 
    private static final int HEIGHT = 400; 

    @Override 
    public void paint(Graphics g) { 

     super.paint(g); 

     int w = 20; 
     int h = 20; 
     int type = BufferedImage.TYPE_INT_ARGB; 

     BufferedImage image = new BufferedImage(w, h, type); 
     Graphics2D g2d = image.createGraphics(); 
     g2d.setColor(Color.GREEN); 
     g2d.fillRect(0, 0, w, h); 
     g2d.dispose(); 
     int MapWidth = image.getWidth(this); 
     int MapHeight = image.getHeight(this); 

     for (int s = MapWidth - MapWidth; s < MapWidth * 10; s++) { 
      for (int i = MapHeight - MapHeight; i < MapHeight * 10; i=i+10) { 
       g.drawImage(image, s, i, (int) w, (int) h, this); 
      } 
     } 

    } 

    public static void main(String[] args) { 

     EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       JFrame frame = new JFrame(); 

       frame.setSize(WIDTH, HEIGHT); 
       frame.add(new Game()); 

       frame.setVisible(true); 
      } 
     }); 
    } 
}