Sprite in JAVA game development

+1 Superman HC · July 16, 2015
I have coded a Sprite which is a two pic animation that swipes those images as well as move through the screen... that works fine without any error.. 
However... the output is like this... while moving the pics form a stack of images on the path... Is that supposed to happen?
If not... could the fault be in Screen manager class or Sprite class ?

/images/forum/upload/2015-07-16/e4cacc73f4a0d3c0ade5da1794fc87a0.png

Post a Reply

Replies

Oldest  Newest  Rating
+2 Sharon Fabin · July 16, 2015
i had this problem also when i followed that tutorial but it was a long time ago, i really think it was a problem with the rendering in the screen class where i put a line of code to paint the screen before or after the screen repainted itself and thats why the screen didnt actually "repainted" itself and just kept all the painting from before, check your screen class carefully.
0 Superman HC · July 17, 2015
Thanks buddy... u made my day...
in the 400 lines of code... it was just the error of a negation (!).. I think my eclipse should be that smart...:D
0 Superman HC · July 17, 2015
hey guys... that above problem is not sorted... but the flickering of images hadn't gone yet... i hv reviewed the screen class many times... Kindly help me find the error in the code: 

package newjavaapp;

import java.awt.*;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.lang.reflect.InvocationTargetException;
import javax.swing.JFrame;
   
public class Screen{
    
   private GraphicsDevice vc;
   // constructor
   public Screen(){
       GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
       vc = env.getDefaultScreenDevice();
   }
   // get all compatible Display Modes
   public DisplayMode[] getCompatibleDisplayModes(){
       return vc.getDisplayModes();
   }
   
   // compare the display Modes
   public DisplayMode findFirstCompatibleMode(DisplayMode modes[]){
       DisplayMode goodModes[] = vc.getDisplayModes();
       for(int x=0; x<modes.length;x++){
           for(int y=0;y<goodModes.length;y++){
               if(displayModesMatch(modes[x], goodModes[y])){
               return modes[x];
               }
           }
       }
       return null;
   }
   //get Current Display mode
   public DisplayMode getCurrentDisplayMode(){
       return vc.getDisplayMode();
   }
   
      //check if two modes match each other
   public boolean displayModesMatch(DisplayMode m1, DisplayMode m2){
       if(m1.getWidth()!=m2.getWidth() || m1.getHeight()!=m2.getHeight()){
           return false;
       }
       if(m1.getBitDepth()!=DisplayMode.BIT_DEPTH_MULTI && m2.getBitDepth()!=DisplayMode.BIT_DEPTH_MULTI && m1.getBitDepth()!=m2.getBitDepth()){
           return false;
       }
       if(m1.getRefreshRate()!=DisplayMode.REFRESH_RATE_UNKNOWN && m2.getRefreshRate()!=DisplayMode.REFRESH_RATE_UNKNOWN && m1.getRefreshRate()!=m2.getRefreshRate()){
           return false;
       }
       return true;
   }
   
   // set window to full screen
   public void setFullScreen(DisplayMode dm){
       JFrame f = new JFrame();
       f.setUndecorated(true);
       f.setResizable(false);
       vc.setFullScreenWindow(f);
       
       if(dm!=null && vc.isDisplayChangeSupported()){
           try{
           vc.setDisplayMode(dm);
           }catch(Exception ex){}
       }
       f.createBufferStrategy(2);
   }
    //set graphics object = to this
   public Graphics2D getGraphics(){
    Window w = vc.getFullScreenWindow();
    if(w!=null){
        BufferStrategy bs = w.getBufferStrategy();
        return(Graphics2D)bs.getDrawGraphics();
    }else{
        return null;
    }
    }
   // Update Display
   public void update(){
       Window w = vc.getFullScreenWindow();
       if(w!=null){
           BufferStrategy bs = w.getBufferStrategy();
           if(!bs.contentsLost()){
               bs.show();
           }
       }
   }
       // return full screen window
       public Window getFullScreenWindow(){
           return vc.getFullScreenWindow();
       }
       //get Width
       public int getWidth(){
           Window w = vc.getFullScreenWindow();
           if(w!=null){
               return w.getWidth();
           }else{
               return 0;
           }
       }
       // get height
       public int getHeight(){
           Window w = vc.getFullScreenWindow();
           if(w!=null){
               return w.getHeight();
           }else{
               return 0;
           }
       }
       //get out of full screen
       public void restoreScreen(){
           Window w = vc.getFullScreenWindow();
           if(w!=null){
               w.dispose();
           }
           vc.setFullScreenWindow(null);
       }
       // get image compatible with monitor
       public BufferedImage createCompatibleImage(int w, int h, int t){
           Window win = vc.getFullScreenWindow();
           if(win !=null){
               GraphicsConfiguration gc = win.getGraphicsConfiguration();
               return gc.createCompatibleImage(w,h,t);
           }
           return null;
       }

}
0 Nicholas Eason · July 19, 2015
It's a component painting issue that I commonly run into when making games with my terrible art skills. One way around this is to paint the entire screen with a clear rectangle, while effective, this provides a flickering effect. Try using a JPanel to store the image, and move the JPanel around instead of painting the image on 1 large JPanel.

The way I used to do this:

//Initialize JPanel

JFrame.setLayout(null);
//This gives the JPanel an Absolute Layout. You can set locations by using the setBounds(x,y,width, height) method.
JPanel.setBounds(x,y,w,h);



Of course, in Game Development, it's safe to assume that the player, for example, will be it's own class.

So (for a more in-depth example):

class Player extends JPanel{

private BufferedImage[] player;
private int x;
private int y;

public Player(){
//set Image, setup Animation, whatever it is. I usually use an array of images, you will see use below
}
public void update(){
setBounds(x,y,//Player Width, //Player Height);
if(index >= player.length)
index == 0;
else
index++;
}
public void paintComponent(Graphics g){

g.drawImage(player[index], 0, 0, null);

}
}

That's very rough, and you'd have to fill in the gaps, but that will essentially remove the problem you're having.
  • 1

Java / Android Development

129,851 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator