Итак, вскоре после того, как я получил работающего игрового персонажа в версии RuntDeale на Python, я начал переводить его на Java, и прогресс был довольно плавным. У меня есть окно, значок и папки ресурсов, все работает так, как должно. Но когда я попытался создать свою собственную реализацию canvas, она продолжает мигать, когда я продолжаю ее обновлять. И если я не обновлю его, он просто покажет пустоту (просто белый экран).
Вот как выглядит класс:
/** * Calin Baenen's version of a canvas element. */ class CPane { // Debug variables. /** * The canvas itself. */ private final JPanel canvas; // Canvas. /** * The buffer that adds data to the canvas. */ private BufferedImage buffer; // Buffer. // Constructor. public CPane(int width, int height) { this(0, 0, width, height); } public CPane(int width, int height, Color initialColor) { this(0, 0, width, height, initialColor); } public CPane(int x, int y, int width, int height) { this(x, y, width, height, Color.WHITE); } public CPane(int x, int y, int width, int height, Color initialColor) { this(x, y, width, height, initialColor, Color.WHITE); } public CPane(int x, int y, int width, int height, Color initialColor, Color baseColor) { this.canvas = new JPanel(); // Make an element to display the drawing on. canvas.setBounds(x, y, width, height); // Set the bounds of the canvas. canvas.setBackground(baseColor); // Make the canvas white. canvas.setDoubleBuffered(true); // Make the canvas double buffered. buffer = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); // Set the buffer. Graphics ctx = buffer.getGraphics(); // Get the graphics for the buffer. // Paint the image with an initisl color. ctx.setColor(initialColor); // Set color. ctx.drawRect(0, 0, width, height); // Draw. ctx.dispose(); // Dispose. } /** * Draws a rectangle to the screen by filling an area with color. */ public void drawRect(int x, int y, int width, int height, Color color) { Graphics ctx = buffer.getGraphics(); // Get the graphics. ctx.setColor(color); // Set the context's color. ctx.fillRect(x, y, width, height); // Draw a rectangle. ctx.dispose(); // Dispose the graphics. this.update(); // Update the canvas. } /** * Draw an image to the canvas. */ public void drawImage(Image image, int x, int y) { this.drawImage(image, x, y, image.getWidth(null), image.getHeight(null)); } /** * Draw an image to the canvas. */ public void drawImage(Image image, int x, int y, int width, int height) { Image shot = image.getScaledInstance(width, height, Image.SCALE_FAST); // Get the scaled image to draw. Graphics ctx = buffer.getGraphics(); // Get the graphics. ctx.drawImage(shot, x, y, null); // Draw the image. ctx.dispose(); // Dispose. this.update(); // Update the canvas. } /** * Update the graphics. */ public void update() { try { Graphics display = canvas.getGraphics(); // Get the display graphics. display.drawImage(buffer, 0, 0, null); // Draw the bufer. display.dispose(); // Dispose the display. } catch(Exception error) {} } }
Я знаю, что это, вероятно, в самом методе update
, но как я могу исправить эту ошибку?
Спасибо!Ваше здоровье!
Оригинал: “https://dev.to/baenencalin/why-does-my-canvas-implementation-keep-blinking-3be5”