Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Rendered Rectangle shape keeps leaving behind trail and I\'m not sure what i\'m

ID: 3567593 • Letter: R

Question

Rendered Rectangle shape keeps leaving behind trail and I'm not sure what i'm doing wrong

//GamePanel.java


package View;

import Model.Character;
import Controller.Main;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Toolkit;
import javax.swing.JPanel;

public class GamePanel extends JPanel{
public static final int PWIDTH = 600; // size of the game panel
public static final int PHEIGHT = 400;
  
public Rectangle areaSelector = null;
  
//off screen rendering
private Graphics graphics;
private Image dbImage = null;
  
public GamePanel()
{
setBackground(Color.blue);
setPreferredSize(new Dimension(PWIDTH, PHEIGHT));
}
  
public void gameRender() {
if (dbImage == null) {
dbImage = createImage(PWIDTH, PHEIGHT);
if (dbImage == null) {
System.out.println("dbImage is null");
return;
} else {
graphics = dbImage.getGraphics();
}
}
  
synchronized (Main.gameData.characters) {
for (Character c : Main.gameData.characters) {
c.render(graphics);
}
}
}
  
  
public void printScreen() {
Graphics g;

try
{
g = this.getGraphics();
if ((g != null) && (dbImage != null))
g.drawImage(dbImage, 0, 0, null);
Toolkit.getDefaultToolkit().sync();
g.dispose();
}
catch (Exception e)
{
System.out.println("Graphics context error : " + e);
}
}
}

//GameData.java


package Model;

import View.GamePanel;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class GameData {
public final List<Character> characters;
public GameData()
{
characters = Collections.synchronizedList(new ArrayList<Character>());
characters.add(new Player(GamePanel.PWIDTH/2, GamePanel.PHEIGHT -30, 100, 20, false, false));
  
}
  
public void update() {
synchronized (characters) {
for (Character c : characters) {
c.update();
}
}
}
}

//Character.Java

/*
Both enemies and main player will be characters as well as the center figure
which the main plaayer will try to protect
*/


package Model;

import java.awt.Graphics;


public abstract class Character implements Collision{
//All characters will have a location that is updated while thread is running
public int x;
public int y;
public int size;
  
//All characters will have a health point system
public int healthPoint;
public boolean isKilled, isEnemy;
  
public Character(int x, int y, int healthPoint, int size, boolean isKilled,boolean isEnemy)
{
this.x = x;
this.y = y;
this.healthPoint = healthPoint;
this.size = size;
this.isKilled = isKilled;
this.isEnemy = isEnemy;
}
  
  
public abstract boolean isDead();
public abstract void render(Graphics g);
public abstract void update();
}

//Player.java

Explanation / Answer

import javax.swing.*;
import java.awt.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;

public class GamePanel extends JPanel implements Runnable{
   private static final int PWIDTH = 800; // size of panel
   private static final int PHEIGHT = 600;
   private Thread animator; // for the animation
   private volatile boolean running = false; // stops the animation
   private volatile boolean gameOver = false; // for game termination

   BufferedImage bgImage;
   // off-screen rendering
   private Graphics dbg;
   private Image dbImage = null;
   // more variables, explained later
   // ...


   public GamePanel() {
       setBackground(Color.blue); // white background
       setPreferredSize( new Dimension(PWIDTH, PHEIGHT));
       // create game components
       // ...


       // load the background image

       try {
           File f = new File("C:\Users\Sarah\My Dropbox\Projects\BHCSI\WiiTestNew\background.jpg");
       BufferedImage im = ImageIO.read(f);
       int transparency = im.getColorModel().getTransparency();
       GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
       GraphicsConfiguration gc = ge.getDefaultScreenDevice().getDefaultConfiguration();
      BufferedImage copy = gc.createCompatibleImage(
   im.getWidth(), im.getHeight(),
           transparency );
      // create a graphics context
      Graphics2D g2d = copy.createGraphics();

      // copy image
      g2d.drawImage(im,0,0,null);
      g2d.dispose();
       bgImage = copy;
   }
       catch(IOException e) {
        System.out.println("Load Image error" + e);
    }

   } // end of GamePanel()

   public void addNotify() /* Wait for the JPanel to be added to the JFrame/JApplet before starting. */ {
       super.addNotify(); // creates the peer
       startGame(); // start the thread
       }

   private void startGame() // initialise and start the thread   
   {
       if (animator == null || !running) {
           animator = new Thread(this);
           animator.start();
       }
   } // end of startGame()

   public void stopGame() // called by the user to stop execution
   { running = false; }

   public void run() /* Repeatedly update, render, sleep */
   {
       running = true;
       while(running) {
           gameUpdate(); // game state is updated
           gameRender(); // render to a buffer
           paintScreen(); // paint with the buffer

           try {
               Thread.sleep(20); // sleep a bit
           }
           catch(InterruptedException ex){}
       }

       System.exit(0); // so enclosing JFrame/JApplet exits
   } // end of run()

   private void gameUpdate() {
       if (!gameOver) // update game state ...
           ;

   }

   private void gameRender() {
       // Render game content
       if (dbImage == null){
       dbImage = createImage(PWIDTH, PHEIGHT);
       if (dbImage == null) {
       System.out.println("dbImage is null");
       return;
       }
       else
       dbg = dbImage.getGraphics();
   }

   // draw the background: use the image or a black colour
   if (bgImage == null) {
   dbg.setColor(Color.black);
   dbg.fillRect (0, 0, PWIDTH, PHEIGHT);
   }
   else
   dbg.drawImage(bgImage, 0, 0, this);
   }

   // more methods, explained later...
   private void paintScreen()
   // use active rendering to put the buffered image on-screen
   {
   Graphics g;
   try {
       g = this.getGraphics();
       if ((g != null) && (dbImage != null))
       g.drawImage(dbImage, 0, 0, null);

       Toolkit.getDefaultToolkit().sync();

       g.dispose();
   }
   catch (Exception e){
       System.out.println("Graphics context error: " + e);
   }
} // end of paintScreen()

} // end of GamePanel class