2013-08-30 575 views
1

所以我試圖改變我從教程製作的全屏遊戲的背景,我試圖改變背景爲綠色,但它保持黑色,代碼有什麼問題?不改變顏色的背景

Screen.java

package debels; 

import java.awt.*; 
import javax.swing.JFrame; 

public class Screen { 
    private GraphicsDevice vc; 

    public Screen(){ 
     GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment(); 
     vc = env.getDefaultScreenDevice(); 
    } 

    public void setFullScreen(DisplayMode dm, JFrame window){ 
     window.setUndecorated(true); 
     window.setResizable(false); 
     vc.setFullScreenWindow(window); 

     if (dm != null && vc.isDisplayChangeSupported()){ 
      try{ 
       vc.setDisplayMode(dm); 
      }catch (Exception e){} 
     } 
    } 

    public Window getFullScreen(){ 
     return vc.getFullScreenWindow(); 
    } 

    public void restoreScreen(){ 
     Window w = vc.getFullScreenWindow(); 
     if (w != null){ 
      w.dispose(); 
     } 
     vc.setFullScreenWindow(null); 
    } 
} 

Main.java

package debels; 

import java.awt.*; 
import javax.swing.*; 

public class Main extends JFrame{ 
    private static final long serialVersionUID = -7498470134154894036L; 

    public static void main(String[] args){ 
     DisplayMode dm = new DisplayMode(800, 600, 16, DisplayMode.REFRESH_RATE_UNKNOWN); 
     Main m = new Main(); 
     m.run(dm); 
    } 

    public void run(DisplayMode dm){ 
     setBackground(Color.GREEN); 
     setForeground(Color.BLUE); 
     setFont(new Font("Arial", Font.PLAIN, 24)); 

     Screen s = new Screen(); 
     try{ 
      s.setFullScreen(dm, this); 
      try{ 
       Thread.sleep(5000); 
      }catch(Exception e){} 
     }finally{ 
      s.restoreScreen(); 
     } 
    } 

    public void paint(Graphics g){ 
     g.drawString("Hello", 200, 200); 
    } 
} 
+0

我會開始說,我不認爲'Thread.sleep'對你有幫助,'paint'方法也沒有。 – MadProgrammer

+0

Thread.sleep只是看全屏,paint方法似乎沒有衝突,因爲它按預期工作 – Debels

+0

*「paint方法似乎沒有衝突,因爲它按預期工作」* - 這就是問題,它是不應該的,因爲它應該,看到我的答案瞭解更多的細節;) – MadProgrammer

回答

5

你的第一個問題將是您的paint方法......的責任

public void paint(Graphics g){ 
    g.drawString("Hello", 200, 200); 
} 

部分paint方法是畫背景。但既然你不打電話super.paint這不會發生。

你應該避免壓倒一切paint在頂層容器,像JFrame,而使用的自定義組件,像JPanel例如,和覆蓋其paintComponent方法(不要忘記調用super.paintComponent)。

另一個潛在的問題是使用Thread.sleep,這可能導致事件分派線程暫停,從而阻止處理新的繪製請求。更好的解決方案可能是使用一個javax.swing.Timer,這將在後臺暫停並提供EDT的上下文中通知(通過一個ActionListener)...

例如

enter image description here

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.DisplayMode; 
import java.awt.EventQueue; 
import java.awt.Font; 
import java.awt.FontMetrics; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.GraphicsDevice; 
import java.awt.GraphicsEnvironment; 
import java.awt.Window; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.Timer; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 

public class TestFullScreen { 

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

    public TestFullScreen() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
       } 

       JFrame frame = new JFrame("Testing"); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.setLayout(new BorderLayout()); 
       frame.add(new TestPane()); 

       final Screen s = new Screen(); 
       DisplayMode dm = new DisplayMode(800, 600, 16, DisplayMode.REFRESH_RATE_UNKNOWN); 
       s.setFullScreen(dm, frame); 

       Timer timer = new Timer(5000, new ActionListener() { 
        @Override 
        public void actionPerformed(ActionEvent e) { 
         s.restoreScreen(); 
        } 
       }); 
       timer.start(); 
      } 
     }); 
    } 

    public class TestPane extends JPanel { 

     public TestPane() { 
      setBackground(Color.GREEN); 
      setForeground(Color.WHITE); 
      setFont(new Font("Arial", Font.PLAIN, 24)); 
     } 

     @Override 
     protected void paintComponent(Graphics g) { 
      super.paintComponent(g); 
      Graphics2D g2d = (Graphics2D) g.create(); 
      int width = getWidth(); 
      int height = getHeight(); 
      FontMetrics fm = g2d.getFontMetrics(); 
      String text = "It's not easy to be green"; 
      int x = (width - fm.stringWidth(text))/2; 
      int y = ((height - fm.getHeight())/2) + fm.getAscent(); 
      System.out.println(width + "x" + height); 
      System.out.println(x + "x" + y); 
      g2d.drawString(text, x, y); 
      g2d.dispose(); 
     } 
    } 

    public class Screen { 

     private GraphicsDevice vc; 

     public Screen() { 
      GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment(); 
      vc = env.getDefaultScreenDevice(); 
     } 

     public void setFullScreen(DisplayMode dm, JFrame window) { 
      window.setUndecorated(true); 
      window.setResizable(false); 
      vc.setFullScreenWindow(window); 

      if (dm != null && vc.isDisplayChangeSupported()) { 
       try { 
        vc.setDisplayMode(dm); 
       } catch (Exception e) { 
       } 
      } 
     } 

     public Window getFullScreen() { 
      return vc.getFullScreenWindow(); 
     } 

     public void restoreScreen() { 
      Window w = vc.getFullScreenWindow(); 
      if (w != null) { 
       w.dispose(); 
      } 
      vc.setFullScreenWindow(null); 
     } 
    } 
} 
+0

我現在很困惑,我對Java的圖形部分很新,我將如何去寫一個字符串屏幕或圖像無需更換油漆? – Debels

+0

你在正確的軌道上。但是你所做的就是用你的文字繪圖代替所有「繪畫」工作... – MadProgrammer

+0

我試着改變背景,前景和字體改變爲繪製方法,但它似乎沒有太大區別,我仍然看到黑屏:/ – Debels

0

這是我爲克服挑戰所做的。我從在主類run方法除去的setBackground,setForeground,和setfont程序和Screen類他們加入到setFullScreen方法:

window.getContentPane().setBackground(Color.PINK); 
    window.setForeground(Color.BLUE); 
    window.setFont(new Font ("Arial", Font.PLAIN, 24)); 

此後,我添加「super.paint(克);」作爲Main類中paint方法的第一行