3

I'm making a game in java and need to paint units on a gameboard. I put all units in a list and paints every unit in that list. The paint method looks like this:

  public void paint(Graphics g) {
        super.paint(g);

        if (unitList != null) {
            Collections.sort(unitList);
            for (Unit unit : unitList) {
                Image image = unit.getImage();
                g.drawImage(
                        image,
                        (int) (playPosition.x + unit.getPosition().getX() - image
                                .getWidth(null) / 2), (int) (playPosition.y
                                + unit.getPosition().getY() - image
                                .getHeight(null) / 2), null);
            }
        }
    }

I have tried to make a BufferStrategy but it only makes the problem worse, guess I am doing something wrong.

Thanks

hboy
  • 301
  • 3
  • 17
  • 4
    Override paintComponent() not paint() – StanislavL Dec 13 '12 at 11:26
  • 1
    maybe caused by `Collections.sort(unitList);`, nobody knows, prepare that before and to use `paintComponent`, – mKorbel Dec 13 '12 at 11:30
  • 3
    @user1900750 this question isn't answerable, for better help sooner post an [SSCCE](http://sscce.org/), short, runnable, compilable – mKorbel Dec 13 '12 at 11:37
  • 1
    As said by mKorbel its hard to answer the question, but have a look at an anwser o mine demontsrating some gameloop/logic in java: http://stackoverflow.com/questions/13825515/java-rectangle-collision-detection-confusion/13827649#13827649 – David Kroukamp Dec 13 '12 at 12:31
  • 1
    For [example](http://stackoverflow.com/a/3256941/230513). – trashgod Dec 13 '12 at 15:19
  • Do not sort, use a SortedSet like TreeSet. That also has less concurrency problems and changes less. In an applet better use ImageIO.read or getResource instead of asynchrone reading with partial images. – Joop Eggen Dec 16 '12 at 19:24

1 Answers1

0

Maybe you haven't implemented BufferStrategy correctly.
Try manual double buffering by doing offscreen painting on an Image, and then just paint the whole said image in your regular overriden paint() method.

You would do that like so:

// Double buffering objects.
Image doubleBufferImage;
Graphics doubleBufferGraphics;

/*
 * Onscreen rendering.
 */
 @Override
 public void paint(Graphics g) {
     doubleBufferImage = createImage(getWidth(), getHeight());
     doubleBufferGraphics = doubleBufferImage.getGraphics();
     paintStuff(doubleBufferGraphics);
     g.drawImage(doubleBufferImage, 0, 0, this);
 }

/*
 * Offscreen rendering.
 */
 public void paintStuff(Graphics g) {
     if (unitList != null) {
        Collections.sort(unitList);
        for (Unit unit : unitList) {
            Image image = unit.getImage();
            g.drawImage(
                    image,
                    (int) (playPosition.x + unit.getPosition().getX() - image
                            .getWidth(null) / 2), (int) (playPosition.y
                            + unit.getPosition().getY() - image
                            .getHeight(null) / 2), null);
        }
    }
 }
Bill Tsagkas
  • 530
  • 1
  • 4
  • 15