Results 1 to 8 of 8
  1. #1
    leggoeggo is offline Member
    Join Date
    Jul 2012
    Posts
    3
    Rep Power
    0

    Default background static sprites active

    not sure if this exact problem has been brought up already or not but here is the basic problem.


    I have a static background, a normal IMG, placed on my panel, above that is another set of static textures (about 225 of them for now, will increase this to over 1000 at a later date) like grass and rocks and such, and above that I have an active sprite IMG that jumps around based on where you click on that specific panel.

    my problem is when I call the repaint() function since there is a lot of IMG data being processed on the screen, the screen tends to flash.


    what I've been trying to do is make it so that the background and the above textures don't get repainted but stay on the screen, and the active sprite is the only thing is does.

    imagine layers
    layer one being the background, once painted will remain on the screen forever until i decide to repaint that layer.
    layer two being all the textures, also once painted will remain on the screen forever until i decide to repaint that layer.
    and a third layer that always gets repainted.

    I'm not asking for code, just a point in the general area or tips. maybe I'm missing something.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,573
    Rep Power
    12

    Default Re: background static sprites active

    Is this a Swing application? If so, bear in mind that repainting is *not* an active process in the sense that you call repaint() and a repaint happens. Rather repaints occur whenever they need and ought to - a process over which you only have so much control. Calling repaint() tells Swing that some element of your application needs to be repainted, and a repaint will occur in the fullness of time. Repaints can happen for other reasons (eg application becomes unminimised) which you don't control, so demanding that some gui element "once painted will remain on the screen forever until i decide to repaint that layer" isn't going to happen.

    The painting process is described in the Oracle article "Painting in AWT and Swing".

    since there is a lot of IMG data being processed on the screen, the screen tends to flash.
    Swing provides a repaint() method that takes a rectangle argument that says what region needs to be repainted. That may help but, with respect, we can't be sure that the cause of your problem is the amount of "IMG data being processed" (or even what that means) without seeing code. Consider constructing a SSCCE that shows this flashing.

    -----

    If this is not a Swing application, say what GUI framework or technology is being used.

  3. #3
    leggoeggo is offline Member
    Join Date
    Jul 2012
    Posts
    3
    Rep Power
    0

    Default Re: background static sprites active

    it is a swing application

    here is some of the code I have done so far. if you run it using netbeans IDE and a random couple pictures it should show the flashing I'm talking about.
    ahh b4 I get to that tho. I have taken a look at the Painting in AWT and Swing link you sent. The rectangle argument seems like it would reduce the burden a lot, but in the future when I have the basics up and running ill have around 10 sprites running around the screen at any given time each doing its own thing (active time bars, battle animations, spell effects etc) based on mouse interactions. so 10 character threads moving fluidly from point a to b repainting all the 15x15 or 30x30 pixel imgs overlapping a 15x15 or 30x30 grid seems like the same problem will occur.

    ------------------------------------------

    Java Code:
    package game;
    
    import java.io.*;
    import java.util.*;
    import java.util.logging.*;
    import java.util.regex.*;
    import javax.swing.*;
    
    public class Game
    {
       
        public static void main(String[] args) throws InterruptedException, FileNotFoundException  
        {
            
            
            BackgroundGeneration bgg = new BackgroundGeneration();
            GlobalVariables global = new GlobalVariables();
            while (true){ 
                bgg.repaint();
            }
    }
    ----------------------------------------------------
    Java Code:
    package game;
    import java.io.*;
    
    public class GlobalVariables 
    {
        public static int[] x1;           //x1 data
        public static int[] y1;           //y1 data
        public static int[] x2;           //x2 data
        public static int[] y2;           //y2 data 
        
        //public static int[] zone;         //the specific zone
        public static int[] picdata;      //what pic is on the zone
        public static boolean[] passdata;     //if the object can be moved on
        public static int[] effectdata;   //what effect does the zone have
        public static int[] objectdata;   //if there is an object on that zone
        //public static int belongsto;      //if the character belong to p1 or p2 or none
        
        public static int ClassNum;       //the class number
        public static int CharLevel;      //the level of the character
        public static int hp;             //the HP of the character
        public static int mp;             //the MP of the character
        public static int atk;            //the atk of the character
        public static int def;            //the def of the character
        public static int str;            //the str of the character
        public static int vit;            //the vit of the character
        public static int dex;            //the dex of the character
        public static int spd;            //the spd of the character
        public static int Xpos;
        public static int Ypos;
        
        public GlobalVariables()
        {
            x1 = new int[225];
            y1 = new int[225];
            x2 = new int[225];
            y2 = new int[225];
              
           
            picdata = new int [225];
            passdata = new boolean [225];
            effectdata = new int [225];
            objectdata = new int [225];
            
            for(int j = 0; j<15;j++)
            {
                for(int i =0; i<15;i++)
                {
                y1[i+j*15] = 60+j*60;
                y2[i+j*15] = 30+j*60;
                x1[i+j*15] = 40+i*60;
                x2[i+j*15] = 70+i*60;
                //zone [i+j*15] = i+j*15;
                picdata[i+j*15] = 1;
                passdata[i+j*15] = false;
                effectdata[i+j*15] = 0;
                objectdata [i+j*15] = 0;
                //System.out.println("" + picdata[i]);
                }
            }
    
        }
    ----------------------------
    Java Code:
    package game;
    
    import java.awt.*;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.awt.event.MouseMotionListener;
    import javax.swing.*;
    
    
    //This class will be used to generate all the backgroud
    //textures
    
    public class BackgroundGeneration extends JFrame
    {
        GlobalVariables global = new GlobalVariables();
        Zone zone = new Zone();
        
        public static JPanel MainPanel;
        public static Image mainpanelbg;
        //All the images used to make the main panel
        //grass = 1
        //stone = 2
        //grass-stone = 3
        public static Image image;
        public static Image image2;
        public static int value = 1;
        
        
        public BackgroundGeneration()
        {
            super("Game Name");
            MainPanel = new JPanel();
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setSize(1000,1000);
            setLocationRelativeTo(null);
            setVisible(true);
            setResizable(false);
            add(MainPanel);
            Handlerclass handler = new Handlerclass();
            MainPanel.addMouseListener(handler);
            MainPanel.addMouseMotionListener(handler);    
            //run();
        } 
    
        public class Handlerclass implements MouseListener, MouseMotionListener
        {
           public void mouseClicked (MouseEvent event)
            {
                GlobalVariables.Xpos = event.getX();
                GlobalVariables.Ypos = event.getY();
                System.out.println("" + GlobalVariables.Xpos + " " + GlobalVariables.Ypos);
            }
    
            public void mousePressed(MouseEvent event) {
               
            }
    
            public void mouseReleased(MouseEvent event) {
             
            }
    
           
            public void mouseEntered(MouseEvent event) {
                
            }
    
            public void mouseExited(MouseEvent event) {
                
            }
            
            public void mouseDragged(MouseEvent event) {
                
            }
            
            public void mouseMoved(MouseEvent event) {
                
            }
        } 
            
        
        
        public void paint (Graphics g)
        {
            mainpanelbg = new ImageIcon("E:\\game\\Game\\mainpanelbg.png").getImage();  
            image2 = new ImageIcon("E:\\game\\Game\\KnightStandingTest2.png").getImage();
            g.drawImage(mainpanelbg, 0, 0, null);    
            
            for (int i = 0; i<225;i++)
            {
                value = GlobalVariables.picdata[i];
                System.out.println(value);
                if (value == 1){
                    image = new ImageIcon("E:\\game\\Game\\grass1.png").getImage();
                } else if (value == 2) {
                    image = new ImageIcon("E:\\game\\Game\\grass1.png").getImage();
                } else if (value == 3) {
                    image = new ImageIcon("E:\\game\\Game\\grass2.png").getImage();
                } else if (value == 4) {
                    image = new ImageIcon("E:\\game\\Game\\grass3.png").getImage();
                }
                g.drawImage(image, GlobalVariables.x1[i], GlobalVariables.y1[i], null);
                g.drawImage(image2, GlobalVariables.Xpos, GlobalVariables.Ypos, null);
            }      
        }
    }
    ------------------------

    sorry wasnt quite sure how to post this all in those scrolly windows.
    and yes im quite new to java so excuse my bad habits >.<
    Last edited by leggoeggo; 07-04-2012 at 05:39 AM.

  4. #4
    mrguy is offline Member
    Join Date
    Jul 2012
    Posts
    27
    Rep Power
    0

    Default Re: background static sprites active

    click "Go Advanced" and click the # button and put code inside the 2 CODE things. I'm new to these forums also.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: background static sprites active

    You need to read the standard Swing drawing tutorials before trying to do this stuff, and I can see from your post that you haven't yet done this yet. For one, you should do all your drawing inside of a component that extends from JComponent such as JComponent itself or JPanel, you should draw inside of its paintComponent method, not its paint method, you should not be reading in images or doing any file i/o inside of paint or paintComponent, not unless you want to create a *very* unresponsive GUI,... etc...

    Also as an aside, none of your variables should be static as you will lose all the benefits of object oriented programming if you do this.

  6. #6
    leggoeggo is offline Member
    Join Date
    Jul 2012
    Posts
    3
    Rep Power
    0

    Default Re: background static sprites active

    read through it, doesn't help, other then pointing out my bad coding practices which i can easily fix at a later time.

    even there example does the exact same thing i'm trying to avoid and there is only a single object and text. it has a refresh rate effect which shows up in the animation.
    I am trying to make it as fluid as possible. You don't see other java games running with jittery animations that cut to white screens and back after almost every repaint.

    which gets me thinking that there is a better way to draw animations. layers (doubt that will work tho since a single red box 20x20 jitters)? another graphics method? must be something there doing right

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,573
    Rep Power
    12

    Default Re: background static sprites active

    Seriously, you should consider each of the suggestions Fubarable made in the first paragraph of #5. In fact, try them! even if you don't consider they'll help. After all your gui flickers, Fubarable's don't.

    And, since it's the animation that's proving difficult, concentrate on that. Remove all code except that which causes a bunch of objects to fly about the screen.
    Last edited by pbrockway2; 07-05-2012 at 01:33 AM.

  8. #8
    kammce's Avatar
    kammce is offline Senior Member
    Join Date
    Dec 2010
    Location
    California
    Posts
    194
    Rep Power
    4

    Default Re: background static sprites active

    One thing that might help with the flickering is to load all of the images to memory before painting them. If you do it whilst repainting, you will have to wait for the data to be put into RAM before your program can access them and that takes more time then just grabbing them right off of the RAM stack.
    My API:
    Java Code:
    cat > a.out || cat > main.class

Similar Threads

  1. Replies: 3
    Last Post: 05-31-2012, 08:56 PM
  2. Replies: 1
    Last Post: 02-18-2012, 09:47 PM
  3. Sprites - My Game (Sort of)
    By David M. in forum Java 2D
    Replies: 2
    Last Post: 04-26-2011, 12:00 AM
  4. Replies: 1
    Last Post: 08-07-2007, 06:05 AM
  5. Replies: 1
    Last Post: 08-01-2007, 10:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •