如何加倍缓冲矩形

如何加倍缓冲矩形

本文介绍了如何加倍缓冲矩形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它总是适用于图像,但矩形和椭圆永远不会缓冲。我的游戏面板类中有一个基本的游戏循环,可以反复绘制玩家。它不会删除矩形,只留下痕迹。我想使用矩形而不是图像来进行学习。我尝试在游戏循环中使用重绘,但它像疯了一样闪烁,仍然无法正常工作。我在这个网站上查看了另一个关于此的教程,但他们使用的opengl女巫对我来说很陌生,我不想花时间去弄明白。如果我不清楚这里是我的游戏面板,框架和玩家类。(玩家不延伸矩形)



JFRame类



It always works with images but rectangles and ovals never buffer right. I have a basic game loop in my gamepanel class that draws the player repeatedly. It doesn't remove the rectangle, just leaves a trace. I want to use a rectangle instead of an image for learning purposes. I tried using repaint in the game loop, but it flickered like crazy and still didn't work. I looked at another tutorial on this in this website but they used opengl witch is foreign to me and I don't want to take the time to figure it out. If I'm not clear here is my Game Panel, Frame, and player class.(Player doesn't extends Rectangle)

JFRame Class

import javax.swing.JFrame;

     public class Game {

public static void main(String[] args) {
    JFrame f = new JFrame();
    f.setTitle("OMG I MADE A GAME");
    f.setResizable(true);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setContentPane(new Panel());
    f.pack();

    f.setVisible(true);
}

  }







JPanel Class






JPanel Class

import java.awt.*;
 import java.awt.image.BufferedImage;
  import java.awt.event.*;

  import javax.swing.JPanel;

   import com.game.entity.Player;


   public class Panel extends JPanel implements Runnable, KeyListener{
  private static final long serialVersionUID = -5122190028751177848L;


// dimensions
public static final int WIDTH = 320;
public static final int HEIGHT = 240;
public static final int SCALE = 2;

// game thread
private Thread thread;
private boolean running;

// image
private BufferedImage image;
private Graphics2D g;

private Player p;

public Panel() {
    super();
    setPreferredSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
    setFocusable(true);
    requestFocus();
}
// DRAWS PANEL TO FRAME
public void addNotify() {
    super.addNotify();
    if(thread == null) {
        thread = new Thread(this);
        addKeyListener(this);
        thread.start();
    }
}

private void init() {

    image = new BufferedImage(
                WIDTH, HEIGHT,
                BufferedImage.TYPE_INT_RGB
            );
    g = (Graphics2D) image.getGraphics();
    p = new Player(100, 100);

    running = true;
}

public void run() {

    init();


    // game loop
    while(running) {

        update();
        draw();
        drawToScreen();
        System.out.println("ELAPSED :" + System.nanoTime()/ 1000000 + " Seconds");

        try {
            Thread.sleep(10);
        }
        catch(Exception e) {
            e.printStackTrace();
        }

    }

}

private void update() {
    p.update();
}
private void draw(){


    // NAME (remember it loops) 
    String name = "2014 Jay H.";
    g.setFont(new Font("Name", 0, 12));
    g.setColor(Color.WHITE);
    g.drawString(name, 0, 10);
    g.setColor(Color.BLUE);
    g.fillRect( 0, 10, 65, 5);

    //TITLE                       looks sexy :D
    g.setColor(Color.GREEN);
    g.setFont(new Font("Title", 0, WIDTH/ 10));
    g.drawString("JAY'S GAME", WIDTH/ 5, 100);


    //DRAW PLAYER
    p.draw(g);

    }
// SCREEN IMAGE (dont have to use. Just use this^)
private void drawToScreen() {
    Graphics g2 = getGraphics();
    g2.drawImage(image, 0, 0,
            WIDTH * SCALE, HEIGHT * SCALE,null);
    g2.dispose();
}

public void keyTyped(KeyEvent key) {}

// PUBLIC KEYRELEASES
public void keyPressed(KeyEvent key) {
    int KeyCode = key.getKeyCode();

    //EXIT SYSTEM
    if(KeyCode == KeyEvent.VK_Q) {System.exit(0);}


    //UP
    if(KeyCode == KeyEvent.VK_W){p.setDY(-2);}
}


// PUBLIC KEYRELEASES
public void keyReleased(KeyEvent key) {
    int KeyCode = key.getKeyCode();

    //UP
        if(KeyCode == KeyEvent.VK_W) {p.setDY(0);} //How I move it


      }





播放器类





Player Class

import java.awt.*;
 import java.awt.image.BufferedImage;
  import java.awt.event.*;

  import javax.swing.JPanel;

   import com.game.entity.Player;


   public class Panel extends JPanel implements Runnable, KeyListener{
  private static final long serialVersionUID = -5122190028751177848L;


// dimensions
public static final int WIDTH = 320;
public static final int HEIGHT = 240;
public static final int SCALE = 2;

// game thread
private Thread thread;
private boolean running;

// image
private BufferedImage image;
private Graphics2D g;

private Player p;

public Panel() {
    super();
    setPreferredSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
    setFocusable(true);
    requestFocus();
}
// DRAWS PANEL TO FRAME
public void addNotify() {
    super.addNotify();
    if(thread == null) {
        thread = new Thread(this);
        addKeyListener(this);
        thread.start();
    }
}

private void init() {

    image = new BufferedImage(
                WIDTH, HEIGHT,
                BufferedImage.TYPE_INT_RGB
            );
    g = (Graphics2D) image.getGraphics();
    p = new Player(100, 100);

    running = true;
}

public void run() {

    init();


    // game loop
    while(running) {

        update();
        draw();
        drawToScreen();
        System.out.println("ELAPSED :" + System.nanoTime()/ 1000000 + " Seconds");

        try {
            Thread.sleep(10);
        }
        catch(Exception e) {
            e.printStackTrace();
        }

    }

}

private void update() {
    p.update();
}
private void draw(){


    // NAME (remember it loops) 
    String name = "2014 Jay H.";
    g.setFont(new Font("Name", 0, 12));
    g.setColor(Color.WHITE);
    g.drawString(name, 0, 10);
    g.setColor(Color.BLUE);
    g.fillRect( 0, 10, 65, 5);

    //TITLE                       looks sexy :D
    g.setColor(Color.GREEN);
    g.setFont(new Font("Title", 0, WIDTH/ 10));
    g.drawString("JAY'S GAME", WIDTH/ 5, 100);


    //DRAW PLAYER
    p.draw(g);

    }
// SCREEN IMAGE (dont have to use. Just use this^)
private void drawToScreen() {
    Graphics g2 = getGraphics();
    g2.drawImage(image, 0, 0,
            WIDTH * SCALE, HEIGHT * SCALE,null);
    g2.dispose();
}

public void keyTyped(KeyEvent key) {}

// PUBLIC KEYRELEASES
public void keyPressed(KeyEvent key) {
    int KeyCode = key.getKeyCode();

    //EXIT SYSTEM
    if(KeyCode == KeyEvent.VK_Q) {System.exit(0);}


    //UP
    if(KeyCode == KeyEvent.VK_W){p.setDY(-2);}
}


// PUBLIC KEYRELEASES
public void keyReleased(KeyEvent key) {
    int KeyCode = key.getKeyCode();

    //UP
        if(KeyCode == KeyEvent.VK_W) {p.setDY(0);} //How I move it


      }

推荐答案


这篇关于如何加倍缓冲矩形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 08:34