我一直試圖在線上遵循一個簡單的教程(https://www.youtube.com/watch?v=1gir2R7G9ws)。當我嘗試輸入他輸入的代碼時,我得不到相同的結果。我輸入的某些內容最終會成爲無法使用的錯誤。當我嘗試使用GetBufferStrategy時,會發生這種情況。我不太清楚我的代碼有什麼問題。我真的可以用一些幫助。如何更改窗口的顏色(緩衝區策略問題)
下面是我得到了什麼:
package components;
import java.awt.*;
import java.awt.image.BufferStrategy;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.LayoutManager;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.event.*;
import javax.swing.*;
/* FrameDemo.java requires no other files. */
public class Window {
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
private Thread thread;
private boolean running = false;
public synchronized void start(){
thread = new Thread();
thread.start();
running = true;
}
public synchronized void stop(){
try{
thread.join();
running=false;
}catch(Exception e) {
e.printStackTrace();
}
}
public void run(){
long lastTime = System.nanoTime();
double amountOfTicks = 60.0;
double ns = 1000000000/amountOfTicks;
double delta = 0;
long timer = System.currentTimeMillis();
int frames = 0;
while(running)
{
long now = System.nanoTime();
delta += (now - lastTime)/ns;
lastTime = now;
while(delta >=1)
{
tick();
delta--;
}
if(running)
render();
frames++;
if(System.currentTimeMillis() - timer > 1000)
{
timer += 1000;
System.out.println("FPS: "+ frames);
frames = 0;
}
}
stop();
}
private void tick(){
}
private void render() {
BufferStrategy bs = this.getBufferStrategy();
if (bs==null){
this.createBufferStrategy(3);
return;
}
Graphics g = bs.getDrawGraphics();
g.setColor(Color.black);
g.fillRect(0,0,873,374);
g.dispose();
bs.show();
}
private void createBufferStrategy(int i) {
}
private BufferStrategy getBufferStrategy() {
return null;
}
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("Mr. Krebs");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel emptyLabel = new JLabel("");
emptyLabel.setPreferredSize(new Dimension(1000, 700));
frame.getContentPane().add(emptyLabel, BorderLayout.CENTER);
//Display the window.
frame.pack();
frame.setVisible(true);
frame.setLocationRelativeTo(null);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
恕我直言視頻教程是一種不適合學習編程的格式。簡單的事情就像在代碼中上下滾動以查看代碼的其他部分,在視頻中效果不佳。 – fabian
你的問題到底是什麼?請在堆棧溢出時直接在此處添加所有需要的信息。 –
我想知道如何在窗口中創建基本圖形。目前,我只是試圖將窗口的屏幕變成黑色。雖然在這一點上,似乎沒有迴應。我的問題確實是我做錯了什麼,我該如何解決它。 –