Results 1 to 2 of 2
  1. #1
    kendaop is offline Member
    Join Date
    Jan 2009
    Posts
    1
    Rep Power
    0

    Default Some confusions with paint()

    Hi, Java student here. I've been trying to teach myself how to make applets from tutorials I found on the internet, but I have some problems. Mainly, it's that I'm somewhat confused on exactly how the paint() and repaint() methods work.

    I'm trying to create a board game that I thought up, using applets. I'm sure there are probably easier ways (Swing applets???) of implementing what I'm trying to do, but I'm in this for the learning experience. Anyways, I mostly understand how paint() works, but some weird things are happening to my applet. The trouble is in the movePlayer() method. I'm trying to essentially create a button for the user to click on, but I can't get everything that I need to display to be displayed at the same time. Before the user clicks the button, there is text at the top of the screen, the game board is below that, and there will eventually be two buttons below that. But, when I'm testing out the first button, things go wrong. Either the game board dissappears or the text gets painted over by the game board (depending on which way I've got the method set up). I'm trying to display the board again, while displaying a different string of text above it.

    Can anyone help me out? I used to have repaint() calls in the movePlayer() method, but they didn't seem to do anything; why not? Does paint() get called automatically after run() finishes? Also, is there a way to pause execution of the applet at a certain point and wait until the user successfully clicks on a button? Kind of like sleep()? have no idea how to do that. Anyways, my code is below. Thanks for your help.

    Java Code:
    import java.applet.*;
    import java.awt.*;
    import java.io.*;
    import java.awt.event.*;
    
    public class Labyrinth2 extends Applet implements Runnable, MouseListener
    {
        int Count;
        boolean startup = true;
        Font font = new Font("Monospaced", Font.BOLD, 45);
        Font font2 = new Font("Monospaced", Font.PLAIN, 20);
        Thread t;
        
        // The player Strings represent the location of the players' pieces on the grid.
        // The Strings are labeled with the corresponding row number first, then the column number.
        int playerOneRow = 4;
        int playerOneCol = 1;
        int playerTwoRow = 1;
        int playerTwoCol = 4;
        int playerThreeRow = 4;
        int playerThreeCol = 7;
        int playerFourRow = 7;
        int playerFourCol = 4;
    
        final int pTwoTargetRow = 7;
        final int pTwoTargetCol = 4;
        final int pThreeTargetRow = 4;
        final int pThreeTargetCol = 1;
        final int pFourTargetRow = 1;
        final int pFourTargetCol = 4;
        final int sleepTime = 1000; // the number of milliseconds the title screen will be displayed
        final int numPlayers = 4;
    
        final int moveBoxMinX = 45;
        final int moveBoxMaxX = 125;
        final int moveBoxMinY = 435;
        final int moveBoxMaxY = 500;
        
        final boolean Debug = true;
        
        boolean gameOver = false;
        boolean endOfTurn = true;
        boolean prompted = false;
        boolean userFail = false;
        boolean breakPause = false;
        boolean buttonIsPressed = false;
        boolean playerIsMoving = false;
        boolean playerChoseWall = false;
        int playerSelection = 0;
        int playerTurn = 1;
        int winner = 0;
        int mouseX, mouseY;
        
    
    
        int[][] horizontalWalls = new int[7][6]; //2D array holding data for which horizontal walls are placed, columns first
        int[][] verticalWalls = new int[7][6]; //2D array holding data for which vertical walls are placed, rows first
        
        public void init()
        {
            int width, height;
            width = getSize().width;
            height = getSize().height;
            addMouseListener(this);
    
            t = new Thread(this);
            t.start();
            initializeWalls();
        } // end of init
    
        public void run() // like the Main method
        {
            repaint(); // displays the title screen 
            try // counts a few seconds before clearing the screen
            {
                t.sleep(sleepTime);
            }
            catch (InterruptedException e)
            {}
         
            startup = false;
            repaint();
            
            while(!gameOver && endOfTurn)
            {
                nextPlayerTurn();
                endOfTurn = false;
                //playerTurn++;
                
                if(playerTurn > numPlayers)
                {
                    playerTurn = 1;
                };
                
            };
            
            //stop();
        } // end of run
       
        public void paint(Graphics g)
        {   
            final int numRows = 7, numColumns = 7, boxSize = 45, xReset = 60;
            int xOrigin = 60, yOrigin = 75;
            super.paint(g);
            
            if(startup)
            {
                g.setFont(font);
                g.drawString("Labyrinth!", 85, 245);
            }
            else
            {
                for(int currentRow = 1; currentRow <= numRows; currentRow++) // draws the grid used for the game board
                {
                    for(int currentColumn = 1; currentColumn <= numColumns; currentColumn++)
                    {
                        g.drawRect(xOrigin, yOrigin, boxSize, boxSize);
                        xOrigin += boxSize;
                    };
                
                    yOrigin += boxSize;
                    xOrigin = xReset;
                };
                
                displayWalls();
                displayPlayers();
            }; // end of if-else statement
        } // end of paint
       
        public void clrScreen()
        {
            Graphics g = getGraphics();
            Dimension d = getSize();
            Color c = getBackground();
            g.setColor(c);
            g.fillRect(0, 0, d.width, d.height);
        } // end of clrScreen
        
        public void displayPlayers()
        {
            Graphics g = getGraphics();
            int playerColumn = 0, playerRow = 0, xLocation, yLocation, radius, boxSize, xAdjustment, yAdjustment;
            radius = 30;
            boxSize = 45;
            xAdjustment = 24;
            yAdjustment = 38;
            
            if(Debug)
            {
                playerOneCol = 3;
                playerOneRow = 2;
                playerTwoCol = 3;
                playerTwoRow = 1;
                playerThreeCol = 7;
                playerThreeRow = 7;
                playerFourCol = 1;
                playerFourRow = 5;
            };
            
            for(int player = 1; player <= numPlayers; player++)
            {
                switch(player)
                {
                    case 1: g.setColor(Color.green);
                        playerColumn = playerOneCol;
                        playerRow = playerOneRow;
                        break;
                    case 2: g.setColor(Color.magenta);
                        playerColumn = playerTwoCol;
                        playerRow = playerTwoRow;
                        break;
                    case 3: g.setColor(Color.blue);
                        playerColumn = playerThreeCol;
                        playerRow = playerThreeRow;
                        break;
                    case 4: g.setColor(Color.orange);
                        playerColumn = playerFourCol;
                        playerRow = playerFourRow;
                        break;
                };
                
                xLocation = playerColumn * boxSize + xAdjustment;
                yLocation = playerRow * boxSize + yAdjustment;
                g.fillOval(xLocation, yLocation, radius, radius);
            };
        } // end of displayPlayers()
        
        public void displayWalls()
        {
            Graphics g = getGraphics();
            g.setColor(Color.red);
            final int wallLength = 46, wallWidth = 4;
            final int startingX = 60, startingY = 119;
            int xLocation = startingX, yLocation = startingY;
            int column = 0, row = 0;
            
            for(row = 0; row < horizontalWalls[row].length; row++) // checks for and displays horizontal wall segments
            {
                for(column = 0; column <= horizontalWalls[row].length; column++)
                {
                    if(horizontalWalls[column][row] == 1)
                    {
                        g.fillRect(xLocation, yLocation, wallLength, wallWidth); // creates a horizontal wall
                    };
                    xLocation += 45;
                };
                yLocation += wallLength -1;
                xLocation = startingX;
            };
            
            xLocation = startingX + wallLength - 3;
            yLocation = startingY - wallLength + 2;
            
            for(row = 0; row <= 6; row++) // checks for and displays vertical wall segments
            {
                for(column = 0; column <= 5; column++)
                {
                    if(verticalWalls[row][column] == 1)
                    {
                        g.fillRect(xLocation, yLocation, wallWidth, wallLength); // creates a vertical wall
                    };
                    xLocation += 45;
                };
                yLocation += wallLength -1;
                xLocation = startingX + wallLength - 3;
            };
        } // end of displayWalls()
        
        public void initializeWalls()
        {
            for(int y = 0; y < 6; y++) // initialize horizontal walls first
            {
                for(int x = 0; x < 7; x++)
                {
                    horizontalWalls[x][y] = 0;
                };
            };
            
            for(int y = 0; y < 7; y++) // then, initialize vertical walls
            {
                for(int x = 0; x < 6; x++)
                {
                    verticalWalls[y][x] = 0;
                };
            };
            
            if(Debug)
            {
                verticalWalls[0][0] = 1;
                verticalWalls[0][5] = 1;
                verticalWalls[6][0] = 1;
                verticalWalls[6][5] = 1;
                horizontalWalls[0][0] = 1;
                horizontalWalls[0][5] = 1;
                horizontalWalls[6][0] = 1;
                horizontalWalls[6][5] = 1;
            };
        } // end of initialize walls
        
        public void nextPlayerTurn()
        {
            Graphics g = getGraphics();
            String color = new String();
            endOfTurn = false;
            
            switch(playerTurn)
            {
                case 1: color = "Green";
                    break;
                case 2: color = "Magenta";
                    break;
                case 3: color = "Blue";
                    break;
                case 4: color = "Orange";
                    break;
            };
            
            g.setFont(font2);
            g.drawString(color + " player: Move or place a wall.", 15, 40);
            g.drawString("Move:  ", 65, 450);
            g.drawString("Place Wall:", 215, 450);
            prompted = true;
            
        } // end of nextPlayerTurn()
        
        public void mouseEntered(MouseEvent e)
        {
        }
        
        public void mouseExited(MouseEvent e)
        {
        }
        
        public void mouseClicked(MouseEvent e)
        {
            /**
            Graphics g = getGraphics();
            mouseX = e.getX();
            mouseY = e.getY();
            
            if(mouseX >= moveBoxMinX && mouseX <= moveBoxMaxX && mouseY >= moveBoxMinY && mouseY <= moveBoxMaxY)
            {
                playerIsMoving = true;
            };
            
            if(Debug && playerIsMoving)
            {
                g.drawString("Success!", 80, 295);
            };
            */
        }
        
        public void mousePressed(MouseEvent e)
        {
            mouseX = e.getX();
            mouseY = e.getY();
            
            if(mouseX >= moveBoxMinX && mouseX <= moveBoxMaxX && mouseY >= moveBoxMinY && mouseY <= moveBoxMaxY
            && prompted)
            {
                playerIsMoving = true;
            };
            e.consume();
        }
        
        public void mouseReleased(MouseEvent e)
        {
            mouseX = e.getX();
            mouseY = e.getY();
            
            if(mouseX >= moveBoxMinX && mouseX <= moveBoxMaxX && mouseY >= moveBoxMinY && mouseY <= moveBoxMaxY 
            && playerIsMoving)
            {
                movePlayer();
            }
            else
            {
                playerIsMoving = false;
            };
            e.consume();
        }
        
        public void movePlayer()
        {
            Graphics g = getGraphics();
            prompted = false;
            
            clrScreen();
            repaint();
            if(Debug)
            {
                g.setColor(Color.red);
                g.drawString("Success!", 80, 275);
            };
            g.setFont(font2);
            g.setColor(Color.black);
            g.drawString("Pick a square to move to.", 25, 40);
            
            try
            {
                t.sleep(1500);
            }
            catch (InterruptedException e)
            {}
            //g.drawString("
        }
        
        public void pause()
        {
            while(!gameOver)
            {
            };
        }
        
        public void stop()
        {
            t.stop();
        }
    } // end of DisplayBoard

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default

    Never use getGraphics on visual components. Do all custom painting in painting method overrides (paint() for AWT, paintComponent() for Swing) and in methods called from those methods: pass the Graphics reference available in paint/paintComponent as a parameter.

    Have you gone through this tutorial?
    Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI with JFC/Swing)

    db

Similar Threads

  1. finished paint!
    By diggitydoggz in forum New To Java
    Replies: 3
    Last Post: 01-04-2009, 11:33 AM
  2. passing parameters to paint mtd
    By themburu in forum Java Applets
    Replies: 3
    Last Post: 07-14-2008, 09:15 PM
  3. other than paint repaint
    By amith in forum Java 2D
    Replies: 1
    Last Post: 07-02-2008, 12:39 AM
  4. radio buttons and paint
    By gtraylo in forum Java Applets
    Replies: 1
    Last Post: 04-19-2008, 01:43 PM
  5. paint() and paintComponent()
    By goldhouse in forum Java 2D
    Replies: 1
    Last Post: 07-17-2007, 04:43 AM

Posting Permissions

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