1 Replies - 281 Views - Last Post: 15 March 2010 - 05:41 PM Rate Topic: -----

#1 j4mes  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 5
  • Joined: 17-February 10

How to repaint

Posted 15 March 2010 - 05:37 PM

Just a little background of the application I'm having a little trouble withI have a mandril image that moves over a background image. This mandril would be considered my "player" In my players small little world there are obstacles images that are placed. The player cannot move into those images.

Now, the game is no fun if all the obstacles are displayed now is it? No. So what i would like to do is draw over the background image black rectangles for the size of my grid, that is accomplished but what i cannot do is as i move have the "player" drawn over the black rectangle. My approach to this was to repaint() or dispose of that object when a method isGridSquareAt(playerCol, playerRow) is true.

Here is the following code.

public void paintComponent(Graphics g) {

        if (myBackgroundPict != null) {
            myBackgroundPict.paintIcon(this, g, 0, 0);


            if ((gridCol > 0) && (gridRow > 0)) {

                squareHeight = getHeight() / gridRow;
                squareWidth = getWidth() / gridCol;


                g.setColor(Color.RED);
                for (int j = 0; j <= squareWidth; j++) {

                    g.drawLine(j * (squareWidth), 0, j * (squareWidth), getHeight());

                }
                // ------------------------------------------------------------------------------ //
                // Will "Draw" Vertical Lines, evenly spaced as functions of the Frames Width,    //
                // and the Grid the user enters                                                   //
                // ------------------------------------------------------------------------------ //

                for (int i = 0; i <= squareHeight; i++) {

                    g.drawLine(0, i * (squareHeight), getWidth(), i * (squareHeight));

                }
                //------------------------------------------------------------ //
                // Paint the mandril icon at the desired location using some   //
                //------------------------------------------------------------ //
                if (myCharIcon != null) {

                    player.draw(g, this, myCharIcon, squareWidth, squareHeight);
                }

                // -------------------------------------------------------------------------------------------------//
                // paint the treasures, based on the same concept of the size function as the previous Player icon //
                // ------------------------------------------------------------------------------------------------//
                if (myObstacle != null) {

                    for (int i = 0; i < myObstacle.length; i++) {

                        myObstacle[i].draw(g, this, obstMount, squareWidth,
                                squareHeight);


                    }
                }

                // Draws Treasures for the amount of treasure arrays
                if (myTreasure != null) {

                    for (int i = 0; i < myTreasure.length; i++) {

                        myTreasure[i].draw(g, this, treasureGold, squareWidth, squareHeight);

                        if (treasureAt(player.col, player.row)) {

                            myTreasure[i].drawPickup(g, this, treasureGold, squareWidth, squareHeight);

                        }
                    }
                    // If Treasure at, player.col, row, although not functional it tries to dispose;



                    for (int i = 0; i < gridSquare.length; i++) {

                        gridSquare[i].draw(g, squareWidth, squareHeight);

                    if(gridSquareAt(Player.col, Player.row)){

                       gridSquare[i] = null;


Is This A Good Question/Topic? 0
  • +

Replies To: How to repaint

#2 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: How to repaint

Posted 15 March 2010 - 05:41 PM

You need to call super.paintComponent(g) first, which will clean up any visual objects left from the previous frame:

public void paintComponent(Graphics g){
     super.paintComponent(g);
     //your code
}


:)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1