1

I'm building a Java application. GameView has a BoardView which contains multiple PawnViews (in the example just one).

Example:

    public class GameView extends Frame
    {
        public GameView()
        {
            AWTUtilities.setWindowOpaque(this, false);
            this.setUndecorated(true);
            this.setLayout(null);
            this.setResizable(false);
            this._boardview = new BoardView();
            int x = 0;
            int y = 0;

            PawnView pv = new PawnView();
            this._boardview.AddPawn(pv, 10, 10);

            this._boardview.MovePawn(pv, 20, 10);
        }
    }

    public class BoardView extends JPanel
    {
        public BoardView()
        {
            this.setOpaque(false);
            RepaintManager.currentManager(null).setDoubleBufferingEnabled(true);
            this.setLayout(null);       
        }

        @Override
        public void update(Graphics g)
        {
            paint(g);
        }

        public void AddPawn(PawnView pawnview, int x, int y)
        {
            this.add(pawnview);
            pawnview.setLocation(x, y);
        }

        public void MovePawn(PawnView pawnview, int x, int y)
        {
            pawnview.setLocation(x, y);
            //this.repaint();
        }
    }

    public class PawnView extends JLabel
    {
        public PawnView()
        {
            this.setOpaque(false);
            RepaintManager.currentManager(null).setDoubleBufferingEnabled(true);
            this.setLayout(null);
        }
    }

Initially everything looks great (without the MovePawn):

http://dl.dropbox.com/u/7438271/1.png

When I call MovePawn it looks like:

http://dl.dropbox.com/u/7438271/2.png

I tried to call this.revalidate(), this.updateUI(), this.repaint(), this.paintImmediately() in various forms but they all make it worse: the whole JPanel gets a white background.

I also tried to override the paintComponent function of the JPanel also without effect.

This only occurs on Mac OS X (fully updated) but I experience some problems with repainting in Windows as well.

Can anyone please help out?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Hans Leautaud
  • 1,742
  • 1
  • 19
  • 34
  • 1
    I think we need more information to be able to better help you. Could you simplify the problem as much as possible and even create a minimal code example that we can compile and run, that demonstrates your problem, but that contains no excess code that is unrelated to the problem, an [sscce](http://sscce)? – Hovercraft Full Of Eels Mar 16 '12 at 22:35

1 Answers1

3

your code isn't runnable,

1) don't mixing AWT Frame with Swing JComponents, not sure (never tested) but I think that AWTUtilities is only for Swing JComponent, then

public class GameView extends Frame

could be

public class GameView extends JFrame

2)

this._boardview = new BoardView();

must be

BoardView _boardview = new BoardView();

3) for why reason is there code line,

RepaintManager.currentManager(null).setDoubleBufferingEnabled(true);

I saw code for printing where is for faster printing turn double buffering off, but value currentManager(null), simply doesn't make me any sence

4) never use public void update(Graphics g), this method is used internally in the API, use paintComponent instead

5) for moving with JComponents use Swing Timer with calling repaint()

6) JLabel is transparent by default remove this.setOpaque(false);

7) JLabel haven't got imlemented any LayoutManager, remove this.setLayout(null);

mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • I know my code isn't runnable. It's a example so you may understand how we build it. 1. Frame of JFrame doens't make a difference 2. this._boardview is correct because i got a global variable BoardView _boardview. It is just isn't there in the example 3. okay 4. i tried paintComponent also but that didn't change anything as well 5. i will check that out 6. okay 7. okay – Hans Leautaud Mar 16 '12 at 20:38
  • but it still isn't a answer to my question and that is why that trail of white is shown. – Hans Leautaud Mar 16 '12 at 20:44
  • @yohannesdedope there isn't any another answer depends od code posted here – mKorbel Mar 16 '12 at 21:35
  • 3
    @yohannesdedope: For reference, here's a working [example](http://stackoverflow.com/a/2562685/230513) and [variation](http://stackoverflow.com/a/2563350/230513). – trashgod Mar 16 '12 at 22:37
  • 2
    And [here](http://stackoverflow.com/questions/4687607/does-adding-a-jlabel-to-a-jpanel-hide-the-jpanel/4687759#4687759) as well. – Hovercraft Full Of Eels Mar 16 '12 at 22:57