2016-12-08 4 views
1

Ich habe versucht, die Hintergrundfarbe meines Jframe blau zu bekommen, aber es zeigt nur die Standard-Jframe-Farben. Wie bekomme ich den Hintergrund, um tatsächlich blau anzuzeigen? Mit anderen Worten, es wird mir nicht erlauben, einen blauen Hintergrund zu bekommen, aber der Jframe wird sich ohne Farbe öffnen. Hintergrundfarbe von Jframe ändert sich überhaupt nicht

package DisplayPackagev1; 

import java.awt.Canvas; 
import java.awt.Color; 
import java.awt.Component; 
import java.awt.Graphics2D; 
import java.awt.image.BufferStrategy; 

import javax.swing.JFrame; 

public class Sea_InvadersDisplay extends Canvas implements Runnable{ 

private static final long serialVersionUID = 1L; 

public static void main(String[] args){ 
    Sea_InvadersDisplay display = new Sea_InvadersDisplay(); 
    JFrame frame = new JFrame(); 
    frame.add(display); 
    frame.pack(); 
    frame.setTitle("Sea Invaders"); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setResizable(false); 
    frame.setVisible(true); 
    display.start(); 
} 

private boolean running = false; 
private Thread thread; 

public synchronized void start(){ 
    if(running) 
    return; 

    running = true; 

    thread = new Thread(this); 
    thread.start(); 
} 

public synchronized void stop(){ 
    if(!running) 
     return; 

    running = false; 

    try { 
     thread.join(); 
    } catch (InterruptedException e) {e.printStackTrace();} 
} 

public int FPS; 

public Sea_InvadersDisplay(){ 
    this.setSize(1300, 690); 
    this.setFocusable(true); 
} 

@Override 
public void run() { 
    long timer = System.currentTimeMillis(); 
    long lastLoopTime = System.nanoTime(); 
    final int TARGET_FPS = 60; 
    final long OPTIMAL_TIME = 999999999/TARGET_FPS; 
    int frames = 0; 

    this.createBufferStrategy(3); 
    BufferStrategy bs = this.getBufferStrategy(); 
    while(running){ 
     long now = System.nanoTime(); 
     long updateLength = now - lastLoopTime; 
     lastLoopTime = now; 
     double delta = updateLength/((double) OPTIMAL_TIME); 
     // double delta = updateLength/((double) OPTIMAL_TIME); means that when I update it, it doesn't jump. 


     frames++; 

     if(System.currentTimeMillis() - timer > 1000){ 
      timer += 1000; 
      FPS = frames; 
      frames = 0; 
      System.out.println(FPS); 
     } 

     draw(bs); 

     try{ 
      Thread.sleep(((lastLoopTime - System.nanoTime()) + OPTIMAL_TIME)/999999999); 
     } catch(Exception e){}; 
    } 
} 

public void draw(BufferStrategy bs){ 
    do { 
     do { 
      Graphics2D g = (Graphics2D) bs.getDrawGraphics(); 
      g.setColor(Color.BLUE); 
      g.fillRect(0, 0, WIDTH, HEIGHT); 

      g.dispose(); 



    }while(bs.contentsRestored()); 
     bs.show(); 
    } while (bs.contentsLost()); 
    } 
// Buffer Strategy, way to use Buffer Damage so that there isn't any flickering. ALos takes up less resources 
} 

Antwort

0
frame.getContentPane().setBackground(Color.BLUE); 

Sie müssen die obige Zeile nennen.

Zum Beispiel:

public static void main(String[] args){ 
    Sea_InvadersDisplay display = new Sea_InvadersDisplay(); 
    JFrame frame = new JFrame(); 
    frame.add(display); 
    frame.pack(); 
    frame.setTitle("Sea Invaders"); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setResizable(false); 
    frame.getContentPane().setBackground(Color.BLUE); 
    frame.setVisible(true); 
    display.start(); 
} 
1

, wenn Sie fügen Sie Ihre Sea_InvadersDisplay Sie füllen Sie alle Bereich dieses Rahmens mit dieser Komponente. Selbst wenn Ihr JFrame Hintergrund irgendeine Hintergrundfarbe hätte, wäre er wegen der Überlagerung Sea_InvadersDisplay nicht sichtbar.

versuchen Sie stattdessen, die Hintergrundfarbe Ihrer Sea_InvadersDisplay festzulegen.

public static void main(String[] args){ 
    Sea_InvadersDisplay display = new Sea_InvadersDisplay(); 
    display.setBackground(Color.BLUE); //set background on display instead 
    JFrame frame = new JFrame(); 
    ... 
    display.start(); 
} 
Verwandte Themen