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

yuvru6vn  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(508)

我完全不懂java图形。
我写了一个简单的“游戏”,你用箭头键控制一个盒子
以下是源代码:

  1. package com.thundercrust.graphics;
  2. public class Drawings extends Canvas implements KeyListener, Runnable {
  3. public static Thread thread;
  4. public static Drawings draw;
  5. private static final long serialVersionUID = 1L;
  6. public static boolean running = false;
  7. public static int x = 640;
  8. public static int y = 320;
  9. public static int bulletX = 0;
  10. public static int bulletY = 0;
  11. public static int direction = 2;
  12. public static boolean fired = false;
  13. public static boolean show = false;
  14. public static String colorCode;
  15. public static final int WIDTH = 1366;
  16. public static final int HEIGHT = WIDTH / 16 * 9;
  17. public static final String title = "A Moving Box!";
  18. JFrame frame = new JFrame();
  19. public void paint(Graphics g) {
  20. Graphics2D g2D = (Graphics2D) g;
  21. g2D.setColor(Color.black);
  22. g2D.fillRect(0, 0, 1366, 768);
  23. g2D.setColor(Color.pink);
  24. g2D.fillRect(50, 50, 1266, 668);
  25. if (colorCode.equals("red")) g2D.setColor(Color.red);
  26. if (colorCode.equals("orange")) g2D.setColor(Color.orange);
  27. if (colorCode.equals("yellow")) g2D.setColor(Color.yellow);
  28. if (colorCode.equals("green")) g2D.setColor(Color.green);
  29. if (colorCode.equals("blue")) g2D.setColor(Color.blue);
  30. if (colorCode.equals("cyan")) g2D.setColor(Color.cyan);
  31. if (colorCode.equals("gray")) g2D.setColor(Color.gray);
  32. g2D.fillRect(x, y, 50, 50);
  33. }
  34. public Drawings() {
  35. frame.addKeyListener(this);
  36. frame.setTitle(title);
  37. frame.setSize(WIDTH, HEIGHT);
  38. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  39. frame.setLocationRelativeTo(null);
  40. frame.setResizable(false);
  41. frame.setVisible(true);
  42. frame.add(this);
  43. }
  44. public void display() {
  45. while (running = true) {
  46. repaint();
  47. try {
  48. Thread.sleep(30);
  49. } catch (Exception e) {
  50. e.printStackTrace();
  51. }
  52. }
  53. }
  54. public static void main(String args[]) {
  55. colorCode = JOptionPane.showInputDialog("Enter the color of the box: ");
  56. running = true;
  57. draw = new Drawings();
  58. draw.start();
  59. }
  60. public void keyPressed(KeyEvent e) {
  61. int keyCode = e.getKeyCode();
  62. if (keyCode == KeyEvent.VK_UP) { y-= 5; direction = 0; }
  63. if (keyCode == KeyEvent.VK_DOWN) { y+= 5; direction = 2; }
  64. if (keyCode == KeyEvent.VK_LEFT) {x-= 5; direction = 3;}
  65. if (keyCode == KeyEvent.VK_RIGHT) {x+= 5; direction = 1;}
  66. if (keyCode == KeyEvent.VK_Z) System.out.println("You pressed z");
  67. }
  68. public void keyReleased(KeyEvent e) {
  69. }
  70. public void keyTyped(KeyEvent e) {
  71. }
  72. public synchronized void start() {
  73. running = true;
  74. thread = new Thread(this, "Display");
  75. thread.start();
  76. }
  77. public synchronized void stop() {
  78. running = false;
  79. try {
  80. thread.join();
  81. } catch (InterruptedException e) {
  82. e.printStackTrace();
  83. }
  84. }
  85. public void run() {
  86. while (running = true) {
  87. System.out.println("The Game is Running!");
  88. repaint();
  89. try {
  90. Thread.sleep(60);
  91. } catch (Exception e) {
  92. e.printStackTrace();
  93. }
  94. }
  95. }

}
我之所以请求帮助,是因为应用程序总是闪烁,它变得非常烦人。
有什么办法解决这个问题吗?
任何帮助都将不胜感激。

lfapxunr

lfapxunr1#

canvas不是双缓冲的,我建议不要这样使用它。相反,可以考虑使用 JPanel 覆盖它的是 paintComponent 方法,这将免费为您提供双重缓冲。
请参见awt和swing中的绘画和一些细节的定制绘画
或者,你可以使用 BufferStrategy ,这将允许您定义自己的双缓冲策略并完全控制绘制过程,允许您控制何时绘制画布(又称活动绘制)

相关问题