2013-10-19 163 views
0

這是一個全屏五秒鐘然後恢復正常的程序。我想要做的是將背景顏色更改爲黃色。我試過getContentPane()。setBackground(Color.YELLOW),但它仍然不起作用。爲什麼我的JFrame程序不能改變背景顏色?

package gamedev; 

    import java.awt.Color; 
    import java.awt.DisplayMode; 
    import java.awt.Font; 
    import java.awt.Graphics;  
    import java.io.PrintWriter; 
    import javax.swing.JFrame; 

    public class GameDev extends JFrame { 

    public static void main(String[] arg) { 

    DisplayMode dm = new DisplayMode(800, 600, 16, DisplayMode.REFRESH_RATE_UNKNOWN); 
    GameDev b = new GameDev(); 
    b.getContentPane().setBackground(Color.yellow); 
    b.run(dm); 

} 

public void run(DisplayMode dm) { 
    setBackground(Color.YELLOW); 
    setForeground(Color.red); 
    setFont(new Font("Arial", Font.PLAIN, 25)); 

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

    } finally { 
     s.restoreScreen(); 
    } 
} 

public void paint(Graphics g) { 

    g.drawString("You know you love me home boy.", 200, 200); 

} 
} 

和第二類

package gamedev; 

    import java.awt.DisplayMode; 
    import java.awt.GraphicsDevice; 
    import java.awt.GraphicsEnvironment; 
    import java.awt.Window; 
    import javax.swing.JFrame; 

    public class Screen { 

    GraphicsDevice videoCard; 

    public Screen() { 

     GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment(); 
     videoCard = env.getDefaultScreenDevice(); // acces computer screen 

    } 

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

    if (dm != null && videoCard.isDisplayChangeSupported()) { 

     try { 

      videoCard.setDisplayMode(dm); 

     } catch (Exception ex) { 

     } 
     } 
    } 

    public Window getFullScreenWindow() { 

    return videoCard.getFullScreenWindow(); //returns ur window 

    } 

public void restoreScreen() { 

    Window w = videoCard.getFullScreenWindow(); 

    if (w != null) { 

     w.dispose(); 
    } 
    videoCard.setFullScreenWindow(null); // takes away from full screen. 
    } 
} 
+3

你」重新封鎖一個Event Dispatching Thread,防止顏色變化。使用類似於javax.swing的定時器,Timer在後臺等待指定的時間 – MadProgrammer

+0

使用應該只用... super.paint(g)內部的繪製方法。 –

回答

1

基本上,你是阻塞事件調度線程,處理任何新傳入的事件,包括油漆請求阻止。相反,您應該使用類似於javax.swing.Timer的東西在後臺等待,直到所需的時間過去,然後恢復屏幕。

javax.swing.Timer將在事件分派線程的上下文中的指定延遲之後提供通知,使您在需要對UI進行修改時使用它是安全的。

再仔細看看Concurrency in Swing瞭解更多詳情...

import java.awt.Color; 
import java.awt.DisplayMode; 
import java.awt.Font; 
import java.awt.Graphics; 
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.Timer; 

public class GameDev extends JFrame { 

    public static void main(String[] arg) { 

     DisplayMode dm = new DisplayMode(800, 600, 16, DisplayMode.REFRESH_RATE_UNKNOWN); 
     GameDev b = new GameDev(); 
     b.getContentPane().setBackground(Color.yellow); 
     b.run(dm); 

    } 

    public void run(DisplayMode dm) { 
     setBackground(Color.YELLOW); 
     setForeground(Color.red); 
     setFont(new Font("Arial", Font.PLAIN, 25)); 

     final Screen s = new Screen(); 
     s.setFullScreen(dm, this); 

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

    public void paint(Graphics g) { 
     super.paint(g); 
     g.drawString("You know you love me home boy.", 200, 200); 

    } 

    public class Screen { 

     GraphicsDevice videoCard; 

     public Screen() { 

      GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment(); 
      videoCard = env.getDefaultScreenDevice(); // acces computer screen 

     } 

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

      if (dm != null && videoCard.isDisplayChangeSupported()) { 

       try { 

        videoCard.setDisplayMode(dm); 

       } catch (Exception ex) { 

       } 
      } 
     } 

     public Window getFullScreenWindow() { 

      return videoCard.getFullScreenWindow(); //returns ur window 

     } 

     public void restoreScreen() { 

      Window w = videoCard.getFullScreenWindow(); 

      if (w != null) { 

       w.dispose(); 
      } 
      videoCard.setFullScreenWindow(null); // takes away from full screen. 
     } 

    } 

} 

你也應該看看Performing Custom Painting,爲您的繪製方法,可以防止它實際上畫的背景