2014-07-15 268 views
0

我一直在使用Graphics.drawImage很長一段時間來渲染圖像,但是在我安裝了java 8之後,它停止工作。沒有錯誤。我不明白髮生了什麼事。Graphics.drawimage不工作Java 8

import java.awt.Canvas; 
import java.awt.Color; 
import java.awt.Graphics; 
import java.awt.GraphicsConfiguration; 
import java.awt.GraphicsDevice; 
import java.awt.GraphicsEnvironment; 
import java.awt.image.BufferStrategy; 
import java.awt.image.BufferedImage; 
import javax.swing.JFrame; 

public class Game implements Runnable { 
    private BufferedImage img = new BufferedImage(10, 10, BufferedImage.TYPE_INT_ARGB); 

    private boolean running; 

    private Thread thread; 

    private JFrame f; 
    private Canvas c; 

    public Game() { 
     GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); 
     GraphicsDevice[] gs = ge.getScreenDevices(); 
     for (GraphicsDevice gd : gs) { 
      GraphicsConfiguration[] gc = gd.getConfigurations(); 
      for (GraphicsConfiguration gc1 : gc) { 
       f = new JFrame("Title", gd.getDefaultConfiguration()); 
       c = new Canvas(gc1); 
       f.getContentPane().add(c); 
       f.setUndecorated(false); 
       f.setIgnoreRepaint(true); 
       f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       f.setSize(700, 600); 
       f.setResizable(false); 
       f.setLocationRelativeTo(null); 
       f.setFocusable(true); 
       f.setVisible(true); 
      } 
     } 
    } 

    public void init() { 
     img.getGraphics().drawImage(img, 0, 0, Color.BLACK, null); 
    } 

    @Override 
    public void run() { 
     long lastTime = System.nanoTime(); 
     double nsPerTick = 1000000000D/60; 

     int ticks = 0; 
     int frames = 0; 

     long lastTimer = System.currentTimeMillis(); 
     double delta = 0; 

     init(); 

     while (running) { 
      long now = System.nanoTime(); 
      delta += (now - lastTime)/nsPerTick; 
      lastTime = now; 
      boolean shouldRender = true; 

      while (delta >= 1) { 
       ticks++; 
       tick(); 
       delta -= 1; 
       shouldRender = true; 
      } 

      try { 
       Thread.sleep(2); 
      } catch (InterruptedException e) { 
       System.out.println(e.getMessage()); 
      } 

      if (shouldRender) { 
       frames++; 
       render(); 
      } 

      if (System.currentTimeMillis() - lastTimer >= 1000) { 
       lastTimer += 1000; 
       frames = 0; 
       ticks = 0; 
      } 
     } 
    } 

    public synchronized void start() { 
     running = true; 

     thread = new Thread(this, "GAME"); 
     thread.start(); 
    } 

    public synchronized void stop() { 
     running = false; 

     try { 
      thread.join(); 
     } catch (InterruptedException e) { 
      System.out.println(e.getMessage()); 
     } 
    } 

    public static void main(String[] args) { 
     new Game().start(); 
    } 

    public void tick() { /*player movement, ect*/ } 

    public void render() { 
     c.createBufferStrategy(3); 
     BufferStrategy bs = c.getBufferStrategy(); 

     Graphics g = bs.getDrawGraphics(); 
     g.drawImage(img, 100, 100, null); 
     //I should also not that strings won't draw either. 
     g.drawString("Hello, world", 50, 50); 
     g.dispose(); 
     bs.show(); 
    } 
} 

我不明白爲什麼這會不會是工作,因爲它是在Java 7的做工精細,而且我之前沒有對代碼進行改動或後,我安裝的Java 8 還有我如果有幫助,我正在使用MacBook Pro。

+2

請編輯您的問題包括[完整的示例](http://stackoverflow.com/help/mcve),顯示問題和平臺上它發生。 – trashgod

+0

線程真的需要證明這個問題嗎?你能否創建一個不涉及顯示問題的線程的最小示例? –

回答

4

在Mac OS X 10.9.4版本,我可以確認的Java 8下即createBufferStrategy(2)作品,而createBufferStrategy(3)失敗,除非我恢復到Java 7順便說一句,注意Swing GUI的對象應當建立和操縱event dispatch thread上。

image

爲試:

import java.awt.Canvas; 
import java.awt.Color; 
import java.awt.Graphics; 
import java.awt.GraphicsConfiguration; 
import java.awt.GraphicsDevice; 
import java.awt.GraphicsEnvironment; 
import java.awt.image.BufferStrategy; 
import java.awt.image.BufferedImage; 
import javax.swing.JFrame; 
import java.awt.EventQueue; 

public class Game implements Runnable { 
    private BufferedImage img = new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB); 
    private boolean running; 
    private Thread thread; 
    private JFrame f; 
    private Canvas c; 

    public Game() { 
     GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); 
     GraphicsDevice[] gs = ge.getScreenDevices(); 
     for (GraphicsDevice gd : gs) { 
      GraphicsConfiguration[] gc = gd.getConfigurations(); 
      for (GraphicsConfiguration gc1 : gc) { 
       f = new JFrame("Title", gd.getDefaultConfiguration()); 
       c = new Canvas(gc1); 
       f.add(c); 
       f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       f.pack(); 
       f.setSize(360, 300); 
       f.setLocationRelativeTo(null); 
       f.setVisible(true); 
      } 
     } 
    } 

    public void init() { 
     img.getGraphics().drawImage(img, 0, 0, Color.BLUE, null); 
    } 

    @Override 
    public void run() { 
     long lastTime = System.nanoTime(); 
     double nsPerTick = 1000000000D/60; 

     int ticks = 0; 
     int frames = 0; 

     long lastTimer = System.currentTimeMillis(); 
     double delta = 0; 

     init(); 

     while (running) { 
      long now = System.nanoTime(); 
      delta += (now - lastTime)/nsPerTick; 
      lastTime = now; 
      boolean shouldRender = true; 

      while (delta >= 1) { 
       ticks++; 
       tick(); 
       delta -= 1; 
       shouldRender = true; 
      } 

      try { 
       Thread.sleep(2); 
      } catch (InterruptedException e) { 
       System.out.println(e.getMessage()); 
      } 

      if (shouldRender) { 
       frames++; 
       render(); 
      } 

      if (System.currentTimeMillis() - lastTimer >= 1000) { 
       lastTimer += 1000; 
       frames = 0; 
       ticks = 0; 
      } 
     } 
    } 

    public synchronized void start() { 
     running = true; 
     thread = new Thread(this, "GAME"); 
     thread.start(); 
    } 

    public synchronized void stop() { 
     running = false; 

     try { 
      thread.join(); 
     } catch (InterruptedException e) { 
      System.out.println(e.getMessage()); 
     } 
    } 

    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       new Game().start(); 
      } 
     }); 
    } 

    public void tick() { /*player movement, ect*/ } 

    public void render() { 
     c.createBufferStrategy(2); 
     BufferStrategy bs = c.getBufferStrategy(); 
     Graphics g = bs.getDrawGraphics(); 
     g.drawImage(img, 100, 100, null); 
     g.drawString("Hello, world: " + System.currentTimeMillis(), 50, 50); 
     g.dispose(); 
     bs.show(); 
    } 
} 
+0

:-)冒着使用非常短的Thread.sleep(2); – mKorbel

+0

@mKorbel:好點; jsellers10000:從上下文來看,整個方案看起來很複雜;因爲默認情況下Swing組件是雙緩衝的,所以可選項包括'javax.swing.Timer'或'SwingWorker'。 – trashgod