Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: GAME VIEW - flashing

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default GAME VIEW - flashing

    Hi guys, i have a little problem. I program a game like Sokoban. When I make a move with a "man" display flashes. I know that I must use doubleBuffer but i dont know hot to make it in my code. Please can you help me ?

        public void initWorld() {
            int x = OFFSET;
            int y = OFFSET;
            Wall wall;
            Place place;
            Box box;
     
     
            for (int i = 0; i < level.length(); i++) {
                char item = level.charAt(i);
     
                if (item == '\n') {
                    y += SPACE;
                    x = OFFSET;
     
                } else if (item == '#') {
                    wall = new Wall(x, y);
                    walls.add(wall);
                    x += SPACE;
                } else if (item == '@') {
                    soko = new Player(x, y);
                    x += SPACE;
                } else if (item == 'o') {
                    box = new Box(x, y);
                    boxes.add(box);
                    x += SPACE;
                } else if (item == '.') {
                    place = new Place(x, y);
                    areas.add(place);
                    x += SPACE;
                } else if (item == ' ') {
                    x += SPACE;
                }
                h = y;
            }
     
        }
     
        public void buildWorld(Graphics g) {
            g.setColor(new Color(250, 240, 170));
             g.fillRect(0, 0, this.getWidth(), this.getHeight());
            ArrayList world = new ArrayList();
            world.addAll(walls);
            world.addAll(areas);
            world.addAll(boxes);
            world.add(soko);
     
            for (int i = 0; i < world.size(); i++) {
     
                Actor item = (Actor) world.get(i);
                g.drawImage(item.getImage(), item.x(), item.y(), this);
     
            }
        }
     
        @Override
        public void paint(Graphics g) {
            super.paint(g); 
            buildWorld(g);
    }


  2. #2
    Member
    Join Date
    Oct 2013
    Posts
    31
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Re: GAME VIEW - flashing

    Well first off fix your sentence... I cannot understand the grammar.. Or it might just be because English is not my main language.
    Secondly, post the things you have tried to do in your code to fix the problem.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: GAME VIEW - flashing

    Take a look at this thread and see how that OP did it:
    http://www.javaprogrammingforums.com...tml#post127731
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: GAME VIEW - flashing

    A comment: The arraylist should NOT be defined and filled in code running inside the call to paint() method.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Oct 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: GAME VIEW - flashing

    I fixed the problem with figure. But it still blinks sometimes. Where should I put the arraylist ? =O

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: GAME VIEW - flashing

    Move the arraylist to where it is defined one time and filled as needed. Don't do it every time paint() is called.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Oct 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: GAME VIEW - flashing

    I move arraylist away. But still blinks sometimes ! When i changed super.paint(g) to super.repaint() it works, but i dont see JPanel's components.

    --- Update ---

    Yeeees, I fixed it, thank you guys

Similar Threads

  1. [SOLVED] my "pong" code keeps flashing!
    By atrixes in forum What's Wrong With My Code?
    Replies: 4
    Last Post: December 15th, 2012, 10:18 PM
  2. Replies: 5
    Last Post: July 24th, 2012, 12:37 AM
  3. Unable to view date
    By Rajiv in forum Web Frameworks
    Replies: 1
    Last Post: August 17th, 2011, 09:03 AM