我一直试图让我的jframe的背景颜色变成蓝色,但它只是显示默认的jframe颜色。我如何获得实际显示蓝色的背景? 换句话说,它不允许我获得蓝色背景,但jframe将打开,没有任何颜色。
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
}
答案 0 :(得分:1)
当您添加Sea_InvadersDisplay
时,您使用此组件填充该框架的所有区域。因此,即使您的JFrame
背景具有任何背景颜色,也会因为覆盖Sea_InvadersDisplay
而无法显示。
而是尝试设置Sea_InvadersDisplay
的背景颜色。
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();
}
答案 1 :(得分:0)
frame.getContentPane().setBackground( Color.BLUE );
您必须拨打以上电话。
例如:
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();
}