【问题标题】:Java g.dispose not clearing screenJava g.dispose 不清除屏幕
【发布时间】:2015-05-24 22:37:40
【问题描述】:

我正在尝试制作基于 2D 瓷砖的游戏。我在尝试绘制新图形时遇到了一个问题,而旧图形似乎没有被删除。

如果有人知道为什么 g.dispose 没有清除图形,请帮忙。

这是我的“主要”课程:

package Main;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferStrategy;

import javax.swing.JFrame;

public class Main extends Canvas implements Runnable {

  private static final long serialVersionUID = 1L;

  private JFrame frame;

  static int size = 40;
  static int tilesX = 20;
  static int tilesY = 20;
  static int width = size * 10;
  static int height = size * 10;
  private boolean running = false;

  public static STATE state = STATE.MENU;
  public static PLAYER type = PLAYER.ARCHER;
  private Thread thread;

  static tileMap grid = new tileMap();
  tile[][] map = tileMap.map;

  public Main() {
    addKeyListener(new controls());
    addMouseListener(new mouse());
    Dimension wSize = new Dimension(width, height);
    setPreferredSize(wSize);
    frame = new JFrame();
  }

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

  }

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

  public enum STATE {
    MENU, GAME
  }

  public enum PLAYER {
    ARCHER, KNIGHT
  }

  public static void main(String[] args) {
    Main game = new Main();
    game.frame = new JFrame("Game");
    game.frame.add(game);
    game.frame.setResizable(false);
    game.frame.pack();
    game.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    game.frame.setVisible(true);
    game.start();
  }

  public void run() {
    while (running) {
      if (state == STATE.MENU) {
        menu();
      } else if (state == STATE.GAME) {
        tick();
        render();
      }
      try {
        Thread.sleep(16);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    }
  }

  private void menu() {
    BufferStrategy bs = getBufferStrategy();
    if (bs == null) {
      createBufferStrategy(2);
      return;
    }

    Graphics g = bs.getDrawGraphics();

    g.drawRect(Main.width / 8, 20, Main.width / 3, 200);
    g.drawString("Archer", Main.width / 8, 20);
    g.drawRect((Main.width - Main.width / 3) - Main.width / 8, 20,
      Main.width / 3, 200);
    g.drawString("Knight", (Main.width - Main.width / 3) - Main.width / 8,
      20);

    g.dispose();
    bs.show();
  }

  private void render() {
    BufferStrategy bs = getBufferStrategy();
    if (bs == null) {
      createBufferStrategy(3);
      return;
    }

    Graphics g = bs.getDrawGraphics();

    Graphics2D d = (Graphics2D) g;

    camera.setCam();

    g.translate(-camera.camX, -camera.camY);

    for (int i = 0; i < map.length; i++) {
      for (int j = 0; j < map[i].length; j++) {
        d.setColor(map[i][j].getC());
        d.fillRect(map[i][j].getX(), map[i][j].getY(), Main.size, Main.size);
        d.setColor(Color.BLACK);
        d.drawRect(map[i][j].getX(), map[i][j].getY(), Main.size, Main.size);
      }
    }

    map[player.p.getX()][player.p.getY()].setC(player.p.getC());



    if (type == PLAYER.ARCHER) {
      d.drawString("Archer", 5, 15);
    } else if (type == PLAYER.KNIGHT) {
      d.drawString("Knight", 5, 15);
    }

    g.dispose();
    bs.show();
  }

  static public void moved() {
    tileMap.map[player.p.getX()][player.p.getY()].setC(Color.GREEN);
  }

  private void tick() {
    if (player.p == null) {
      player.createP();
    }
  }
}

【问题讨论】:

    标签: java canvas graphics jframe dispose


    【解决方案1】:

    Graphics#dispose 释放Graphics 上下文可能持有的任何内部资源,从而减少内存开销,它不会“清除”上下文。

    来自JavaDocs

    处理此图形上下文并释放它正在使用的所有系统资源。调用 dispose 后无法使用 Graphics 对象。

    它不会影响底层内容,那会很烦人,因为使用 Graphics 对象的副本是一种在不影响原始上下文的情况下进行复杂更改的好方法。

    要“清除”上下文,您可以使用fillRect 在执行下一个绘画周期之前绘制颜色/背景。在Graphics g = bs.getDrawGraphics();之后执行此操作

    【讨论】:

    • 我会试试的。 'Graphics g = bs.getDrawGraphics();' 会是什么?用于?
    • 它获取对当前缓冲区Graphics 上下文的引用。查看BufferStrategy2D Graphics 了解更多详情
    • 啊,没关系,我只是惨败!非常感谢 fillRect 方法效果很好!
    猜你喜欢
    • 1970-01-01
    • 2011-06-20
    • 2018-07-25
    • 1970-01-01
    • 1970-01-01
    • 2012-04-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多