Click here to Skip to main content
14,328,524 members
Rate this:
Please Sign up or sign in to vote.
See more:
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

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


      }
Posted

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

geometric forms and images are not the same!

A rectangle has to be drawn, an image is just a "bunch of pixel" that need to be placed.

You could make a function or even a class which extends java.awt.Rectangle[^] and call that if needed.
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100