2015-07-21 79 views
0

*我試圖打印一些東西,但背景爲粉紅色和白色文本。但文字顏色確實改變,但屏幕始終呈黑色。 *Java圖形中的背景顏色2D始終是黑色

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 ex){ 

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


} 

現在主要的方法:

import javax.swing.JFrame; 
import java.awt.*; 
public class myGuiMain extends JFrame { 
public static void main(String arg[]){ 


    // Screen.java 

    DisplayMode dm = new DisplayMode(800,600,16,DisplayMode.REFRESH_RATE_UNKNOWN); 
    myGuiMain m = new myGuiMain(); 
    m.run(dm); 
} 

public void run(DisplayMode dm){ 

    setForeground(Color.WHITE); 
    setBackground(Color.PINK); 
    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("This is gonna be awesome",200,200); 

} 
} 

結果:

enter image description here

我一直在尋找互聯網上,但什麼也沒有結束。如果有人能解決這個問題,請提前致謝。

+1

您認爲'foreground'和'background'屬性應用於何時?我想你可能想看看[在AWT和Swing中繪畫](http://www.oracle.com/technetwork/java/painting-140037.html)和[執行自定義繪畫](http:// docs .oracle.com/JavaSE的/教程/ uiswing /畫/) – MadProgrammer

回答

1

問題1:JFrame延伸。通常這是不鼓勵的,因爲它會將你鎖定到一個用例中(你不能真正將框架添加到其他任何東西),此外,你並沒有真正爲課程添加任何新功能。最好是在需要時簡單創建實例

問題2:覆蓋頂級容器(JFrame)的paint。這通常是由於以下原因而不鼓勵的:打破油漆鏈(你已經完成)都很容易;它不是雙緩衝(對閃爍的更新說你好);子組件可以被繪製而不需要父容器被重新繪製(你好奇怪的油漆更新和證書)。

最好創建一個自定義類,從一些延伸喜歡JPanel並重寫它的paintComponent方法,有執行您的自定義的繪畫(和呼叫super.paintComponent你做任何自定義塗裝前)

更多細節

Painting in AWT and SwingPerforming Custom Painting

喜歡的東西...

Painting

import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.DisplayMode; 
import java.awt.EventQueue; 
import java.awt.Font; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.GraphicsDevice; 
import java.awt.GraphicsEnvironment; 
import java.awt.Window; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 

public class myGuiMain { 

    public static void main(String arg[]) { 
     new myGuiMain(); 
    } 

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

       JFrame frame = new JFrame("Testing"); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.add(new TestPane()); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 

       DisplayMode dm = new DisplayMode(800, 600, 16, DisplayMode.REFRESH_RATE_UNKNOWN); 
       try { 
        Screen.setFullScreen(dm, frame); 
        try { 
         Thread.sleep(5000); 
        } catch (Exception ex) { 

        } 
       } finally { 
        Screen.restoreScreen(); 
       } 
      } 
     }); 
    } 

    public class TestPane extends JPanel { 

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

     @Override 
     public Dimension getPreferredSize() { 
      return new Dimension(600, 400); 
     } 

     protected void paintComponent(Graphics g) { 
      super.paintComponent(g); 
      Graphics2D g2d = (Graphics2D) g.create(); 
      g2d.setColor(getForeground()); 
      g2d.drawString("This is gonna be awesome", 200, 200); 
      g2d.dispose(); 
     } 

    } 

    public static class Screen { 

     public static void setFullScreen(DisplayMode dm, JFrame window) { 
      GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment(); 
      GraphicsDevice vc = env.getDefaultScreenDevice(); 
      window.setUndecorated(true); 
      window.setResizable(false); 
      vc.setFullScreenWindow(window); 

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

       } 
      } 
     } 

     public static void restoreScreen() { 
      GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment(); 
      GraphicsDevice vc = env.getDefaultScreenDevice(); 
      Window w = vc.getFullScreenWindow(); 
      if (w != null) { 
       w.dispose(); 
      } 
      vc.setFullScreenWindow(null); 
     } 

    } 
} 

您可能還想看看2D Graphics