在画布上绘制图形时,屏幕一直闪烁(Java)

在画布上绘制图形时,屏幕一直闪烁(Java),java,canvas,graphics,box,Java,Canvas,Graphics,Box,我完全不懂Java图形 我写了一个简单的“游戏”,你用箭头键控制一个盒子 以下是源代码: package com.thundercrust.graphics; public class Drawings extends Canvas implements KeyListener, Runnable { public static Thread thread; public static Drawings draw; private static final long serialVers

我完全不懂Java图形

我写了一个简单的“游戏”,你用箭头键控制一个盒子

以下是源代码:

package com.thundercrust.graphics;

public class Drawings extends Canvas implements KeyListener, Runnable {

public static Thread thread;

public static Drawings draw;

private static final long serialVersionUID = 1L;

public static boolean running = false;

public static int x = 640;
public static int y = 320;

public static int bulletX = 0;
public static int bulletY = 0;

public static int direction = 2;

public static boolean fired = false;
public static boolean show = false;

public static String colorCode;

public static final int WIDTH = 1366;
public static final int HEIGHT = WIDTH / 16 * 9;
public static final String title = "A Moving Box!";

JFrame frame = new JFrame();

public void paint(Graphics g) {
    Graphics2D g2D = (Graphics2D) g;
    g2D.setColor(Color.black);
    g2D.fillRect(0, 0, 1366, 768);
    g2D.setColor(Color.pink);
    g2D.fillRect(50, 50, 1266, 668);
    if (colorCode.equals("red")) g2D.setColor(Color.red);
    if (colorCode.equals("orange")) g2D.setColor(Color.orange);
    if (colorCode.equals("yellow")) g2D.setColor(Color.yellow);
    if (colorCode.equals("green")) g2D.setColor(Color.green);
    if (colorCode.equals("blue")) g2D.setColor(Color.blue);
    if (colorCode.equals("cyan")) g2D.setColor(Color.cyan);
    if (colorCode.equals("gray")) g2D.setColor(Color.gray);
    g2D.fillRect(x, y, 50, 50);

}

public Drawings() {

    frame.addKeyListener(this);

    frame.setTitle(title);
    frame.setSize(WIDTH, HEIGHT);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLocationRelativeTo(null);
    frame.setResizable(false);
    frame.setVisible(true);

    frame.add(this);
}

public void display() {
    while (running = true) {
        repaint();
        try {
            Thread.sleep(30);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

public static void main(String args[]) {
    colorCode = JOptionPane.showInputDialog("Enter the color of the box: ");
    running = true;
    draw = new Drawings();
    draw.start();
}

public void keyPressed(KeyEvent e) {
    int keyCode = e.getKeyCode();
    if (keyCode == KeyEvent.VK_UP) { y-= 5; direction = 0; }
    if (keyCode == KeyEvent.VK_DOWN) { y+= 5; direction = 2; }
    if (keyCode == KeyEvent.VK_LEFT) {x-= 5; direction = 3;}
    if (keyCode == KeyEvent.VK_RIGHT) {x+= 5; direction = 1;}
    if (keyCode == KeyEvent.VK_Z) System.out.println("You pressed z");
}

public void keyReleased(KeyEvent e) {

}

public void keyTyped(KeyEvent e) {


}

public synchronized void start() {
    running = true;
    thread = new Thread(this, "Display");
    thread.start();
}

public synchronized void stop() {
    running = false;
    try {
        thread.join(); 
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

public void run() {
    while (running = true) {
        System.out.println("The Game is Running!");
        repaint();
        try {
            Thread.sleep(60);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
}

我之所以寻求帮助,是因为应用程序总是闪烁,而且非常烦人

有办法解决这个问题吗


任何帮助都将不胜感激。

画布不是双缓冲的,我建议不要以这种方式使用它。取而代之的是,考虑使用<代码> jPoCTs/COD>并重写它的代码> PruttCys< /Cult>方法,这将为您提供免费的双缓冲。p> 见和一些死亡


或者,您可以使用,这将允许您定义自己的双缓冲策略,并完全控制绘制过程,让您控制画布何时绘制(也称为活动绘制)

画布不是双缓冲的,我建议以这种方式使用。取而代之的是,考虑使用<代码> jPoCTs/COD>并重写它的代码> PruttCys< /Cult>方法,这将为您提供免费的双缓冲。请参阅和,以了解如何实施BufferStrategy?请参阅指向
BufferStrategy
的链接,从那里开始,这里有一个示例。您还可以查看哪些讨论了不同的渲染模式